Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

node-vibrant

Vibrant-Colors465.6kMIT4.0.3TypeScript support: included

Extract prominent colors from an image. Supports both node and browser environment.

color, detection, varation, image, picture, canvas, vibrant, muted, colour

readme

node-vibrant

Join the discussion on Github Best of JS

Extract prominent colors from an image.

  • Identical API for node.js, browser, and worker environments

Install

$ npm install node-vibrant

Usage

// Node
import { Vibrant } from "node-vibrant/node";
// Browser
import { Vibrant } from "node-vibrant/browser";
// Web Worker
import { Vibrant } from "node-vibrant/worker";

// Using builder
Vibrant.from("path/to/image")
    .getPalette()
    .then((palette) => console.log(palette));

// Using constructor
let v = new Vibrant("path/to/image", opts);
v.getPalette().then((palette) => console.log(palette));

Worker Usage

Quantization is the most time-consuming stage in node-vibrant. Luckily, the quantization can be run in the WebWorker to avoid freezing the UI thread.

Here's how to use this feature:

import { Vibrant, WorkerPipeline } from "node-vibrant/worker";
import PipelineWorker from "node-vibrant/worker.worker?worker";

Vibrant.use(new WorkerPipeline(PipelineWorker as never));

This requires your bundler to handle ?worker transforms similar to how Vite does

Documentation

Docs can be seen currently in the docs folder. It includes reference docs and step-by-step guides.

We also have a few examples that you can reference for your needs.

PRs welcomed to expand either of these!

Notes

Result Consistency

The results are consistent within each user's browser instance regardless of the visible region or display size of an image, unlike the original vibrant.js implementation.

However, due to the nature of the HTML5 canvas element, image rendering is platform/machine-dependent. The resulting swatches may vary between browsers, Node.js versions, and between machines. See Canvas Fingerprinting.

The test specs use CIE delta E 1994 color difference to measure inconsistencies across platforms. It compares the generated color on Node.js, Chrome, Firefox, and IE11. At quality == 1 (no downsampling) with no filters and the results are rather consistent. Color diffs between browsers are mostly not perceptible by the human eye. Downsampling will cause perceptible inconsistent results across browsers due to differences in canvas implementations.

changelog

v4.0.0

node-vibrant's internal packaging is now vastly more stable and improved. We'll continue to ship the default package with the same API as before, but now you can also import individual modules for more control over your environment.

Breaking Changes

  • You now must import from node-vibrant/browser, node-vibrant/node, or node-vibrant/worker to get the correct environment-specific implementation
  • Vibrant class is now a named export
  • Node 18+ is now required
  • ES5 support is dropped
  • Vibrant.getPalette no longer accepts a name or callback API
    • Please migrate to the promise-based API to replace the callback API
    • Please let us know if you were using the name API: https://github.com/Vibrant-Colors/node-vibrant/issues
      • It was unclear how this was used in practice. We can reconsider adding it back if there's usecases I wasn't aware of
  • Vibrant.getPalettes no longer accepts a names or callback API
    • Please migrate to the promise-based API to replace the callback API
    • Please let us know if you were using the names API: https://github.com/Vibrant-Colors/node-vibrant/issues
      • It was unclear how this was used in practice. We can reconsider adding it back if there's usecases I wasn't aware of
  • Builder.getSwatches alias was removed in favor of calling Builder.getPalette
  • Removed the deprecated Builder.getHex API, use the Builder.hex property instead
  • Removed the deprecated Builder.getPopulation API, use the Builder.population property instead
  • Removed the deprecated Builder.getHsl API, use the Builder.hsl property instead
  • Removed the deprecated Builder.getRgb API, use the Builder.rgb property instead
  • Workers are imported differently using the ?worker import fix alias

v3.2.0

node-vibrant is now a monorepo. Building blocks of node-vibrant has been separated into multiple small packages under @vibrant/* scope. The goal is to make it more flexible, allowing alternative algorithms, additional image format support and etc to be implemented by 3rd party packages.

The node-vibrant package still provides default experience out-of-box.

Breaking Changes

  • strickNullChecks flag is now enabled.
  • Prebuilt bundle will not be provided. You should use your own webpack workflow.

3.1.0

  • Fix empty swatches sometimes showing up for images
  • Update deps
  • Moved from Bluebird to native promises
  • Fix issue with dist folder not appearing on install
  • Fix issue with images not loading properly

3.0.0

New WebWorker support in v3.0

Quantization is the most time-consuming stage in node-vibrant. In v3.0, the quantization can be run in the WebWorker to avoid freezing the UI thread.

Here's how to use this feature:

  1. Use WebWorker build dist/vibrant.worker.js or dist/vibrant.worker.min.js. Or if you are re-bundling with webpack, use lib/bundle.worker.js as entry
  2. Use WebWorker quantizer:
    let v = Vibrant.from(src).useQuantizer(Vibrant.Quantizer.WebWorker);
    // Other configurations