Skip to content

Instantly share code, notes, and snippets.

@liquorice
Created August 11, 2014 06:55
Show Gist options
  • Save liquorice/052294c8237f0b47b48e to your computer and use it in GitHub Desktop.
Save liquorice/052294c8237f0b47b48e to your computer and use it in GitHub Desktop.
Determine if a colour appears light or dark
var light_or_dark = function(hex) {
var rgb = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex),
luma = Math.sqrt(
0.299 * parseInt(rgb[1], 16) +
0.587 * parseInt(rgb[2], 16) +
0.144 * parseInt(rgb[3], 16)
);
return (luma > 13) ? "light" : "dark";
};
@liquorice
Copy link
Author

Useful for picking between light/dark text to go over a block colour.

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