Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am getting a UIColor returned from this method:

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

and getting color like this:

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

Now I want to get red, green, blue from selectedColor, in order to use those values. I want values between 0 and 1.

like image 629
Rahul Vyas Avatar asked Sep 30 '09 08:09

Rahul Vyas


People also ask

How do I get RGB values from UIColor Swift?

let swiftColor = UIColor(red: 1, green: 165/255, blue: 0, alpha: 1) println("RGB Value is:"); println(swiftColor.

What is Alpha in UIColor?

alpha. The opacity value of the new color object, specified as a value from 0.0 to 1.0.

How do I change the RGB color in Swift?

In Objective-C, we use this code to set RGB color codes for views: #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] view.

What is Alpha in Argb?

Alpha indicates how opaque each pixel is and allows an image to be combined over others using alpha compositing. rgba(red, green, blue, alpha) The alpha value is declared as a decimal number from 0 to 1, where 0 is fully transparent and 1 is fully opaque.


1 Answers

The reason for the crash when accessing SelectedColor.CGColor could be that you do not retain the result from getColor, perhaps what you need is:

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

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

With this is mind getting the color components is really easy:

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)); 
like image 198
PeyloW Avatar answered Nov 01 '22 14:11

PeyloW