Last active
December 25, 2015 14:09
-
-
Save acalism/6988897 to your computer and use it in GitHub Desktop.
inverse color
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
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
inverse Color