Home > Web Front-end > JS Tutorial > Javascript converts rgb color into hexadecimal format_javascript skills

Javascript converts rgb color into hexadecimal format_javascript skills

WBOY
Release: 2016-05-16 15:50:39
Original
1273 people have browsed it

I tried it myself and it works great

function zero_fill_hex(num, digits) {
 var s = num.toString(16);
 while (s.length < digits)
  s = "0" + s;
 return s;
}
function rgb2hex(rgb) {

 if (rgb.charAt(0) == '#')
  return 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);
}
Copy after login

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template