将 UIColor(扩展范围 sRGB)转换为 CIColor

Converting UIColor (extended range sRGB) to CIColor

当我运行以下Swift3.0.1行在iOS10.2.1:

let color = CIColor(color: UIColor(hue: 0.72, saturation: 1.00, brightness: 0.78, alpha: 1));

我收到以下错误:

'*** -CIColor not defined for the UIColor UIExtendedGrayColorSpace 1 1; need to first convert colorspace.'

根据文档:

The color object. The color information represented by this object is in an RGB colorspace. On applications linked for iOS 10 or later, the color is specified in an extended range sRGB color space. On earlier versions of iOS, the color is specified in a device RGB colorspace.

来源:https://developer.apple.com/reference/uikit/uicolor/1621931-init

现在 CIColor 在设备 RGB 颜色空间中,而 UIColor 在扩展中,从这个 UIColor 中获取 CIColor 的适当语法是什么sRGB 色彩空间?

根据评论中的反馈,我找到了错误的来源:

稍后在该方法中,我在 UIColor 的实例上调用了 .ciColor。这失败了,因为 UIColor 实例不是首先由 CIColor 创建的。

似乎满足以下条件:

  • UIColor => CIColor: 必须由 CIColor(color: UIColor instance) 构造函数初始化
  • CIColor => UIColor => CIColor: .ciColor 属性 可用。

正如 Jake Chasan 所建议的那样,CIColor 实例必须使用 UIColor 实例进行初始化。所以这个 swift 代码对我有用:

func colorToRGB(uiColor: UIColor) -> CIColor
{
    return CIColor(color: uiColor)
}