0

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 -> 153 -> 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 profile at all. 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.

defined in IB

squarefrog
  • 4,750
  • 4
  • 35
  • 64
Juraj Antas
  • 3,059
  • 27
  • 37

3 Answers3

0

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 >> 16);
    uint8_t g = (uint8_t)(hexLiteral >> 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:&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. enter image description here

Alex Rouse
  • 1,681
  • 1
  • 12
  • 14
  • Defined in IB: UIDeviceRGBColorSpace 0.529648 0.529632 0.529641 1 Defined in code: UIDeviceRGBColorSpace 0.6 0.6 0.6 1 – Juraj Antas Apr 28 '15 at 15:22
  • 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. – Juraj Antas Apr 28 '15 at 16:34
  • 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. – Alex Rouse Apr 28 '15 at 16:43
0

You can do it in such way

Example

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's color picker

Weird colors in XCode Interface Builder?

Wrong color in Interface Builder

Xcode 6 beta color picker issue

...

Community
  • 1
  • 1
OMGHaveFun
  • 838
  • 1
  • 10
  • 16
0

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.

Juraj Antas
  • 3,059
  • 27
  • 37