Skip to content

Instantly share code, notes, and snippets.

@Youka
Last active October 1, 2019 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Youka/c15c0a2b6f69edf437fe9f28f2b6c2a8 to your computer and use it in GitHub Desktop.
Save Youka/c15c0a2b6f69edf437fe9f28f2b6c2a8 to your computer and use it in GitHub Desktop.
Color as hexadecimal string to RGBA array
function hex2RGBA(hex: string) {
if(hex !== null && hex.charAt(0) === '#') {
const pattern = /[0-9a-f]{1,2}/ig,
rgba = [0, 0, 0, 255];
for(
let i = 0, match;
i < 4 && (match = pattern.exec(hex)) !== null;
i++
)
rgba[i] = parseInt(match.toString(), 16);
return rgba;
}else
return [0, 0, 0, 0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment