@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
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/* eslint-disable no-param-reassign */
|
|
4
|
+
const color_1 = require("../color");
|
|
5
|
+
const util_1 = require("../util");
|
|
6
|
+
const DefaultOpts = {
|
|
7
|
+
targetDarkLuma: 0.26,
|
|
8
|
+
maxDarkLuma: 0.45,
|
|
9
|
+
minLightLuma: 0.55,
|
|
10
|
+
targetLightLuma: 0.74,
|
|
11
|
+
minNormalLuma: 0.3,
|
|
12
|
+
targetNormalLuma: 0.5,
|
|
13
|
+
maxNormalLuma: 0.7,
|
|
14
|
+
targetMutesSaturation: 0.3,
|
|
15
|
+
maxMutesSaturation: 0.4,
|
|
16
|
+
targetVibrantSaturation: 1.0,
|
|
17
|
+
minVibrantSaturation: 0.35,
|
|
18
|
+
weightSaturation: 3,
|
|
19
|
+
weightLuma: 6.5,
|
|
20
|
+
weightPopulation: 0.5,
|
|
21
|
+
};
|
|
22
|
+
function findMaxPopulation(swatches) {
|
|
23
|
+
let p = 0;
|
|
24
|
+
swatches.forEach((s) => { p = Math.max(p, s.population); });
|
|
25
|
+
return p;
|
|
26
|
+
}
|
|
27
|
+
function isAlreadySelected(palette, s) {
|
|
28
|
+
return palette.Vibrant === s
|
|
29
|
+
|| palette.DarkVibrant === s
|
|
30
|
+
|| palette.LightVibrant === s
|
|
31
|
+
|| palette.Muted === s
|
|
32
|
+
|| palette.DarkMuted === s
|
|
33
|
+
|| palette.LightMuted === s;
|
|
34
|
+
}
|
|
35
|
+
function createComparisonValue(saturation, targetSaturation, luma, targetLuma, population, maxPopulation, opts) {
|
|
36
|
+
function weightedMean(...values) {
|
|
37
|
+
let sum = 0;
|
|
38
|
+
let weightSum = 0;
|
|
39
|
+
for (let i = 0; i < values.length; i += 2) {
|
|
40
|
+
const value = values[i];
|
|
41
|
+
const weight = values[i + 1];
|
|
42
|
+
sum += value * weight;
|
|
43
|
+
weightSum += weight;
|
|
44
|
+
}
|
|
45
|
+
return sum / weightSum;
|
|
46
|
+
}
|
|
47
|
+
function invertDiff(value, targetValue) {
|
|
48
|
+
return 1 - Math.abs(value - targetValue);
|
|
49
|
+
}
|
|
50
|
+
return weightedMean(invertDiff(saturation, targetSaturation), opts.weightSaturation, invertDiff(luma, targetLuma), opts.weightLuma, population / maxPopulation, opts.weightPopulation);
|
|
51
|
+
}
|
|
52
|
+
function findColorVariation(palette, swatches, maxPopulation, targetLuma, minLuma, maxLuma, targetSaturation, minSaturation, maxSaturation, opts) {
|
|
53
|
+
let max = null;
|
|
54
|
+
let maxValue = 0;
|
|
55
|
+
swatches.forEach((swatch) => {
|
|
56
|
+
const [, s, l] = swatch.hsl;
|
|
57
|
+
if (s >= minSaturation && s <= maxSaturation
|
|
58
|
+
&& l >= minLuma && l <= maxLuma
|
|
59
|
+
&& !isAlreadySelected(palette, swatch)) {
|
|
60
|
+
const value = createComparisonValue(s, targetSaturation, l, targetLuma, swatch.population, maxPopulation, opts);
|
|
61
|
+
if (max === null || value > maxValue) {
|
|
62
|
+
max = swatch;
|
|
63
|
+
maxValue = value;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
return max;
|
|
68
|
+
}
|
|
69
|
+
function generateVariationColors(swatches, maxPopulation, opts) {
|
|
70
|
+
const palette = {};
|
|
71
|
+
// mVibrantSwatch = findColor(TARGET_NORMAL_LUMA, MIN_NORMAL_LUMA, MAX_NORMAL_LUMA,
|
|
72
|
+
// TARGET_VIBRANT_SATURATION, MIN_VIBRANT_SATURATION, 1f);
|
|
73
|
+
palette.Vibrant = findColorVariation(palette, swatches, maxPopulation, opts.targetNormalLuma, opts.minNormalLuma, opts.maxNormalLuma, opts.targetVibrantSaturation, opts.minVibrantSaturation, 1, opts);
|
|
74
|
+
// mLightVibrantSwatch = findColor(TARGET_LIGHT_LUMA, MIN_LIGHT_LUMA, 1f,
|
|
75
|
+
// TARGET_VIBRANT_SATURATION, MIN_VIBRANT_SATURATION, 1f);
|
|
76
|
+
palette.LightVibrant = findColorVariation(palette, swatches, maxPopulation, opts.targetLightLuma, opts.minLightLuma, 1, opts.targetVibrantSaturation, opts.minVibrantSaturation, 1, opts);
|
|
77
|
+
// mDarkVibrantSwatch = findColor(TARGET_DARK_LUMA, 0f, MAX_DARK_LUMA,
|
|
78
|
+
// TARGET_VIBRANT_SATURATION, MIN_VIBRANT_SATURATION, 1f);
|
|
79
|
+
palette.DarkVibrant = findColorVariation(palette, swatches, maxPopulation, opts.targetDarkLuma, 0, opts.maxDarkLuma, opts.targetVibrantSaturation, opts.minVibrantSaturation, 1, opts);
|
|
80
|
+
// mMutedSwatch = findColor(TARGET_NORMAL_LUMA, MIN_NORMAL_LUMA, MAX_NORMAL_LUMA,
|
|
81
|
+
// TARGET_MUTED_SATURATION, 0f, MAX_MUTED_SATURATION);
|
|
82
|
+
palette.Muted = findColorVariation(palette, swatches, maxPopulation, opts.targetNormalLuma, opts.minNormalLuma, opts.maxNormalLuma, opts.targetMutesSaturation, 0, opts.maxMutesSaturation, opts);
|
|
83
|
+
// mLightMutedColor = findColor(TARGET_LIGHT_LUMA, MIN_LIGHT_LUMA, 1f,
|
|
84
|
+
// TARGET_MUTED_SATURATION, 0f, MAX_MUTED_SATURATION);
|
|
85
|
+
palette.LightMuted = findColorVariation(palette, swatches, maxPopulation, opts.targetLightLuma, opts.minLightLuma, 1, opts.targetMutesSaturation, 0, opts.maxMutesSaturation, opts);
|
|
86
|
+
// mDarkMutedSwatch = findColor(TARGET_DARK_LUMA, 0f, MAX_DARK_LUMA,
|
|
87
|
+
// TARGET_MUTED_SATURATION, 0f, MAX_MUTED_SATURATION);
|
|
88
|
+
palette.DarkMuted = findColorVariation(palette, swatches, maxPopulation, opts.targetDarkLuma, 0, opts.maxDarkLuma, opts.targetMutesSaturation, 0, opts.maxMutesSaturation, opts);
|
|
89
|
+
return palette;
|
|
90
|
+
}
|
|
91
|
+
function withLuminance(hsl, l) {
|
|
92
|
+
const [h, s] = hsl;
|
|
93
|
+
return [h, s, l];
|
|
94
|
+
}
|
|
95
|
+
function generateEmptySwatches(palette, maxPopulation, opts) {
|
|
96
|
+
if (palette.Vibrant === null && palette.DarkVibrant === null && palette.LightVibrant === null) {
|
|
97
|
+
if (palette.DarkVibrant === null && palette.DarkMuted !== null) {
|
|
98
|
+
const [h, s, l] = withLuminance(palette.DarkMuted.hsl, opts.targetDarkLuma);
|
|
99
|
+
palette.DarkVibrant = new color_1.Swatch((0, util_1.hslToRgb)(h, s, l), 0);
|
|
100
|
+
}
|
|
101
|
+
if (palette.LightVibrant === null && palette.LightMuted !== null) {
|
|
102
|
+
const [h, s, l] = withLuminance(palette.LightMuted.hsl, opts.targetDarkLuma);
|
|
103
|
+
palette.DarkVibrant = new color_1.Swatch((0, util_1.hslToRgb)(h, s, l), 0);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (palette.Vibrant === null && palette.DarkVibrant !== null) {
|
|
107
|
+
const [h, s, l] = withLuminance(palette.DarkVibrant.hsl, opts.targetNormalLuma);
|
|
108
|
+
palette.Vibrant = new color_1.Swatch((0, util_1.hslToRgb)(h, s, l), 0);
|
|
109
|
+
}
|
|
110
|
+
else if (palette.Vibrant === null && palette.LightVibrant !== null) {
|
|
111
|
+
const [h, s, l] = withLuminance(palette.LightVibrant.hsl, opts.targetNormalLuma);
|
|
112
|
+
palette.Vibrant = new color_1.Swatch((0, util_1.hslToRgb)(h, s, l), 0);
|
|
113
|
+
}
|
|
114
|
+
if (palette.DarkVibrant === null && palette.Vibrant !== null) {
|
|
115
|
+
const [h, s, l] = withLuminance(palette.Vibrant.hsl, opts.targetDarkLuma);
|
|
116
|
+
palette.DarkVibrant = new color_1.Swatch((0, util_1.hslToRgb)(h, s, l), 0);
|
|
117
|
+
}
|
|
118
|
+
if (palette.LightVibrant === null && palette.Vibrant !== null) {
|
|
119
|
+
const [h, s, l] = withLuminance(palette.Vibrant.hsl, opts.targetLightLuma);
|
|
120
|
+
palette.LightVibrant = new color_1.Swatch((0, util_1.hslToRgb)(h, s, l), 0);
|
|
121
|
+
}
|
|
122
|
+
if (palette.Muted === null && palette.Vibrant !== null) {
|
|
123
|
+
const [h, s, l] = withLuminance(palette.Vibrant.hsl, opts.targetMutesSaturation);
|
|
124
|
+
palette.Muted = new color_1.Swatch((0, util_1.hslToRgb)(h, s, l), 0);
|
|
125
|
+
}
|
|
126
|
+
if (palette.DarkMuted === null && palette.DarkVibrant !== null) {
|
|
127
|
+
const [h, s, l] = withLuminance(palette.DarkVibrant.hsl, opts.targetMutesSaturation);
|
|
128
|
+
palette.DarkMuted = new color_1.Swatch((0, util_1.hslToRgb)(h, s, l), 0);
|
|
129
|
+
}
|
|
130
|
+
if (palette.LightMuted === null && palette.LightVibrant !== null) {
|
|
131
|
+
const [h, s, l] = withLuminance(palette.LightVibrant.hsl, opts.targetMutesSaturation);
|
|
132
|
+
palette.LightMuted = new color_1.Swatch((0, util_1.hslToRgb)(h, s, l), 0);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const DefaultGenerator = (swatches, opts) => {
|
|
136
|
+
const optsWithDefaults = Object.assign(Object.assign({}, DefaultOpts), opts);
|
|
137
|
+
const maxPopulation = findMaxPopulation(swatches);
|
|
138
|
+
const palette = generateVariationColors(swatches, maxPopulation, optsWithDefaults);
|
|
139
|
+
generateEmptySwatches(palette, maxPopulation, optsWithDefaults);
|
|
140
|
+
return palette;
|
|
141
|
+
};
|
|
142
|
+
exports.default = DefaultGenerator;
|
|
143
|
+
//# sourceMappingURL=default.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default.js","sourceRoot":"","sources":["../../../src/generator/default.ts"],"names":[],"mappings":";;AAAA,sCAAsC;AACtC,oCAAiD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
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Default = void 0;
|
|
7
|
+
const default_1 = __importDefault(require("./default"));
|
|
8
|
+
exports.Default = default_1.default;
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/generator/index.ts"],"names":[],"mappings":";;;;;;AAAA,wDAAgC;AAGZ,kBAHb,iBAAO,CAGa"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Filter, Image, ImageData, ImageSource, ComputedOptions } from '../typing';
|
|
2
|
+
declare abstract class ImageBase implements Image {
|
|
3
|
+
abstract load(image: ImageSource, opts: ComputedOptions): Promise<ImageBase>;
|
|
4
|
+
abstract readonly pixelCount: number;
|
|
5
|
+
abstract readonly imageData: ImageData;
|
|
6
|
+
abstract readonly width: number;
|
|
7
|
+
abstract readonly height: number;
|
|
8
|
+
abstract cleanup(): void;
|
|
9
|
+
applyFilter(filter: Filter): Promise<ImageData>;
|
|
10
|
+
}
|
|
11
|
+
export default ImageBase;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class ImageBase {
|
|
4
|
+
applyFilter(filter) {
|
|
5
|
+
const { imageData } = this;
|
|
6
|
+
if (typeof filter === 'function') {
|
|
7
|
+
const pixels = imageData.data;
|
|
8
|
+
const n = pixels.length / 4;
|
|
9
|
+
let offset;
|
|
10
|
+
let r;
|
|
11
|
+
let g;
|
|
12
|
+
let b;
|
|
13
|
+
let a;
|
|
14
|
+
for (let i = 0; i < n; i += 1) {
|
|
15
|
+
offset = i * 4;
|
|
16
|
+
r = pixels[offset + 0];
|
|
17
|
+
g = pixels[offset + 1];
|
|
18
|
+
b = pixels[offset + 2];
|
|
19
|
+
a = pixels[offset + 3];
|
|
20
|
+
// Mark ignored color
|
|
21
|
+
if (!filter(r, g, b, a))
|
|
22
|
+
pixels[offset + 3] = 0;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return Promise.resolve(imageData);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.default = ImageBase;
|
|
29
|
+
//# sourceMappingURL=base.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../../src/image/base.ts"],"names":[],"mappings":";;AAIA,MAAe,SAAS;IAatB,WAAW,CAAC,MAAc;QACxB,MAAM,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC;QAE3B,IAAI,OAAO,MAAM,KAAK,UAAU,EAAE,CAAC;YACjC,MAAM,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC;YAC9B,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;YAC5B,IAAI,MAAM,CAAC;YACX,IAAI,CAAC,CAAC;YACN,IAAI,CAAC,CAAC;YACN,IAAI,CAAC,CAAC;YACN,IAAI,CAAC,CAAC;YAEN,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC9B,MAAM,GAAG,CAAC,GAAG,CAAC,CAAC;gBACf,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;gBACvB,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;gBACvB,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;gBACvB,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;gBACvB,qBAAqB;gBACrB,IAAI,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;oBAAE,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;YAClD,CAAC;QACH,CAAC;QAED,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACpC,CAAC;CACF;AAED,kBAAe,SAAS,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ImageData, ImageSource, ComputedOptions } from '../typing';
|
|
2
|
+
import ImageBase from './base';
|
|
3
|
+
export default class SharpImage extends ImageBase {
|
|
4
|
+
#private;
|
|
5
|
+
load(image: ImageSource, opts: ComputedOptions): Promise<ImageBase>;
|
|
6
|
+
get pixelCount(): number;
|
|
7
|
+
get imageData(): ImageData;
|
|
8
|
+
get width(): number;
|
|
9
|
+
get height(): number;
|
|
10
|
+
cleanup(): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
12
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
13
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
14
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
15
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
16
|
+
};
|
|
17
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
18
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
19
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
20
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
21
|
+
};
|
|
22
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
+
};
|
|
25
|
+
var _SharpImage_imageData;
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
const sharp_1 = __importDefault(require("sharp"));
|
|
28
|
+
const base_1 = __importDefault(require("./base"));
|
|
29
|
+
class SharpImage extends base_1.default {
|
|
30
|
+
constructor() {
|
|
31
|
+
super(...arguments);
|
|
32
|
+
_SharpImage_imageData.set(this, void 0);
|
|
33
|
+
}
|
|
34
|
+
load(image, opts) {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
let sharpInstance;
|
|
37
|
+
// Check to see if image is a sharp instance.
|
|
38
|
+
// Because sharp doesn't return a class, there's not much else we can do to verify type.
|
|
39
|
+
if (typeof image === 'object' && 'resize' in image) {
|
|
40
|
+
sharpInstance = image;
|
|
41
|
+
}
|
|
42
|
+
else if (typeof image === 'string' || image instanceof Buffer) {
|
|
43
|
+
sharpInstance = (0, sharp_1.default)(image);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
return Promise.reject(new Error(`Cannot load image of type ${typeof image}`));
|
|
47
|
+
}
|
|
48
|
+
if (opts.maxDimension > 0) {
|
|
49
|
+
sharpInstance = sharpInstance.resize(opts.maxDimension, opts.maxDimension, {
|
|
50
|
+
fit: 'inside',
|
|
51
|
+
withoutEnlargement: true,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
const buffer = yield sharpInstance.ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
55
|
+
__classPrivateFieldSet(this, _SharpImage_imageData, {
|
|
56
|
+
data: buffer.data,
|
|
57
|
+
width: buffer.info.width,
|
|
58
|
+
height: buffer.info.height,
|
|
59
|
+
}, "f");
|
|
60
|
+
return this;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
get pixelCount() {
|
|
64
|
+
return this.imageData.width * this.imageData.height;
|
|
65
|
+
}
|
|
66
|
+
get imageData() {
|
|
67
|
+
return __classPrivateFieldGet(this, _SharpImage_imageData, "f");
|
|
68
|
+
}
|
|
69
|
+
get width() {
|
|
70
|
+
return this.imageData.width;
|
|
71
|
+
}
|
|
72
|
+
get height() {
|
|
73
|
+
return this.imageData.height;
|
|
74
|
+
}
|
|
75
|
+
// eslint-disable-next-line class-methods-use-this
|
|
76
|
+
cleanup() {
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
_SharpImage_imageData = new WeakMap();
|
|
80
|
+
exports.default = SharpImage;
|
|
81
|
+
//# sourceMappingURL=sharp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sharp.js","sourceRoot":"","sources":["../../../src/image/sharp.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA0B;AAG1B,kDAA+B;AAE/B,MAAqB,UAAW,SAAQ,cAAS;IAAjD;;QACE,wCAAuB;IAqDzB,CAAC;IAnDO,IAAI,CAAC,KAAkB,EAAE,IAAqB;;YAClD,IAAI,aAAoB,CAAC;YAEzB,6CAA6C;YAC7C,wFAAwF;YACxF,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,QAAQ,IAAI,KAAK,EAAE,CAAC;gBACnD,aAAa,GAAmB,KAAK,CAAC;YACxC,CAAC;iBAAM,IAAI,OAAO,KAAK,KAAK,QA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ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const vibrant_1 = __importDefault(require("./vibrant"));
|
|
6
|
+
const sharp_1 = __importDefault(require("./image/sharp"));
|
|
7
|
+
vibrant_1.default.DefaultOpts.ImageClass = sharp_1.default;
|
|
8
|
+
module.exports = vibrant_1.default;
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;AAAA,wDAAgC;AAChC,0DAAuC;AAEvC,iBAAO,CAAC,WAAW,CAAC,UAAU,GAAG,eAAU,CAAC;AAE5C,iBAAS,iBAAO,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const mmcq_1 = __importDefault(require("./mmcq"));
|
|
7
|
+
exports.default = mmcq_1.default;
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/quantizer/index.ts"],"names":[],"mappings":";;;;;AAAA,kDAA0B;AAE1B,kBAAe,cAAI,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const color_1 = require("../color");
|
|
7
|
+
const vbox_1 = __importDefault(require("./vbox"));
|
|
8
|
+
const pqueue_1 = __importDefault(require("./pqueue"));
|
|
9
|
+
const fractByPopulations = 0.75;
|
|
10
|
+
function splitBoxes(pq, target) {
|
|
11
|
+
let lastSize = pq.size;
|
|
12
|
+
while (pq.size < target) {
|
|
13
|
+
const vbox = pq.pop();
|
|
14
|
+
if (!vbox || vbox.count < 1) {
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
const [vbox1, vbox2] = vbox.split();
|
|
18
|
+
pq.push(vbox1);
|
|
19
|
+
if (vbox2 && vbox2.count > 0)
|
|
20
|
+
pq.push(vbox2);
|
|
21
|
+
// No more new boxes, converged
|
|
22
|
+
if (pq.size === lastSize) {
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
lastSize = pq.size;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function generateSwatches(pq) {
|
|
31
|
+
const swatches = [];
|
|
32
|
+
while (pq.size) {
|
|
33
|
+
const v = pq.pop();
|
|
34
|
+
const color = v.avg;
|
|
35
|
+
swatches.push(new color_1.Swatch(color, v.count));
|
|
36
|
+
}
|
|
37
|
+
return swatches;
|
|
38
|
+
}
|
|
39
|
+
const MMCQ = (pixels, opts) => {
|
|
40
|
+
if (pixels.length === 0 || opts.colorCount < 2 || opts.colorCount > 256) {
|
|
41
|
+
throw new Error('Wrong MMCQ parameters');
|
|
42
|
+
}
|
|
43
|
+
const vbox = vbox_1.default.build(pixels);
|
|
44
|
+
const pq = new pqueue_1.default((a, b) => a.count - b.count);
|
|
45
|
+
pq.push(vbox);
|
|
46
|
+
// first set of colors, sorted by population
|
|
47
|
+
splitBoxes(pq, fractByPopulations * opts.colorCount);
|
|
48
|
+
// Re-order
|
|
49
|
+
const pq2 = new pqueue_1.default((a, b) => a.count * a.volume - b.count * b.volume);
|
|
50
|
+
pq2.contents = pq.contents;
|
|
51
|
+
// next set - generate the median cuts using the (npix * vol) sorting.
|
|
52
|
+
splitBoxes(pq2, opts.colorCount - pq2.size);
|
|
53
|
+
// calculate the actual colors
|
|
54
|
+
return generateSwatches(pq2);
|
|
55
|
+
};
|
|
56
|
+
exports.default = MMCQ;
|
|
57
|
+
//# sourceMappingURL=mmcq.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mmcq.js","sourceRoot":"","sources":["../../../src/quantizer/mmcq.ts"],"names":[],"mappings":";;;;;AAKA,oCAAkC;AAClC,kDAA0B;AAC1B,sDAA8B;AAE9B,MAAM,kBAAkB,GAAG,IAAI,CAAC;AAEhC,SAAS,UAAU,CAAC,EAAgB,EAAE,MAAc;IAClD,IAAI,QAAQ,GAAG,EAAE,CAAC,IAAI,CAAC;IAEvB,OAAO,EAAE,CAAC,IAAI,GAAG,MAAM,EAAE,CAAC;QACxB,MAAM,IAAI,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC;QAEtB,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC;YAC5B,MAAM;QACR,CAAC;QAED,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;QAEpC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACf,IAAI,KAAK,IAAI,KAAK,CAAC,KAAK,GAAG,CAAC;YAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAE7C,+BAA+B;QAC/B,IAAI,EAAE,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YACzB,MAAM;QACR,CAAC;aAAM,CAAC;YACN,QAAQ,GAAG,EAAE,CAAC,IAAI,CAAC;QACrB,CAAC;IACH,CAAC;AACH,CAAC;AAED,SAAS,gBAAgB,CAAC,EAAgB;IACxC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAE9B,OAAO,EAAE,CAAC,IAAI,EAAE,CAAC;QACf,MAAM,CAAC,GAAG,EAAE,CAAC,G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CAAC,CAAC;AAEF,kBAAe,IAAI,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface PQueueComparator<T> {
|
|
2
|
+
(a: T, b: T): number;
|
|
3
|
+
}
|
|
4
|
+
export default class PQueue<T> {
|
|
5
|
+
#private;
|
|
6
|
+
contents: T[];
|
|
7
|
+
private sort;
|
|
8
|
+
constructor(comparator: PQueueComparator<T>);
|
|
9
|
+
push(item: T): void;
|
|
10
|
+
peek(index?: number): T;
|
|
11
|
+
pop(): T;
|
|
12
|
+
get size(): number;
|
|
13
|
+
map<U>(mapper: (item: T, index: number) => any): U[];
|
|
14
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
8
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
11
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
|
+
};
|
|
13
|
+
var _PQueue_sorted, _PQueue_comparator;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
class PQueue {
|
|
16
|
+
sort() {
|
|
17
|
+
if (!__classPrivateFieldGet(this, _PQueue_sorted, "f")) {
|
|
18
|
+
this.contents.sort(__classPrivateFieldGet(this, _PQueue_comparator, "f"));
|
|
19
|
+
__classPrivateFieldSet(this, _PQueue_sorted, true, "f");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
constructor(comparator) {
|
|
23
|
+
_PQueue_sorted.set(this, void 0);
|
|
24
|
+
_PQueue_comparator.set(this, void 0);
|
|
25
|
+
__classPrivateFieldSet(this, _PQueue_comparator, comparator, "f");
|
|
26
|
+
this.contents = [];
|
|
27
|
+
__classPrivateFieldSet(this, _PQueue_sorted, false, "f");
|
|
28
|
+
}
|
|
29
|
+
push(item) {
|
|
30
|
+
this.contents.push(item);
|
|
31
|
+
__classPrivateFieldSet(this, _PQueue_sorted, false, "f");
|
|
32
|
+
}
|
|
33
|
+
peek(index) {
|
|
34
|
+
this.sort();
|
|
35
|
+
return this.contents[typeof index === 'number' ? index : this.contents.length - 1];
|
|
36
|
+
}
|
|
37
|
+
pop() {
|
|
38
|
+
this.sort();
|
|
39
|
+
return this.contents.pop();
|
|
40
|
+
}
|
|
41
|
+
get size() {
|
|
42
|
+
return this.contents.length;
|
|
43
|
+
}
|
|
44
|
+
map(mapper) {
|
|
45
|
+
this.sort();
|
|
46
|
+
return this.contents.map(mapper);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
_PQueue_sorted = new WeakMap(), _PQueue_comparator = new WeakMap();
|
|
50
|
+
exports.default = PQueue;
|
|
51
|
+
//# sourceMappingURL=pqueue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pqueue.js","sourceRoot":"","sources":["../../../src/quantizer/pqueue.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAIA,MAAqB,MAAM;IAOjB,IAAI;QACV,IAAI,CAAC,uBAAA,IAAI,sBAAQ,EAAE,CAAC;YAClB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,uBAAA,IAAI,0BAAY,CAAC,CAAC;YACrC,uBAAA,IAAI,kBAAW,IAAI,MAAA,CAAC;QACtB,CAAC;IACH,CAAC;IAED,YAAY,UAA+B;QAX3C,iCAAiB;QAEjB,qCAAiC;QAU/B,uBAAA,IAAI,sBAAe,UAAU,MAAA,CAAC;QAC9B,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;QACnB,uBAAA,IAAI,kBAAW,KAAK,MAAA,CAAC;IACvB,CAAC;IAED,IAAI,CAAC,IAAO;QACV,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACzB,uBAAA,IAAI,kBAAW,KAAK,MAAA,CAAC;IACvB,CAAC;IAED,IAAI,CAAC,KAAc;QACjB,IAAI,CAAC,IAAI,EAAE,CAAC;QACZ,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACrF,CAAC;IAED,GAAG;QACD,IAAI,CAAC,IAAI,EAAE,CAAC;QACZ,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAG,CAAC;IAC9B,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;IAC9B,CAAC;IAED,GAAG,CAAI,MAAuC;QAC5C,IAAI,CAAC,IAAI,EAAE,CAAC;QACZ,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACnC,CAAC;CACF;;kBA3CoB,MAAM"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Vec3 } from '../color';
|
|
2
|
+
import type { Pixels } from '../typing';
|
|
3
|
+
export interface Dimension {
|
|
4
|
+
r1: number;
|
|
5
|
+
r2: number;
|
|
6
|
+
g1: number;
|
|
7
|
+
g2: number;
|
|
8
|
+
b1: number;
|
|
9
|
+
b2: number;
|
|
10
|
+
[d: string]: number;
|
|
11
|
+
}
|
|
12
|
+
export default class VBox {
|
|
13
|
+
#private;
|
|
14
|
+
static build(pixels: Pixels): VBox;
|
|
15
|
+
dimension: Dimension;
|
|
16
|
+
hist: Uint32Array;
|
|
17
|
+
constructor(r1: number, r2: number, g1: number, g2: number, b1: number, b2: number, hist: Uint32Array);
|
|
18
|
+
invalidate(): void;
|
|
19
|
+
get volume(): number;
|
|
20
|
+
get count(): number;
|
|
21
|
+
clone(): VBox;
|
|
22
|
+
get avg(): Vec3;
|
|
23
|
+
contains(rgb: Vec3): boolean;
|
|
24
|
+
split(): VBox[];
|
|
25
|
+
private doCut;
|
|
26
|
+
}
|