How to get hex color value rather than RGB value?

Using the following jQuery will get the RGB value of an element’s background color:

$('#selector').css('backgroundColor');

Is there a way to get the hex value rather than the RGB?

Answer

var hexDigits = new Array
        ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"); 

//Function to convert rgb color to hex format
function rgb2hex(rgb) {
 rgb = rgb.match(/^rgb((d+),s*(d+),s*(d+))$/);
 return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

function hex(x) {
  return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
 }

(Source)