如何访问 UIColor 的 RGB 值?

How can I access the RGB values of a UIColor?

我有一个 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
    }
}

这是我在另一个 Whosebug 问题中发现的。但是,当我尝试在第 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.'

明明说要转换颜色space。但是 CIColor.colorSpace 是只获取的。我做错了什么?

使用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)