Init Generic RGB in Swift

I am trying to convert a string containing a color in a Generic RGB color space to UIColor in Swift. For example, a typical line would look like this:

0.121569 0.129412 0.156863 1

Using the color picker on macOS, I found that these values ​​use the Generic RGB color space.

enter image description here

However, when I try to convert these values ​​to UIColor, it uses the sRGB color space.

let red = CGFloat((components[0] as NSString).doubleValue)
let green = CGFloat((components[1] as NSString).doubleValue)
let blue = CGFloat((components[2] as NSString).doubleValue)
let alpha = CGFloat((components[3] as NSString).doubleValue)

print(UIColor(red: red, green: green, blue: blue, alpha: alpha))
// Log Result: NSCustomColorSpace sRGB IEC61966-2.1 colorspace 0.121569 0.129412 0.156863 1 

Therefore, my application displays a different color. I confirmed this by changing the color space in the Color Picker to IEC61966-2.1, and it really displayed different values:

enter image description here

Any idea how I will convert the general RGB values ​​to the correct UIColor values?

EDIT , XML

+4
1

CGColor. :

let sp = CGColorSpace(name:CGColorSpace.genericRGBLinear)!
let comps : [CGFloat] = [0.121569, 0.129412, 0.156863, 1]
let c = CGColor(colorSpace: sp, components: comps)!
let sp2 = CGColorSpace(name:CGColorSpace.sRGB)!
let c2 = c.converted(to: sp2, intent: .relativeColorimetric, options: nil)!
let color = UIColor(cgColor: c2)

EDIT , . , Xcode FontAndColorThemes. sRGB, RGB.

, :

    let sp = CGColorSpace(name:CGColorSpace.sRGB)!
    let comps : [CGFloat] = [0.0, 0.456, 0.0, 1]
    let c = CGColor(colorSpace: sp, components: comps)!
    let v1 = UIView(frame:CGRect(x: 50, y: 50, width: 50, height: 50))
    v1.backgroundColor = UIColor(cgColor:c)
    self.view.addSubview(v1)

. , , :

enter image description here

, "eyedropper", , . , "eyedropper" Xcode iOS-. , sRGB.

+3

Source: https://habr.com/ru/post/1676485/


All Articles