Created
May 10, 2013 14:48
-
-
Save marti1125/5554868 to your computer and use it in GitHub Desktop.
rgb2hex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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