From 5badd3f35d8790d306e2b05b6b0e938df5f099ab Mon Sep 17 00:00:00 2001 From: Chip Cullen Date: Mon, 6 Apr 2026 14:52:41 -0400 Subject: [PATCH 1/5] Using color.js for the logic --- package.json | 1 + src/components/Swatch.tsx | 50 ++--- src/utils/colorStringToArray.test.ts | 9 - src/utils/colorStringToArray.ts | 18 -- src/utils/formatColor.test.ts | 11 - src/utils/formatColor.ts | 32 --- src/utils/isLchOutOfRgbGamut.ts | 22 +- src/utils/isValidColor.test.ts | 16 +- src/utils/isValidColor.ts | 117 ++++------- src/utils/toHex.ts | 149 -------------- src/utils/toHex8.test.ts | 9 - src/utils/toHex8.ts | 40 ---- src/utils/toHsl.test.ts | 21 -- src/utils/toHsl.ts | 73 ------- src/utils/toHsla.test.ts | 16 -- src/utils/toHsla.ts | 66 ------ src/utils/toLch.test.ts | 22 -- src/utils/toLch.ts | 51 ----- src/utils/toRgb.test.ts | 105 ---------- src/utils/toRgb.ts | 135 ------------ src/utils/toRgba.test.ts | 123 ----------- src/utils/toRgba.ts | 174 ---------------- src/utils/translatedColor.ts | 283 +++++++------------------ src/utils/w3conversions.ts | 297 --------------------------- yarn.lock | 5 + 25 files changed, 157 insertions(+), 1688 deletions(-) delete mode 100644 src/utils/colorStringToArray.test.ts delete mode 100644 src/utils/colorStringToArray.ts delete mode 100644 src/utils/formatColor.test.ts delete mode 100644 src/utils/formatColor.ts delete mode 100644 src/utils/toHex.ts delete mode 100644 src/utils/toHex8.test.ts delete mode 100644 src/utils/toHex8.ts delete mode 100644 src/utils/toHsl.test.ts delete mode 100644 src/utils/toHsl.ts delete mode 100644 src/utils/toHsla.test.ts delete mode 100644 src/utils/toHsla.ts delete mode 100644 src/utils/toLch.test.ts delete mode 100644 src/utils/toLch.ts delete mode 100644 src/utils/toRgb.test.ts delete mode 100644 src/utils/toRgb.ts delete mode 100644 src/utils/toRgba.test.ts delete mode 100644 src/utils/toRgba.ts delete mode 100644 src/utils/w3conversions.ts diff --git a/package.json b/package.json index 03b87ed..659bb1d 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "@types/react": "^19.2.14", "@types/react-dom": "^19.2.3", "@vitejs/plugin-react": "^6.0.1", + "colorjs.io": "^0.6.1", "happy-dom": "^20.8.9", "query-string": "^9.3.1", "react": "^19.2.4", diff --git a/src/components/Swatch.tsx b/src/components/Swatch.tsx index 4124c4d..a64fd72 100644 --- a/src/components/Swatch.tsx +++ b/src/components/Swatch.tsx @@ -1,7 +1,6 @@ import * as React from "react"; +import Color from 'colorjs.io'; import { colorTypes } from '../utils/colorTypes'; -import { toRgba } from '../utils/toRgba'; -import { formatColor } from '../utils/formatColor'; type SwatchProps = { color: string; @@ -9,39 +8,40 @@ type SwatchProps = { }; const Swatch: React.FC = props => { - const { - color, - colorType - } = props; + const { color, colorType } = props; if (colorType === colorTypes.lch) { - // react doesn't support lch colors, so we have to use dangerouslySetInnerHTML - const rgbaFallback = formatColor(toRgba(color, colorType), colorTypes.rgba); + let rgbaFallback = 'rgba(0 0 0 / 1)'; + try { + const c = new Color(color).to('srgb'); + const [r, g, b] = c.coords.map((v: number) => Math.round(v * 255)); + rgbaFallback = `rgba(${r} ${g} ${b} / ${c.alpha})`; + } catch { /* use default */ } + return ( <> -
-
+
+
+