Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the RGB values of a UIColor? [duplicate]

I have an iOS drawing app with buttons that let a user choose colors to draw with. Since my brush uses RGB values but my color constants are UIColors, I need to convert. I tried

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
    }
}

Which I found in another StackOverflow question. However, when I try to get an RGB value in line 8 I get this error:

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

It clearly says that I need to convert color space. But CIColor.colorSpace is get-only. What am I doing wrong?

like image 937
A Tyshka Avatar asked Mar 12 '23 05:03

A Tyshka


1 Answers

Use 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)
like image 143
Rob Avatar answered Mar 21 '23 06:03

Rob