简体   繁体   English

Swift中的Init Generic RGB

[英]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. 我试图将包含通用RGB颜色空间中的颜色的字符串转换为Swift中的UIColor。 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. 使用macOS中的颜色选择器,我发现这些值使用的是通用RGB颜色空间。

在此输入图像描述

However, when I attempt to convert these values into UIColor, it uses the sRGB color space. 但是,当我尝试将这些值转换为UIColor时,它使用sRGB颜色空间。

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: 我通过将拾色器中的颜色空间更改为IEC61966-2.1来确认这一点,它确实显示了不同的值:

在此输入图像描述

Any idea how I would convert the Generic RGB values into the correct UIColor values? 知道如何将Generic RGB值转换为正确的UIColor值吗?

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 编辑为了澄清,我无法将字符串中的颜色值更改为另一个方案,因为我正在读取XML文件中外部源的颜色

Color conversion by way of color space is performed at the level of CGColor. 通过颜色空间的颜色转换在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. 事实证明,您正在尝试使用Xcode FontAndColorThemes文件中的数字。 Those numbers are sRGB, not generic RGB. 这些数字是sRGB,而不是通用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: 好吧,结果与评论颜色相同,因为此屏幕截图显示:

在此输入图像描述

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. 当我在Xcode的样本和我的iOS样本上使用“eyedropper”工具时,我得到了相同的答案。 This seems to me to prove that these colors were always sRGB. 在我看来,这似乎证明这些颜色总是sRGB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM