Skip to content

Instantly share code, notes, and snippets.

@zachvictor
Last active February 1, 2020 08:49
Show Gist options
  • Save zachvictor/01c525baa851d330610db179838b7029 to your computer and use it in GitHub Desktop.
Save zachvictor/01c525baa851d330610db179838b7029 to your computer and use it in GitHub Desktop.
Translate Coolors.co palette to hex and rgb values in JavaScript/JSON object
var cp = (function () {
class CoolorsPalette {
constructor() {
// Row spans all palette columns: i.e., one shade of each color
this.rows = Array.from(document.querySelectorAll('#palette-shades div.palette-shades-row'))
const numCols = this.rows[0].querySelectorAll('div.palette-shades-col').length
this.colors = Array(numCols).fill([])
this.rows.forEach(row => {
Array
.from(row.querySelectorAll('div.palette-shades-col'))
.forEach((cell, i) => { // cell = distinct color-shade
const hex = cell.attributes['data-color'].value
const [r, g, b] = cell.style.background.match(/\d+/g)
this.colors[i].push({ hex, r, g, b, cell, row })
})
})
}
}
return new CoolorsPalette()
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment