@behold/sharp-vibrant

中文文档
0.2.1发布于 2023/11/04MITTypeScriptCommonJS近 30 天下载 1729

Extract prominent colors from an image in a node environment using sharp.

原始使用文档
文档目录8 节
  1. Usage
  2. node.js
  3. Contribution Guidelines
  4. References
  5. Vibrant
  6. Vibrant.Swatch
  7. Vibrant.Util
  8. NPM Tasks

sharp-vibrant

Extract prominent colors from an image in nodejs using sharp for image loading/processing.

This is a fork of node-vibrant that reduces dependencies/complexity at the cost of absolutely no browser support. This fork was created for internal use and is not published to any package manager.

Usage

node.js

// ES5
var Vibrant = require('sharp-vibrant')
// ES6
import * as Vibrant from '@livechart/sharp-vibrant'
// TypeScript
import Vibrant = require('@livechart/sharp-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))

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 sharp-vibrant.

Vibrant.from(src: ImageSource): Builder

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

constructor(src: ImageSource, opts: Partial<Options>)

NameDescription
imagePath to image file (support HTTP/HTTPs)
optsOptions (optional)
ImageSource
export type ImageSource = string | Buffer
Options
export interface Options {
    colorCount: number
    quality: number
    maxDimension: number
    filters: Array<Filter>
    ImageClass: ImageClass
    quantizer: Quantizer
    generator?: Generator
}
FieldDefaultDescription
colorCount64amount of colors in initial palette from which the swatches will be generated
quality5Scale down factor used in downsampling stage. 1 means no downsampling. If maxDimension is set, this value will not be used.
maxDimensionundefinedThe max size of the image's longer side used in downsampling stage. This field will override quality.
filters[]An array of filters
ImageClassImage.NodeAn Image implementation class
quantizerVibrant.Quantizer.MMCQA Quantizer implementation class
generatorVibrant.Generator.DefaultAn Generator instance
Resolvable<T>
export type Resolvable<T> = T | Promise<T>
Quantizer
export interface Quantizer {
    (pixels: Pixels, opts: Options): Resolvable<Array<Swatch>>
}
Generator
export interface Generator {
    (swatches: Array<Swatch>, opts?: Object): Resolvable<Palette>
}
Filter

Returns true if the color is to be kept.

export interface Filter {
    (red: number, green: number, blue: number, alpha: number): boolean
}
getPalette(cb?: Callback<PaletteResult>): Promise<PaletteResult>
NameDescription
cb(Optional) callback function. Can be omitted when using Promise.
Callback<T>
export interface Callback<T> {
    (err?: Error, result?: T): void
}
ImageDimensions
interface ImageDimensions {
    readonly width: number
    readonly height: number
}
PaletteResult
interface PaletteResult {
    readonly pixelCount: number
    readonly imageDimensions: ImageDimensions
    readonly palette: Palette
}

Vibrant.Builder

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((paletteResult) => {})

constructor(src: ImageSource, opts: Partial<Options>)

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<PaletteResult>): Promise<PaletteResult>

Builds a Vibrant instance as configured and calls its getPalette method.

Vibrant.Swatch

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

Vec3

export interface Vec3 extends Array<number> {
    0: number,
    1: number,
    2: number
}

constructor(rgb: Vec3, population: number)

Internal use.

NameDescription
rgb[r, g, b]
populationPopulation 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 EPerceptionReturns
<= 1.0Not perceptible by human eyes."Perfect"
1 - 2Perceptible through close observation."Close"
2 - 10Perceptible at a glance."Good"
11 - 49Colors are more similar than opposite"Similar"
50 - 100Colors are exact oppositeWrong

NPM Tasks

TaskDescription
build:nodeBuild node.js target
buildBuild all targets
clean:nodeClean node.js build
cleanClean all builds
test:nodeRun node.js specs (mocha)
testRun all specs

版本与发布记录

@behold/sharp-vibrant 共发布 1 个版本,首次发布于 2023/11/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.1latest2023/11/043 年前

关于 @behold/sharp-vibrant 的常见问题

@behold/sharp-vibrant 是什么?

Extract prominent colors from an image in a node environment using sharp.

如何安装 @behold/sharp-vibrant?

在项目目录运行 npm install @behold/sharp-vibrant,或使用 pnpm add / yarn add / bun add。

@behold/sharp-vibrant 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@behold/sharp-vibrant 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@behold/sharp-vibrant 可以商用吗?

@behold/sharp-vibrant 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@behold/sharp-vibrant 的下载量有多少?

根据 npm 官方下载统计,@behold/sharp-vibrant 在 2026/10/08 – 2026/11/06 这 30 天内被下载 1,729 次,日均约 58 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@behold/sharp-vibrant 还在维护吗?

最新版本 0.2.1 发布于 2023/11/04(3 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/11/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。