Created
November 21, 2019 19:18
-
-
Save kendhia/50525b0ae51fa87274b868499fe72d85 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const verifyColor = (a, alpha) => { | |
return /^((([A-Fa-f\d]){3}){1,2})$/.test(a) && Number(alpha)>= 0 && Number(alpha)<=1 | |
} | |
const parseColor = (a) => { | |
if (a.length === 3){ | |
const groups = /^([a-f\d]{1})([a-f\d]{1})([a-f\d]{1})$/i.exec(a) | |
return [parseInt(groups[1], 16)*17, parseInt(groups[2], 16)*17,parseInt(groups[3], 16)*17] | |
} else if (a.length === 6) { | |
const groups = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(a) | |
return [parseInt(groups[1], 16), parseInt(groups[2], 16),parseInt(groups[3], 16)] | |
} | |
} | |
const hexToRgba = (a, alpha) => { | |
if (verifyColor(a, alpha)) { | |
const result = parseColor(a) | |
return `rgba(${result[0]}, ${result[1]}, ${result[2]}, ${alpha})` | |
} | |
throw "Unvalid Color HEX value" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment