Home  >  Article  >  Web Front-end  >  RGB颜色值转HTML十六进制(HEX)代码的JS函数_javascript技巧

RGB颜色值转HTML十六进制(HEX)代码的JS函数_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:53:381484browse

复制代码 代码如下:

//转到固定长度的十六进制字符串,不够则补0
function zero_fill_hex(num, digits) {
var s = num.toString(16);
while (s.length s = "0" + s;
return s;
}

//妈的,怎么都没搜到怎么用javascript找出一个背景色的数值,只好自己解析
function rgb2hex(rgb) {
//nnd, Firefox / IE not the same, fxck
if (rgb.charAt(0) == '#')
return rgb;
var n = Number(rgb);
var ds = rgb.split(/\D+/);
var decimal = Number(ds[1]) * 65536 + Number(ds[2]) * 256 + Number(ds[3]);
return "#" + zero_fill_hex(decimal, 6);
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn