Skip to content

Instantly share code, notes, and snippets.

@itanka9
Created November 8, 2017 11:18
Show Gist options
  • Select an option

  • Save itanka9/73f0823d6acdabf724dad111c34fa434 to your computer and use it in GitHub Desktop.

Select an option

Save itanka9/73f0823d6acdabf724dad111c34fa434 to your computer and use it in GitHub Desktop.
function getContrastColor (color) {
const [RED, GREEN, BLUE] = [0, 1, 2]
const fraction = (val, pos) => parseInt(val.slice(pos * 2, pos * 2 + 2), 16)
const contrast = (fraction(color, RED) * 299 + fraction(color, GREEN) * 587 + fraction(color, BLUE) * 114) / 1000
if (contrast > 180) {
return 'rgba(0, 0, 0, 0.8)'
} else {
return 'rgba(255, 255, 255, 0.8)'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment