function rgb2hex(rgb) { var components = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/); function hex(x) { return ("0" + parseInt(x).toString(16)).slice(-2); } return "#" + hex(components[1]) + hex(components[2]) + hex(components[3]); }