Skip to content

Instantly share code, notes, and snippets.

@kumarchandresh
Last active September 9, 2022 19:22
Show Gist options
  • Select an option

  • Save kumarchandresh/bdfde4ff325f531ff4be2640263a475b to your computer and use it in GitHub Desktop.

Select an option

Save kumarchandresh/bdfde4ff325f531ff4be2640263a475b to your computer and use it in GitHub Desktop.
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