//16 進数のカラー値の正規表現
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; i
var hex = Number(aColor).toString(16);
if(hex === "0"){
16 進数 = 16 進数
}
strHex = hex;
}
If(strHex.length !== 7){
strHex = that;
}
return strHex;
}else if(reg.test(that)){
var aNum = that.replace(/#/,"").split("");
If(aNum.length === 6){
それを返します;
}else if(aNum.length === 3){
var numHex = "#";
for(var i=0; i
numHex = (aNum aNum);
}
return numHex;
}
}その他{
それを返します;
}};
/*16 進数の色を RGB 形式に変換します*/
String.prototype.colorRgb = function(){
var sColor = this.toLowerCase();
If(sColor && reg.test(sColor)){
If(sColor.length === 4){
var sColorNew = "#";
for(var i=1; i
sColorNew = sColor.slice(i,i 1).concat(sColor.slice(i,i 1));
}
sColor = sColorNew;
}
//6 桁のカラー値を処理します
var sColorChange = [];
for(var i=1; i
sColorChange.push(parseInt("0x" sColor.slice(i,i 2)));
}
return "RGB(" sColorChange.join(",") ")";
}その他{
return sColor;
}};
色変換メソッドを使用します: