Skip to content

Instantly share code, notes, and snippets.

@apapirovski
Created May 2, 2012 20:16
Show Gist options
  • Save apapirovski/2580052 to your computer and use it in GitHub Desktop.
Save apapirovski/2580052 to your computer and use it in GitHub Desktop.
Calculate luma (brightness) of a hexadecimal colour
// Calculate luma (brightness) of a hexadecimal colour
// ---------------------------------------------------
// You can use this to calculate whether two colours
// are suitable to be used as a background and text
function hex2luma(hex) {
return (
(parseInt(hex.substring(0, 2), 16) * 299) +
(parseInt(hex.substring(2, 4), 16) * 587) +
(parseInt(hex.substring(4, 6), 16) * 114)
) / 1000;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment