changeHexToRgba(str,alpha) {
const REGCOLOR = /^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/;
const ISRGBA = REGCOLOR.test(str);
if (ISRGBA === false) {
throw Error("Not a valid value");
}
// 去掉#号
const colorStr = str.slice(1);
const len = colorStr.length;
if (len === 3) {
const color = colorStr
.split("")
.map(ele => parseInt(`0x${ele.repeat(2)}`))
.join(",");
return `rgba(${color},${alpha})`;
}
const color = colorStr
.match(/[A-z0-9]{2}/g)
.map(ele => parseInt(`0x${ele}`));
if (len === 6) {
return `rgba(${color.join(",")},${alpha})`;
}
if (len === 8) {
const opacity = (color.pop() / 255).toFixed(2);
return `rgba(${color.slice(0, 3).join(",")}, ${opacity})`;
}
}
05-31
600

08-04
685

06-09
1150
