2024-08-14 16:20:56 +08:00

19 lines
611 B
JavaScript

export const hexColorToRgba = (hexColor, alphaMaxVal = 1) => {
hexColor = hexColor.replace('#', '');
//用于分割16进制色彩通道
let reg = new RegExp('\\w{1,2}', 'g');
//分割颜色通道
let rgbaArray255 = hexColor.match(reg);
rgbaArray255 = rgbaArray255.map((channel, index) => {
//计算每个通道的10进制值
let colorVal = parseInt(channel, 16);
if (index === 3) {
//这是alpha通道
return Math.round((colorVal / (255 / alphaMaxVal)) * 100) / 100;
}
return colorVal;
});
return rgbaArray255;
};