Skip to content

Instantly share code, notes, and snippets.

@TiagoFuelber
Created October 23, 2019 17:53
Show Gist options
  • Save TiagoFuelber/90245b05939d2055ce337cb23cc7a45e to your computer and use it in GitHub Desktop.
Save TiagoFuelber/90245b05939d2055ce337cb23cc7a45e to your computer and use it in GitHub Desktop.
Transform a HEX color to RGBA
export default function hexToRgbA(hex, alpha = 1) {
var 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})`;
}
throw new Error('Bad Hex');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment