十六进制的颜色转变为rgb,设置透明度,通用方法

changeToRgb(color) {
var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
var sColor = color.toLowerCase();
if (sColor && reg.test(sColor)) {
if (sColor.length === 4) {
var sColorNew = "#";
for (var i = 1; i < 4; i += 1) {
sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
}
sColor = sColorNew;
}
//处理六位的颜色值
var sColorChange = [];
for (var i = 1; i <= 6; i += 2) {
sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));
}
//此处是返回的颜色 如需要透明度,0.3是指透明度30%,直接返回
//"rgba(" + sColorChange.join(",") + ",0.3)"
return "rgba(" + sColorChange.join(",") + ")";
} else {
return sColor;
}
},

posted @ 2021-08-26 17:04  衣兜里  阅读(904)  评论(0编辑  收藏  举报