node-vibrant: Image Color Palette Extraction

4.0.4 · active · verified Sun Apr 19

node-vibrant is a JavaScript library engineered for extracting prominent colors and generating a color palette from images. It effectively analyzes image data to produce a set of 'swatches,' including vibrant, muted, dark vibrant, light vibrant, dark muted, and light muted colors. The library is currently at stable version 4.0.4 and has resumed active development following a substantial four-year hiatus before the significant 4.0.0 release. Key features include comprehensive support for both Node.js and browser environments, a full rewrite in TypeScript since version 3.0.0-alpha.1, and a modern Promise-based API for handling asynchronous operations. This combination makes it a robust solution for applications requiring dynamic, type-safe color palette generation from images, integrating smoothly with contemporary JavaScript development workflows.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to extract a color palette from an image URL using the Promise-based API in a Node.js environment, displaying the dominant color swatches.

import { Vibrant } from 'node-vibrant/node'; // Or 'node-vibrant/browser' for browsers

async function extractColorsFromImage(imageUrl: string) {
  try {
    console.log(`Extracting colors from: ${imageUrl}`);
    const palette = await Vibrant.from(imageUrl).getPalette();

    console.log('Generated Color Palette:');
    console.log('  Vibrant:', palette.Vibrant?.hex || 'N/A');
    console.log('  Muted:', palette.Muted?.hex || 'N/A');
    console.log('  Dark Vibrant:', palette.DarkVibrant?.hex || 'N/A');
    console.log('  Light Vibrant:', palette.LightVibrant?.hex || 'N/A');
    console.log('  Dark Muted:', palette.DarkMuted?.hex || 'N/A');
    console.log('  Light Muted:', palette.LightMuted?.hex || 'N/A');

    if (palette.Vibrant) {
      console.log(`  Vibrant RGB: ${palette.Vibrant.rgb}`);
      console.log(`  Vibrant Population: ${palette.Vibrant.population}`);
    }
  } catch (error) {
    console.error('Failed to extract colors:', error);
  }
}

// Example usage with a public image URL
// For a local file in Node.js, replace 'imageUrl' with a local path like 'file:///path/to/image.jpg'
const exampleImageUrl = 'https://picsum.photos/id/1018/1000/600';
extractColorsFromImage(exampleImageUrl);

view raw JSON →