From e4b1be591c2caceda947005acdaf1c6ed33f4939 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Fri, 21 Jan 2022 23:17:55 +0000 Subject: [PATCH] fix(theme): use alternative contrast algorithm --- src/mobx/stores/helpers/STheme.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/mobx/stores/helpers/STheme.ts b/src/mobx/stores/helpers/STheme.ts index c4ebb173..4bef8878 100644 --- a/src/mobx/stores/helpers/STheme.ts +++ b/src/mobx/stores/helpers/STheme.ts @@ -220,8 +220,11 @@ function getContrastingColour(hex: string, fallback?: string): string { const colour = rgba(hex); 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; - return (r / 255) * 0.299 + (g / 255) * 0.587 + (b / 255) * 0.114 >= 0.186 - ? "black" - : "white"; + // const hsp = Math.sqrt(0.299 * r ** 2 + 0.587 * g ** 2 + 0.114 * b ** 2); + // Using Skia numbers. + const hsp = Math.sqrt(0.2126 * r ** 2 + 0.7152 * g ** 2 + 0.0722 * b ** 2); + return hsp > 175 ? "black" : "white"; }