Return to Snippet

Revision: 55136
at January 27, 2012 22:24 by macodev


Initial Code
function rgb2hex(rgb) {
 //convert rgb value to hex
 rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/);
 function hex(x) {
  return ("0" + parseInt(x).toString(16)).slice(-2);
 }
 return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

//example get background color with jQuery
var bgcolor=rgb2hex($('body').css('background-color'));

Initial URL


Initial Description
Convert rgb color detected by jQuery into hex value.

Initial Title
Convert rgb to hex

Initial Tags
css, background, convert

Initial Language
PHP