Skip to content

Instantly share code, notes, and snippets.

@naoki-sawada
Created November 21, 2022 07:26
Show Gist options
  • Save naoki-sawada/65caf59eb0ae027277d00e199155a237 to your computer and use it in GitHub Desktop.
Save naoki-sawada/65caf59eb0ae027277d00e199155a237 to your computer and use it in GitHub Desktop.
import hexRgb from "hex-rgb";
// see: https://www.w3.org/TR/WCAG20/#relativeluminancedef
export function isDarkColor(hexColor: string): boolean {
const { red, green, blue } = hexRgb(hexColor);
const [r, g, b] = [red / 255, green / 255, blue / 255].map((v) =>
v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4)
);
const luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
return luminance <= 0.179;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment