Created
November 21, 2022 07:26
-
-
Save naoki-sawada/65caf59eb0ae027277d00e199155a237 to your computer and use it in GitHub Desktop.
This file contains 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
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