Skip to content

Instantly share code, notes, and snippets.

@kieranstartup
Created August 24, 2018 15:51
Show Gist options
  • Save kieranstartup/347a72480f5ed41eb3a76fae85a05232 to your computer and use it in GitHub Desktop.
Save kieranstartup/347a72480f5ed41eb3a76fae85a05232 to your computer and use it in GitHub Desktop.
// Credit to https://gist.github.com/apapirovski/2580052
// 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;
}
const luminance = hex2luma(hexGoesHere);
if (luminance > 125) {
// Make text black
} else {
//Make text white
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment