Extract prominent colors from an image. Fixed version.
Quantization is the most time-consuming stage in @akigami/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:
- Use WebWorker build
dist/vibrant.worker.js
ordist/vibrant.worker.min.js
. Or if you are re-bundling with webpack, uselib/bundle.worker.js
as entry - Use WebWorker quantizer:
let v = Vibrant.from(src) .useQuantizer(Vibrant.Quantizer.WebWorker) // Other configurations
- Identical API for both node.js and browser environment
- Support browserify/webpack
- Consistent results (*See Result Consistency)
$ npm install @akigami/vibrant
// ES5
var Vibrant = require('@akigami/vibrant')
// ES6
import * as Vibrant from '@akigami/vibrant'
// TypeScript
import Vibrant from '@akigami/vibrant'
// Using builder
Vibrant.from('path/to/image').getPalette((err, palette) => console.log(palette))
// Promise
Vibrant.from('path/to/image').getPalette()
.then((palette) => console.log(palette))
// Using constructor
let v = new Vibrant('path/to/image', opts)
v.getPalette((err, palette) => console.log(palette))
// Promise
v.getPalette().then((palette) => console.log(palette))
If you installed @akigami/vibrant with npm
, compiled bundles are available under node_modules/@akigami/vibrant/dist
.
<!-- Debug version -->
<script src="/path/to/dist/vibrant.js"></script>
<!-- Uglified version -->
<script src="/path/to/dist/vibrant.min.js"></script>
<script>
// Use `Vibrant` in script
// Vibrant is exported to global. window.Vibrant === Vibrant
Vibrant.from('path/to/image').getPalette(function(err, palette) {});
// Promise
Vibrant.from('path/to/image').getPalette().then(function(palette) {});
// Or
var v = new Vibrant('/path/to/image', opts);
// ... same as in node.js
</script>
- Make changes
- Write test specs if necessary
- Pass tests
- Commit source files only (without compiled files)
Main class of @akigami/vibrant
.
Make a Builder
for an image. Returns a Builder
instance.
Name | Description |
---|---|
image |
Path to image file (support HTTP/HTTPs) |
opts |
Options (optional) |
export type ImageSource = string
| HTMLImageElement // Browser only
| Buffer // Node.js only
export interface Options {
colorCount: number
quality: number
maxDimension: number
filters: Array<Filter>
ImageClass: ImageClass
quantizer: Quantizer
generator?: Generator
}
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 |
export type Resolvable<T> = T | Bluebird<T>
export interface Quantizer {
(pixels: Pixels, opts: Options): Resolvable<Array<Swatch>>
}
export interface Generator {
(swatches: Array<Swatch>, opts?: Object): Resolvable<Palette>
}
Returns true
if the color is to be kept.
export interface Filter {
(red: number, green: number, blue: number, alpha: number): boolean
}
Name | Description |
---|---|
cb |
(Optional) callback function. Can be omitted when using Promise . |
export interface Callback<T> {
(err?: Error, result?: T): void
}
Alias of getPalette
.
Helper class for change configurations and create a Vibrant
instance. Methods of a Builder
instance can be chained like:
Vibrant.from(src)
.quality(1)
.clearFilters()
// ...
.getPalette()
.then((palette) => {})
Arguments are the same as Vibrant.constructor
.
Sets opts.quality
to q
. Returns this Builder
instance.
Sets opts.colorCount
to n
. Returns this Builder
instance.
Sets opts.maxDimension
to d
. Returns this Builder
instance.
Adds a filter function. Returns this Builder
instance.
Removes a filter function. Returns this Builder
instance.
Clear all filters. Returns this Builder
instance.
Specifies which Image
implementation class to use. Returns this Builder
instance.
Specifies which Quantizer
implementation class to use. Returns this Builder
instance.
Sets opts.generator
to generator
. Returns this Builder
instance.
Builds and returns a Vibrant
instance as configured.
Builds a Vibrant
instance as configured and calls its getPalette
method.
Alias of getPalette
.
Represents a color swatch generated from an image's palette.
export interface Vec3 extends Array<number> {
0: number,
1: number,
2: number
}
Internal use.
Name | Description |
---|---|
rgb |
[r, g, b] |
population |
Population of the color in an image |
Returns an appropriate color to use for any 'title' text which is displayed over this Swatch
's color.
Returns an appropriate color to use for any 'body' text which is displayed over this Swatch
's color.
Utility methods. Internal usage.
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.
Compute CIE delta E 1994 diff between rgb1
and rgb2
.
Compute CIE delta E 1994 diff between hex1
and hex2
.
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 |
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 |
@akigami/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.@akigami/vibrant
provides asynchronous API since most node.js image processing library is asynchronous. And the originalvibrant.js
workflow is asynchronous any way (though you will have to handle the image loading yourself, while@akigami/vibrant
does it for you).@akigami/vibrant
uses one singleopts
object to hold all options for future expansions. And it feels more node.js-like.@akigami/vibrant
uses method call to initiate image processing instead of constructor so that developers can use it withPromise
.
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.