Created
December 7, 2022 02:19
-
-
Save yukulele/5490b85be2dd44e780fa47c7967f5e04 to your computer and use it in GitHub Desktop.
hexToRGB
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 hexToRGB(hex = '#000') { | |
hex = hex.replace(/^#/, '') | |
if ( | |
hex.length !== 3 && hex.length !== 6 | |
|| /[^\da-f]/i.test(hex) | |
) throw new Error('Invaild hex color string') | |
return hex | |
.match(/(..?)(..?)(..?)/) | |
.slice(1) | |
.map(n => Number.parseInt(n, 16)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment