Skip to content

Instantly share code, notes, and snippets.

@macouella
Last active November 12, 2020 03:34
Show Gist options
  • Save macouella/cf3dad75a6796706882a6ee657c2bc7c to your computer and use it in GitHub Desktop.
Save macouella/cf3dad75a6796706882a6ee657c2bc7c to your computer and use it in GitHub Desktop.
Extract all colours from a site :)
function rgb2hex(rgb) {
if (rgb.search("rgb") == -1) {
return rgb;
} else {
rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/);
function hex(x) {
return ("0" + parseInt(x).toString(16)).slice(-2);
}
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
}
var keys = Object.keys(window.getComputedStyle(document.body)).filter(r =>
/color/gi.test(r)
);
[
...Array.from(document.querySelectorAll("*")).reduce((colors, el) => {
keys.forEach(ee => {
colors.add(getComputedStyle(el)[ee]);
});
return colors;
}, new Set())
]
.map(n => {
try {
return rgb2hex(n);
} catch (e) {
return null;
}
})
.filter(ab => typeof ab === "string" && ab.indexOf("#") === 0)
.toString();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment