The function should return a float, but it got messed up!

I'm going crazy here.

I have a function that should return a floating point 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;
}

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

I tried to get this value differently and it never works.

float originalHue = [self getHue:original];

leads to a building error, as it says: "incompatible types during initialization"

float *originalHue = [self getHue:original];

returns zero return.

I tried other ways, but it never gets the correct value.

Any thoughts?

Hi guys Andre

+3
source share
4 answers

? , (, id)?

, id, , float.

+5

, . . . , h = 0,0005, 0,0005, 0,00005 , . , , , , double/long.

+3

, :

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

:

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
0

It happened to me. But as it turned out, I was referring to an instance method from a class method that you should not do.

Basically, I mistakenly did:

+(int) someMethod {
     // stuff
     [self otherMethod];
}


-(int) otherMethod {
    // do something
}

So check (not in the case of OPs, but if you have a similar error) if this is a problem.

0
source

Source: https://habr.com/ru/post/1740128/


All Articles