Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

distanceFromColor returns NAN #45

Open
dxclancy opened this issue Nov 5, 2015 · 0 comments
Open

distanceFromColor returns NAN #45

dxclancy opened this issue Nov 5, 2015 · 0 comments

Comments

@dxclancy
Copy link

dxclancy commented Nov 5, 2015

Thanks for you library!

For this line in distanceFromColor deltaH is NAN, which causes the result to be NAN.

CGFloat deltaH = sqrt(pow((A1 - A2), 2.0) + pow((B1 - B2), 2.0) - pow(deltaC, 2.0));

These were values the values in the debugger - I've had other combos trigger this.

(lldb) po self
UIDeviceRGBColorSpace 0.411765 0.176471 0.894118 1
(lldb) po color
UIDeviceRGBColorSpace 0.0655637 0.077451 0.0641544 1 

This is because the sqrt is being taken from is a very small negative number. Likely because of float/double precision issues?

In this case, i am setting deltaH to zero. Perhaps it would be more appropriate to take the sqrt of abs? Either way will result in a very small number later on when this result is used.

    CGFloat varA = pow((A1-A2), 2.0);
    CGFloat varB = pow((B1-B2), 2.0);
    CGFloat varC = pow(deltaC, 2.0);
    CGFloat varD = varA + varB - varC;
    CGFloat deltaH = sqrt(varD);   // varD negative
    if (isnan(deltaH))
    {
        deltaH = 0;
        NSLog(@"NAN");
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant