Last active
July 3, 2025 01:54
-
-
Save NeuronButter/d94d517715d73c8b7c328cd308641c6c to your computer and use it in GitHub Desktop.
Get Contrasting Black or White Colour (TS/JS)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // modified version of https://hackmd.io/@Markdown-It/HJeV6339X | |
| export function getContrastYIQ(hexcolor: string): string { | |
| const hex = hexcolor.replace('#', '') | |
| const r = parseInt(hex.slice(0, 2), 16) | |
| const g = parseInt(hex.slice(2, 4), 16) | |
| const b = parseInt(hex.slice(4, 6), 16) | |
| const yiq = (r * 299 + g * 587 + b * 114) / 1000 | |
| return yiq >= 120 ? 'black' : 'white' | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment