rgb html转换,RGB与十六进制数值互转(html)

保存下来,浏览器打开就可以使用了,挺方便

CSS入门

十六进制颜色值

值"#FF9999"是由红绿蓝三原色组成的颜色,#号后的两位表示红,其后的两位为绿,最后的两位为蓝。

function updateHex()

{

var red = parseInt(document.forms.colorcalc.red.value);

var green = parseInt(document.forms.colorcalc.green.value);

var blue = parseInt(document.forms.colorcalc.blue.value);

document.forms.colorcalc.hexcolor.value =

"#" + Color2Hex(red)+Color2Hex(green)+Color2Hex(blue);

}

function Color2Hex(n)

{

return ToHex(n >> 4) + ToHex(n);

}

function ToHex(n)

{

n = n % 16;

if (n <= 0)

return "0";

if (n < 10)

return n.toString();

if (n == 10)

return "A";

if (n == 11)

return "B";

if (n == 12)

return "C";

if (n == 13)

return "D";

if (n == 14)

return "E";

if (n == 15)

return "F";

}

function CheckDecimal(s)

{

var n = parseInt(s);

var i, c;

for (i = 0; i < s.length; ++i)

{

c = s.charAt(i);

if (c < '0' || '9' < c)

{

alert("Please enter a value between 0 and 255");

return false;

}

}

if (n < 0 || 255 < n)

{

alert("Please enter a value between 0 and 255");

return false;

}

return true;

}

function CheckHex(s)

{

if (!isHexColor(s))

{

alert("Please enter a value starting with '#' followed" +

"\nby 6 hexadecimal digits, for example: #00FF66");

return false;

}

return true;

}

function isHexColor(s)

{

var i, c;

if (s.charAt(0) != '#' || s.length != 7)

return false;

for (i = 1; i < 7; ++i)

{

c = s.charAt(i);

if ('0' <= c && c <= '9')

continue;

if ('A' <= c && c <= 'F')

continue;

if ('a' <= c && c <= 'f')

continue;

return false; // bad character

}

return true;

}

function updateRGB()

{

var s = document.forms.colorcalc.hexcolor.value;

if (CheckHex(s))

{

var red = HexColor2Decimal(s, 1);

var green = HexColor2Decimal(s, 3);

var blue = HexColor2Decimal(s, 5);

document.forms.colorcalc.red.value = red.toString();

document.forms.colorcalc.green.value = green.toString();

document.forms.colorcalc.blue.value = blue.toString();

}

}

function HexColor2Decimal(s, n)

{

return 16 * HexDigit2Dec(s.charAt(n))

+ HexDigit2Dec(s.charAt(n+1));

}

// called with hex character

function HexDigit2Dec(hex)

{

if ('0' <= hex && hex <= '9')

return parseInt(hex);

if (hex == 'A' || hex == 'a')

return 10;

if (hex == 'B' || hex == 'b')

return 11;

if (hex == 'C' || hex == 'c')

return 12;

if (hex == 'D' || hex == 'd')

return 13;

if (hex == 'E' || hex == 'e')

return 14;

if (hex == 'F' || hex == 'f')

return 15;

return 0;

}

输入 RGB 或十六进制数值后按相应的按钮进转换

red:

value=255 name=red>

十六进制数值green:

value=255 name=green>

value=#FFFFFF name=hexcolor>

blue:

value=255 name=blue>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值