Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hsingtism
Last active April 16, 2024 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsingtism/2a150db7f73ce49b6f8401dfb4e4f852 to your computer and use it in GitHub Desktop.
Save hsingtism/2a150db7f73ce49b6f8401dfb4e4f852 to your computer and use it in GitHub Desktop.
A function that calculates if text color should be black given a background color. (W3C compliant for all values)
function useBlack(r, g, b) {
const e = v => Math.pow(v / 255 + 0.055, 2.4)
r < 11 ? r /= 15496 : r = e(r) / 5.348631
g < 11 ? g /= 4606. : g = e(g) / 1.589931
b < 11 ? b /= 45631 : b = e(b) / 15.74957
return r + g + b > 0.17912878
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment