mirror of
https://github.com/t8y2/dbx.git
synced 2026-10-30 16:21:39 +00:00
19 lines
554 B
TypeScript
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})`;
|
|
}
|