function getRGB(scolor){ return new Array(Number("0x"+scolor.substr(1,2)), Number("0x"+scolor.substr(3,2)), Number("0x"+scolor.substr(5,2))); } var s=getRGB("#ffcc22"); alert(s)
<script > function LongToRGB(ColorStr) {
var Red=parseInt("0x"+ColorStr.substr(1,2)); var Green=parseInt("0x"+ColorStr.substr(3,2)); var Blue=parseInt("0x"+ColorStr.substr(5,2)); return (Red+","+Green+","+Blue);
} alert(LongToRGB("#ffcc22"));</script></script>
<script language="javascript"> // 转RGB,返回一个包含RGB值的数组 function toRgb(value) { value = value.replace("#", ""); if (value.length != 6) return; var rgb = new Array(0, 0, 0); var r = value.substring(0, 2); var g = value.substring(2, 4); var b = value.substring(4, 6);
rgb[0] = dec(r); rgb[1] = dec(g); rgb[2] = dec(b); return rgb; }// 十六进制转十进制 function dec(value) { var v = new Array(); var one = ""; var j = 0;
for (var i=value.length-1; i>-1; i--) { one = value.substr(i, 1); switch (one) { case "a": one = 10; break; case "b": one = 11; break; case "c": one = 12; break; case "d": one = 13; break; case "e": one = 14; break; case "f": one = 15; break; default: one = parseInt(one); break; } v[j] = one; j++; }
value = 0; for (var i=0; i<v.length; i++) { value += v[i]*Math.pow(16, i) }
return new Array(Number("0x"+scolor.substr(1,2)),
Number("0x"+scolor.substr(3,2)),
Number("0x"+scolor.substr(5,2)));
}
var s=getRGB("#ffcc22");
alert(s)
function LongToRGB(ColorStr)
{
var Red=parseInt("0x"+ColorStr.substr(1,2));
var Green=parseInt("0x"+ColorStr.substr(3,2));
var Blue=parseInt("0x"+ColorStr.substr(5,2));
return (Red+","+Green+","+Blue);
}
alert(LongToRGB("#ffcc22"));</script></script>
// 转RGB,返回一个包含RGB值的数组
function toRgb(value)
{
value = value.replace("#", "");
if (value.length != 6) return;
var rgb = new Array(0, 0, 0);
var r = value.substring(0, 2);
var g = value.substring(2, 4);
var b = value.substring(4, 6);
rgb[0] = dec(r);
rgb[1] = dec(g);
rgb[2] = dec(b);
return rgb;
}// 十六进制转十进制
function dec(value)
{
var v = new Array();
var one = "";
var j = 0;
for (var i=value.length-1; i>-1; i--)
{
one = value.substr(i, 1);
switch (one)
{
case "a":
one = 10;
break;
case "b":
one = 11;
break;
case "c":
one = 12;
break;
case "d":
one = 13;
break;
case "e":
one = 14;
break;
case "f":
one = 15;
break;
default:
one = parseInt(one);
break;
}
v[j] = one;
j++;
}
value = 0;
for (var i=0; i<v.length; i++)
{
value += v[i]*Math.pow(16, i)
}
return value;
}// 使用方法如下所示
var color = "#ffcc22";
var rgb = toRgb(color); // 返回的值是数组,分别是:r、g、b
document.write(color + "<br>");
document.write("R=" + rgb[0] + "<br>");
document.write("G=" + rgb[1] + "<br>");
document.write("B=" + rgb[2] + "<br>");
</script>