Color in Storyboard Not Matching Uicolor

Color in storyboard not matching UIColor

Xcode 8+, iOS 10+

I recently faced this problem and none of the posted answers did it. It turns out that with the release of iOS 10 SDK, the UIColor initializer init(red:green:blue:alpha:) now uses the extended sRGB range, so you have to set accordingly when configuring your color values on the Storyboard.

Sample Image

See Apple's documentation: https://developer.apple.com/reference/uikit/uicolor/1621925-init

colors in spritekit not matching

I think I found the reason: SKSpriteNode has a color with P3DisplayColorSpace as the default. You may play around:

  enum Colors {
static let red = UIColor.init(displayP3Red: 231/255, green: 76/255, blue: 60/255, alpha: 1)

}

element.color = Colors.red
print(Colors.red)
print(element.color)
print (type(of:element.color))
print (type(of:Colors.red))
print(Colors.red == Colors.red)
print( element.color == element.color)
print (Colors.red == element.color)
print( element.color.cgColor.components![0] == Colors.red.cgColor.components![0])
print( element.color.cgColor.components![0] )
print( Colors.red.cgColor.components![0] )

How to select color from the given image for Storyboard?

It seems that your navigationBar is set to translucent. You can change it as in this image Sample Image
That should be it. Happy Coding.

Hex color programmatically in swift differs with main storyboard

User DeviceRGB to get exact same color while giving color using storyboard

DeviceRGB

Added simulator screen shot here.

Smiluator

Sending A UIColor From Phone To Watch Results In Weird Colors

OK. I have a workaround. It's definitely a bug, but I can't wait until iOS 11/WatchOS 4 comes out. This workaround is effective (so far -lots more testing to go).

Here's the branch with the workaround.

if let color = label.textColor {
if let destColorSpace: CGColorSpace = CGColorSpace(name: CGColorSpace.sRGB) {
let newColor = color.cgColor.converted(to: destColorSpace, intent: CGColorRenderingIntent.perceptual, options: nil)
colorArray.append(UIColor(cgColor: newColor!))
}
}

It looks like the core color of the IB-based color is bad. Not sure how it gets the color right.

What I do, is create a new CGColor from the original one, casting it to sRGB. The new CGColor is valid.

It's a yucky and kludgy fix, but it should work fine.

I need to test for memory leaks, though. CG tends to leak like a sieve.

UIView with background color looks different than CALayer with background color. Why, and how to make them match?

You have in fact given them different colors.

  • The layer's color is set in code and is UIColor.blue.cgColor.

  • The view's color is created in the storyboard and is

      R:0.02 G:0.2 B:1 A:1

    as revealed by examining it in the view debugger.

Why? Because you have failed to take into account the storyboard color's color space. It is Generic RGB, whereas the color you create in code is Device RGB, which is sRGB.

Another way to see this is to do some logging. In the running app, the two colors are

<CGColor 0x60000296aa60> 
[<CGColorSpace 0x60000296ab20>
(kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)]
( 0 0 1 1 )>

and

<CGColor 0x600002976940> 
[<CGColorSpace 0x60000296ab20>
(kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)]
( 0.01675 0.198341 1 1 )>

As you can see, the second color has been converted to sRGB color space, which is not the same as the Generic RGB you started with, and thus we get a different color after the conversion.

and what would I change to make them look the same

If you set the storyboard color's colorspace to Device RGB and then set the sliders to give a simple blue, you'll end up with the same colors in the running app.



Related Topics



Leave a reply



Submit