Skip to content

Instantly share code, notes, and snippets.

@zmnv
Created July 3, 2019 10:53
Show Gist options
  • Save zmnv/5236102394c6dcc8f6bba969817586a0 to your computer and use it in GitHub Desktop.
Save zmnv/5236102394c6dcc8f6bba969817586a0 to your computer and use it in GitHub Desktop.
/**
* Преобразовать `#FF0000` -> `rgba(255, 0, 0, 1)`
* https://stackoverflow.com/a/28056903
*
* @param {string} hex
* @param {number} alpha
*/
export function hexToRGB(hex, alpha) {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
if (alpha) {
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}
return `rgb(${r}, ${g}, ${b})`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment