Skip to content

Instantly share code, notes, and snippets.

@marti1125
Created May 10, 2013 14:48
Show Gist options
  • Save marti1125/5554868 to your computer and use it in GitHub Desktop.
Save marti1125/5554868 to your computer and use it in GitHub Desktop.
rgb2hex
function rgb2hex(rgb) {
if ( rgb.search("rgb") == -1 ) {
return rgb;
}
else if ( rgb == 'rgba(0, 0, 0, 0)' ) {
return 'transparent';
}
else {
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]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment