Skip to content

Instantly share code, notes, and snippets.

@NiccoMlt
Last active October 14, 2021 07:18
Show Gist options
  • Save NiccoMlt/4e4ecd2bb6c26ccb3847c9cc68ec86b4 to your computer and use it in GitHub Desktop.
Save NiccoMlt/4e4ecd2bb6c26ccb3847c9cc68ec86b4 to your computer and use it in GitHub Desktop.
Infer color palettes from a webpage on Node.js leveraging Microsoft Playwright and node-vibrant
/*
* The MIT License (MIT)
* Copyright © 2021 Niccolò Maltoni
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const Vibrant = require('node-vibrant')
const playwright = require('playwright');
/**
* Invoke a browser with Microsoft Playwright to screenshot an external page.
*
* @param {string} url the URL of the page to fetch and screenshot
* @param {'firefox'|'chromium'|'webkit'} [browserType='chromium'] the type of browser to use
* @async
* @returns {Promise<Buffer>} a buffer containing a screenshot of the page at the rendered URL
*/
async function fetchPage(url, browserType = 'chromium') {
const browser = await playwright[browserType].launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto(url, {
waitUntil: 'networkidle',
});
const screenshotBuffer = await page.screenshot();
await browser.close();
return screenshotBuffer;
}
/**
* Infer a color palette from a buffer.
*
* @param {Buffer} buffer a buffer containing an image to inspect
* @async
* @returns {Promise<import('@vibrant/color').Palette>} a resultant palette
*/
async function inferColors(buffer) {
const vibrant = Vibrant.from(buffer);
return vibrant.getPalette();
}
fetchPage('https://www.magnews.com/')
.then(buffer => inferColors(buffer))
.then(palette => Object
.entries(palette)
.filter(([, swatch]) => !!swatch)
.map(([name, swatch]) => [name, swatch.hex])
.forEach(([name, hex]) => console.log(`${name}: ${hex}`)))
.catch(error => console.log(error))
@NiccoMlt
Copy link
Author

NiccoMlt commented Oct 13, 2021

Executing this outputs the following colors:

Vibrant: #7d7d81
DarkVibrant: #414143
LightVibrant: #bbbbbd
Muted: #848484
DarkMuted: #4a453d
LightMuted: #ddddde

which originate from the following screenshot:
screenshot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment