How to get RGB values from UIColor?
89,808
Solution 1
const CGFloat *colors = CGColorGetComponents( curView.backgroundColor.CGColor );
These links provide further details:
Solution 2
In iOS 5 you could use:
CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha =0.0;
[multipliedColor getRed:&red green:&green blue:&blue alpha:&alpha];
Solution 3
SWIFT 3 & 4
I found that cgColor.components would not always return 4 color values, so I changed this so it gets them from a CIColor wrapper
extension UIColor {
var redValue: CGFloat{ return CIColor(color: self).red }
var greenValue: CGFloat{ return CIColor(color: self).green }
var blueValue: CGFloat{ return CIColor(color: self).blue }
var alphaValue: CGFloat{ return CIColor(color: self).alpha }
}
SWIFT 2
extension UIColor {
var red: CGFloat{ return CGColorGetComponents(self.CGColor)[0] }
var green: CGFloat{ return CGColorGetComponents(self.CGColor)[1] }
var blue: CGFloat{ return CGColorGetComponents(self.CGColor)[2] }
var alpha: CGFloat{ return CGColorGetComponents(self.CGColor)[3] }
}
It's not the most efficient way so I wouldn't go using this where a view will be constantly re-drawn.
Solution 4
Hopefully this will be helpful
CGFloat red, green, blue, alpha;
//Create a sample color
UIColor *redColor = [UIColor redColor];
//Call
[redColor getRed: &red
green: &green
blue: &blue
alpha: &alpha];
NSLog(@"red = %f. Green = %f. Blue = %f. Alpha = %f",
red,
green,
blue,
alpha);
Solution 5
You can use CIColor components (swift 5)
let ciColor = CIColor(color: backgroundColor)
let alpha = ciColor.alpha
let red = ciColor.red
let blue = ciColor.blue
let green = ciColor.green
this works for non-RGB color space too
Related videos on Youtube
Author by
defmech
Updated on July 20, 2021Comments
-
defmech almost 3 years
I'm creating a color object using the following code.
curView.backgroundColor = [[UIColor alloc] initWithHue:229 saturation:40 brightness:75 alpha:1];
How can I retrieve RGB values from the created color object?
-
Roddy almost 13 yearspossible duplicate of Extracting rgb from UIColor
-
geekinit over 12 yearsJust a note, the parameters passed into the method -initWithHue:saturation:brightness:alpha: should all be between 0.0 and 1.0.
-
Waseem Shah almost 10 years
-
-
defmech over 15 yearsAnswer below should have been a comment. Oops.
-
Jason about 14 yearsNote: this only works for colors in the RGB space. For example, this will not work on [UIColor whiteColor] as that is not in RGB.
-
Max Steinmeyer over 13 yearsI posted some sample code in this question to get this working in non-RGB contexts: stackoverflow.com/questions/4700168/…
-
Jon Trauntvein over 12 yearsThis is supported only in iOS 5.0 or newer.
-
Hemang almost 10 yearsIf someone finding problem on how to get values from
colors
, you can write something like,CGFloat red = colors[0];
-
braden about 9 yearsdid it not generate a warning for trying to implicitly cast CGFloat to float? :)
-
devios1 about 9 yearsBe warned that this method will only succeed if the starting color is in "a compatible color space", which can vary depending on the device/OS. For example I just discovered that calling this on [UIColor darkGrayColor] will fail on an iPad 2 running iOS 7. However the same color works fine on an iPad Air running iOS 8.
-
Albert Renshaw almost 6 years@devios1 This can likely solved by first serializing the color then deserializing it using
NSKeyedArchiver
, like so:multipliedColor = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject: multipliedColor]];
-
pkamb over 5 years
error: <EXPR>:3:1: error: 'CGColorGetComponents' has been replaced by property 'CGColor.components'
-
Gurjinder Singh almost 4 yearslet color = myUIColor.cgColor.components let r = color![0] let g = color![1] let b = color![2]