Skip to content

Instantly share code, notes, and snippets.

@bjtitus
Created October 26, 2012 16:13
Show Gist options
  • Save bjtitus/3959689 to your computer and use it in GitHub Desktop.
Save bjtitus/3959689 to your computer and use it in GitHub Desktop.
UIColor contrast additions
//
// UIColor+ContrastAdditions
//
// Created by Brandon Titus (bjtitus.net) on 10/26/12.
//
//
#import <UIKit/UIKit.h>
#import "UIColor-Expanded.h"
// Clone it from here:
// http://github.com/ars/uicolor-utilities
@interface UIColor (ContrastAdditions)
//Uses the Hex color values to tell you whether this UIColor contrasts well with black
-(BOOL)contrastsBlack50;
// Converts the color components to YIQ http://en.wikipedia.org/wiki/YIQ and tells you whether this UIColor contrasts well with black
-(BOOL)contrastsBlackYIQ;
@end
//
// UIColor+ContrastAdditions.m
//
// Created by Brandon Titus (bjtitus.net) on 10/26/12.
//
// Released into the public domain
// Original idea: http://24ways.org/2010/calculating-color-contrast
#import "UIColor+ContrastAdditions.h"
@implementation UIColor (ContrastAdditions)
-(BOOL)contrastsBlack50
{
return ([self rgbHex] > 0xffffff/2);
}
-(BOOL)contrastsBlackYIQ
{
float yiq = (((self.red*256)*299) + ((self.green*256)*587) + ((self.blue*256)*114))/1000;
return (yiq >=128);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment