Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(desktop): add shared hex color parsing utilities

Richie McIlroy1476f17e6406
diff --git a/apps/desktop/src/utils/hex-color.test.ts b/apps/desktop/src/utils/hex-color.test.ts
new file mode 100644
index 000000000..281c63f25
--- /dev/null
+++ b/apps/desktop/src/utils/hex-color.test.ts
@@ -0,0 +1,42 @@
+import { describe, expect, it } from "vitest";
+import {
+ getHexColorDigitCount,
+ hexToRgb,
+ normalizeHexColor,
+ normalizeOpaqueHexColor,
+} from "./hex-color";
+
+describe("hex-color", () => {
+ it("normalizes shorthand values with or without a hash", () => {
+ expect(normalizeHexColor("fff")).toBe("#FFFFFF");
+ expect(normalizeHexColor("#ddd")).toBe("#DDDDDD");
+ expect(normalizeOpaqueHexColor("abc")).toBe("#AABBCC");
+ });
+
+ it("normalizes full-length values", () => {
+ expect(normalizeHexColor("123456")).toBe("#123456");
+ expect(normalizeHexColor("#89abcd")).toBe("#89ABCD");
+ expect(normalizeHexColor("abcd")).toBe("#AABBCCDD");
+ });
+
+ it("reports the entered hex digit count", () => {
+ expect(getHexColorDigitCount("fff")).toBe(3);
+ expect(getHexColorDigitCount("#123456")).toBe(6);
+ expect(getHexColorDigitCount("12345678")).toBe(8);
+ expect(getHexColorDigitCount("rgba(0,0,0,1)")).toBe(0);
+ });
+
+ it("parses shorthand and alpha values into rgba tuples", () => {
+ expect(hexToRgb("fff")).toEqual([255, 255, 255, 255]);
+ expect(hexToRgb("#1234")).toEqual([17, 34, 51, 68]);
+ expect(hexToRgb("#12345678")).toEqual([18, 52, 86, 120]);
+ });
+
+ it("rejects invalid values", () => {
+ expect(normalizeHexColor("")).toBeNull();
+ expect(normalizeHexColor("12")).toBeNull();
+ expect(normalizeHexColor("zzzzzz")).toBeNull();
+ expect(normalizeOpaqueHexColor("#12345678")).toBeNull();
+ expect(hexToRgb("rgba(0,0,0,1)")).toBeNull();
+ });
+});
diff --git a/apps/desktop/src/utils/hex-color.ts b/apps/desktop/src/utils/hex-color.ts
new file mode 100644
index 000000000..f5e8aed95
--- /dev/null
+++ b/apps/desktop/src/utils/hex-color.ts
@@ -0,0 +1,50 @@
+export function normalizeHexColor(value: string) {
+ const trimmed = value.trim();
+ if (trimmed.length === 0) return null;
+
+ const raw = trimmed.startsWith("#") ? trimmed.slice(1) : trimmed;
+ if (!/^[\dA-F]+$/i.test(raw)) return null;
+
+ if (raw.length === 3 || raw.length === 4) {
+ return `#${raw
+ .split("")
+ .map((char) => `${char}${char}`)
+ .join("")
+ .toUpperCase()}`;
+ }
+
+ if (raw.length === 6 || raw.length === 8) {
+ return `#${raw.toUpperCase()}`;
+ }
+
+ return null;
+}
+
+export function getHexColorDigitCount(value: string) {
+ const trimmed = value.trim();
+ if (trimmed.length === 0) return 0;
+
+ const raw = trimmed.startsWith("#") ? trimmed.slice(1) : trimmed;
+ if (!/^[\dA-F]+$/i.test(raw)) return 0;
+
+ return raw.length;
+}
+
+export function normalizeOpaqueHexColor(value: string) {
+ const normalized = normalizeHexColor(value);
+ if (!normalized || normalized.length !== 7) return null;
+ return normalized;
+}
+
+export function hexToRgb(hex: string): [number, number, number, number] | null {
+ const normalized = normalizeHexColor(hex);
+ if (!normalized) return null;
+
+ const raw = normalized.slice(1);
+ const r = Number.parseInt(raw.slice(0, 2), 16);
+ const g = Number.parseInt(raw.slice(2, 4), 16);
+ const b = Number.parseInt(raw.slice(4, 6), 16);
+ const a = raw.length === 8 ? Number.parseInt(raw.slice(6, 8), 16) : 255;
+
+ return [r, g, b, a];
+}