Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/resources/scripts/lib/helpers.ts
7458 views
1
/**
2
* Given a valid six character HEX color code, converts it into its associated
3
* RGBA value with a user controllable alpha channel.
4
*/
5
function hexToRgba(hex: string, alpha = 1): string {
6
// noinspection RegExpSimplifiable
7
if (!/#?([a-fA-F0-9]{2}){3}/.test(hex)) {
8
return hex;
9
}
10
11
// noinspection RegExpSimplifiable
12
const [r, g, b] = hex.match(/[a-fA-F0-9]{2}/g)!.map((v) => parseInt(v, 16));
13
14
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
15
}
16
17
export { hexToRgba };
18
19