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

Share:
89,808

Related videos on Youtube

defmech
Author by

defmech

Updated on July 20, 2021

Comments

  • defmech
    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?

  • defmech
    defmech over 15 years
    Answer below should have been a comment. Oops.
  • Jason
    Jason about 14 years
    Note: 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
    Max Steinmeyer over 13 years
    I posted some sample code in this question to get this working in non-RGB contexts: stackoverflow.com/questions/4700168/…
  • Jon Trauntvein
    Jon Trauntvein over 12 years
    This is supported only in iOS 5.0 or newer.
  • Hemang
    Hemang almost 10 years
    If someone finding problem on how to get values from colors, you can write something like, CGFloat red = colors[0];
  • braden
    braden about 9 years
    did it not generate a warning for trying to implicitly cast CGFloat to float? :)
  • devios1
    devios1 about 9 years
    Be 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
    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
    pkamb over 5 years
    error: <EXPR>:3:1: error: 'CGColorGetComponents' has been replaced by property 'CGColor.components'
  • Gurjinder Singh
    Gurjinder Singh almost 4 years
    let color = myUIColor.cgColor.components let r = color![0] let g = color![1] let b = color![2]