Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Init Generic RGB in Swift

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

0.121569 0.129412 0.156863 1

Using the color picker in macOS, I discovered that these values are using the Generic RGB color space.

enter image description here

However, when I attempt to convert these values into 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 

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

enter image description here

Any idea how I would convert the Generic RGB values into the correct UIColor values?

EDIT For clarification, I am unable to change the color values in the string into another scheme as I am reading the colors from an external source in an XML file

like image 545
Alexander MacLeod Avatar asked Feb 05 '23 12:02

Alexander MacLeod


1 Answers

Color conversion by way of color space is performed at the level of CGColor. Example:

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 I think the premise of your original problem is erroneous. You are trying, it turns out, to use the numbers in an Xcode FontAndColorThemes file. Those numbers are sRGB, not generic RGB.

To prove it, I ran this code:

    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)

That color is taken from the Default color theme's Comment color. Well, the result is identical to the Comment color, as this screen shot demonstrates:

enter image description here

I get the same answer when I use the "eyedropper" tool as when I simply open the color swatch to read the inspector. And I get the same answer when I use the "eyedropper" tool on Xcode's swatch and on my iOS swatch. This seems to me to prove that these colors were always sRGB.

like image 147
matt Avatar answered Feb 10 '23 23:02

matt