@thi.ng/pixel-dominant-colors 1.1.1 → 1.1.2

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2024-07-22T13:15:57Z
3
+ - **Last updated**: 2024-07-25T11:43:52Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,12 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ### [1.1.2](https://github.com/thi-ng/umbrella/tree/@thi.ng/pixel-dominant-colors@1.1.2) (2024-07-25)
13
+
14
+ #### ♻️ Refactoring
15
+
16
+ - add/extract `dominantColorsArray()` ([4aea78e](https://github.com/thi-ng/umbrella/commit/4aea78e))
17
+
12
18
  ## [1.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/pixel-dominant-colors@1.1.0) (2024-07-22)
13
19
 
14
20
  #### 🚀 Features
package/README.md CHANGED
@@ -123,7 +123,7 @@ For Node.js REPL:
123
123
  const pdc = await import("@thi.ng/pixel-dominant-colors");
124
124
  ```
125
125
 
126
- Package sizes (brotli'd, pre-treeshake): ESM: 246 bytes
126
+ Package sizes (brotli'd, pre-treeshake): ESM: 223 bytes
127
127
 
128
128
  ## Dependencies
129
129
 
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Fn2 } from "@thi.ng/api";
1
+ import type { Fn2, NumericArray } from "@thi.ng/api";
2
2
  import type { KMeansOpts } from "@thi.ng/k-means";
3
3
  import type { FloatBuffer } from "@thi.ng/pixel/float";
4
4
  /**
@@ -24,6 +24,8 @@ export interface DominantColorOpts extends KMeansOpts {
24
24
  * `area` the normalized cluster size.
25
25
  *
26
26
  * @remarks
27
+ * This function is syntax sugar for {@link dominantColorsArray}.
28
+ *
27
29
  * See thi.ng/k-means for details about clustering implementation & options.
28
30
  *
29
31
  * @param img -
@@ -33,5 +35,19 @@ export interface DominantColorOpts extends KMeansOpts {
33
35
  export declare const dominantColors: (img: FloatBuffer, num: number, opts?: Partial<DominantColorOpts>) => {
34
36
  color: number[];
35
37
  area: number;
38
+ ids: number[];
39
+ }[];
40
+ /**
41
+ * Similar to {@link dominantColors}, but accepting an array of color samples
42
+ * instead of a `FloatBuffer` image.
43
+ *
44
+ * @param num
45
+ * @param samples
46
+ * @param opts
47
+ */
48
+ export declare const dominantColorsArray: (num: number, samples: NumericArray[], opts?: Partial<KMeansOpts>) => {
49
+ color: number[];
50
+ area: number;
51
+ ids: number[];
36
52
  }[];
37
53
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -1,15 +1,20 @@
1
1
  import { kmeans } from "@thi.ng/k-means/kmeans";
2
- const dominantColors = (img, num, opts = {}) => {
3
- const n = img.width * img.height;
4
- const mapped = [];
5
- const filter = opts.filter || (() => true);
6
- for (let i = 0, j = 0, s = img.stride[0]; i < n; i++, j += s) {
7
- const p = img.data.subarray(j, j + s);
8
- if (filter(p, i)) mapped.push(p);
2
+ const dominantColors = (img, num, opts) => {
3
+ const samples = [];
4
+ const filter = opts?.filter || (() => true);
5
+ let i = 0;
6
+ for (let p of img) {
7
+ if (filter(p, i)) samples.push(p);
8
+ i++;
9
9
  }
10
- if (!mapped.length) return [];
11
- return kmeans(Math.min(num, mapped.length), mapped, opts).sort((a, b) => b.items.length - a.items.length).map((c) => ({ color: [...c.centroid], area: c.items.length / n }));
10
+ return samples.length ? dominantColorsArray(num, samples, opts) : [];
12
11
  };
12
+ const dominantColorsArray = (num, samples, opts) => kmeans(Math.min(num, samples.length), samples, opts).sort((a, b) => b.items.length - a.items.length).map((c) => ({
13
+ color: [...c.centroid],
14
+ area: c.items.length / samples.length,
15
+ ids: c.items
16
+ }));
13
17
  export {
14
- dominantColors
18
+ dominantColors,
19
+ dominantColorsArray
15
20
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/pixel-dominant-colors",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "k-means based dominant color extraction from images/pixel buffers",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -37,8 +37,8 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@thi.ng/api": "^8.11.7",
40
- "@thi.ng/k-means": "^0.6.95",
41
- "@thi.ng/pixel": "^7.0.0"
40
+ "@thi.ng/k-means": "^0.7.0",
41
+ "@thi.ng/pixel": "^7.1.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@microsoft/api-extractor": "^7.47.0",
@@ -77,5 +77,5 @@
77
77
  "parent": "@thi.ng/pixel",
78
78
  "year": 2021
79
79
  },
80
- "gitHead": "bd22b0826134b79064169371665b4d6caa9b6066\n"
80
+ "gitHead": "ec052b25dff4db6c4d1748e946c38fb5cf403dbf\n"
81
81
  }