Created
March 11, 2015 21:54
-
-
Save nagelflorian/1197e0e5cccd16492cc0 to your computer and use it in GitHub Desktop.
Returns black or white based on relative luminance
This file contains hidden or 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
func strongestContrast(color: UIColor) -> UIColor { | |
let colorRef: CGColorRef = color.CGColor | |
let components = CGColorGetComponents(colorRef) | |
let relativeLuminance = 1 - (0.2126 * components[0] + 0.7152 * components[1] + 0.0722 * components[2]) | |
if (relativeLuminance >= 0.5) { | |
return UIColor.whiteColor() | |
} else { | |
return UIColor.blackColor() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More infos on Relative Luminance here.