node-vibrant

Extract prominent colors from an image

README

node-vibrant Build Status


Extract prominent colors from an image.

NOTICE


This project is refactored into a monorepo in version 3.2.0 (see develop branch, npm versionnode-vibrant@3.2.0-alpha).

We will not merge new PRs to v3.1 related to new featuresets during this time. However, bug fixes and security vulnerability fixes are still highly encouraged.

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:
  1. ```ts
  2.    let v = Vibrant.from(src)
  3.      .useQuantizer(Vibrant.Quantizer.WebWorker)
  4.      // Other configurations
  5. ```

Features

- Identical API for both node.js and browser environment
- Support browserify/webpack
- Consistent results (*See Result Consistency)

Install


  1. ``` sh
  2. $ npm install node-vibrant
  3. ```

Usage

node.js / browserify


  1. ``` js
  2. // ES5
  3. var Vibrant = require('node-vibrant')
  4. // ES6
  5. import * as Vibrant from 'node-vibrant'
  6. // TypeScript
  7. import Vibrant = require('node-vibrant')

  8. // Using builder
  9. Vibrant.from('path/to/image').getPalette((err, palette) => console.log(palette))
  10. // Promise
  11. Vibrant.from('path/to/image').getPalette()
  12.   .then((palette) => console.log(palette))

  13. // Using constructor
  14. let v = new Vibrant('path/to/image', opts)
  15. v.getPalette((err, palette) => console.log(palette))
  16. // Promise
  17. v.getPalette().then((palette) => console.log(palette))
  18. ```

Browser


If you installed node-vibrant with npm, compiled bundles are available under node_modules/node-vibrant/dist.
Or you can download bundles from Relases.

  1. ``` html
  2. <script src="/path/to/dist/vibrant.js"></script>
  3. <script src="/path/to/dist/vibrant.min.js"></script>
  4. <script>
  5.   // Use `Vibrant` in script
  6.   // Vibrant is exported to global. window.Vibrant === Vibrant
  7.   Vibrant.from('path/to/image').getPalette(function(err, palette) {});
  8.   // Promise
  9.   Vibrant.from('path/to/image').getPalette().then(function(palette) {});
  10.   // Or
  11.   var v = new Vibrant('/path/to/image', opts);
  12.   // ... same as in node.js
  13. </script>
  14. ```

Contribution Guidelines

1. Make changes
2. Write test specs if necessary
3. Pass tests
4. Commit source files only (without compiled files)

References


Vibrant

Main class of node-vibrant.

Vibrant.from(src: ImageSource): Builder

Make a Builder for an image. Returns a Builder instance.

#### `constructor(src: ImageSource, opts: Partial)`

Name    |  Description
image |  Path to image file (support HTTP/HTTPs)
opts  |  Options (optional)

ImageSource

  1. ```ts
  2. export type ImageSource = string
  3.   | HTMLImageElement  // Browser only
  4.   | Buffer            // Node.js only
  5. ```

Options

  1. ```ts
  2. export interface Options {
  3.     colorCount: number
  4.     quality: number
  5.     maxDimension: number
  6.     filters: Array<Filter>
  7.     ImageClass: ImageClass
  8.     quantizer: Quantizer
  9.     generator?: Generator
  10. }
  11. ```

Field          | Default                         | Description
colorCount   | 64                              | amount of colors in initial palette from which the swatches will be generated
quality      | 5                               | Scale down factor used in downsampling stage. 1 means no downsampling. If maxDimension is set, this value will not be used.
maxDimension | undefined                     | The max size of the image's longer side used in downsampling stage. This field will override quality.
filters      | []                            | An array of filters
ImageClass   | Image.Node or Image.Browser | An Image implementation class
quantizer    | Vibrant.Quantizer.MMCQ        | A Quantizer implementation class
generator    | Vibrant.Generator.Default     | An Generator instance

##### `Resolvable`

  1. ```ts
  2. export type Resolvable<T> = T | Promise<T>
  3. ```

Quantizer

  1. ```ts
  2. export interface Quantizer {
  3.     (pixels: Pixels, opts: Options): Resolvable<Array<Swatch>>
  4. }
  5. ```

Generator

  1. ```ts
  2. export interface Generator {
  3.     (swatches: Array<Swatch>, opts?: Object): Resolvable<Palette>
  4. }
  5. ```


Filter

Returns true if the color is to be kept.

  1. ```ts
  2. export interface Filter {
  3.     (red: number, green: number, blue: number, alpha: number): boolean
  4. }
  5. ```

#### `getPalette(cb?: Callback): Promise`

Name | Description
cb | (Optional) callback function. Can be omitted when using Promise.

##### `Callback`

  1. ```ts
  2. export interface Callback<T> {
  3.     (err?: Error, result?: T): void
  4. }
  5. ```

#### `getSwatches(cb?: Callback): Promise`
Alias of getPalette.

Vibrant.Builder

Helper class for change configurations and create a Vibrant instance. Methods of a Builder instance can be chained like:

  1. ```ts
  2. Vibrant.from(src)
  3.   .quality(1)
  4.   .clearFilters()
  5.   // ...
  6.   .getPalette()
  7.   .then((palette) => {})
  8. ```

#### `constructor(src: ImageSource, opts: Partial)`
Arguments are the same as Vibrant.constructor.

quality(q: number): Builder

Sets opts.quality to q. Returns this Builder instance.

maxColorCount(n: number): Builder

Sets opts.colorCount to n. Returns this Builder instance.

maxDimension(d: number): Builder

Sets opts.maxDimension to d. Returns this Builder instance.

addFilter(f: Filter): Builder

Adds a filter function. Returns this Builder instance.

removeFilter(f: Filter): Builder

Removes a filter function. Returns this Builder instance.

clearFilters(): Builder

Clear all filters. Returns this Builder instance.

useImageClass(imageClass: ImageClass): Builder

Specifies which Image implementation class to use. Returns this Builder instance.

useQuantizer(quantizer: Quantizer): Builder

Specifies which Quantizer implementation class to use. Returns this Builder instance.

useGenerator(generator: Generator): Builder

Sets opts.generator to generator. Returns this Builder instance.

build(): Vibrant

Builds and returns a Vibrant instance as configured.

#### `getPalette(cb?: Callback): Promise`
Builds a Vibrant instance as configured and calls its getPalette method.

#### `getSwatches(cb? Callback): Promise`
Alias of getPalette.

Vibrant.Swatch

Represents a color swatch generated from an image's palette.

Vec3


  1. ```ts
  2. export interface Vec3 extends Array<number> {
  3.     0: number,
  4.     1: number,
  5.     2: number
  6. }
  7. ```

constructor(rgb: Vec3, population: number)

Internal use.

Name         | Description
rgb        | [r, g, b]
population | Population of the color in an image

getHsl(): Vec3

getPopulation(): number

getRgb(): Vec3

getHex(): string

getTitleTextColor(): string

Returns an appropriate color to use for any 'title' text which is displayed over this Swatch's color.

getBodyTextColor(): string

Returns an appropriate color to use for any 'body' text which is displayed over this Swatch's color.

Vibrant.Util

Utility methods. Internal usage.

hexToRgb(hex: string): Vec3

rgbToHex(r: number, g: number, b: number): string

hslToRgb(h: number, s: number, l: number): Vec3

rgbToHsl(r: number, g: number, b: number): Vec3

xyzToRgb(x: number, y: number, z: number): Vec3

rgbToXyz(r: number, g: number, b: number): Vec3

xyzToCIELab(x: number, y: number, z: number): Vec3

rgbToCIELab(l: number, a: number, b: number): Vec3

deltaE94(lab1: number, lab2: number): number

Computes CIE delta E 1994 diff between lab1 and lab2. The 2 colors are in CIE-Lab color space. Used in tests to compare 2 colors' perceptual similarity.

rgbDiff(rgb1: Vec3, rgb2: Vec3): number

Compute CIE delta E 1994 diff between rgb1 and rgb2.

hexDiff(hex1: string, hex2: string): number

Compute CIE delta E 1994 diff between hex1 and hex2.

getColorDiffStatus(d: number): string

Gets a string to describe the meaning of the color diff. Used in tests.

Delta E  | Perception                             | Returns
<= 1.0   | Not perceptible by human eyes.         | "Perfect"
1 - 2    | Perceptible through close observation. | "Close"
2 - 10   | Perceptible at a glance.               | "Good"
11 - 49  | Colors are more similar than opposite  | "Similar"
50 - 100 | Colors are exact opposite              | Wrong

NPM Tasks


Task            | Description
build:browser | Build browser target
build:node    | Build node.js target
build         | Build all targets
clean:browser | Clean browser build
clean:node    | Clean node.js build
clean         | Clean all builds
test:browser  | Run browser specs (karma)
test:node     | Run node.js specs (mocha)
test          | Run all specs

Notes

Intentional Deviation From vibrant.js

- node-vibrant takes image path, not the image object as parameter for the obvious reason that node.js environment has no access to HTML DOM object.
- node-vibrant provides asynchronous API since most node.js image processing library is asynchronous. And the original vibrant.js workflow is asynchronous any way (though you will have to handle the image loading yourself, while node-vibrant does it for you).
- node-vibrant uses one single opts object to hold all options for future expansions. And it feels more node.js-like.
- node-vibrant uses method call to initiate image processing instead of constructor so that developers can use it with Promise.

Result Consistency

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

However, due to the very nature of HTML5 canvas element, image rendering is platform/machine-dependent. Thus the resulting swatches in browser environment varies and may not be the same as in node.js nor in another machine. 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) and no filters, the results are rather consistent. Color diffs between browsers are mostly not perceptible by human eyes. Downsampling _will_ cause perceptible inconsistent results across browsers due to differences in canvas implementations.