fix(theme): use alternative contrast algorithm

This commit is contained in:
Paul Makles 2022-01-21 23:17:55 +00:00
parent 50b0e0f5f2
commit e4b1be591c

View file

@ -220,8 +220,11 @@ function getContrastingColour(hex: string, fallback?: string): string {
const colour = rgba(hex); const colour = rgba(hex);
if (!colour) return fallback ? getContrastingColour(fallback) : "black"; if (!colour) return fallback ? getContrastingColour(fallback) : "black";
// https://awik.io/determine-color-bright-dark-using-javascript/
// http://alienryderflex.com/hsp.html
const [r, g, b] = colour; const [r, g, b] = colour;
return (r / 255) * 0.299 + (g / 255) * 0.587 + (b / 255) * 0.114 >= 0.186 // const hsp = Math.sqrt(0.299 * r ** 2 + 0.587 * g ** 2 + 0.114 * b ** 2);
? "black" // Using Skia numbers.
: "white"; const hsp = Math.sqrt(0.2126 * r ** 2 + 0.7152 * g ** 2 + 0.0722 * b ** 2);
return hsp > 175 ? "black" : "white";
} }