mrh89
mrh89

Reputation: 89

Issue with [UIColor getWhite:alpha:]

I'm trying to use get the white value of a UIColor by the following (redColor is just for example):

UIColor *col = [UIColor redColor];
CGFloat *white;

if([col getWhite:white alpha:nil])
{
    NSLog(@"worked");
}
else
{
    NSLog(@"didn't");
}

But this always prints "didn't", and I don't understand why. UIColor.h's definition says "If the receiver is of a compatible color space, any non-NULL parameters are populated and 'YES' is returned. Otherwise, the parameters are left unchanged and 'NO' is returned." so I'm presuming that the receiver is of a non-compatible color space.... But I don't know what that means. Any ideas?

Upvotes: 0

Views: 1245

Answers (1)

jbat100
jbat100

Reputation: 16827

You are passing a pointer to a random piece of memory (CGFloat *white;)

You should create a static CGFloat and pass a reference to it

UIColor *col = [UIColor redColor];
CGFloat white;

if([col getWhite:&white alpha:nil])
{
    NSLog(@"worked");
}
else
{
    NSLog(@"didn't");
}

It is possible that it will not be able to convert from grayscale, you can check by trying

UIColor *col = [UIColor colorWithWhite:1.0 alpha:1.0];
CGFloat white;

if([col getWhite:&white alpha:nil])
{
    NSLog(@"worked");
}
else
{
    NSLog(@"didn't");
}

Upvotes: 5

Related Questions