1hexToRGB(hex: string, alpha: string) {
2
3 const r = parseInt(hex.slice(1, 3), 16);
4 const g = parseInt(hex.slice(3, 5), 16);
5 const b = parseInt(hex.slice(5, 7), 16);
6
7 if (alpha) {
8 return `rgba(${r}, ${g}, ${b}, ${alpha})`;
9 } else {
10 return `rgb(${r}, ${g}, ${b})`;
11 }
12}
1//If you write your own code, remember hex color shortcuts (eg., #fff, #000)
2
3function hexToRgbA(hex){
4 var c;
5 if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
6 c= hex.substring(1).split('');
7 if(c.length== 3){
8 c= [c[0], c[0], c[1], c[1], c[2], c[2]];
9 }
10 c= '0x'+c.join('');
11 return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+',1)';
12 }
13 throw new Error('Bad Hex');
14}
15
16hexToRgbA('#fbafff')
17
18/* returned value: (String)
19rgba(251,175,255,1)
20*/
21