Files

20 lines
448 B
TypeScript
Raw Permalink Normal View History

function hexToRgba(hex: string, alpha = 1): string {
// noinspection RegExpSimplifiable
if (!/#?([a-fA-F0-9]{2}){3}/.test(hex)) {
return hex;
}
// noinspection RegExpSimplifiable
2026-10-05 18:10:18 -04:00
const pairs = hex.match(/[a-fA-F0-9]{2}/g);
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
if (!pairs || pairs.length < 3) {
return hex;
}
const [r, g, b] = pairs.map((v) => parseInt(v, 16));
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}
export { hexToRgba };