6

I find that when you set a view's backgroundColor in xib, the color displayed is distinctly different from when you set backgroundColor programmatically.

Here's an example.

I have two views in this simple demo. I set the upper view's backgroundColor in xib, like this: enter image description here

The hex color value is 0x1BA9BA. Then I set the lower view's backgroundColor programmatically with the same hex color value. I use the following code:

NSInteger hexValue = 0x1ba9ba;
self.testView.backgroundColor = [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16)) / 255.0 
                                                  green:((float)((hexValue & 0xFF00) >> 8)) / 255.0 
                                                  blue:((float)(hexValue & 0xFF))/255.0 
                                                  alpha:1.0];

The result is as follows:

enter image description here

As you can see, there's a clear difference in color. What am I missing here?

Hampotato
  • 267
  • 4
  • 18
  • 1
    Maybe you can change your RGB slider settings to sRGB or device RGB. – gabbler Jan 20 '15 at 04:21
  • @gabbler It was sRGB. I also tried device RGB, the result is the same. – Hampotato Jan 20 '15 at 04:37
  • Please use generic RGB color space, change the space firstly and then modify rgb value, then the upper view will look like the bottom view. – gabbler Jan 20 '15 at 05:12
  • @gabbler I tried what you said and it works! Thank you so much :) – Hampotato Jan 20 '15 at 05:39
  • @gabbler Right you are, I demonstrated that independently (hadn't seen your comment). Feel free to enter as an answer - you were there first and spot on. – matt Jan 20 '15 at 05:49
  • @matt, your answer and screen shot pointed me to the right direction, glad it is fixed now. – gabbler Jan 20 '15 at 06:39
  • I don't feel that this question, or at least it's answer, is really a duplicate of the one it's flagged with. The other question yields someone doing an extreme hack (offset of RGB based on color comparisons). This answer here actually exposes the real problem: Programmatically you're using Generic RGB colorspace, but in the color picker you may be (accidentally?) using a different colorspace, like sRGB. This was in fact my exact problem! – Mason G. Zhwiti Feb 19 '15 at 23:06
  • This question does NOT deserve the duplicate flag. Finally it solved my problem (in contrast to the linked question). – qqilihq Oct 26 '15 at 14:29

1 Answers1

10

I applied your RGB sliders to the first view and your code to the second view and the colors are absolutely identical (to my eye) in the simulator; this is a screen shot from the simulator:

enter image description here

However, I entered those numbers into the color picker (27, 169, 186) when the color picker's color space was set to Generic RGB. If I switch to sRGB and then enter those numbers, I can reproduce your problem. So it is a color space issue after all; just start with the color space set to Generic RGB and now the numbers will match up.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Maybe you're having a color management problem on your computer? On my machine, changing the color space in the color picker changes the slider values, but not the way the color looks in the simulator. – matt Jan 20 '15 at 05:07
  • 1
    Changing the color space didn't change the color in the simulator either on my machine. But there definitely is something wrong with the color picker, since when I opened the xib as source code, it says which are not the correct numbers. – Hampotato Jan 20 '15 at 05:33