Last active
December 16, 2015 14:29
-
-
Save camskene/5449308 to your computer and use it in GitHub Desktop.
Hexadecimal to RGB.
Converts hex string to rgb string
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
| /** | |
| * | |
| * getRGB("#ffffff") | |
| * rgb(255,255,255) | |
| * | |
| */ | |
| function getRGB(hex) { | |
| // check for hash | |
| hex = (hex.substr(0,1) == "#") ? hex.substr(1) : hex; | |
| // string to build rgb value | |
| var rgb = "rgb("; | |
| // loop through the hex string | |
| for (var i = 0; i <= 4; i += 2) { | |
| rgb += (parseInt(hex.substr(i, 2), 16)) | |
| // don't want a comma after the last value so bail | |
| if (i == 4 ) break; | |
| rgb += ","; | |
| } | |
| rgb += ")"; | |
| return rgb; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment