Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hex colors in iOS are not accurate [duplicate]

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?

like image 817
Hampotato Avatar asked Jan 20 '15 04:01

Hampotato


1 Answers

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.

like image 80
matt Avatar answered Sep 18 '22 00:09

matt