@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.
Files changed (102) hide show
  1. package/.eslintcache +1 -1
  2. package/.vscode/settings.json +1 -1
  3. package/README.md +22 -9
  4. package/fixtures/sample/images/palettes.json +16 -0
  5. package/fixtures/sample/types.ts +8 -4
  6. package/lib/builder.d.ts +2 -4
  7. package/lib/builder.js +31 -41
  8. package/lib/builder.js.map +1 -1
  9. package/lib/color.d.ts +1 -1
  10. package/lib/color.js +46 -51
  11. package/lib/color.js.map +1 -1
  12. package/lib/filter/default.js +1 -4
  13. package/lib/filter/default.js.map +1 -1
  14. package/lib/filter/index.js +2 -6
  15. package/lib/filter/index.js.map +1 -1
  16. package/lib/generator/default.js +12 -14
  17. package/lib/generator/default.js.map +1 -1
  18. package/lib/generator/index.js +2 -7
  19. package/lib/generator/index.js.map +1 -1
  20. package/lib/image/base.d.ts +2 -0
  21. package/lib/image/base.js +1 -3
  22. package/lib/image/base.js.map +1 -1
  23. package/lib/image/sharp.d.ts +2 -0
  24. package/lib/image/sharp.js +35 -28
  25. package/lib/image/sharp.js.map +1 -1
  26. package/lib/index.d.ts +1 -1
  27. package/lib/index.js +4 -8
  28. package/lib/index.js.map +1 -1
  29. package/lib/quantizer/index.js +2 -7
  30. package/lib/quantizer/index.js.map +1 -1
  31. package/lib/quantizer/mmcq.js +8 -13
  32. package/lib/quantizer/mmcq.js.map +1 -1
  33. package/lib/quantizer/pqueue.js +23 -27
  34. package/lib/quantizer/pqueue.js.map +1 -1
  35. package/lib/quantizer/vbox.js +51 -55
  36. package/lib/quantizer/vbox.js.map +1 -1
  37. package/lib/src/builder.d.ts +17 -0
  38. package/lib/src/builder.js +80 -0
  39. package/lib/src/builder.js.map +1 -0
  40. package/lib/src/color.d.ts +30 -0
  41. package/lib/src/color.js +81 -0
  42. package/lib/src/color.js.map +1 -0
  43. package/lib/src/filter/default.d.ts +1 -0
  44. package/lib/src/filter/default.js +7 -0
  45. package/lib/src/filter/default.js.map +1 -0
  46. package/lib/src/filter/index.d.ts +3 -0
  47. package/lib/src/filter/index.js +24 -0
  48. package/lib/src/filter/index.js.map +1 -0
  49. package/lib/src/generator/default.d.ts +20 -0
  50. package/lib/src/generator/default.js +143 -0
  51. package/lib/src/generator/default.js.map +1 -0
  52. package/lib/src/generator/index.d.ts +3 -0
  53. package/lib/src/generator/index.js +9 -0
  54. package/lib/src/generator/index.js.map +1 -0
  55. package/lib/src/image/base.d.ts +11 -0
  56. package/lib/src/image/base.js +29 -0
  57. package/lib/src/image/base.js.map +1 -0
  58. package/lib/src/image/sharp.d.ts +11 -0
  59. package/lib/src/image/sharp.js +81 -0
  60. package/lib/src/image/sharp.js.map +1 -0
  61. package/lib/src/index.d.ts +2 -0
  62. package/lib/src/index.js +9 -0
  63. package/lib/src/index.js.map +1 -0
  64. package/lib/src/quantizer/index.d.ts +2 -0
  65. package/lib/src/quantizer/index.js +8 -0
  66. package/lib/src/quantizer/index.js.map +1 -0
  67. package/lib/src/quantizer/mmcq.d.ts +3 -0
  68. package/lib/src/quantizer/mmcq.js +57 -0
  69. package/lib/src/quantizer/mmcq.js.map +1 -0
  70. package/lib/src/quantizer/pqueue.d.ts +14 -0
  71. package/lib/src/quantizer/pqueue.js +51 -0
  72. package/lib/src/quantizer/pqueue.js.map +1 -0
  73. package/lib/src/quantizer/vbox.d.ts +26 -0
  74. package/lib/src/quantizer/vbox.js +253 -0
  75. package/lib/src/quantizer/vbox.js.map +1 -0
  76. package/lib/src/typing.d.ts +57 -0
  77. package/lib/src/typing.js +3 -0
  78. package/lib/src/typing.js.map +1 -0
  79. package/lib/src/util.d.ts +31 -0
  80. package/lib/src/util.js +201 -0
  81. package/lib/src/util.js.map +1 -0
  82. package/lib/src/vibrant.d.ts +21 -0
  83. package/lib/src/vibrant.js +117 -0
  84. package/lib/src/vibrant.js.map +1 -0
  85. package/lib/typing.d.ts +18 -10
  86. package/lib/typing.js +1 -2
  87. package/lib/util.js +22 -37
  88. package/lib/util.js.map +1 -1
  89. package/lib/vibrant.d.ts +3 -4
  90. package/lib/vibrant.js +42 -52
  91. package/lib/vibrant.js.map +1 -1
  92. package/package.json +8 -8
  93. package/src/builder.ts +2 -6
  94. package/src/image/base.ts +4 -0
  95. package/src/image/sharp.ts +18 -3
  96. package/src/index.ts +1 -1
  97. package/src/test/common/data.ts +3 -1
  98. package/src/test/common/helper.ts +41 -21
  99. package/src/test/vibrant.spec.ts +4 -2
  100. package/src/typing.ts +19 -7
  101. package/src/vibrant.ts +16 -10
  102. package/tsconfig.json +4 -14
package/src/index.ts CHANGED
@@ -3,4 +3,4 @@ import SharpImage from './image/sharp';
3
3
 
4
4
  Vibrant.DefaultOpts.ImageClass = SharpImage;
5
5
 
6
- export = Vibrant;
6
+ export default Vibrant;
@@ -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.getHex()}`)
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
- const paletteCallback = (references: any, sample: TestSample, done: Mocha.Done) =>
72
- (err?: Error, palette?: Palette) => {
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
- assertPalette(references, palette!)
88
+ assertResult(env, sample, result!)
77
89
 
78
90
  done()
79
91
  }, 0)
80
92
  }
81
93
 
82
- export const testVibrant = (Vibrant: VibrantStatic, sample: TestSample, pathKey: SamplePathKey, env: 'node', builderCallback: (b: Builder) => Builder = null!) => {
83
- return (done: Mocha.Done) => {
84
- let builder = Vibrant.from(sample[pathKey])
85
- .quality(1)
86
-
87
- if (typeof builderCallback === 'function') builder = builderCallback(builder)
88
-
89
- builder.getPalette(paletteCallback(sample.palettes[env], sample, done))
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 = (Vibrant: VibrantStatic, sample: TestSample, pathKey: SamplePathKey, env: 'node', builderCallback: (b: Builder) => Builder = null!) => {
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
  }
@@ -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, 'filePath', 'node'))
16
- it(`${sample.name} (Promise)`, testVibrantAsPromised(Vibrant, sample, 'filePath', 'node'))
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 type Pixels = Uint8ClampedArray | Buffer;
17
- export interface ImageData {
18
- data: Pixels,
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 Image {
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
- palette(): Palette | null {
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
- this.#palette = palette;
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 palette;
81
+ return paletteResult;
80
82
  }, (err) => {
81
83
  image.cleanup();
82
84
  throw err;
83
85
  });
84
- if (cb) result.then((palette) => cb(null!, palette), (err) => cb(err));
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": "commonjs",
14
- "moduleResolution": "node",
13
+ "module": "es2022",
14
+ "moduleResolution": "bundler",
15
15
  "strict": true,
16
+ "rootDir": "./src",
16
17
  "outDir": "lib",
17
- "baseUrl": "./src",
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
  }