Skip to content

Instantly share code, notes, and snippets.

@juji
Last active March 9, 2021 04:10
Show Gist options
  • Save juji/b900c8b896d36647ca8d80960c7aef2c to your computer and use it in GitHub Desktop.
Save juji/b900c8b896d36647ca8d80960c7aef2c to your computer and use it in GitHub Desktop.
hex to rgba
(str, alpha) => {
if(!/^#([A-Fa-f0-9]{3}){1,2}$/.test(str))
throw new Error('Bad hex')
let c = str.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}, ${alpha})`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment