diff --git a/package-lock.json b/package-lock.json index 5c85c222..3330e8b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35,9 +35,6 @@ "motion": "^14.9.0", "pdfjs-dist": "^6.5.438", "qrcode": "^1.5.4", - "react": "^19.3.1", - "react-best-gradient-color-picker": "^3.0.14", - "react-dom": "^19.3.1", "semver": "^7.7.1", "sortablejs": "^1.15.7", "svelte": "^5.57.3", @@ -57,8 +54,6 @@ "@types/mime-types": "^3.0.1", "@types/node": "^26.6.6", "@types/qrcode": "^1.5.6", - "@types/react": "^19.3.0", - "@types/react-dom": "^19.3.0", "@types/semver": "^7.8.0", "@types/sortablejs": "^1.15.9", "@types/webextension-polyfill": "^0.12.6", @@ -4306,26 +4301,6 @@ "@types/node": "*" } }, - "node_modules/@types/react": { - "version": "19.3.0", - "resolved": "https://registry.npmjs.org/@types/react/-/react-19.3.0.tgz", - "integrity": "sha512-N0rFCuH9YoxG9/m61l9MfpJKfmLOVU0em7ipIz6TRgSSkvReLB9vL85GB+yr8Bs5leqpvg96JSwF4ZS1s4viQg==", - "devOptional": true, - "license": "MIT", - "dependencies": { - "csstype": "^3.2.2" - } - }, - "node_modules/@types/react-dom": { - "version": "19.3.0", - "resolved": "https://registry.npmjs.org/@types/react-dom/-/react-dom-19.3.0.tgz", - "integrity": "sha512-ZI7bU42mZXXKHn/qNLEw2IrbiINU7X5+vfgdixBHkCNpYWXjKgfQ/P+uyGb5CjOLB9UcnTeg3rylQtV2hym44Q==", - "dev": true, - "license": "MIT", - "peerDependencies": { - "@types/react": "^19.3.0" - } - }, "node_modules/@types/semver": { "version": "7.8.0", "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.8.0.tgz", @@ -5279,15 +5254,6 @@ "node": "18 || 20 || >=22" } }, - "node_modules/base64-arraybuffer": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-1.0.2.tgz", - "integrity": "sha512-I3yl4r9QB5ZRY3XuJVEPfc2XhZO6YweFPI+UovAzn+8/hb3oJ6lnysaFcjVpkCPfVWFUDvoZ8kmVDP7WyRtYtQ==", - "license": "MIT", - "engines": { - "node": ">= 0.6.0" - } - }, "node_modules/baseline-browser-mapping": { "version": "2.11.34", "resolved": "https://registry.npmjs.org/baseline-browser-mapping/-/baseline-browser-mapping-2.11.34.tgz", @@ -5705,15 +5671,6 @@ "node": ">= 8" } }, - "node_modules/css-line-break": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/css-line-break/-/css-line-break-2.1.0.tgz", - "integrity": "sha512-FHcKFCZcAha3LwfVBhCQbW2nCNbkZXn7KVUJcsT5/P8YmfsVja0FMPJr0B903j/E69HUphKiV9iQArX8SDYA4w==", - "license": "MIT", - "dependencies": { - "utrie": "^1.0.2" - } - }, "node_modules/css-select": { "version": "5.2.2", "resolved": "https://registry.npmjs.org/css-select/-/css-select-5.2.2.tgz", @@ -5758,13 +5715,6 @@ "node": ">=18" } }, - "node_modules/csstype": { - "version": "3.2.3", - "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.2.3.tgz", - "integrity": "sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==", - "devOptional": true, - "license": "MIT" - }, "node_modules/d3-array": { "version": "3.2.4", "resolved": "https://registry.npmjs.org/d3-array/-/d3-array-3.2.4.tgz", @@ -7315,19 +7265,6 @@ "dev": true, "license": "MIT" }, - "node_modules/html2canvas": { - "version": "1.4.1", - "resolved": "https://registry.npmjs.org/html2canvas/-/html2canvas-1.4.1.tgz", - "integrity": "sha512-fPU6BHNpsyIhr8yyMpTLLxAbkaK8ArIBcmZIRiBLiDhjeqvXolaEmDGmELFuX9I4xDcaKKcJl+TKZLqruBbmWA==", - "license": "MIT", - "dependencies": { - "css-line-break": "^2.1.0", - "text-segmentation": "^1.0.3" - }, - "engines": { - "node": ">=8.0.0" - } - }, "node_modules/http-proxy-agent": { "version": "7.0.2", "resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-7.0.2.tgz", @@ -8735,12 +8672,6 @@ "dev": true, "license": "MIT" }, - "node_modules/lodash.throttle": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/lodash.throttle/-/lodash.throttle-4.1.1.tgz", - "integrity": "sha512-wIkUCfVKpVsWo3JSZlc+8MB5it+2AN5W8J7YVMST30UrvcQNZ1Okbj+rbVniijTWE6FGYy4XJq/rHkas8qJMLQ==", - "license": "MIT" - }, "node_modules/long": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/long/-/long-5.3.2.tgz", @@ -9805,48 +9736,6 @@ "node": ">=6" } }, - "node_modules/react": { - "version": "19.3.1", - "resolved": "https://registry.npmjs.org/react/-/react-19.3.1.tgz", - "integrity": "sha512-QNhWdt7nrgVWWQG58QuGsAR3jv3Hbe4mb4vz7w2csnHavciZJgda1CPNaldF6eVxv90ud3jUXpKlHL69gO38yw==", - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/react-best-gradient-color-picker": { - "version": "3.0.14", - "resolved": "https://registry.npmjs.org/react-best-gradient-color-picker/-/react-best-gradient-color-picker-3.0.14.tgz", - "integrity": "sha512-UYY7PRNUWMAS02Migb4jGSFFo3jFkYIck8cKiJR4IJUermGq5s4dNoi/s5xVsgK0MKSV4j4cJxyOIoFuw7st4w==", - "license": "MIT", - "dependencies": { - "html2canvas": "^1.4.1", - "lodash.throttle": "^4.1.1", - "tinycolor2": "1.4.2" - }, - "peerDependencies": { - "@types/react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0", - "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0", - "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" - }, - "peerDependenciesMeta": { - "@types/react": { - "optional": true - } - } - }, - "node_modules/react-dom": { - "version": "19.3.1", - "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.3.1.tgz", - "integrity": "sha512-Y0bARz53lhEC1NMCASs0/j+faB/zqt3R8hpvg1e8+61ZVJ4UL0UdC4cBbXfZMcIuhrfgeILTLe+ZeBCavQPtKQ==", - "license": "MIT", - "dependencies": { - "scheduler": "^0.28.0" - }, - "peerDependencies": { - "react": "^19.3.1" - } - }, "node_modules/readdirp": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-5.1.1.tgz", @@ -10069,12 +9958,6 @@ "node": ">=v12.22.7" } }, - "node_modules/scheduler": { - "version": "0.28.0", - "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.28.0.tgz", - "integrity": "sha512-juorfCmIkIw8tT+p5BXSm6PJjQF/ycEYmKyzURCIt/RaZIhL+PulbQ9Yu2z1HdOJDdqDTlxA1+xKBmHXJsczAw==", - "license": "MIT" - }, "node_modules/seedrandom": { "version": "3.0.5", "resolved": "https://registry.npmjs.org/seedrandom/-/seedrandom-3.0.5.tgz", @@ -10485,30 +10368,12 @@ "node": ">=18" } }, - "node_modules/text-segmentation": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/text-segmentation/-/text-segmentation-1.0.3.tgz", - "integrity": "sha512-iOiPUo/BGnZ6+54OsWxZidGCsdU8YbE4PSpdPinp7DeMtUJNJBoJ/ouUSTJjHkh1KntHaltHl/gDs2FC4i5+Nw==", - "license": "MIT", - "dependencies": { - "utrie": "^1.0.2" - } - }, "node_modules/tiny-emitter": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/tiny-emitter/-/tiny-emitter-2.1.0.tgz", "integrity": "sha512-NB6Dk1A9xgQPMoGqC5CVXn123gWyte215ONT5Pp5a0yt4nlEoO1ZWeCwpncaekPHXO60i47ihFnZPiRPjRMq4Q==", "license": "MIT" }, - "node_modules/tinycolor2": { - "version": "1.4.2", - "resolved": "https://registry.npmjs.org/tinycolor2/-/tinycolor2-1.4.2.tgz", - "integrity": "sha512-vJhccZPs965sV/L2sU4oRQVAos0pQXwsvTLkWYdqJ+a8Q5kPFzJTuOFwy7UniPli44NKQGAglksjvOcpo95aZA==", - "license": "MIT", - "engines": { - "node": "*" - } - }, "node_modules/tinyglobby": { "version": "0.2.17", "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.17.tgz", @@ -10847,15 +10712,6 @@ "punycode": "^2.1.0" } }, - "node_modules/utrie": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/utrie/-/utrie-1.0.2.tgz", - "integrity": "sha512-1MLa5ouZiOmQzUbjbu9VmjLzn1QLXBhwpUa7kdLUQK+KQ5KA9I1vk5U4YHe/X2Ch7PYnJfWuWT+VbuxbGwljhw==", - "license": "MIT", - "dependencies": { - "base64-arraybuffer": "^1.0.2" - } - }, "node_modules/v8-to-istanbul": { "version": "9.3.0", "resolved": "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-9.3.0.tgz", diff --git a/package.json b/package.json index 1f350745..6e9544dd 100644 --- a/package.json +++ b/package.json @@ -50,8 +50,6 @@ "@types/mime-types": "^3.0.1", "@types/node": "^26.6.6", "@types/qrcode": "^1.5.6", - "@types/react": "^19.3.0", - "@types/react-dom": "^19.3.0", "@types/semver": "^7.8.0", "@types/sortablejs": "^1.15.9", "@types/webextension-polyfill": "^0.12.6", @@ -97,9 +95,6 @@ "motion": "^14.9.0", "pdfjs-dist": "^6.5.438", "qrcode": "^1.5.4", - "react": "^19.3.1", - "react-best-gradient-color-picker": "^3.0.14", - "react-dom": "^19.3.1", "semver": "^7.7.1", "sortablejs": "^1.15.7", "svelte": "^5.57.3", diff --git a/src/interface/components/ColourPicker.css b/src/interface/components/ColourPicker.css deleted file mode 100644 index 85461f77..00000000 --- a/src/interface/components/ColourPicker.css +++ /dev/null @@ -1,103 +0,0 @@ -div:has(> #rbgcp-wrapper) { - background: transparent !important; -} - -#rbgcp-inputs-wrap { - padding-top: 4px !important; - margin-bottom: -8px; - - #rbgcp-hex-input, - #rbgcp-input { - height: 28px !important; - } -} - -.dark { - #rbgcp-wrapper { - div[style="padding-top: 11px; position: relative;"] div { - color: white !important; - } - - #rbgcp-inputs-wrap #rbgcp-hex-input, - #rbgcp-inputs-wrap #rbgcp-input { - color: white !important; - background-color: #37373b !important; - border: none !important; - } - - div:has(> #rbgcp-solid-btn), - div:has(> #rbgcp-advanced-btn), - #rbgcp-color-model-btn > div, - #rbgcp-gradient-controls-wrap { - background-color: #37373b !important; - color: white !important; - - svg { - circle { - fill: white !important; - } - - polyline, - line, - g, - path { - stroke: white !important; - } - } - - #rbgcp-radial-btn, - #rbgcp-linear-btn { - &[style*="background: white;"] { - background-color: #28282b !important; - } - - svg { - path, - g, - polyline, - circle { - stroke: white !important; - fill: transparent !important; - } - } - } - - div:has(> #rbgcp-stop-input) svg { - path { - stroke: unset !important; - fill: white !important; - } - } - - #rbgcp-comparibles-btn svg path { - fill: white !important; - } - - > div { - color: white !important; - - &[style*="background: white;"] { - background: #28282b !important; - } - } - } - - div:has(> #rbgcp-degree-input) { - width: 70px !important; - } - - #rbgcp-degree-input { - width: 50px !important; - } - - #rbgcp-degree-input, - #rbgcp-stop-input { - color: white !important; - } - - #rbgcp-gradient-controls-wrap > div, - #rbgcp-gradient-controls-wrap > div > div:not([role="button"]) { - background-color: #37373b !important; - } - } -} diff --git a/src/interface/components/ColourPicker.svelte b/src/interface/components/ColourPicker.svelte index 551497a0..9a317ad5 100644 --- a/src/interface/components/ColourPicker.svelte +++ b/src/interface/components/ColourPicker.svelte @@ -1,105 +1,113 @@ {#if standalone} -
- {#if pickerReady && ReactAdapter && ColourPickerEl} - - {/if} +
+
{:else} @@ -107,15 +115,13 @@ bind:this={background} class="flex absolute top-0 left-0 z-50 justify-center items-center w-full h-full shadow-2xl cursor-pointer bg-black/20 border border-[#DDDDDD]/30 dark:border-[#38373D]/30 {$settingsState.DarkMode ? 'dark' : ''}" onclick={handleBackgroundClick} - onkeydown={(e) => { if (e.key === 'Enter') handleBackgroundClick(e as unknown as MouseEvent) }} + onkeydown={(event) => { if (event.key === 'Enter') handleBackgroundClick(event as unknown as MouseEvent) }} >
- {#if pickerReady && ReactAdapter && ColourPickerEl} - - {/if} +
{/if} diff --git a/src/interface/components/ColourPicker.tsx b/src/interface/components/ColourPicker.tsx deleted file mode 100644 index 2399e94b..00000000 --- a/src/interface/components/ColourPicker.tsx +++ /dev/null @@ -1,122 +0,0 @@ -import ColorPicker from "react-best-gradient-color-picker"; -import { useEffect, useRef, useState } from "react"; -import { settingsState } from "@/seqta/utils/listeners/SettingsState.ts"; - -const defaultPresets = [ - "linear-gradient(30deg, rgba(229,209,218,1) 0%, RGBA(235,169,202,1) 46%, rgba(214,155,162,1) 100%)", - "linear-gradient(40deg, rgba(201,61,0,1) 0%, RGBA(170, 5, 58, 1) 100%)", - "linear-gradient(40deg, rgba(0, 141, 201, 0.76) 0%, rgba(8, 5, 170, 0.66) 100%)", - "linear-gradient(40deg, rgba(0, 201, 20, 0.76) 0%, rgba(4, 160, 105, 0.66) 100%)", - "linear-gradient(40deg, rgba(199, 20, 55, 0.76) 0%, rgba(95, 11, 160, 0.66) 100%)", - "linear-gradient(40deg, rgba(24, 20, 199, 0.76) 0%, rgba(23, 173, 65, 0.66) 100%)", - "radial-gradient(circle, rgba(20, 199, 178, 0.76) 32%, rgba(3, 120, 57, 0.66) 100%)", - "radial-gradient(circle, rgba(13, 15, 145, 0.76) 12%, rgba(103, 3, 120, 0.66) 100%)", - "linear-gradient(20deg, rgb(230, 21, 21) 0%, rgb(230, 109, 21) 12%, rgb(230, 34, 21) 26%, rgb(230, 21, 21) 39%, rgb(230, 84, 21) 48%, rgb(230, 34, 21) 58%, rgb(230, 96, 21) 69%, rgb(230, 34, 21) 80%, rgb(230, 71, 21) 89%, rgb(230, 21, 21) 100%)", - "rgba(114, 1, 170, 0.89)", - "rgba(93, 135, 63, 0.89)", - "rgba(4, 4, 138, 0.77)", - "rgba(21, 20, 20, 0.89)", - "linear-gradient(340deg, rgb(205, 74, 82) 18%, rgba(132, 8, 8, 0.89) 46%, rgb(204, 78, 85) 72%)", - "radial-gradient(circle, rgb(74, 205, 158) 0%, rgba(8, 72, 132, 0.89) 99%)", - "rgba(17, 94, 89, 1)", - "rgba(30, 64, 175, 0.89)", - "rgba(134, 25, 143, 1)", - "rgba(14, 165, 233, 0.9)", -]; - -interface PickerProps { - customOnChange?: (color: string) => void; - customState?: string; - savePresets?: boolean; -} - -export default function Picker({ - customOnChange, - customState, - savePresets = true, -}: PickerProps) { - const [customThemeColor, setCustomThemeColor] = useState(); - const [presets, setPresets] = useState(); - - const latestValuesRef = useRef({ - customThemeColor, - customOnChange, - savePresets, - presets, - }); - - useEffect(() => { - if (customState !== undefined && customState !== null) { - setCustomThemeColor(customState); - } else { - setCustomThemeColor(settingsState.selectedColor ?? null); - } - - if (presets === undefined) { - const savedPresets = localStorage.getItem("colorPickerPresets"); - setPresets(savedPresets ? JSON.parse(savedPresets) : defaultPresets); - } - }, []); - - useEffect(() => { - latestValuesRef.current = { - customThemeColor, - customOnChange, - savePresets, - presets, - }; - }, [customThemeColor, customOnChange, savePresets, presets]); - - useEffect(() => { - return () => { - const { customThemeColor, customOnChange, savePresets, presets } = - latestValuesRef.current; - if (!(customThemeColor && !customOnChange && savePresets && presets)) - return; - - // Only proceed if presets are different (avoid unnecessary updates) - const existingIndex = presets.indexOf(customThemeColor); - let updatedPresets; - - if (existingIndex === 0) { - // No need to update if the selected color is already the first element - return; - } else if (existingIndex > -1) { - updatedPresets = [ - customThemeColor, - ...presets.slice(0, existingIndex), - ...presets.slice(existingIndex + 1), - ]; - } else { - updatedPresets = [customThemeColor, ...presets].slice(0, 18); - } - - localStorage.setItem( - "colorPickerPresets", - JSON.stringify(updatedPresets), - ); - }; - }, []); - - useEffect(() => { - if (customThemeColor && !customOnChange) { - settingsState.selectedColor = customThemeColor; - } - }, [customThemeColor, customOnChange]); - - return ( - { - if (customOnChange) { - customOnChange(color); - setCustomThemeColor(color); - } else { - setCustomThemeColor(color); - } - }} - /> - ); -} diff --git a/src/interface/components/colourPicker/GradientPicker.svelte b/src/interface/components/colourPicker/GradientPicker.svelte new file mode 100644 index 00000000..84fedc0b --- /dev/null +++ b/src/interface/components/colourPicker/GradientPicker.svelte @@ -0,0 +1,517 @@ + + +
+
+
+ + {#each MODES as mode (mode.kind)} + + {/each} +
+ + {#if model.kind !== "solid"} +
+
{ + if (dragging !== null) moveStop(dragging, barPosition(event)); + }} + onpointerup={() => (dragging = null)} + onpointercancel={() => (dragging = null)} + > +
+ {#each stops as stop, index (index)} + + {/each} +
+
+ {#if model.kind === "linear"} + + {/if} + +
+ {/if} +
+ +
{ + area!.setPointerCapture(event.pointerId); + pickArea(event); + }} + onpointermove={(event) => { + if (area!.hasPointerCapture(event.pointerId)) pickArea(event); + }} + onkeydown={areaKeys} + > +
+
+ +
+ {#if EyeDropper} + + {/if} +
+ updateHsva({ h: Number(event.currentTarget.value) })} + /> + updateHsva({ a: Number(event.currentTarget.value) / 100 })} + /> +
+ +
+ +
+ + +
+ + {#if shownPresets.length} +
+ {#each shownPresets as preset (preset)} + + {/each} +
+ {/if} +
+ + diff --git a/src/interface/components/colourPicker/colourValue.test.ts b/src/interface/components/colourPicker/colourValue.test.ts new file mode 100644 index 00000000..660d1f17 --- /dev/null +++ b/src/interface/components/colourPicker/colourValue.test.ts @@ -0,0 +1,95 @@ +import { describe, expect, it } from "@jest/globals"; +import { + colourAt, + formatValue, + hsvaToRgba, + parseColour, + parseValue, + rgbaToHex, + rgbaToHsva, +} from "./colourValue"; + +describe("parseColour", () => { + it.each([ + ["#fff", { r: 255, g: 255, b: 255, a: 1 }], + ["#11588380", { r: 17, g: 88, b: 131, a: 0.5 }], + ["rgba(17,88,131,1)", { r: 17, g: 88, b: 131, a: 1 }], + ["RGBA(235,169,202,1)", { r: 235, g: 169, b: 202, a: 1 }], + ["rgb(230, 21, 21)", { r: 230, g: 21, b: 21, a: 1 }], + ["rgb(10 20 30 / 50%)", { r: 10, g: 20, b: 30, a: 0.5 }], + ["hsl(0, 100%, 50%)", { r: 255, g: 0, b: 0, a: 1 }], + ["transparent", { r: 0, g: 0, b: 0, a: 0 }], + ])("parses %s", (input, expected) => { + expect(parseColour(input)).toEqual(expected); + }); + + it.each(["", "red", "#12345", "rgb(1, 2)", "rgba(a, b, c)"])("rejects %s", (input) => { + expect(parseColour(input)).toBeNull(); + }); +}); + +describe("parseValue", () => { + it("parses the default gradient", () => { + expect(parseValue("linear-gradient(45deg, rgba(17,88,131,1) 0%, rgba(18,106,98,1) 100%)")).toEqual({ + kind: "linear", + angle: 45, + stops: [ + { colour: { r: 17, g: 88, b: 131, a: 1 }, position: 0 }, + { colour: { r: 18, g: 106, b: 98, a: 1 }, position: 100 }, + ], + }); + }); + + it("parses radial gradients and skips the shape", () => { + const value = parseValue("radial-gradient(circle, rgba(20, 199, 178, 0.76) 32%, rgba(3, 120, 57, 0.66) 100%)"); + expect(value.kind).toBe("radial"); + expect(value.kind !== "solid" && value.stops.map((stop) => stop.position)).toEqual([32, 100]); + }); + + it("maps side keywords and fills missing stop positions", () => { + const value = parseValue("linear-gradient(to right, #000, #888, #fff)"); + expect(value).toMatchObject({ kind: "linear", angle: 90 }); + expect(value.kind !== "solid" && value.stops.map((stop) => stop.position)).toEqual([0, 50, 100]); + }); + + it("treats plain colours as solid and falls back on garbage", () => { + expect(parseValue("rgba(4, 4, 138, 0.77)")).toEqual({ kind: "solid", colour: { r: 4, g: 4, b: 138, a: 0.77 } }); + expect(parseValue("not a colour").kind).toBe("solid"); + }); +}); + +describe("formatValue", () => { + it("round-trips gradients in the stored format", () => { + const css = "linear-gradient(45deg, rgba(17, 88, 131, 1) 0%, rgba(18, 106, 98, 1) 100%)"; + expect(formatValue(parseValue(css))).toBe(css); + }); + + it("sorts stops by position", () => { + expect( + formatValue({ + kind: "radial", + stops: [ + { colour: { r: 255, g: 255, b: 255, a: 1 }, position: 80 }, + { colour: { r: 0, g: 0, b: 0, a: 0.5 }, position: 10 }, + ], + }), + ).toBe("radial-gradient(circle, rgba(0, 0, 0, 0.5) 10%, rgba(255, 255, 255, 1) 80%)"); + }); +}); + +describe("conversions", () => { + it("converts between RGB, HSV and hex", () => { + const colour = { r: 17, g: 88, b: 131, a: 1 }; + expect(hsvaToRgba(rgbaToHsva(colour))).toEqual(colour); + expect(rgbaToHex(colour)).toBe("#115883"); + }); + + it("interpolates colours between stops", () => { + const stops = [ + { colour: { r: 0, g: 0, b: 0, a: 1 }, position: 0 }, + { colour: { r: 200, g: 100, b: 50, a: 0 }, position: 100 }, + ]; + expect(colourAt(stops, 50)).toEqual({ r: 100, g: 50, b: 25, a: 0.5 }); + expect(colourAt(stops, 120)).toEqual(stops[1].colour); + }); +}); diff --git a/src/interface/components/colourPicker/colourValue.ts b/src/interface/components/colourPicker/colourValue.ts new file mode 100644 index 00000000..df446b68 --- /dev/null +++ b/src/interface/components/colourPicker/colourValue.ts @@ -0,0 +1,188 @@ +export type Rgba = { r: number; g: number; b: number; a: number }; +export type Hsva = { h: number; s: number; v: number; a: number }; +export type GradientStop = { colour: Rgba; position: number }; +export type ColourValue = + | { kind: "solid"; colour: Rgba } + | { kind: "linear"; angle: number; stops: GradientStop[] } + | { kind: "radial"; stops: GradientStop[] }; + +const FALLBACK: Rgba = { r: 17, g: 88, b: 131, a: 1 }; + +const SIDE_ANGLES: Record = { + "to top": 0, + "to top right": 45, + "to right top": 45, + "to right": 90, + "to bottom right": 135, + "to right bottom": 135, + "to bottom": 180, + "to bottom left": 225, + "to left bottom": 225, + "to left": 270, + "to top left": 315, + "to left top": 315, +}; + +export const clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value)); + +const round = (value: number, digits = 0) => { + const factor = 10 ** digits; + return Math.round(value * factor) / factor; +}; + +function parseAlpha(part: string): number { + return clamp(part.endsWith("%") ? parseFloat(part) / 100 : parseFloat(part), 0, 1); +} + +function hexToRgba(hex: string): Rgba | null { + const digits = hex.slice(1); + if (![3, 4, 6, 8].includes(digits.length)) return null; + const full = digits.length <= 4 ? [...digits].map((d) => d + d).join("") : digits; + const [r, g, b, a = 255] = full.match(/../g)!.map((pair) => parseInt(pair, 16)); + return { r, g, b, a: round(a / 255, 2) }; +} + +function hslToRgba(h: number, s: number, l: number, a: number): Rgba { + const k = (n: number) => (n + h / 30) % 12; + const f = (n: number) => l - s * Math.min(l, 1 - l) * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1)); + return { r: Math.round(f(0) * 255), g: Math.round(f(8) * 255), b: Math.round(f(4) * 255), a }; +} + +export function parseColour(input: string): Rgba | null { + const value = input.trim().toLowerCase(); + if (value === "transparent") return { r: 0, g: 0, b: 0, a: 0 }; + if (/^#[0-9a-f]+$/.test(value)) return hexToRgba(value); + + const fn = value.match(/^(rgba?|hsla?)\(([^()]*)\)$/); + if (!fn) return null; + const parts = fn[2].split(/[\s,/]+/).filter(Boolean); + if (parts.length < 3 || parts.length > 4) return null; + const a = parts[3] === undefined ? 1 : parseAlpha(parts[3]); + if (Number.isNaN(a)) return null; + + if (fn[1].startsWith("rgb")) { + const [r, g, b] = parts + .slice(0, 3) + .map((part) => clamp(Math.round(part.endsWith("%") ? parseFloat(part) * 2.55 : parseFloat(part)), 0, 255)); + return [r, g, b].some(Number.isNaN) ? null : { r, g, b, a }; + } + + const [h, s, l] = [parseFloat(parts[0]), parseFloat(parts[1]) / 100, parseFloat(parts[2]) / 100]; + return [h, s, l].some(Number.isNaN) ? null : hslToRgba(((h % 360) + 360) % 360, clamp(s, 0, 1), clamp(l, 0, 1), a); +} + +export function formatColour({ r, g, b, a }: Rgba): string { + return `rgba(${r}, ${g}, ${b}, ${round(a, 2)})`; +} + +export function rgbaToHex({ r, g, b }: Rgba): string { + return `#${[r, g, b].map((channel) => channel.toString(16).padStart(2, "0")).join("")}`; +} + +export function rgbaToHsva({ r, g, b, a }: Rgba): Hsva { + const [red, green, blue] = [r / 255, g / 255, b / 255]; + const max = Math.max(red, green, blue); + const delta = max - Math.min(red, green, blue); + let h = 0; + if (delta) { + if (max === red) h = ((green - blue) / delta) % 6; + else if (max === green) h = (blue - red) / delta + 2; + else h = (red - green) / delta + 4; + } + return { h: (h * 60 + 360) % 360, s: max ? (delta / max) * 100 : 0, v: max * 100, a }; +} + +export function hsvaToRgba({ h, s, v, a }: Hsva): Rgba { + const f = (n: number) => { + const k = (n + h / 60) % 6; + return Math.round((v / 100) * (1 - (s / 100) * Math.max(0, Math.min(k, 4 - k, 1))) * 255); + }; + return { r: f(5), g: f(3), b: f(1), a }; +} + +function splitTopLevel(input: string): string[] { + const parts: string[] = []; + let depth = 0; + let current = ""; + for (const char of input) { + if (char === "(") depth++; + if (char === ")") depth--; + if (char === "," && depth === 0) { + parts.push(current.trim()); + current = ""; + } else { + current += char; + } + } + if (current.trim()) parts.push(current.trim()); + return parts; +} + +function parseStop(part: string): { colour: Rgba; position?: number } | null { + const withPosition = part.match(/^(.+?)\s+(-?[\d.]+)%$/); + const colour = withPosition ? parseColour(withPosition[1]) : null; + if (colour) return { colour, position: clamp(parseFloat(withPosition![2]), 0, 100) }; + const plain = parseColour(part); + return plain ? { colour: plain } : null; +} + +function fillPositions(stops: { colour: Rgba; position?: number }[]): GradientStop[] { + const positions = stops.map((stop) => stop.position); + positions[0] ??= 0; + positions[positions.length - 1] ??= 100; + for (let i = 1; i < positions.length - 1; i++) { + if (positions[i] !== undefined) continue; + const next = positions.findIndex((position, j) => j > i && position !== undefined); + const start = positions[i - 1]!; + positions[i] = start + (positions[next]! - start) / (next - i + 1); + } + return stops.map((stop, i) => ({ colour: stop.colour, position: round(positions[i]!, 2) })); +} + +export function parseValue(input: string): ColourValue { + const gradient = input.trim().match(/^(linear|radial)-gradient\(([\s\S]*)\)$/i); + if (!gradient) return { kind: "solid", colour: parseColour(input) ?? FALLBACK }; + + const kind = gradient[1].toLowerCase() as "linear" | "radial"; + const args = splitTopLevel(gradient[2]); + let angle = 180; + if (args.length && !parseStop(args[0])) { + const direction = args.shift()!.toLowerCase().replace(/\s+/g, " "); + const degrees = direction.match(/^(-?[\d.]+)deg$/); + if (degrees) angle = ((parseFloat(degrees[1]) % 360) + 360) % 360; + else if (direction in SIDE_ANGLES) angle = SIDE_ANGLES[direction]; + } + + const stops = args.map(parseStop).filter((stop) => stop !== null); + if (stops.length < 2) return { kind: "solid", colour: stops[0]?.colour ?? FALLBACK }; + const filled = fillPositions(stops); + return kind === "linear" ? { kind, angle, stops: filled } : { kind, stops: filled }; +} + +export function formatStops(stops: GradientStop[]): string { + return [...stops] + .sort((a, b) => a.position - b.position) + .map((stop) => `${formatColour(stop.colour)} ${round(stop.position)}%`) + .join(", "); +} + +export function formatValue(value: ColourValue): string { + if (value.kind === "solid") return formatColour(value.colour); + if (value.kind === "linear") return `linear-gradient(${round(value.angle)}deg, ${formatStops(value.stops)})`; + return `radial-gradient(circle, ${formatStops(value.stops)})`; +} + +export function colourAt(stops: GradientStop[], position: number): Rgba { + const sorted = [...stops].sort((a, b) => a.position - b.position); + const after = sorted.findIndex((stop) => stop.position >= position); + if (after <= 0) return { ...sorted[after === 0 ? 0 : sorted.length - 1].colour }; + const [from, to] = [sorted[after - 1], sorted[after]]; + const t = (position - from.position) / (to.position - from.position || 1); + const mix = (a: number, b: number) => a + (b - a) * t; + return { + r: Math.round(mix(from.colour.r, to.colour.r)), + g: Math.round(mix(from.colour.g, to.colour.g)), + b: Math.round(mix(from.colour.b, to.colour.b)), + a: round(mix(from.colour.a, to.colour.a), 2), + }; +} diff --git a/src/interface/components/utils/ReactAdapter.svelte b/src/interface/components/utils/ReactAdapter.svelte deleted file mode 100644 index 42e5e35e..00000000 --- a/src/interface/components/utils/ReactAdapter.svelte +++ /dev/null @@ -1,32 +0,0 @@ - - -
diff --git a/src/interface/index.css b/src/interface/index.css index 4ad4e772..6b9a12bf 100644 --- a/src/interface/index.css +++ b/src/interface/index.css @@ -1,5 +1,3 @@ -@import "./components/ColourPicker.css" layer(base); - @import "../css/tailwind.css"; ::-webkit-scrollbar { diff --git a/src/interface/pages/themeCreator.svelte b/src/interface/pages/themeCreator.svelte index 034cbdf1..3ddf62d2 100644 --- a/src/interface/pages/themeCreator.svelte +++ b/src/interface/pages/themeCreator.svelte @@ -281,12 +281,12 @@ void themeManager.updatePreviewDebounced(snapshot); }); - type SettingType = 'switch' | 'button' | 'slider' | 'colourPicker' | 'select' | 'codeEditor' | 'imageUpload' | 'conditional' | 'lightDarkToggle'; + type SettingType = 'switch' | 'button' | 'slider' | 'select' | 'codeEditor' | 'imageUpload' | 'conditional' | 'lightDarkToggle'; type SwitchProps = { state: boolean; onChange: (value: boolean) => void }; type ButtonProps = { onClick: () => void; text: string }; type SliderProps = { state: number; onChange: (value: number) => void; min?: number; max?: number }; - type ColourPickerProps = { color: string; onChange: (color: string) => void }; + type SelectProps = { options: Array<{ value: string; label: string }>; value: string; onChange: (value: string) => void }; type CodeEditorProps = { value: string; onChange: (value: string) => void }; type LightDarkToggleProps = { state: boolean; onChange: (value: boolean) => void }; @@ -296,7 +296,7 @@ children: SettingItem; }; - type ComponentProps = SwitchProps | ButtonProps | SliderProps | ColourPickerProps | SelectProps | CodeEditorProps | LightDarkToggleProps | ConditionalProps; + type ComponentProps = SwitchProps | ButtonProps | SliderProps | SelectProps | CodeEditorProps | LightDarkToggleProps | ConditionalProps; type SettingItem = { type: SettingType; @@ -349,10 +349,7 @@