初始化UIColor displayP3颜色时的最佳做法

问题描述 投票:0回答:2

在创建UIColor时,最好使用init(displayP3Red:green:blue:alpha:)进行可用性检查吗?老实说,我看不出两者之间的区别所以我不确定它有多重要,但我希望有人可以对此有所了解。

if #available(iOS 10.0, *) {
    self.init(displayP3Red: r, green: g, blue: b, alpha: a)
} else {
    self.init(red: r, green: g, blue: b, alpha: a)
}
ios swift ios10 ios11 uicolor
2个回答
3
投票

不,这些电话不等同。将相同的RGBA值传递给每个将产生不同的颜色。

Here's a handy article讨论色彩空间的差异。它使得颜色空间的一个很好的类比就像一个尺子上的长度单位。如果我说某个对象的长度为1.0,除非你知道“1.0”有多长,否则这没有任何意义。寸?米?弗隆?秒差距?该单位的定义对测量中的数量意味着巨大的差异。

同样,如果我说颜色的红色成分为1.0,除非我还说红色1.0是多少,否则这并不意味着什么。 (同样适用于绿色和蓝色组件。)对于Web和应用程序设计的大部分历史,可以安全地假设所有测量都与sRGB标准相关 - 1.0红色表示“与sRGB一样红”。

但随着手机,电脑,电视等的出现,支持更大的色域,这种假设已不再安全。显示器P3是较新的Apple设备使用的颜色空间(并且与较新的4K HDR电视使用的颜色空间非常匹配)。 P3支持比sRGB更广泛的颜色,这意味着“像sRGB一样红”可能不够红。

为了使sRGB和P3设备之间的互操作更容易一些,Apple的API包括“扩展sRGB”色彩空间。在常规sRGB中,与大多数颜色空间一样,组件值限制在0.0-1.0范围内 - 也就是说,您不能使用1.1的红色组件,如果您尝试,则只需将其限制为1.0 。对于0.0-1.0范围内的组件值,扩展sRGB定义为与sRGB相同,但允许使用该范围之外的值来表示sRGB色域之外的颜色。 (这就是Apple文档在说UIColor(red:green:blue:alpha:)使用扩展sRGB色彩空间时的含义。)


iOS中的UIColor(以及位于它下面的CGColor)不提供用于色彩空间转换的便利实用程序,但是macOS中的NSColor确实如此,这使得它可以很方便地说明差异:

Xcode playground screenshot showing sRGB vs P3 red differences

第一个红色相当于iOS中的UIColor(displayP3Red: 1, green: 0, blue: 0, alpha: 1)。第二个相当于iOS中的UIColor(red: 1, green: 0, blue: 0, alpha: 1)。如果您在P3显示屏*上查看此答案,您可能会注意到颜色样本中至少存在细微差别。

在下面,注意转换后的值。 “sRGB max red”,又名UIColor(red: 1, green: 0, blue: 0, alpha: 1)大致相当于P3中的(r: 0.918, g: 0.2, b: 0.139, a: 1)。 “P3 max red”,又名UIColor(displayP3Red: 1, green: 0, blue: 0, alpha: 1)在sRGB之外,但可以用“扩展sRGB”表示为(r: 1.093, g: -0.227, b: -0.15, a: 1)

*带有P3显示器的Apple设备包括iPhone 7,iPad Pro 2016,2015年iMac,2016年秋季MacBook Pro及更高版本


0
投票

区别在于返回值:

self.init(displayP3Red: r, green: g, blue: b, alpha: a)

返回sRGB值:

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

self.init(red: r, green: g, blue: b, alpha: a)

返回RGB值:

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/documentation/uikit/uicolor

sRGB格式是在每个设备上具有相同的颜色。

你可以看到更详细的例子:http://inaka.net/blog/2014/09/05/getting-the-right-colors-in-your-ios-app/

© www.soinside.com 2019 - 2024. All rights reserved.