简体   繁体   中英

Why is color defined in IB different than one defined in code?

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.

IB中定义

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. 在此处输入图片说明

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

Weird colors in XCode Interface Builder?

Wrong color in Interface Builder

Xcode 6 beta color picker issue

...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM