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

Mazyod
  • 22,319
  • 10
  • 92
  • 157
  • Post a complete drawRect as that two lines together with `UIGraphicsGetCurrentContext` are not drawing the color over the view. – A-Live May 28 '12 at 04:45
  • Thanks for testing it out. Just replace `CGContextEOFillPath(myContext);` with `CGContextFillRect`. Simpler, just to test. – Mazyod May 28 '12 at 08:34
  • 1
    Have you tried `[UIColor colorWithRed:99/255.0 green:99/255.0 blue:99/255.0 alpha:1.0]`? – Clafou May 28 '12 at 08:46
  • @Mazyod doesn't work anyway with `CGContextFillRect(myContext, self.frame);`. Please post the code you are using. – A-Live May 28 '12 at 08:53
  • **NOTE:** I've got some *very* interesting information. Let me post them before you guys try something else. – Mazyod May 28 '12 at 09:42

2 Answers2

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

Community
  • 1
  • 1
Mazyod
  • 22,319
  • 10
  • 92
  • 157
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]
Clafou
  • 15,250
  • 7
  • 58
  • 89
  • 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. – Mazyod May 28 '12 at 09:36
  • Weird! If you open your .xib in a text editor and look around for the color value, what do you see? – Clafou May 28 '12 at 09:42
  • 2 MC40NjI3NDUwOTggMC40NjI3NDUwOTggMC40NjI3NDUwOTgAA .. Makes no sense to me. – Mazyod May 28 '12 at 10:04
  • Althoug you are right with the white space issue this is a problem of color space. – Frederic Yesid Peña Sánchez Feb 21 '14 at 13:40