Home >Web Front-end >JS Tutorial >JS function to convert RGB color values ​​into HTML hexadecimal (HEX) code_javascript skills

JS function to convert RGB color values ​​into HTML hexadecimal (HEX) code_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 18:53:381576browse

Copy code The code is as follows:

//Go to fixed-length hexadecimal characters string, if not enough, fill in 0
function zero_fill_hex(num, digits) {
var s = num.toString(16);
while (s.length < digits)
s = "0" s;
return s;
}

//Damn it, I couldn’t find how to use javascript to find the value of a background color, so I had to parse it myself
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