Skip to content

Instantly share code, notes, and snippets.

@dapize
Last active April 17, 2021 07:54
Show Gist options
  • Save dapize/f5a5cfa2e6b24606db02c978d60a4fb5 to your computer and use it in GitHub Desktop.
Save dapize/f5a5cfa2e6b24606db02c978d60a4fb5 to your computer and use it in GitHub Desktop.
Hexadecimal to RGBA
function hexToRgbA(hex){
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(',')+',1)';
}
throw new Error('Bad Hex');
}
/*
hexToRgbA('#fbafff') ===> rgba(251,175,255,1)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment