Skip to content

Instantly share code, notes, and snippets.

@thybzi
Created June 15, 2022 11:46
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 thybzi/72f3d2bcfde15cc26b2e29be90b5c29f to your computer and use it in GitHub Desktop.
Save thybzi/72f3d2bcfde15cc26b2e29be90b5c29f to your computer and use it in GitHub Desktop.
/**
* Convert #1a2b3c to { r: 26, g: 43, b: 60 }
* @param {string} hex
* @returns {*}
* @see https://stackoverflow.com/a/5624139
*/
function hexToRgb(hex) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16),
} : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment