Skip to content

Instantly share code, notes, and snippets.

@esedic
Created December 5, 2023 11:45
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 esedic/810a24cc4223ad0f523d22e202d413b7 to your computer and use it in GitHub Desktop.
Save esedic/810a24cc4223ad0f523d22e202d413b7 to your computer and use it in GitHub Desktop.
Extract computed background color hex value from given element object
var el = document.getElementById('target');
function extractHexBackgroundColorFromElement(element) {
return getComputedStyle(element)['background-color'].split('(')[1].split(')')[0].split(',').map((x) => x.trim()).map((x) => { x = parseInt(x).toString(16); return (x.length === 1) ? '0' + x : x }).join('');
}
console.log(extractHexBackgroundColorFromElement(el));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment