Title: Why is color defined in IB different than one defined in code?
Tags: ios;objective-c;xcode;uicolor
Question: I have a single view app. I define two ```UIView``` instances inside that single view. I want both to have same color.

For first view I define it in Interface Builder and pick a color. I want ```#999999``` (grey). New color picker has possibility to enter hex color values so it is easy to enter: ```999999```.

For the second view, I define it by setting background color of second view. I set same ```#999999``` color using ```[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1]```. 

Simple calculation: ```#99 -&gt; 153 -&gt; 153/255 == 0.6.``` So far good.

However resulting colors are different. Why? This feels like a bug in xcode. After debugging resulting colors:

Color defined in IB:  ```UIDeviceRGBColorSpace 0.529648 0.529632 0.529641 1```

Color defined in code:```UIDeviceRGBColorSpace 0.6 0.6 0.6 1```



UPDATE:
I know about "duplicates" to this question. They have one thing in common. They properly identified the problem but failed miserably to solve it. It tested developer color pickers from Panic and Skala. They have same problem. And as insult to injury they offer copy color as hex values. Guest what, numbers there are not affected by color sgonzalez@example.com. So for example color ```#999``` they provide exactly ```0.6``` for each color component. Unfortunately, if color is used directly it has color profile attached and resulting color is different. Only way to get exact RBG color is to use "safe web colors" where resulting color is with generic RGB color profile, in other words not affected. Problem is that safe web colors doesn't contain all colors our designers use.



Comment: Formatted code, and fixed grammar.

Comment: your calculation is wrong! #99 -> 160!

Comment: @rmaddy sorry. my bad! i added 16 accidentally!

Comment: @AndreSlotta No. #99 is 153 (9 * 16 + 9 = 153).

Here is the accepted answer: This whole issue is fixed by Apple in El Capitan. So if you are using xcode 7 on El Capitan this is no longer an issue. Entering #hex color value doesn't change color space anymore.


Here is another answer: You can do it in such way



And code for another view:

```self.testView.backgroundColor = [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0];
```

UPDATE:

In Xcode, click the colorspace popup in the color picker and choose "Generic RGB", then enter the red, green and blue values from Photoshop, NOT THE HEX VALUE

And we can find so many duplicates:

Wrong color in Interface Builder&#39;s color picker

Weird colors in XCode Interface Builder?

Wrong color in Interface Builder

Xcode 6 beta color picker issue

...

Comment for this answer: If you do like i show, result will be same. If use RGB Sliders - result is not the same.
Comment for this answer: update: chose Generic RGB and change values and don't touch hex  value
Comment for this answer: Sometimes it's better to do as possible, rather than seek the most convenient way.
Comment for this answer: well, question is why if you define color in IB it is not the same color as defined in code. I know how to define colors. But result is not the same. Colors from IB are a bit darker.
Comment for this answer: True, when using web safe colors it works. But how to enter my custom colors there?
Comment for this answer: yes, this kind of works. But it is far away from being convenient.

Here is another answer: Here is a category on UIColor that can create a ```UIColor``` from a hex string if it will help.

```+ (UIColor *)rz_colorFromHex:(uint32_t)hexLiteral
{
    uint8_t r = (uint8_t)(hexLiteral &gt;&gt; 16);
    uint8_t g = (uint8_t)(hexLiteral &gt;&gt; 8);
    uint8_t b = (uint8_t)hexLiteral;

    return [self rz_colorFrom8BitRed:r green:g blue:b];
}

+ (UIColor *)rz_colorFromHexString:(NSString *)string
{
    NSParameterAssert(string);
    if ( string == nil ) {
        return nil;
    }

    unsigned int hexInteger = 0;
    NSScanner *scanner = [NSScanner scannerWithString:string];

    [scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"x#"]];
    [scanner scanHexInt:&amp;hexInteger];

    return [self rz_colorFromHex:hexInteger];
}
```

As to why your colors are different.  I would set a breakpoint and see that the colors are in fact the same while it is running.  You can even print out the rgb channels for each of the colors to make sure they aren't different.

I would be surprised if it is an Xcode bug, but let me know what happens when you inspect the colors from the debugger.

EDIT:

So looking into this more there are different color spaces that you can use for RGB.  By default interface builder is using a different one that iOS uses.  To switch them click the little gear when you are using the color picker and set it to Generic RGB.


Comment for this answer: Updated my answer, hopefully this helps.
Comment for this answer: Hacky, but if you set it to `A9A9A9` and then switch from sRGB to generic it should switch it to `999999` for you.  Not sure why it does that.
Comment for this answer: Defined in IB: UIDeviceRGBColorSpace 0.529648 0.529632 0.529641 1
Defined in code: UIDeviceRGBColorSpace 0.6 0.6 0.6 1
Comment for this answer: Answer make sense. When I select generic RGB, it is indeed the value I see reported by debugger. I am still battling how to enter #999 with color picker. Everytime I switch to generic RGB and enter 99999 it switches to sRGB.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:04/28/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><objective-c><xcode><uicolor>', 'Title': 'Why is color defined in IB different than one defined in code?', 'ViewCount': 377, 'id': 29922921, 'user_AboutMe': '<p>I like the smell of cocoa in the morning ...</p>\n', 'user_AccountId': 2612653.0, 'user_DisplayName': 'Juraj Antas', 'user_Reputation': 2980.0, 'user_Views': 114.0, 'user_id': 2262481.0}}