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.

Revisions

  1. Igi Manaloto revised this gist Sep 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion extract-colours-from-site.js
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ function rgb2hex(rgb) {
    }
    }

    var keys = Object.keys(window.getComputedStyle($0)).filter(r =>
    var keys = Object.keys(window.getComputedStyle(document.body)).filter(r =>
    /color/gi.test(r)
    );
    [
  2. Igi Manaloto created this gist Sep 3, 2019.
    32 changes: 32 additions & 0 deletions extract-colours-from-site.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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($0)).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();