Skip to content

Instantly share code, notes, and snippets.

@mtabini
Created November 26, 2010 16:23
Show Gist options
  • Save mtabini/716917 to your computer and use it in GitHub Desktop.
Save mtabini/716917 to your computer and use it in GitHub Desktop.
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;
}
@chrene
Copy link

chrene commented Nov 25, 2013

awesome code :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment