Skip to content

Instantly share code, notes, and snippets.

@nagelflorian
Created March 11, 2015 21:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nagelflorian/1197e0e5cccd16492cc0 to your computer and use it in GitHub Desktop.
Save nagelflorian/1197e0e5cccd16492cc0 to your computer and use it in GitHub Desktop.
Returns black or white based on relative luminance
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()
}
}
@nagelflorian
Copy link
Author

More infos on Relative Luminance here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment