Posted By

virgil on 10/14/10


Tagged


Versions (?)

RGB 2 HEX


 / Published in: JavaScript
 

bgcolor = $('div').css('background-color');

if(bgcolor != 'transparent') { bgcolor = rgb2hex(bgcolor); };

  1. //Function to convert hex format to a rgb color
  2. function rgb2hex(rgb) {
  3. var hexDigits = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
  4. rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
  5. function hex(x) {
  6. return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
  7. }
  8. return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
  9. }

Report this snippet  

You need to login to post a comment.