我是 Swift 的新手,我在 SpriteKit 中遇到了 UIColors 问题。我在这样的枚举中声明了一组颜色:
enum Colors {
static let red = UIColor(red: 231/255, green: 76/255, blue: 60/255, alpha: 1)
}
然后我将一个 SKSpriteNode(任意称为元素)设置为该颜色,例如:
element.color = Colors.red
如果我现在打印值,这将是结果:
print(Colors.red)
UIExtendedSRGBColorSpace 0.905882 0.298039 0.235294 1
print(element.color)
UIExtendedSRGBColorSpace 0.905882 0.298039 0.235294 1
显然它们看起来一样,但如果我这样做:
print(element.color == Colors.red)
它会返回false
谁能向我解释为什么会发生这种情况?提前致谢。
我想我找到了原因:SKSpriteNode 有一个默认颜色为 P3DisplayColorSpace。你可以玩:
enum Colors {
static let red = UIColor.init(displayP3Red: 231/255, green: 76/255, blue: 60/255, alpha: 1)
}
element.color = Colors.red
print(Colors.red)
print(element.color)
print (type(of:element.color))
print (type(of:Colors.red))
print(Colors.red == Colors.red)
print( element.color == element.color)
print (Colors.red == element.color)
print( element.color.cgColor.components![0] == Colors.red.cgColor.components![0])
print( element.color.cgColor.components![0] )
print( Colors.red.cgColor.components![0] )
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句