Convert rgb to hex


/ Published in: PHP
Save to your folder(s)

Convert rgb color detected by jQuery into hex value.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.