Last active
February 1, 2020 08:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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