Is there a conventional method for inverting NSColor values?
Simple: The components are all 0–1, so subtract each component from 1 to get the complement's value.
Examples:
- Red = 1, 0, 0; complement = 0, 1, 1 = cyan
- Yellow = 1, 1, 0; complement = 0, 0, 1 = blue
- White = 1, 1, 1; complement = 0, 0, 0 = black
- Deep orange = 1, 0.25, 0; complement = 0, 0.75, 1 = sky blue
As you can guess, this is a symmetric operation. Inverting the inverse will get you exactly the color you started with.
From what you describe, it sounds like what you really want is a function to find a color's complement, not its inverse. Inverting means something different in this context.
Here's a StackOverflow question regarding complements in JavaScript. Perhaps some of the code is adaptable?
Here is some interesting and potentially useful info about the various ways color can be worked with in Cocoa color spaces. From what it looks like here, if you can use the HSV color space, then the complement of a color could be found by simply taking hue > 179 ? hue -= 180 : hue = += 180
, since the hues are defined around the color wheel in a full circle.
Here is some oneliner with custom setter (alpha is not reversed!):
-(void)setReverseFontColor:(UIColor *)reverseFontColor {
_reverseFontColor = [AppSingleStyle reversedColorFromOriginal:reverseFontColor];
}
+(UIColor*)reversedColorFromOriginal:(UIColor*)originalColor {
return [UIColor colorWithRed:(1-CGColorGetComponents(originalColor.CGColor)[0]) green:(1-CGColorGetComponents(originalColor.CGColor)[1]) blue:(1-CGColorGetComponents(originalColor.CGColor)[2]) alpha:CGColorGetAlpha(originalColor.CGColor)];
}