Browse Source

Add shadow and tweak dark/light logic

Kirk Trombley 2 years ago
parent
commit
f59c1a1933
3 changed files with 15 additions and 4 deletions
  1. 3 1
      math.js
  2. 3 1
      script.js
  3. 9 2
      styles.css

+ 3 - 1
math.js

@@ -11,7 +11,9 @@ const clamp = (mn, v, mx) => Math.min(Math.max(v, mn), mx);
 // Contrast
 const getContrastingTextColor = (hex) => {
   const { r, g, b } = d3.color(hex);
-  return vectorDot([r, g, b], [0.3, 0.6, 0.1]) >= 128 ? "#222" : "#ddd";
+  return vectorDot([r, g, b], [0.3, 0.6, 0.1]) >= 128
+    ? "var(--color-dark)"
+    : "var(--color-light)";
 };
 
 // "Visual Importance"

+ 3 - 1
script.js

@@ -81,7 +81,9 @@ const getColorButtonStyle = (hex) => {
 targetColor.subscribe((hex, { previous }) => {
   const style = document.querySelector(":root").style;
   style.setProperty("--background", hex);
-  style.setProperty("--highlight", getContrastingTextColor(hex));
+  const highlight = getContrastingTextColor(hex);
+  style.setProperty("--highlight", highlight);
+  style.setProperty("--shadow-component", highlight.includes("light") ? "255" : "0");
 
   if (previous) {
     const prevButton = document.createElement("button");

+ 9 - 2
styles.css

@@ -44,8 +44,15 @@ select {
 
 :root {
   /* separated since these are used by the actual script */
-  --highlight: #ddd;
-  --background: #222;
+  --highlight: var(--color-light);
+  --background: var(--color-dark);
+  --shadow-component: 255;
+  --shadow: rgb(
+    var(--shadow-component),
+    var(--shadow-component),
+    var(--shadow-component),
+    0.2
+  );
 }
 
 /* Font */