Color difference between drawRect and Interface Builder?
Asked Answered
T

2

8

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 ...

Tormentil answered 28/5, 2012 at 4:20 Comment(5)
Post a complete drawRect as that two lines together with UIGraphicsGetCurrentContext are not drawing the color over the view.Overeat
Thanks for testing it out. Just replace CGContextEOFillPath(myContext); with CGContextFillRect. Simpler, just to test.Tormentil
Have you tried [UIColor colorWithRed:99/255.0 green:99/255.0 blue:99/255.0 alpha:1.0]?Eligible
@Tormentil doesn't work anyway with CGContextFillRect(myContext, self.frame);. Please post the code you are using.Overeat
NOTE: I've got some very interesting information. Let me post them before you guys try something else.Tormentil
T
22

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

Tormentil answered 15/7, 2012 at 15:50 Comment(2)
Thanks for that. I wouldn't have thought to do that in IBBallocks
I had the same problem and wven tested colors with Fireworks jejejLegendre
E
1

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]
Eligible answered 28/5, 2012 at 8:54 Comment(4)
What you are saying makes perfect sense, however, it is not the case. The screenshot I uploaded has two different gray colors, the one drawn by drawRect + leadColor = RGB 99,99,99. (i.e. colorWithWhite is the same as the code you provided. Still, I tested it, no use). However, the color set in IB is showing up as RGB 80,80,80.Tormentil
Weird! If you open your .xib in a text editor and look around for the color value, what do you see?Eligible
<object class="NSColor" key="IBUIBackgroundColor"> <int key="NSColorSpace">2</int> <bytes key="NSRGB">MC40NjI3NDUwOTggMC40NjI3NDUwOTggMC40NjI3NDUwOTgAA</bytes> </object> .. Makes no sense to me.Tormentil
Althoug you are right with the white space issue this is a problem of color space.Legendre

© 2022 - 2024 — McMap. All rights reserved.