views:

189

answers:

3

Hi guys,

I'm going crazy here.

I have a function that should return a float number:

- (float) getHue:(UIColor *)original
{
    NSLog(@"getHue");
    const CGFloat *componentColors = CGColorGetComponents(original.CGColor);

    float red = componentColors[0];
    float green = componentColors[1];
    float blue = componentColors[2];

    float h = 0.0f;
    float maxChannel = fmax(red, fmax(green, blue));
    float minChannel = fmin(red, fmin(green, blue));
    if (maxChannel == minChannel)
        h = 0.0f;
    else if (maxChannel == red)
        h = 0.166667f * (green - blue) / (maxChannel - minChannel) + 0.000000f;
    else if (maxChannel == green)
        h = 0.166667f * (blue - red) / (maxChannel - minChannel) + 0.333333f;
    else if (maxChannel == blue)
        h = 0.166667f * (red - green) / (maxChannel - minChannel) + 0.666667f;
    else
        h = 0.0f;

    if (h < 0.0f)
        h += 1.0f;

    NSLog(@"getHue results: %f", h);

    return h;
}

The NSLog will trace it correctly (i.e: 0.005), but the actual return value of the function is NULL.

I've tried getting that value in so many ways and it never works.

float originalHue = [self getHue:original];

results in a building error, as it says: "incompatible types in initialization"

float *originalHue = [self getHue:original];

results in a null return.

I've tried other ways, but it never actually gets the value properly.

Any thoughts?

Cheers guys, Andre

+3  A: 

I used your code but didn't find any issues. It is working fine for me. Just clean build your app once. Above code will work fine until h = 0.0005 if its beyond the value 0.0005 thats is 0.00005 or more, then you will get infinite value. So please check for it and do the necessary changes like using double/long instead.

Manjunath
I've done it. Same result :/Try using this:float *originalHue = [self getHue:original];NSLog(@"originalHue = %f", originalHue);Doesn't it return 0.000, even though the function logs the result correctly?
Andre
sometimes deleting the build directory and restarting XCode is also needed
f3r3nc
Above code will work fine until h = 0.0005 if its beyond the value 0.0005 thats is 0.00005 or more, then you will get infinite value. So please check for it and do the necessary changes like using double/long instead.
Manjunath
[self getHue:original]; NSLog(@"originalHue = %f", originalHue); will always print 0.000 only. Try to print the content of the pointer:NSLog(@"originalHue = %f", *originalHue); This will print exact value :) Or else you tell me which color exactly you are passsing to this method?
Manjunath
+4  A: 

Have you declared your method in the interface of your class? If so, did you per accident indicate a different return value (e.g. id)?

If it is not declared in your interface the compiler will treat the return value as id, so you would either need to declare it in the interface or cast the return value to a float.

frenetisch applaudierend
THAT WAS IT! Geez... that took forever. I'm sorry for my n00bish question, but I'm new to Objective-C.So just to make sure I understand the logic: every time I have a function that returns something, I should declare it in the Interface?
Andre
Nice clairvoyant debugging, and I learned something too.
nevan
hahaha good catch :) cheez…
Manjunath
If you implement a method (or a C function for that matter) which needs to be used outside the scope of your \*.m file you need to declare it in the header. Otherwise the compiler will let you use the method but does no checking of the types and automatically assumes that return values are of type id (which is a void\* and thus you could assign that to a float\*). If you need the method only in your \*.m file, just make sure it is above all methods that use it.
frenetisch applaudierend
A: 

Your code works fine for me using this to call it:

UIColor *aColor = [UIColor colorWithRed:0.3 green:0.1 blue:0.5 alpha:1.0];
float theHue = [self getHue:aColor];
NSLog(@"Float: %f", theHue);

Returns:

2010-04-07 11:49:33.242 Floating[8888:207] getHue
2010-04-07 11:49:33.243 Floating[8888:207] getHue results: 0.750000
2010-04-07 11:49:33.243 Floating[8888:207] Float: 0.750000
nevan