Last active
October 1, 2019 06:43
-
-
Save Youka/c15c0a2b6f69edf437fe9f28f2b6c2a8 to your computer and use it in GitHub Desktop.
Color as hexadecimal string to RGBA array
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
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