2022-06-26 20:34:09 +02:00
|
|
|
/**
|
|
|
|
* Given a valid six character HEX color code, converts it into its associated
|
|
|
|
* RGBA value with a user controllable alpha channel.
|
|
|
|
*/
|
2022-06-26 21:13:52 +02:00
|
|
|
function hexToRgba(hex: string, alpha = 1): string {
|
2022-06-26 20:34:09 +02:00
|
|
|
// noinspection RegExpSimplifiable
|
|
|
|
if (!/#?([a-fA-F0-9]{2}){3}/.test(hex)) {
|
|
|
|
return hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// noinspection RegExpSimplifiable
|
2022-06-26 21:13:52 +02:00
|
|
|
const [r, g, b] = hex.match(/[a-fA-F0-9]{2}/g)!.map((v) => parseInt(v, 16));
|
2022-06-26 20:34:09 +02:00
|
|
|
|
|
|
|
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export { hexToRgba };
|