Last active
August 29, 2015 14:03
-
-
Save gabssnake/f3b46c3fefd0aaceae62 to your computer and use it in GitHub Desktop.
Convert CSS hex color to rgba() with opacity
This file contains 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
// 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