//十六进制颜色值的正则表达式 var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; //转换为十六进制方法 function colorHex(string){ if(/^(rgb|RGB)/.test(string)){ var aColor = string.replace(/(?:\(|\)|rgb|RGB)*/g,"").split(","); var strHex = "#"; for(var i=0; i<aColor.length; i++){ var hex = Number(aColor[i]).toString(16); if(hex === "0"){ hex += hex; } strHex += hex; } if(strHex.length !== 7){ strHex = string; } return strHex; }else if(reg.test(string)){ var aNum = string.replace(/#/,"").split(""); if(aNum.length === 6){ return string; }else if(aNum.length === 3){ var numHex = "#"; for(var i=0; i<aNum.length; i+=1){ numHex += (aNum[i]+aNum[i]); } return numHex; } }else{ return string; } } console.log(colorHex("RGB(255, 255, 0)"))
//十六进制颜色值的正则表达式 var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; /*16进制颜色转为RGB格式*/ function colorRgb(string){ var sColor = string.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<7; i+=2){ sColorChange.push(parseInt("0x"+sColor.slice(i,i+2))); } return "RGB(" + sColorChange.join(",") + ")"; }else{ return sColor; } } console.log(colorRgb("#00538b"))