colorTransform(sHex, alpha = 1) {
let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
/* 16进制颜色转为RGB格式 */
let sColor = sHex.toLowerCase()
if (sColor && reg.test(sColor)) {
if (sColor.length === 4) {
let sColorNew = '#'
for (let i = 1; i < 4; i += 1) {
sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
}
sColor = sColorNew
}
// 处理六位的颜色值
let sColorChange = []
for (let i = 1; i < 7; i += 2) {
// eslint-disable-next-line radix
sColorChange.push(parseInt(`0x${sColor.slice(i, i + 2)}`))
}
return `rgba(${sColorChange.join(',')},${alpha})`
}
return sColor
}
js 将16进制颜色转为RGBA
最新推荐文章于 2024-03-19 17:01:31 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)