Skip to content

Instantly share code, notes, and snippets.

@camskene
Last active December 16, 2015 14:29
Show Gist options
  • Save camskene/5449308 to your computer and use it in GitHub Desktop.
Save camskene/5449308 to your computer and use it in GitHub Desktop.
Hexadecimal to RGB. Converts hex string to rgb string
/**
*
* 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