js方法实现rgb颜色转换成16进制格式的代码的方法

原文地址:http://www.cnblogs.com/vaal-water/archive/2013/04/08/3008880.html

自己试过很好用

复制代码
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);
}
原文地址:https://www.cnblogs.com/yy-hh/p/rgb.html