@livechart/sharp-vibrant 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintcache +1 -1
- package/.vscode/settings.json +1 -1
- package/README.md +22 -9
- package/fixtures/sample/images/palettes.json +16 -0
- package/fixtures/sample/types.ts +8 -4
- package/lib/builder.d.ts +2 -4
- package/lib/builder.js +31 -41
- package/lib/builder.js.map +1 -1
- package/lib/color.d.ts +1 -1
- package/lib/color.js +46 -51
- package/lib/color.js.map +1 -1
- package/lib/filter/default.js +1 -4
- package/lib/filter/default.js.map +1 -1
- package/lib/filter/index.js +2 -6
- package/lib/filter/index.js.map +1 -1
- package/lib/generator/default.js +12 -14
- package/lib/generator/default.js.map +1 -1
- package/lib/generator/index.js +2 -7
- package/lib/generator/index.js.map +1 -1
- package/lib/image/base.d.ts +2 -0
- package/lib/image/base.js +1 -3
- package/lib/image/base.js.map +1 -1
- package/lib/image/sharp.d.ts +2 -0
- package/lib/image/sharp.js +35 -28
- package/lib/image/sharp.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +4 -8
- package/lib/index.js.map +1 -1
- package/lib/quantizer/index.js +2 -7
- package/lib/quantizer/index.js.map +1 -1
- package/lib/quantizer/mmcq.js +8 -13
- package/lib/quantizer/mmcq.js.map +1 -1
- package/lib/quantizer/pqueue.js +23 -27
- package/lib/quantizer/pqueue.js.map +1 -1
- package/lib/quantizer/vbox.js +51 -55
- package/lib/quantizer/vbox.js.map +1 -1
- package/lib/src/builder.d.ts +17 -0
- package/lib/src/builder.js +80 -0
- package/lib/src/builder.js.map +1 -0
- package/lib/src/color.d.ts +30 -0
- package/lib/src/color.js +81 -0
- package/lib/src/color.js.map +1 -0
- package/lib/src/filter/default.d.ts +1 -0
- package/lib/src/filter/default.js +7 -0
- package/lib/src/filter/default.js.map +1 -0
- package/lib/src/filter/index.d.ts +3 -0
- package/lib/src/filter/index.js +24 -0
- package/lib/src/filter/index.js.map +1 -0
- package/lib/src/generator/default.d.ts +20 -0
- package/lib/src/generator/default.js +143 -0
- package/lib/src/generator/default.js.map +1 -0
- package/lib/src/generator/index.d.ts +3 -0
- package/lib/src/generator/index.js +9 -0
- package/lib/src/generator/index.js.map +1 -0
- package/lib/src/image/base.d.ts +11 -0
- package/lib/src/image/base.js +29 -0
- package/lib/src/image/base.js.map +1 -0
- package/lib/src/image/sharp.d.ts +11 -0
- package/lib/src/image/sharp.js +81 -0
- package/lib/src/image/sharp.js.map +1 -0
- package/lib/src/index.d.ts +2 -0
- package/lib/src/index.js +9 -0
- package/lib/src/index.js.map +1 -0
- package/lib/src/quantizer/index.d.ts +2 -0
- package/lib/src/quantizer/index.js +8 -0
- package/lib/src/quantizer/index.js.map +1 -0
- package/lib/src/quantizer/mmcq.d.ts +3 -0
- package/lib/src/quantizer/mmcq.js +57 -0
- package/lib/src/quantizer/mmcq.js.map +1 -0
- package/lib/src/quantizer/pqueue.d.ts +14 -0
- package/lib/src/quantizer/pqueue.js +51 -0
- package/lib/src/quantizer/pqueue.js.map +1 -0
- package/lib/src/quantizer/vbox.d.ts +26 -0
- package/lib/src/quantizer/vbox.js +253 -0
- package/lib/src/quantizer/vbox.js.map +1 -0
- package/lib/src/typing.d.ts +57 -0
- package/lib/src/typing.js +3 -0
- package/lib/src/typing.js.map +1 -0
- package/lib/src/util.d.ts +31 -0
- package/lib/src/util.js +201 -0
- package/lib/src/util.js.map +1 -0
- package/lib/src/vibrant.d.ts +21 -0
- package/lib/src/vibrant.js +117 -0
- package/lib/src/vibrant.js.map +1 -0
- package/lib/typing.d.ts +18 -10
- package/lib/typing.js +1 -2
- package/lib/util.js +22 -37
- package/lib/util.js.map +1 -1
- package/lib/vibrant.d.ts +3 -4
- package/lib/vibrant.js +42 -52
- package/lib/vibrant.js.map +1 -1
- package/package.json +8 -8
- package/src/builder.ts +2 -6
- package/src/image/base.ts +4 -0
- package/src/image/sharp.ts +18 -3
- package/src/index.ts +1 -1
- package/src/test/common/data.ts +3 -1
- package/src/test/common/helper.ts +41 -21
- package/src/test/vibrant.spec.ts +4 -2
- package/src/typing.ts +19 -7
- package/src/vibrant.ts +16 -10
- package/tsconfig.json +4 -14
package/src/index.ts
CHANGED
package/src/test/common/data.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
+
import sharp from 'sharp';
|
|
2
3
|
|
|
3
4
|
import { Sample } from '../../../fixtures/sample/types'
|
|
4
5
|
export const SNAPSHOT: Sample[] = require('../../../fixtures/sample/images/palettes.json')
|
|
@@ -7,9 +8,10 @@ export interface TestSample extends Sample {
|
|
|
7
8
|
relativeUrl: string
|
|
8
9
|
}
|
|
9
10
|
|
|
10
|
-
export type SamplePathKey = Exclude<keyof TestSample, 'palettes'>
|
|
11
|
+
export type SamplePathKey = Exclude<keyof TestSample, 'palettes' | 'dimensions'>
|
|
11
12
|
|
|
12
13
|
export const SAMPLES: TestSample[] = SNAPSHOT.map((s) => Object.assign(s, {
|
|
14
|
+
sharp: sharp(path.join(__dirname, '../../../fixtures/sample/images/', s.name)),
|
|
13
15
|
filePath: path.join(__dirname, '../../../fixtures/sample/images/', s.name),
|
|
14
16
|
relativeUrl: `base/fixtures/sample/images/${s.name}`
|
|
15
17
|
}))
|
|
@@ -1,25 +1,32 @@
|
|
|
1
1
|
/* eslint-env mocha */
|
|
2
2
|
import { expect } from 'chai'
|
|
3
|
-
import { VibrantStatic } from '../../typing'
|
|
3
|
+
import type { PaletteResult, VibrantStatic, ImageDimensions } from '../../typing'
|
|
4
4
|
import Builder from '../../builder'
|
|
5
5
|
import { Palette, Swatch } from '../../color'
|
|
6
6
|
import util = require('../../util')
|
|
7
|
-
import {
|
|
7
|
+
import type {
|
|
8
8
|
TestSample, SamplePathKey
|
|
9
9
|
} from './data'
|
|
10
10
|
|
|
11
11
|
import { table, getBorderCharacters } from 'table'
|
|
12
12
|
|
|
13
|
+
type Env = 'node'
|
|
13
14
|
|
|
14
15
|
const TABLE_OPTS = {
|
|
15
16
|
border: getBorderCharacters('void')
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
|
|
19
19
|
const displayColorDiffTable = (diff: string[][]) => {
|
|
20
20
|
console.log(table(diff, TABLE_OPTS))
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
const assertDimensions = (reference: ImageDimensions, dimensions: ImageDimensions) => {
|
|
24
|
+
expect(dimensions, 'dimensions should be returned').not.to.be.null
|
|
25
|
+
|
|
26
|
+
expect(dimensions.width, `width is not correct`).to.eq(reference.width)
|
|
27
|
+
expect(dimensions.height, `height is not correct`).to.eq(reference.height)
|
|
28
|
+
}
|
|
29
|
+
|
|
23
30
|
const assertPalette = (reference: Palette, palette: Palette) => {
|
|
24
31
|
expect(palette, 'palette should be returned').not.to.be.null
|
|
25
32
|
|
|
@@ -32,7 +39,7 @@ const assertPalette = (reference: Palette, palette: Palette) => {
|
|
|
32
39
|
|
|
33
40
|
if (expected === null) {
|
|
34
41
|
if (actual !== null) {
|
|
35
|
-
console.warn(`WARN: ${name} color was not expected. Got ${actual.
|
|
42
|
+
console.warn(`WARN: ${name} color was not expected. Got ${actual.hex}`)
|
|
36
43
|
}
|
|
37
44
|
} else {
|
|
38
45
|
expect(actual, `${name} color was expected`).not.to.be.null
|
|
@@ -68,37 +75,50 @@ const assertPalette = (reference: Palette, palette: Palette) => {
|
|
|
68
75
|
.to.equal(0)
|
|
69
76
|
}
|
|
70
77
|
|
|
71
|
-
|
|
72
|
-
(
|
|
78
|
+
function assertResult(env: Env, sample: TestSample, result: PaletteResult) {
|
|
79
|
+
assertDimensions(sample.dimensions, result.imageDimensions)
|
|
80
|
+
assertPalette(sample.palettes[env], result.palette)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const paletteCallback = (env: Env, sample: TestSample, done: Mocha.Done) =>
|
|
84
|
+
(err?: Error, result?: PaletteResult) => {
|
|
73
85
|
setTimeout(() => {
|
|
74
86
|
|
|
75
87
|
expect(err, `should not throw error '${err}'`).to.be.null
|
|
76
|
-
|
|
88
|
+
assertResult(env, sample, result!)
|
|
77
89
|
|
|
78
90
|
done()
|
|
79
91
|
}, 0)
|
|
80
92
|
}
|
|
81
93
|
|
|
82
|
-
export const testVibrant = (
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
94
|
+
export const testVibrant = (
|
|
95
|
+
Vibrant: VibrantStatic,
|
|
96
|
+
sample: TestSample,
|
|
97
|
+
pathKey: SamplePathKey,
|
|
98
|
+
env: Env,
|
|
99
|
+
builderCallback: (b: Builder) => Builder = null!
|
|
100
|
+
) => (done: Mocha.Done) => {
|
|
101
|
+
let builder = Vibrant.from(sample[pathKey]).quality(1)
|
|
102
|
+
|
|
103
|
+
if (typeof builderCallback === 'function') {
|
|
104
|
+
builder = builderCallback(builder)
|
|
90
105
|
}
|
|
91
|
-
}
|
|
92
106
|
|
|
107
|
+
builder.getPalette(paletteCallback(env, sample, done))
|
|
108
|
+
}
|
|
93
109
|
|
|
94
|
-
export const testVibrantAsPromised = (
|
|
110
|
+
export const testVibrantAsPromised = (
|
|
111
|
+
Vibrant: VibrantStatic,
|
|
112
|
+
sample: TestSample,
|
|
113
|
+
pathKey: SamplePathKey,
|
|
114
|
+
env: Env,
|
|
115
|
+
builderCallback: (b: Builder) => Builder = null!
|
|
116
|
+
) => {
|
|
95
117
|
return () => {
|
|
96
|
-
let builder = Vibrant.from(sample[pathKey])
|
|
97
|
-
.quality(1)
|
|
118
|
+
let builder = Vibrant.from(sample[pathKey]).quality(1)
|
|
98
119
|
|
|
99
120
|
if (typeof builderCallback === 'function') builder = builderCallback(builder)
|
|
100
121
|
|
|
101
|
-
return builder.getPalette()
|
|
102
|
-
.then(palette => assertPalette(sample.palettes[env], palette))
|
|
122
|
+
return builder.getPalette().then(result => assertResult(env, sample, result))
|
|
103
123
|
}
|
|
104
124
|
}
|
package/src/test/vibrant.spec.ts
CHANGED
|
@@ -12,8 +12,10 @@ import Vibrant = require('../')
|
|
|
12
12
|
describe('Palette Extraction', () => {
|
|
13
13
|
describe('process samples', () =>
|
|
14
14
|
SAMPLES.forEach((sample) => {
|
|
15
|
-
it(`${sample.name} (callback)`, testVibrant(Vibrant, sample, '
|
|
16
|
-
it(`${sample.name} (Promise)`, testVibrantAsPromised(Vibrant, sample, '
|
|
15
|
+
it(`${sample.name} via sharp (callback)`, testVibrant(Vibrant, sample, 'sharp', 'node'))
|
|
16
|
+
it(`${sample.name} via sharp (Promise)`, testVibrantAsPromised(Vibrant, sample, 'sharp', 'node'))
|
|
17
|
+
it(`${sample.name} via file path (callback)`, testVibrant(Vibrant, sample, 'filePath', 'node'))
|
|
18
|
+
it(`${sample.name} via file path (Promise)`, testVibrantAsPromised(Vibrant, sample, 'filePath', 'node'))
|
|
17
19
|
})
|
|
18
20
|
)
|
|
19
21
|
})
|
package/src/typing.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Palette, Swatch } from './color';
|
|
2
2
|
import type Builder from './builder';
|
|
3
|
+
import { type Sharp } from 'sharp';
|
|
3
4
|
|
|
4
5
|
export interface VibrantStatic {
|
|
5
6
|
from(src: ImageSource): Builder
|
|
@@ -11,17 +12,28 @@ export interface Callback<T> {
|
|
|
11
12
|
|
|
12
13
|
export type ImageCallback = Callback<Image>;
|
|
13
14
|
|
|
14
|
-
export type ImageSource = string | Buffer;
|
|
15
|
+
export type ImageSource = string | Buffer | Sharp;
|
|
15
16
|
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
width: number,
|
|
20
|
-
height: number
|
|
17
|
+
export interface ImageDimensions {
|
|
18
|
+
readonly width: number
|
|
19
|
+
readonly height: number
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
export interface
|
|
22
|
+
export interface ImagePixelInfo {
|
|
24
23
|
readonly pixelCount: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type Pixels = Uint8ClampedArray | Buffer;
|
|
27
|
+
export interface ImageData extends ImageDimensions {
|
|
28
|
+
data: Pixels
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface PaletteResult extends ImagePixelInfo {
|
|
32
|
+
readonly imageDimensions: ImageDimensions
|
|
33
|
+
readonly palette: Palette
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface Image extends ImageDimensions, ImagePixelInfo {
|
|
25
37
|
readonly imageData: ImageData
|
|
26
38
|
|
|
27
39
|
load(image: ImageSource, opts: ComputedOptions): Promise<Image>
|
package/src/vibrant.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type {
|
|
|
4
4
|
Options,
|
|
5
5
|
ComputedOptions,
|
|
6
6
|
Callback,
|
|
7
|
+
PaletteResult,
|
|
7
8
|
} from './typing';
|
|
8
9
|
|
|
9
10
|
import { Palette, Swatch } from './color';
|
|
@@ -46,8 +47,6 @@ class Vibrant {
|
|
|
46
47
|
|
|
47
48
|
opts: ComputedOptions;
|
|
48
49
|
|
|
49
|
-
#palette: Palette | null = null;
|
|
50
|
-
|
|
51
50
|
#src: ImageSource;
|
|
52
51
|
|
|
53
52
|
constructor(src: ImageSource, opts?: Partial<Options>) {
|
|
@@ -65,23 +64,30 @@ class Vibrant {
|
|
|
65
64
|
.then((colors) => Promise.resolve(generator!(colors)));
|
|
66
65
|
}
|
|
67
66
|
|
|
68
|
-
|
|
69
|
-
return this.#palette;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
getPalette(cb?: Callback<Palette>): Promise<Palette> {
|
|
67
|
+
getPalette(cb?: Callback<PaletteResult>): Promise<PaletteResult> {
|
|
73
68
|
const image = new this.opts.ImageClass();
|
|
74
69
|
const result = image.load(this.#src, this.opts)
|
|
75
70
|
.then((loadedImage) => this.process(loadedImage))
|
|
76
71
|
.then((palette) => {
|
|
77
|
-
|
|
72
|
+
const paletteResult: PaletteResult = {
|
|
73
|
+
pixelCount: image.pixelCount,
|
|
74
|
+
imageDimensions: {
|
|
75
|
+
width: image.width,
|
|
76
|
+
height: image.height,
|
|
77
|
+
},
|
|
78
|
+
palette,
|
|
79
|
+
};
|
|
78
80
|
image.cleanup();
|
|
79
|
-
return
|
|
81
|
+
return paletteResult;
|
|
80
82
|
}, (err) => {
|
|
81
83
|
image.cleanup();
|
|
82
84
|
throw err;
|
|
83
85
|
});
|
|
84
|
-
|
|
86
|
+
|
|
87
|
+
if (cb) {
|
|
88
|
+
result.then((palette) => cb(null!, palette), (err) => cb(err));
|
|
89
|
+
}
|
|
90
|
+
|
|
85
91
|
return result;
|
|
86
92
|
}
|
|
87
93
|
}
|
package/tsconfig.json
CHANGED
|
@@ -10,12 +10,12 @@
|
|
|
10
10
|
"lib": [
|
|
11
11
|
"es6"
|
|
12
12
|
],
|
|
13
|
-
"module": "
|
|
14
|
-
"moduleResolution": "
|
|
13
|
+
"module": "es2022",
|
|
14
|
+
"moduleResolution": "bundler",
|
|
15
15
|
"strict": true,
|
|
16
|
+
"rootDir": "./src",
|
|
16
17
|
"outDir": "lib",
|
|
17
|
-
"
|
|
18
|
-
"sourceMap": true,
|
|
18
|
+
"sourceMap": true
|
|
19
19
|
},
|
|
20
20
|
"include": [
|
|
21
21
|
"./src"
|
|
@@ -23,15 +23,5 @@
|
|
|
23
23
|
"exclude": [
|
|
24
24
|
"./src/test"
|
|
25
25
|
],
|
|
26
|
-
"import/resolver": {
|
|
27
|
-
"node": {
|
|
28
|
-
"extensions": [
|
|
29
|
-
".js",
|
|
30
|
-
".jsx",
|
|
31
|
-
".ts",
|
|
32
|
-
".tsx"
|
|
33
|
-
]
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
26
|
"compileOnSave": false
|
|
37
27
|
}
|