22

I can not find an article or a document describing which color space should be used for RGB values when initialising an instance from UIColor class.

The article Getting the right colors in your ios app says we should use Generic RGB. On the other hand, I have found several posts saying that we should definitely use sRGB on iOS.

It seems the default color space is sRGB as written here in CGColorSpace Reference

There is a new color space called "Display P3" used in the iPad Pro and iPhone 7. The profile in the existing image resources has to be converted to the Display P3. For Digital Color Meter app the P3 profile has to be selected in order to get "Display P3" RGB values. See the screenshot.

How to pick the right RGB values for the Display P3 color space. enter image description here

Vladimír Slavík
  • 1,727
  • 1
  • 21
  • 31
  • So we can't just take the color from Sketch color picker, divide it by 255 and pass to `UIColor(red:, green:, blue:, alpha:)`, am i right? The only solution i see is to use Digital Color Picker with "Display in sRGB" – Oleksii Nezhyborets Dec 29 '16 at 20:24
  • 1
    Colors are not defined by using RGB values only; you will need to specify the color space the RGB values were defined in. – Yohst Jun 19 '17 at 18:37

2 Answers2

14

TL;DR

iOS uses Extended sRGB as its default.

Detailed Explanation

This can easily be verified via the debugger in Xcode:

  • put a breakpoint anywhere in your code,
  • when lldb comes up, type po UIColor.red,
  • the above command will return UIExtendedSRGBColorSpace 1 0 0 1 as of iOS 11, Xcode 9.2.

This color space can use the same sRGB values without a color change but at the same time permits RGB values to go negative as well as larger than 1.0 to express colors outside the sRGB color space (i.e. the extended sRGB color space).

There is a WWDC video, Working with Wide Color, from 2016 that explains this phenomenon nicely (go to 8:35).

So if you use sRGB colors in your app you should be good to go even for e-sRGB displays. Of course, if you need one of those specific colors outside the sRGB gamut, you will need to use the e-sRGB color space.

Yohst
  • 1,671
  • 18
  • 37
  • 1
    The explanation that e-sRGB allows for values outside of the 0.0-1.0 range, but is compatible with sRGB was the key point for me here. It’s a shame that Apple have removed the WWDC video though. – Doug Jul 06 '21 at 06:38
12

I wrote a blog post that explains this in detail:

http://www.vsanthanam.com/writing/2017/7/6/colors-in-ios-ensuring-consistency-between-designs-interface-builder-and-uicolor

Essentially, while Sketch uses a custom color picker that assumes sRGB color components, Xcode's color pick does not.

UIColor's most common factory method +colorWithRed:green:blue:alpha: assumes the sRGB Color Space (Extended sRGB if linked with iOS 10.x), but it also has another factory method, colorWithDisplayP3Red:green:blue:alpha: which will take in your Display P3 color space RGBA components, but stores them internally as translated values with a reference to the sRGB color space.

As always, take a look at Apple's documentation on UIColor for more info:

https://developer.apple.com/documentation/uikit/uicolor?changes=latest_minor

Interestingly enough, AppKit's UIColor counterpart, NSColor, supports way more flexibility with more available factory methods (including one designed with a custom color space specifically to mimic UIColor's +colorWithRed:green:blue:alpha: behavior). I'm not sure why UIColor is more limited.

vsanthanam510
  • 360
  • 4
  • 14