Skip to content

Instantly share code, notes, and snippets.

@RedWolves
Created July 9, 2010 03:43
Show Gist options
  • Save RedWolves/468998 to your computer and use it in GitHub Desktop.
Save RedWolves/468998 to your computer and use it in GitHub Desktop.
function rgbToHex(rgb) {
if (rgb.match(/^#[0-9A-Fa-f]{6}$/)) {
return rgb;
}
var rgbvals = /rgb\((.+),(.+),(.+)\)/i.exec(rgb);
if (!rgbvals) {
return rgb;
}
var rval = parseInt(rgbvals[1]);
var gval = parseInt(rgbvals[2]);
var bval = parseInt(rgbvals[3]);
var pad = function(value) {
return (value.length < 2 ? '0' : '') + value;
};
return '#' + pad(rval.toString(16)) + pad(gval.toString(16)) + pad(bval.toString(16));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment