Skip to content

Instantly share code, notes, and snippets.

@noudadrichem
Created September 7, 2020 07:02
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 noudadrichem/a638f7dd74254aee146d022a56e749bf to your computer and use it in GitHub Desktop.
Save noudadrichem/a638f7dd74254aee146d022a56e749bf to your computer and use it in GitHub Desktop.
Gives back tags based on RGB code if it's light or dark
public isColourDarkOrLight(rgb: number[]): 'light' | 'dark' {
// RGB to HSP equation: http://alienryderflex.com/hsp.html
const [r, g, b] = rgb;
const hsp = Math.sqrt(
0.299 * (r * r) +
0.587 * (g * g) +
0.114 * (b * b)
);
return hsp > 127.5 ? 'light' : 'dark';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment