Pheu Verg
Pheu Verg

Reputation: 230

UIColor doesn't work with it's own properties

If I use the color directly, it works. But if i want to save the colour value in rgb and alpha it doesn't work. It doesn't draw anything.

UIColor *aaa = [UIColor grayColor];
float r,g,b,a;

[aaa getRed:&r green:&g blue:&b alpha:&a];
aaa = [UIColor colorWithRed:r green:g blue:b alpha:a];

CGColorRef col = aaa.CGColor;

How can I save my rgb?

Upvotes: 2

Views: 304

Answers (2)

Phillip Mills
Phillip Mills

Reputation: 31016

I believe your problem is that grayColor is not defined with a colorspace that's compatible with the getRed:green:blue:alpha: method. You can verify this by initializing the rgba values to something impossible for a color and noting that they are not changed by the call, which matches the documented return behavior. Or, replace grayColor with something that explicitly includes colors, such as cyanColor and the getRed:green:blue:alpha: method works.

Here's a bit of code to demonstrate:

float r = 5.0,g = 6.0,b = 7.0,a = 8.0, w = 3.0, a2 = 4.0;
UIColor *aaa = [UIColor grayColor];
[aaa getRed:&r green:&g blue:&b alpha:&a];
[aaa getWhite:&w alpha:&a2];

(On OS X there are methods for converting between colorspaces but I'm not seeing iOS equivalents.)

Upvotes: 2

Hugo
Hugo

Reputation: 496

You have to initialize the floats, otherwise the references &f, &g, &b, &a reference nothing:

UIColor *aaa = [UIColor grayColor];
float r = 0.0f, g = 0.0f, b = 0.0f, a = 0.0f;

[aaa getRed:&r green:&g blue:&b alpha:&a];
aaa = [UIColor colorWithRed:r green:g blue:b alpha:a];

CGColorRef col = aaa.CGColor;

Upvotes: -1

Related Questions