Skip to content

Instantly share code, notes, and snippets.

@adamdehaven
Created May 14, 2020 12:23
Show Gist options
  • Select an option

  • Save adamdehaven/4b2f8dcca5161ad46e544ecb0a5c5918 to your computer and use it in GitHub Desktop.

Select an option

Save adamdehaven/4b2f8dcca5161ad46e544ecb0a5c5918 to your computer and use it in GitHub Desktop.
Determine the inverse color (white or black) for a given RGB value
function getRgbInverse(red, green, blue) {
const rgb = [parseInt(red), parseInt(green), parseInt(blue)]
let hsp = Math.sqrt(
0.299 * (rgb[0] * rgb[0]) +
0.587 * (rgb[1] * rgb[1]) +
0.114 * (rgb[2] * rgb[2])
)
// Determine if text color for background should be light or dark
if (hsp > 127.5) {
return [0, 0, 0] // black
} else {
return [255, 255, 255] // white
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment