views:

741

answers:

2

I am getting a UIColor returned from this method:

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

and getting color like this:

SelectedColor=[(ColorPickerView *)alertView getColor];

Now I want to get red, green, blue from SelectedColor, in order to use those values. I want values between 0 and 1. Also when I use SelectedColor.CGColor, my application is crashing.

+1  A: 

I think you should have a a look here, where Ars' guide shows how to extend the UIColor class with support for accessing the color components.

unwind
+7  A: 

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));
PeyloW
thanks PeyloW.Do you know about image masking in ihpone?
Rahul Vyas
Like masking away rounded corners and such? Sure add a new question.
PeyloW