How to get red, green, blue (RGB) and alpha back from a UIColor object?

I get the UIColor returned from this method:

- (UIColor *)getUserSelectedColor { return [UIColor colorWithRed:redSlider.value green:greenSlider.value blue:blueSlider.value alpha:1.0]; } 

and get a color like this:

 UIColor *selectedColor = [(ColorPickerView *)alertView getUserSelectedColor]; 

Now I want to get red, green, blue from selectedColor to use these values. I want values ​​from 0 to 1.

+49
objective-c iphone cocoa-touch rgb uicolor
Sep 30 '09 at 8:07
source share
9 answers

The reason for the crash when accessing SelectedColor.CGColor may be that you are not saving the result from getColor , maybe you need to:

 SelectedColor = [[(ColorPickerView *)alertView getColor] retain]; 

You can only get the RGB color component from UIColor , which uses the RGB color space, since you are using colorWithRed:green:blue:alpha: which is not a problem, but change this value if your code changes.

With this mind, getting color components is very simple:

 const CGFloat* components = CGColorGetComponents(SelectedColor.CGColor); NSLog(@"Red: %f", components[0]); NSLog(@"Green: %f", components[1]); NSLog(@"Blue: %f", components[2]); NSLog(@"Alpha: %f", CGColorGetAlpha(SelectedColor.CGColor)); 
+110
Sep 30 '09 at 8:23
source share

This solution also works for colors other than RGB. black or white.

 UIColor *color = [UIColor blackColor]; CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0; // iOS 5 if ([color respondsToSelector:@selector(getRed:green:blue:alpha:)]) { [color getRed:&red green:&green blue:&blue alpha:&alpha]; } else { // < iOS 5 const CGFloat *components = CGColorGetComponents(color.CGColor); red = components[0]; green = components[1]; blue = components[2]; alpha = components[3]; } // This is a non-RGB color if(CGColorGetNumberOfComponents(color.CGColor) == 2) { CGFloat hue; CGFloat saturation; CGFloat brightness; [color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]; } 
+22
Jul 22 '12 at 10:21
source share

In most cases, this will work if the conversion to RGB does not work.

 float red, green, blue, alpha; BOOL conversionToRGBWentOk = [color getRed:&red green:&green blue:&blue alpha:&alpha]; 

What these methods are for, really. If conversionToRGBWentOk is NO , you will have a problem.

+7
Sep 11 '12 at 21:08
source share

I think you need to look here where the Ars tutorial shows how to extend the UIColor class with access support for color components.

+4
Sep 30 '09 at 8:13
source share

you just just do it

 CGFloat red,green,blue,alpha; [UIColorobject getRed:&red green:&green blue:&blue alpha:&alpha]; 

in red, green, blue and alpha you get the rgb value if you have questions please ask ...

thank

+2
Jun 10 '14 at 1:37
source share

This code snippet should work with both RGB and grayscale:

 CGFloat *components = (CGFloat *) CGColorGetComponents(<UIColor instance>.CGColor); if(CGColorGetNumberOfComponents(<UIColor instance>.CGColor) == 2) { //assuming it is grayscale - copy the first value components[2] = components[1] = components[0]; } 
+1
May 30 '11 at 11:12
source share

Just add the ColorLiteral property, as shown in the example. Xcode will offer you a complete list of colors that you can choose.

 self.view.backgroundColor = ColorLiteral 
0
Sep 28 '17 at 10:05
source share

I think that would be the way to go. If you also need to use the alpha parameter, you can interpolate the alpha from the input, as for RG and B.

 - (UIColor *)getColorBetweenColor:(UIColor *)color1 andColor:(UIColor *)color2 percentage:(CGFloat)percent { CGFloat red1, green1, blue1, alpha1; CGFloat red2, green2, blue2, alpha2; [color1 getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1]; [color2 getRed:&red2 green:&green2 blue:&blue2 alpha:&alpha2]; double resultRed = red1 + percent * (red2 - red1); double resultGreen = green1 + percent * (green2 - green1); double resultBlue = blue1 + percent * (blue2 - blue1); return [UIColor colorWithRed:resultRed green:resultGreen blue:resultBlue alpha:1]; } 
0
Nov 09 '17 at 7:40
source share

Here are some useful macros I made for this and other color controls:

In your case, you would just use

 getRGBA(myColor, red, green, blue, alpha); NSLog(@"Red Value: %f", red); NSLog(@"Blue Value: %f", green); NSLog(@"Green Value: %f", blue); 



Macros:

 #define rgba(r,g,b,a) [UIColor colorWithRed:((float)(r))/255.0f green:((float)(g))/255.0f blue:((float)(b))/255.0f alpha:a] #define rgb(r,g,b) rgba(r, g, b, 1.0f) #define rgbaf(r,g,b,a) [UIColor colorWithRed:(r) green:(g) blue:(b) alpha:a] #define rgbf(r,g,b) rgbaf(r, g, b, 1.0f) #define rgba_fromColor(__color, __r, __g, __b, __a) \ CGFloat __r, __g, __b, __a;\ UIColor *__unpackedColor = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:__color]];/*Bring system colors into compatible color-space (eg DarkGrayColor)*/\ [__unpackedColor getRed:&__r green:&__g blue:&__b alpha:&__a]; #define getRGBA(__color, __r, __g, __b, __a) rgba_fromColor(__color, __r, __g, __b, __a) #define getRed(__color) (\ (^float (void){\ rgba_fromColor(__color, r, g, b, a);\ return r;\ })()\ ) #define getGreen(__color) (\ (^float (void){\ rgba_fromColor(__color, r, g, b, a);\ return g;\ })()\ ) #define getBlue(__color) (\ (^float (void){\ rgba_fromColor(__color, r, g, b, a);\ return b;\ })()\ ) #define getAlpha(__color) (\ (^float (void){\ rgba_fromColor(__color, r, g, b, a);\ return a;\ })()\ ) #define hsba(h,s,b,a) [UIColor colorWithHue:((float)(h))/360.0f saturation:((float)(s))/100.0f brightness:((float)(b))/100.0f alpha:a] #define hsb(h,s,b) hsba(h, s, b, 1.0f) #define hsbaf(h,s,b,a) [UIColor colorWithHue:(h) saturation:(s) brightness:(b) alpha:a] #define hsbf(h,s,b) rgbaf(h, s, b, 1.0f) #define hsba_fromColor(__color, __h, __s, __b, __a) \ CGFloat __h, __s, __b, __a;\ UIColor *__unpackedColor = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:__color]];/*Bring system colors into compatible color-space (eg DarkGrayColor)*/\ [__unpackedColor getHue:&__h saturation:&__s brightness:&__b alpha:&__a]; #define getHSBA(__color, __h, __s, __b, __a) hsba_fromColor(__color, __h, __s, __b, __a) #define getHue(__color) (\ (^float (void){\ hsba_fromColor(__color, h, s, b, a);\ return h;\ })()\ ) #define getSaturation(__color) (\ (^float (void){\ hsba_fromColor(__color, h, s, b, a);\ return s;\ })()\ ) #define getBrightness(__color) (\ (^float (void){\ hsba_fromColor(__color, h, s, b, a);\ return b;\ })()\ ) /* ///already defined in RGBA macros #define getAlpha(__color) (\ (^float (void){\ hsba_fromColor(__color, h, s, b, a);\ return a;\ })()\ ) */ 
0
Jul 27 '18 at 6:51
source share



All Articles