Color difference between drawRect and Interface Bu

2019-02-06 19:21发布

Simply, I have 2 views in interface builder, one is set to the color 99,99,99 using the RGB sliders in interface builder.

enter image description here

The other view is colored programmatically to achieve a certain shape. I fill it using:

//Obviously, this is in drawRect.
[[UIColor leadColor] set];
CGContextEOFillPath(myContext);

//And this is a category on UIColor
+ (UIColor *)leadColor {
    return [UIColor colorWithWhite:99/255.0 alpha:1.0];
}

The result:

enter image description here

Why does this difference exist??


EDIT: (unecessary drawRect Code removed)


EDIT2:

So, here I am lying to myself .. "Interface builder showed RGB 99,99,99 as 80,80,80. I bet it offsets the number by 19." >.> ... A desperate man using Xcode thinks crazy stuff like this .. The result:

Perfect!!

PERFECT!!, but why???? Another Xcode bug? I found like 10 of those in the past month ...

2条回答
爷的心禁止访问
2楼-- · 2019-02-06 20:16

colorWithWhite uses grayscale space, and a color of 99 in grayscale space doesn't map to a color of (99,99,99) in RGB space.

So in order to get the same result as in Interface Builder, you need to use RGB space. Replace your call to colorWithWhite with this:

[UIColor colorWithRed:99/255.0 green:99/255.0 blue:99/255.0 alpha:1.0]
查看更多
相关推荐>>
3楼-- · 2019-02-06 20:23

I finally reached the fine-tuning stage of this app, and had to solve this issue, so, I searched and easily found the solution:

How do I enter RGB values into Interface Builder?

Illustration:

enter image description here

查看更多
登录 后发表回答