js基础5 — 将十六进制颜色转为rgba()
var colors = ['#eEE','#ffffff','#000000','#cf8','#be2626']; String.prototype.colorRgb = function(){ var color = this.toLowerCase(); var pattern = /^#([0-9|a-f]{3}|[0-9|a-f]{6})$/; if(color && pattern.test(color)) { if(color.length == 4) { // 将三位转换为六位 color = '#' + color[1] + color[1] + color[2] + color[2] + color[3] + color[3]; } //处理六位的颜色值 var colorNew = []; for (var i=1; i<7; i+=2) { colorNew.push(parseInt("0x"+color.slice(i, i+2))); } return "RGB(" + colorNew.join(",") + ")"; } return color; }; var arr2 = []; for(var i=0; i<colors.length; i++) { arr2.push(colors[i].colorRgb()); } console.log(arr2);