Files
SupportBot-Dashboard/dashboard/src/lib/normalize-hex.ts
T
Char b82f69783d Add dashboard UI source and track it in the repository.
Expose the Vite/React website source under dashboard/ (with node_modules still ignored) so the full dashboard can be built and maintained from this repo.
2026-07-26 13:09:19 +01:00

24 lines
697 B
TypeScript

/** Normalize user/config colour strings to #rrggbb (lowercase). */
export function normalizeHex(value: string, fallback = "#000000"): string {
let s = String(value ?? "").trim();
if (!s) return fallback;
if (!s.startsWith("#")) s = `#${s}`;
const hex = s.slice(1);
if (/^[0-9a-fA-F]{3}$/.test(hex)) {
return `#${hex
.split("")
.map((c) => c + c)
.join("")
.toLowerCase()}`;
}
if (/^[0-9a-fA-F]{6}$/.test(hex)) {
return `#${hex.toLowerCase()}`;
}
return fallback;
}
export function isValidHex(value: string): boolean {
const s = String(value ?? "").trim();
return /^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/.test(s) || /^[0-9a-fA-F]{6}$/.test(s);
}