Skip to content

Instantly share code, notes, and snippets.

@upalexgill
Last active October 14, 2020 11:07
Show Gist options
  • Select an option

  • Save upalexgill/8bce3acbbec24a8ef24ab4185f223728 to your computer and use it in GitHub Desktop.

Select an option

Save upalexgill/8bce3acbbec24a8ef24ab4185f223728 to your computer and use it in GitHub Desktop.
const color = "rgb(0, 0, 0)"
const rgb = colour.match(/\d+/g) // rgb: [0: 0, 1: 0, 2: 0]
const getContrastColour = rgb => {
// http://www.w3.org/TR/AERT#color-contrast
const brightness = Math.round(
(parseInt(rgb[0], 10) * 299 +
parseInt(rgb[1], 10) * 587 +
parseInt(rgb[2], 10) * 114) /
1000
)
const contrastColour = brightness > 125 ? "#000" : "#fff"
return contrastColour
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment