Fun With Colors

I found this Determining Ideal Text Colors and figured it couldn’t be too hard to convert to Cocoa. This is the result:

- (NSColor *)idealTextColor:(NSColor *) bkgrdColor
{
float threshold = 0.411765;
float red, green, blue, alpha;
[bkgrdColor getRed:&red green:&green blue:&blue alpha:&alpha];
float bgDelta = (red * 0.299 + green * 0.587 + blue * 0.114);
return 1.0 - bgDelta < threshold ? [[NSColor blackColor] autorelease] : [[NSColor whiteColor] autorelease];
}

Just give it the color of the background, and it returns an NSColor (either white or black) that gives the best text contrast. I’m going to and this to Horizon real soon now, it should make the cells easier to read.
If I’ve done something dumb in the code, please let me know. :P

Stumble it!

Leave a Reply