【发布时间】:2014-08-27 01:34:22
【问题描述】:
该属性为由UInt8s 组成的颜色提供了足够好的亮度表示,用于red、green 和blue。
var luma: UInt8 {
return UInt8(
[(red, 54), (green, 183), (blue, 19)].reduce(0){
let (color, eightBitWeight) = $1
return $0 + Int(color) * eightBitWeight
} >> 8
)
}
我希望它看起来像这样,但它无法编译:
[(red, 54), (green, 183), (blue, 19)].reduce(0){ luma, (color, eightBitWeight) in
luma + Int(color) * eightBitWeight
} >> 8
我们能更接近一点,没有比前者更多的代码吗?
【问题讨论】:
-
你为什么不直接使用
Int(red)*54 + Int(green)*183 + Int(blue)*19? -
代码不会自行记录权重的范围。