/** * [parserColor 颜色转换为十六进制] * @param {[String]} value [需要转换的颜色值] * @return {[String]} [返回转换后的颜色值,#0000FF形式] */ function parserColor(value){ var str="", ar=[], arri= "", i=0, vlen=value.length, colorObj={ "black":"000000", "red":"0000FF", "blue":"FF0000", "white":"FFFFFF", "yellow":"FFFF00", "orange":"FFA500" }; //rgb(0,0,255) if(/rgb/.test(value)){ arr = value.match(/\d+/g); vlen = arr.length; for(; i < vlen ; i++){ arri = parseInt(arr[i]); //转换为十六进制 str += arri < 10 ? "0" + arri.toString(16) : arri.toString(16); } }else if(/^#/.test(value)){ //#00f if(vlen == 4){ str = value.replace(/[A-Za-z0-9]/g,"$&$&"); }else if(vlen == 7){ //#FF0000 str = value.replace(/^#([A-Za-z0-9]*)/,"$1"); }else{ str = "FFFFFF"; } }else{ //red/orange value = value.toLowerCase(); str = colorObj[value] ? colorObj[value] : "FFFFFF";//不匹配默认为白色 } return "#" + str.toUpperCase(); }三、实例:
console.log(parserColor("white"));//#FFFFFFF console.log(parserColor("#00f"));//#0000FF console.log(parserColor("rgb(0,0,255)"));//#0000FF
转载请尊重原创,注明出处,本页地址,谢谢!