function toColorRgba(color, opacity) { // 16进制颜色值的正则 var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; // 把颜色变成小写 var color = color.toString().toLowerCase(); if (reg.test(color)) { // 如果只有三位的值,需要转换成六位 #fff => #ffffff if (color.length == 4) { var colorNew = '#'; for (var i = 1; i < 4; i += 1) { colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1)); } color = colorNew; } // 处理六位的颜色值,转为RGB格式 var colorChange = []; for (var i = 1; i < 7; i += 2) { colorChange.push(parseInt('0x' + color.slice(i, i + 2))); } var rgbaStr = 'rgba(' + colorChange.join(',') + ',' + opacity/100 + ')'; return rgbaStr; } else { return color; } }
标签:slice,进制,16,color,RGB,var,colorChange,colorNew From: https://www.cnblogs.com/stt-bky/p/17172150.html