Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

ios - How to match UIExtendedSRGBColorSpace to my color values

I set the color for UIButton with the XCode visual editor. I set it using RGB sliders.enter image description here

Then I set variable green:

let green = UIColor(red: 0, green: 210/255, blue: 0, alpha: 1)

When I printed out green value and UIButton.backgroundColor I got next values accordingly:

UIExtendedSRGBColorSpace -0.146119 0.836984 -0.0130851 1
UIExtendedSRGBColorSpace 0 0.823529 0 1

So, as I guess, the color spaces are equal, but values are not. Why is it so? The Apple's UIButton() does some hidden conversion? What is the purpose? Is it possible to have the same values for this button property and for green property.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Next to the "RGB Sliders" popup menu there is a button which allows you to choose a color space:

enter image description here

In your case it is set to "Display P3", a color space which is "larger" than the sRGB color space and allows to display more colors on newer devices with a P3 display. This color is represented in the "extended sRGB colorspace" where the components are not restricted to the range from 0.0 to 1.0 (see "Color and Color Spaces" in UIColor for more information). In your case

UIExtendedSRGBColorSpace -0.416964 0.838774 -0.249501 1

with negative red and blue components, i.e. a color outside color gamut of sRGB.

If you set the colorspace in the color chooser to "sRGB" then the result for 0/210/0 will be

UIExtendedSRGBColorSpace 0 0.823529 0 1

and identical to

let green = UIColor(red: 0, green: 210/255, blue: 0, alpha: 1)

Alternatively, use the Display P3 color space for the programmatically created color as well:

print(label.backgroundColor!)
// UIExtendedSRGBColorSpace -0.416964 0.838774 -0.249501 1

let green = UIColor(displayP3Red: 0, green: 210/255, blue: 0, alpha: 1)
print(green)
// UIDisplayP3ColorSpace 0 0.823529 0 1
print(UIColor(cgColor: green.cgColor))
// UIExtendedSRGBColorSpace -0.416964 0.838774 -0.249501 1

print(label.backgroundColor! == green)
// true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...