From 06cc37cffaf278bef856e9cfe124b3a28ae34af5 Mon Sep 17 00:00:00 2001 From: tophf Date: Tue, 15 Feb 2022 23:18:01 +0300 Subject: [PATCH] fix theme detection in color picker --- js/color/color-mimicry.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/js/color/color-mimicry.js b/js/color/color-mimicry.js index c4152b3b..209c6792 100644 --- a/js/color/color-mimicry.js +++ b/js/color/color-mimicry.js @@ -18,7 +18,7 @@ function colorMimicry(el, targets, dummyContainer = document.body) { let numTotal = 0; const rootStyle = getStyle(document.documentElement); for (const k in targets) { - const base = {r: 255, g: 255, b: 255, a: 1}; + const base = {r: 0, g: 0, b: 0, a: 0}; blend(base, rootStyle[targets[k]]); colors[k] = base; numTotal++; @@ -45,6 +45,10 @@ function colorMimicry(el, targets, dummyContainer = document.body) { el.remove(); } for (const k in targets) { + const c = colors[k]; + if (!isOpaque(c)) { + blend(colors[k] = {r: 255, g: 255, b: 255, a: 1}, c); + } const {r, g, b, a} = colors[k]; colors[k] = `rgba(${r}, ${g}, ${b}, ${a})`; // https://www.w3.org/TR/AERT#color-contrast @@ -69,7 +73,7 @@ function colorMimicry(el, targets, dummyContainer = document.body) { base.b = Math.round(b * q1 + base.b * q2); base.a = mixedA; } - return Math.abs(base.a - 1) < 1e-3; + return isOpaque(base); } // speed-up for sequential invocations within the same event loop cycle @@ -86,4 +90,8 @@ function colorMimicry(el, targets, dummyContainer = document.body) { function clearCache() { styleCache.clear(); } + + function isOpaque({a}) { + return Math.abs(a - 1) < 1e-3; + } }