Skip to content

Instantly share code, notes, and snippets.

@acalism
Last active December 25, 2015 14:09
Show Gist options
  • Save acalism/6988897 to your computer and use it in GitHub Desktop.
Save acalism/6988897 to your computer and use it in GitHub Desktop.
inverse color
// UIColor+InverseColor.h
@interface UIColor(InverseColor)
- (UIColor*)inverseColor;
@end
// UIColor+InverseColor.m
@implement UIColor(InverseColor)
- (UIColor*)inverseColor {
CGColorRef oldCGColor = self.CGColor;
NSInteger numOfComponents = CGColorGetNumberOfComponents(oldCGColor);
// can not invert - the only component is the alpha
// e.g. self == [UIColor groupTableViewBackgroundColor]
if (1 == numOfComponents) {
return [UIcolor colorWithColor:oldCGColor];
}
#if 0
CGFloat r, g, b, a;
[self getRed:&r green:&g blue:&b alpha:&a];
return [UIColor colorWithRed:1.f-r green:1.-g blue:1.-b alpha:a];
#endif
const CGFloat *oldCGColorComponents = CGColorGetComponents(oldCGColor);
CGFloat newCGColorComponents[numOfComponents];
int i = numOfComponents - 1;
newCGColorComponents[i] = oldCGColorComponents[i]; // alpha
while (i-- > 0) {
newCGColorComponents[i] = 1 - oldCGColorComponents[i];
}
CGColorRef newCGColor = CGColorCreate(CGColorGetColorSpace(oldCGColor), newCGColorComponents);
UIColor *newColor = [UIColor colorWithCGColor:newCGColor];
CGColorRelease(newCGColor);
return newColor;
}
@acalism
Copy link
Author

acalism commented Oct 15, 2013

inverse Color

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