Skip to content

Instantly share code, notes, and snippets.

@yukulele
Created December 7, 2022 02:19
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 yukulele/5490b85be2dd44e780fa47c7967f5e04 to your computer and use it in GitHub Desktop.
Save yukulele/5490b85be2dd44e780fa47c7967f5e04 to your computer and use it in GitHub Desktop.
hexToRGB
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