Posted By

macodev on 01/27/12


Tagged

css hex background convert RGB


Versions (?)

Convert rgb to hex


 / Published in: PHP
 

Convert rgb color detected by jQuery into hex value.

  1. function rgb2hex(rgb) {
  2. //convert rgb value to hex
  3. rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/);
  4. function hex(x) {
  5. return ("0" + parseInt(x).toString(16)).slice(-2);
  6. }
  7. return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
  8. }
  9.  
  10. //example get background color with jQuery
  11. var bgcolor=rgb2hex($('body').css('background-color'));

Report this snippet  

You need to login to post a comment.