var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
/*RGB颜色转换为16进制*/
String.prototype.colorHex = function(){
var that = this;
if(/^(rgb|RGB)/.test(that)){
var aColor = that.replace(/(?:\(|\)|rgb|RGB)*/g,"").split(",");
var strHex = "#";
for(var i=0; ivar hex = Number(aColor[i]).toString(16);
if (hex.length == 1) {
hex = "0" + hex;
} //问题出在这里
if(hex === "0"){
hex += hex;
}
strHex += hex;
}
if(strHex.length !== 7){
strHex = that;
}
return strHex;
}else if(reg.test(that)){
var aNum = that.replace(/#/,"").split("");
if(aNum.length === 6){
return that;
}else if(aNum.length === 3){
var numHex = "#";
for(var i=0; inumHex += (aNum[i]+aNum[i]);
}
return numHex;
}
}else{
return that;
}
};
修改这里
//把16进制转为 10进制
parseInt('ff',16)
//把10进制数转为 16进制 字符串
(255).toString(16)
//左补0
var ss = (255).toString(16)
if (ss.length<=1){
ss = '0' + ss
}
你看看这个文章能不能明白《RGB色与Hex色互换代码》