SKShapeNode.fillColor 不等于原始 UIColor
SKShapeNode.fillColor not equal to original UIColor
我刚刚浪费了很多时间寻找为什么下面的代码给出不相等结果的答案:
let originalColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
let section = SKShapeNode(path: path.cgPath)
section.fillColor = originalColor
let isEqual = section.fillColor == originalColor //=> gives false
我不知道,这有什么问题。我比较了颜色空间和 UIColor
对应的 CGColor
,只有 CGColor
有不同的 hashValue
。但这是为什么呢?上面的代码应该可以工作,非常简单,我在这里缺少什么?
编辑
我使用下面的代码来比较 UIColor
s:
extension SKColor {
static func !=(lhs: UIColor, rhs: UIColor) -> Bool {
return !lhs.isEqualTo(rhs)
}
func isEqualTo(_ color: UIColor) -> Bool {
var red1: CGFloat = 0, green1: CGFloat = 0, blue1: CGFloat = 0, alpha1: CGFloat = 0
getRed(&red1, green:&green1, blue:&blue1, alpha:&alpha1)
var red2: CGFloat = 0, green2: CGFloat = 0, blue2: CGFloat = 0, alpha2: CGFloat = 0
color.getRed(&red2, green:&green2, blue:&blue2, alpha:&alpha2)
return red1 == red2 && green1 == green2 && blue1 == blue2 && alpha1 == alpha2
}
}
其他一些 UIColor
的比较结果 - 以相同的方式实例化(red/green/blue 后缀 1 是形状的 fillColor
:
(lldb) po red1
0.8784313797950745
(lldb) po red2
0.8784313725490196
(lldb) po green1
0.10196078568696976
(lldb) po green2
0.10196078431372549
(lldb) po blue1
0.30980393290519714
(lldb) po blue2
0.30980392156862746
尝试改变这个:
return red1 == red2 && green1 == green2 && blue1 == blue2 && alpha1 == alpha2
为此:
return Float(red1) == Float(red2) &&
Float(green1) == Float(green2) &&
Float(blue1) == Float(blue2) &&
Float(alpha1) == Float(alpha2)
我刚刚浪费了很多时间寻找为什么下面的代码给出不相等结果的答案:
let originalColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
let section = SKShapeNode(path: path.cgPath)
section.fillColor = originalColor
let isEqual = section.fillColor == originalColor //=> gives false
我不知道,这有什么问题。我比较了颜色空间和 UIColor
对应的 CGColor
,只有 CGColor
有不同的 hashValue
。但这是为什么呢?上面的代码应该可以工作,非常简单,我在这里缺少什么?
编辑
我使用下面的代码来比较 UIColor
s:
extension SKColor {
static func !=(lhs: UIColor, rhs: UIColor) -> Bool {
return !lhs.isEqualTo(rhs)
}
func isEqualTo(_ color: UIColor) -> Bool {
var red1: CGFloat = 0, green1: CGFloat = 0, blue1: CGFloat = 0, alpha1: CGFloat = 0
getRed(&red1, green:&green1, blue:&blue1, alpha:&alpha1)
var red2: CGFloat = 0, green2: CGFloat = 0, blue2: CGFloat = 0, alpha2: CGFloat = 0
color.getRed(&red2, green:&green2, blue:&blue2, alpha:&alpha2)
return red1 == red2 && green1 == green2 && blue1 == blue2 && alpha1 == alpha2
}
}
其他一些 UIColor
的比较结果 - 以相同的方式实例化(red/green/blue 后缀 1 是形状的 fillColor
:
(lldb) po red1
0.8784313797950745
(lldb) po red2
0.8784313725490196
(lldb) po green1
0.10196078568696976
(lldb) po green2
0.10196078431372549
(lldb) po blue1
0.30980393290519714
(lldb) po blue2
0.30980392156862746
尝试改变这个:
return red1 == red2 && green1 == green2 && blue1 == blue2 && alpha1 == alpha2
为此:
return Float(red1) == Float(red2) &&
Float(green1) == Float(green2) &&
Float(blue1) == Float(blue2) &&
Float(alpha1) == Float(alpha2)