Created
March 21, 2020 22:58
-
-
Save mikehwagz/a3ddd5c66cab8be30002ac8a71baf609 to your computer and use it in GitHub Desktop.
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
export function hexToRGBA(hex, alpha) { | |
let c | |
if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) { | |
c = hex.substring(1).split('') | |
if (c.length == 3) { | |
c = [c[0], c[0], c[1], c[1], c[2], c[2]] | |
} | |
c = '0x' + c.join('') | |
return ( | |
'rgba(' + | |
[(c >> 16) & 255, (c >> 8) & 255, c & 255].join(',') + | |
', ' + | |
alpha + | |
')' | |
) | |
} else { | |
console.warn('Bad hex') | |
return hex | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment