From e90348eb1a2e84f2589d46bb5b4abb3f939ede9c Mon Sep 17 00:00:00 2001 From: agatemosu Date: Wed, 24 Jul 2024 09:37:13 +0200 Subject: [PATCH] Change the text color if the bg is dark --- script.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/script.js b/script.js index 8e9ebce..470eeb0 100644 --- a/script.js +++ b/script.js @@ -57,16 +57,17 @@ function createColorPicker(colorPicker, tierLabel, defaultColor) { }); pickr.on("save", (color) => { - let selectedColor; - if (color === null) { pickr.setColor(clearColor); - selectedColor = clearColor; - } else { - selectedColor = color.toHEXA().toString(); + return; } - tierLabel.style.backgroundColor = selectedColor; + const hsl = color.toHSLA(); + const lightness = hsl[2]; + + tierLabel.style.backgroundColor = color.toHEXA().toString(); + tierLabel.style.color = lightness < 50 ? "white" : "black"; + pickr.hide(); }); }