转换rgb为16进制颜色值

function rgb2hex(rgb) { 
 rgb = rgb.match(/^rgb\((\d+),(\d+),(\d+)\)$/);
 function hex(x) { 
  return ("0" + parseInt(x).toString(16)).slice(-2); 
 } 
 return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]); 
}
var rgb = 'rgb(255,255,255)';
console.log(rgb2hex(rgb));

  

posted @ 2015-03-12 11:56  yo胡yo  阅读(272)  评论(0编辑  收藏  举报