如何访问UIColor的RGB值?

时间:2016-09-05 20:09:47

标签: ios swift uicolor core-image

我有一个带有按钮的iOS绘图应用程序,让用户可以选择要绘制的颜色。由于我的画笔使用RGB值,但我的颜色常数是UIColors,我需要转换。我试过了

private var currentButton: UIButton? {
    willSet{
        currentButton?.layer.borderWidth = 0
    }
    didSet{
        currentButton?.layer.borderWidth = 2
        let index = (currentButton?.tag)! - 1
        drawView.brush.blue = colors[index].ciColor.blue //Here is where I get the error
        drawView.brush.green = colors[index].ciColor.green
        drawView.brush.red = colors[index].ciColor.red
    }
}

我在另一个StackOverflow问题中找到了。但是,当我尝试在第8行获取RGB值时,我收到此错误:

Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: '*** -CIColor not defined for the UIColor UIExtendedSRGBColorSpace 1 1 1 1; need to first convert colorspace.'

它清楚地表明我需要转换色彩空间。但是CIColor.colorSpace是get-only。我做错了什么?

1 个答案:

答案 0 :(得分:6)

使用getRed(_:green:blue:alpha:)

var red:   CGFloat = 0
var green: CGFloat = 0
var blue:  CGFloat = 0
var alpha: CGFloat = 0

color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)