Skip to content

Instantly share code, notes, and snippets.

@douglashill
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save douglashill/2c3d70b65d171b2e36b5 to your computer and use it in GitHub Desktop.

Select an option

Save douglashill/2c3d70b65d171b2e36b5 to your computer and use it in GitHub Desktop.
@implementation NSColor (DHLuminance)
- (CGFloat)dh_luminance
{
return 0.3 * [self redComponent] + 0.6 * [self greenComponent] + 0.1 * [self blueComponent];
}
@end
// Example usage for an attributed string:
// NSForegroundColorAttributeName : [colour dh_luminance] > 0.45 ? [NSColor blackColor] : [NSColor colorWithWhite:0.9 alpha:1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment