2022-06-26 18:34:09 +00: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 19:13:52 +00:00
|
|
|
function hexToRgba(hex: string, alpha = 1): string {
|
2022-06-26 18:34:09 +00:00
|
|
|
// noinspection RegExpSimplifiable
|
|
|
|
if (!/#?([a-fA-F0-9]{2}){3}/.test(hex)) {
|
|
|
|
return hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// noinspection RegExpSimplifiable
|
2022-11-25 20:25:03 +00:00
|
|
|
const [r, g, b] = hex.match(/[a-fA-F0-9]{2}/g)!.map(v => parseInt(v, 16));
|
2022-06-26 18:34:09 +00:00
|
|
|
|
|
|
|
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export { hexToRgba };
|