Files
dbx/src/lib/color.ts
T

19 lines
554 B
TypeScript

export function hexToRgba(color: string | undefined, alpha: number): string | undefined {
if (!color) return undefined;
const hex = color.trim().replace(/^#/, "");
const normalized =
hex.length === 3
? hex
.split("")
.map((c) => c + c)
.join("")
: hex;
if (!/^[0-9a-fA-F]{6}$/.test(normalized)) return undefined;
const value = Number.parseInt(normalized, 16);
const r = (value >> 16) & 255;
const g = (value >> 8) & 255;
const b = value & 255;
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}