Skip to content

Instantly share code, notes, and snippets.

@NeuronButter
Last active July 3, 2025 01:54
Show Gist options
  • Select an option

  • Save NeuronButter/d94d517715d73c8b7c328cd308641c6c to your computer and use it in GitHub Desktop.

Select an option

Save NeuronButter/d94d517715d73c8b7c328cd308641c6c to your computer and use it in GitHub Desktop.
Get Contrasting Black or White Colour (TS/JS)
// 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