Skip to content

Instantly share code, notes, and snippets.

@gabssnake
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabssnake/f3b46c3fefd0aaceae62 to your computer and use it in GitHub Desktop.
Save gabssnake/f3b46c3fefd0aaceae62 to your computer and use it in GitHub Desktop.
Convert CSS hex color to rgba() with opacity
// adapted from awesomest http://gist.github.com/lrvick/2080648
var hexToRGB = function ( hex ) {
var r,g,b;
hex = parseInt( hex.replace('#',''), 16 );
r = hex >> 16;
g = hex >> 8 & 0xFF;
b = hex & 0xFF;
return [r,g,b];
};
// add opacity
var colorOpacity = function ( hex, opacity ) {
return 'rgba('+ hexToRGB( hex ).join(',') + ',' + opacity +')';
};
// test
var newColor = colorOpacity( '#663399', 0.4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment