Color difference between drawRect and Interface Bu

2019-02-06 19:18发布

问题:

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

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:

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!!, but why???? Another Xcode bug? I found like 10 of those in the past month ...

回答1:

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:



回答2:

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]