在日常开发中,经常使用不同颜色域值之间的转换,并给出下面的解决方案。

复制代码代码如下所示:

正则表达式十六十六进制颜色值

VaR reg = / ^ #($ / { 0-9a-fa-f } { 3 } | { 0-9a-fa-f } { 6 });

用于16波段的RGB颜色转换。

string.prototype.colorhex =函数(){

var =;

(如果 / ^(RGB | RGB) /。试验()){

Var(颜色= that.replace /(:(| )| RGB | RGB * / g),。分裂)(

无功strhex =# ;

对于(var i = 0;i < acolor.length;i++){

VaR进制=数量(颜色),ToString(16);

如果(十六进制= 0){

十六进制;

}

strhex =进制;

}

如果(strhex.length!= 7){

strhex =;

}

返回strhex;

} else if(reg.test()){

var安努姆= that.replace( / # /,。分裂)();

如果(anum.length = 6){

返回;

} else if(anum.length = 3){

无功numhex =# ;

对于(var i = 0;i < anum.length;我+ = 1){

numhex =(该+肛门);

}

返回numhex;

}

其他{ }

返回;

} };

16十六进制颜色到RGB格式。

string.prototype.colorrgb =函数(){

无功scolor = this.tolowercase();

如果(scolor reg.test(scolor)){

如果(scolor.length = 4){

无功scolornew =# ;

对于(var i = 1;i < 4;i + = 1){

scolornew = scolor.slice(I,I + 1)。Concat(scolor.slice(I,I + 1));

}

scolor = scolornew;

}

处理六位颜色值

无功scolorchange = { };

对于(var i = 1;i < 7;i + = 2){

SColorChange.push(parseInt(0x+ scolor.slice(I,I + 2)));

}

返回RGB(+ scolorchange.join(

其他{ }

返回scolor;

} };



使用颜色转换方法:

复制代码代码如下所示:

AR sRGB =RGB(23, 245, 56)

无功shexcolor = srgb.colorhex();

无功srgbcolor = shex.colorrgb();