Javascript转换RGB与16进制颜色值
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>RGB2HEX</title>
<script type="text/javascript" charset="utf-8">
function RGBtoHex(R,G,B)
{
return toHex(R)+toHex(G)+toHex(B)
}
function toHex(N) {
if (N==null)
return "00";
N=parseInt(N);
if (N==0 || isNaN(N))
return "00";
N=Math.max(0,N);
N=Math.min(N,255);
N=Math.round(N);
return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);
}
//Hex-to-RGB Conversion
<!--
R = HexToR("#FFFFFF");
G = HexToG("#FFFFFF");
B = HexToB("#FFFFFF");
function HexToR(h) { return parseInt((cutHex(h)).substring(0,2),16) }
function HexToG(h) { return parseInt((cutHex(h)).substring(2,4),16) }
function HexToB(h) { return parseInt((cutHex(h)).substring(4,6),16) }
function cutHex(h) { return (h.charAt(0)=="#") ? h.substring(1,7) : h}
//-->
</script>
</head>
<body>
Try it yourself:
<form name=rgb>
<input type=text name=hex size=8 value="FFFFFF">
<input type=button name=btn value="Convert to RGB"
onCLick="this.form.r.value=HexToR(this.form.hex.value);
this.form.g.value=HexToG(this.form.hex.value);
this.form.b.value=HexToB(this.form.hex.value);
">
R:<input type=text name=r size=3>
G:<input type=text name=g size=3>
B:<input type=text name=b size=3>
</form>
Try it yourself:
<form name=rgb>
R:<input type=text name=r size=3 value=255>
G:<input type=text name=g size=3 value=255>
B:<input type=text name=b size=3 value=255>
<input type=button name=btn value="Convert to Hex"
onCLick="this.form.hex.value=RGBtoHex(this.form.r.value,this.form.g.value,this.form.b.value)">
<input type=text name=hex size=8>
</form>
</body>
</html>
本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。

浙公网安备 33010602011771号