/** * 16进制转rgba * 主要用于视觉图上给的有16进制颜色和opacity时,会于视觉稿有差异,现在通过这个方法直接转换成rgba * @param {不透明度} opacity */ /* eslint-disable-next-line */ String.prototype.colorRgba = function(opacity = 1) { // 16进制颜色值的正则 const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/ // 把颜色值变成小写 let color = this.toLowerCase() if (reg.test(color)) { // 如果只有三位的值,需变成六位,如:#fff => #ffffff if (color.length === 4) { let colorNew = "#" for (let i = 1; i < 4; i += 1) { colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1)) } color = colorNew } // 处理六位的颜色值,转为RGB const colorChange = [] for (let i = 1; i < 7; i += 2) { colorChange.push(parseInt(`0x${color.slice(i, i + 2)}`, 16)) } return `rgba(${colorChange.join(",")},${opacity})` } return color }