Skip to content

Instantly share code, notes, and snippets.

@wolfiestyle
Created February 21, 2013 22:04
Show Gist options
  • Select an option

  • Save wolfiestyle/5008743 to your computer and use it in GitHub Desktop.

Select an option

Save wolfiestyle/5008743 to your computer and use it in GitHub Desktop.
js functions to convert between hex string and rgb object colors
function hexToRgb(hex)
{
var res = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return res ? {
r: parseInt(res[1], 16),
g: parseInt(res[2], 16),
b: parseInt(res[3], 16)
} : null;
}
function rgbToHex(r, g, b)
{
return ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment