Last active
May 18, 2017 04:28
-
-
Save JDMCreator/af76d2fe8839ae852ffc9b5cadb07448 to your computer and use it in GitHub Desktop.
Convert HEX to RGBA (supports RGB, RRGGBB, RGBA and RRGGBBAA with or without hashtag symbol) // 111 bytes
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
/* | |
Returns a [R,G,B,A] array. [R,G,B] are [0,255] numbers and A is a [0,1] number | |
*/ | |
function(a){ | |
return [ | |
( v=255, // We set a variable inside the array to save "var" | |
c = "0x"+ // Convert from hexadecimal to decimal | |
/\w{8}/.exec( // We take the first eight alphanumerical characters | |
a.replace( // We convert short hexadecimal to long hexadecimal (RGB(A)->RRGGBB(AA)) | |
!a[6] && /./g, // We only convert if it's a short hexadecimal | |
'$&$&') // We double each character | |
+"FF" // We add "FF" as the alpha value for RRGGBB | |
) // The result of the exec() function will be converted to a string because of the concanation | |
) // The variable declaration returns the last variable i.e. c | |
>>> 24, // Red part | |
c >> 16 & v, // Green part | |
c >> 8 & v, // Blue part | |
(c & v)/v // Alpha part. We convert it to a [0,1] range | |
] | |
} |
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(b){return[(v=255,c="0x"+/\w{8}/.exec(b.replace(!b[6]&&/./g,"$&$&")+"FF"))>>>24,c>>16&v,c>>8&v,(c&v)/v]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment