Last active
September 9, 2022 19:22
-
-
Save kumarchandresh/bdfde4ff325f531ff4be2640263a475b 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
| function hexToRgb(hex) { | |
| if (!/^#?([A-F0-9]{3}){1,2}$/gim.test(hex)) { | |
| throw new TypeError("Bad Hex"); | |
| } | |
| digits = hex.replace("#", "").split(""); | |
| if (digits.length == 3) { | |
| digits = digits.flatMap(n => Array(2).fill(n)); | |
| } | |
| val = "0x" + digits.join(""); | |
| r = (val >> 16) & 0xff; | |
| g = (val >> 8) & 0xff; | |
| b = val & 0xff; | |
| return `rgb(${r}, ${g}, ${b})`; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment