Created
November 26, 2010 16:23
-
-
Save mtabini/716917 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BOOL colorSimilarToColor(UIColor *left, UIColor *right) { | |
float tolerance = 0.05; // 5% | |
CGColorRef leftColor = [left CGColor]; | |
CGColorRef rightColor = [right CGColor]; | |
if (CGColorGetColorSpace(leftColor) != CGColorGetColorSpace(rightColor)) { | |
return FALSE; | |
} | |
int componentCount = CGColorGetNumberOfComponents(leftColor); | |
const float *leftComponents = CGColorGetComponents(leftColor); | |
const float *rightComponents = CGColorGetComponents(rightColor); | |
for (int i = 0; i < componentCount; i++) { | |
float difference = leftComponents[i] / rightComponents[i]; | |
if (fabs(difference - 1) > tolerance) { | |
return FALSE; | |
} | |
} | |
return TRUE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome code :)