@thi.ng/pixel-analysis 0.3.4 → 0.4.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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2025-07-10T14:20:23Z
3
+ - **Last updated**: 2025-07-15T09:30:47Z
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.
@@ -11,6 +11,20 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
11
11
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
12
12
  and/or version bumps of transitive dependencies.
13
13
 
14
+ ### [0.4.1](https://github.com/thi-ng/umbrella/tree/@thi.ng/pixel-analysis@0.4.1) (2025-07-15)
15
+
16
+ #### ♻️ Refactoring
17
+
18
+ - extract `derivedColorsResults()` ([c8fb4ed](https://github.com/thi-ng/umbrella/commit/c8fb4ed))
19
+
20
+ ## [0.4.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/pixel-analysis@0.4.0) (2025-07-14)
21
+
22
+ #### 🚀 Features
23
+
24
+ - add color channel precision ([beb2c21](https://github.com/thi-ng/umbrella/commit/beb2c21))
25
+ - add `AnalysisOpts.prec`
26
+ - update `analyzeColors()`
27
+
14
28
  ## [0.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/pixel-analysis@0.3.0) (2025-06-24)
15
29
 
16
30
  #### 🚀 Features
@@ -8,4 +8,11 @@ import type { AnalysisOpts, AnalyzedImage } from "./api.js";
8
8
  * @param opts
9
9
  */
10
10
  export declare const analyzeColors: (img: FloatBuffer | IntBuffer, opts?: Partial<AnalysisOpts>) => AnalyzedImage;
11
+ /**
12
+ * Computes a number of metrics (partial {@link AnalyzedImage}) derived from
13
+ * given raw SRGB colors. Helper function for {@link analyzeColors}.
14
+ *
15
+ * @param colors
16
+ */
17
+ export declare const derivedColorsResults: (colors: number[][]) => Pick<AnalyzedImage, "css" | "srgb" | "hsv" | "oklch" | "hueRange" | "satRange" | "chromaRange" | "lumaRange" | "contrast" | "colorContrast">;
11
18
  //# sourceMappingURL=analyze-colors.d.ts.map
package/analyze-colors.js CHANGED
@@ -1,4 +1,4 @@
1
- import { contrast } from "@thi.ng/color/contrast";
1
+ import { contrast as contrastWCAG } from "@thi.ng/color/contrast";
2
2
  import { css } from "@thi.ng/color/css/css";
3
3
  import { hsv } from "@thi.ng/color/hsv/hsv";
4
4
  import { luminanceSrgb } from "@thi.ng/color/luminance-rgb";
@@ -21,6 +21,7 @@ import { pluck } from "@thi.ng/transducers/pluck";
21
21
  import { reduce } from "@thi.ng/transducers/reduce";
22
22
  import { transduce } from "@thi.ng/transducers/transduce";
23
23
  import { dot } from "@thi.ng/vectors/dot";
24
+ import { roundN } from "@thi.ng/vectors/roundn";
24
25
  import { warmIntensityHsv } from "./hues.js";
25
26
  const analyzeColors = (img, opts) => {
26
27
  let $img = img.format !== FLOAT_RGBA ? img.as(FLOAT_RGBA) : img;
@@ -34,32 +35,51 @@ const analyzeColors = (img, opts) => {
34
35
  const imgGray = $img.as(FLOAT_GRAY);
35
36
  const imgHsv = $img.as(FLOAT_HSVA);
36
37
  const dominantColors = opts?.dominantFn ?? dominantColorsKmeans;
37
- const colors = dominantColors($img, opts?.numColors ?? 4).sort(
38
- compareByKey("area", compareNumDesc)
39
- );
38
+ const prec = opts?.prec ?? 1e-3;
39
+ const colors = dominantColors($img, opts?.numColors ?? 4).sort(compareByKey("area", compareNumDesc)).map((x) => {
40
+ roundN(null, x.color, prec);
41
+ return x;
42
+ });
40
43
  const colorAreas = colors.map((x) => x.area);
41
- const dominantLuma = colors.map((x) => luminanceSrgb(x.color));
42
- const dominantSrgb = colors.map((x) => srgb(x.color));
43
- const dominantHsv = dominantSrgb.map((x) => hsv(x));
44
- const dominantOklch = dominantSrgb.map((x) => oklch(x));
45
- const dominantCss = dominantSrgb.map((x) => css(x));
46
- const hueRange = transduce(pluck(0), minMax(), dominantHsv);
47
- const satRange = transduce(pluck(1), minMax(), dominantHsv);
48
- const chromaRange = transduce(pluck(1), minMax(), dominantOklch);
49
- const lumaRange = reduce(minMax(), dominantLuma);
44
+ const derived = derivedColorsResults(colors.map((x) => x.color));
50
45
  const lumaRangeImg = reduce(minMax(), imgGray.data);
51
- const weightedLuma = dot(dominantLuma, colorAreas);
46
+ const weightedLuma = dot(derived.lumaRange, colorAreas);
52
47
  const weightedChroma = dot(
53
- dominantOklch.map((x) => x[1]),
48
+ derived.oklch.map((x) => x[1]),
54
49
  colorAreas
55
50
  );
56
51
  const weightedSat = dot(
57
- dominantHsv.map((x) => x[1]),
52
+ derived.hsv.map((x) => x[1]),
58
53
  colorAreas
59
54
  );
55
+ return {
56
+ img: $img,
57
+ imgGray,
58
+ imgHsv,
59
+ ...derived,
60
+ area: colorAreas,
61
+ warmth: warmIntensityHsv(imgHsv, opts?.minSat),
62
+ contrastImg: lumaRangeImg[1] - lumaRangeImg[0],
63
+ lumaRangeImg,
64
+ weightedSat,
65
+ weightedLuma,
66
+ weightedChroma
67
+ };
68
+ };
69
+ const derivedColorsResults = (colors) => {
70
+ const dominantLuma = colors.map((x) => luminanceSrgb(x));
71
+ const dominantSrgb = colors.map((x) => srgb(x));
72
+ const dominantHsv = dominantSrgb.map((x) => hsv(x));
73
+ const dominantOklch = dominantSrgb.map((x) => oklch(x));
74
+ const dominantCss = dominantSrgb.map((x) => css(x));
75
+ const hueRange = transduce(pluck(0), minMax(), dominantHsv);
76
+ const satRange = transduce(pluck(1), minMax(), dominantHsv);
77
+ const chromaRange = transduce(pluck(1), minMax(), dominantOklch);
78
+ const lumaRange = reduce(minMax(), dominantLuma);
79
+ const contrast = lumaRange[1] - lumaRange[0];
60
80
  const colorContrast = fit(
61
81
  transduce(
62
- map((pair) => contrast(...pair)),
82
+ map((pair) => contrastWCAG(...pair)),
63
83
  max(),
64
84
  permutations(dominantSrgb, dominantSrgb)
65
85
  ),
@@ -69,28 +89,19 @@ const analyzeColors = (img, opts) => {
69
89
  1
70
90
  );
71
91
  return {
72
- img: $img,
73
- imgGray,
74
- imgHsv,
75
92
  css: dominantCss,
76
93
  srgb: dominantSrgb,
77
94
  hsv: dominantHsv,
78
95
  oklch: dominantOklch,
79
- area: colorAreas,
80
96
  hueRange,
81
97
  satRange,
82
98
  chromaRange,
83
99
  lumaRange,
84
- lumaRangeImg,
85
- warmth: warmIntensityHsv(imgHsv, opts?.minSat),
86
- contrast: lumaRange[1] - lumaRange[0],
87
- contrastImg: lumaRangeImg[1] - lumaRangeImg[0],
88
- colorContrast,
89
- weightedSat,
90
- weightedLuma,
91
- weightedChroma
100
+ contrast,
101
+ colorContrast
92
102
  };
93
103
  };
94
104
  export {
95
- analyzeColors
105
+ analyzeColors,
106
+ derivedColorsResults
96
107
  };
package/api.d.ts CHANGED
@@ -20,6 +20,12 @@ export interface AnalysisOpts {
20
20
  * Min. saturation to consider for computing {@link warmIntensity}.
21
21
  */
22
22
  minSat: number;
23
+ /**
24
+ * Channel precision for dominant colors.
25
+ *
26
+ * @defaultValue 0.001
27
+ */
28
+ prec: number;
23
29
  }
24
30
  export interface AnalyzedImage {
25
31
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/pixel-analysis",
3
- "version": "0.3.4",
3
+ "version": "0.4.1",
4
4
  "description": "Image color & feature analysis utilities",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -40,13 +40,13 @@
40
40
  },
41
41
  "dependencies": {
42
42
  "@thi.ng/api": "^8.11.30",
43
- "@thi.ng/color": "^5.7.46",
43
+ "@thi.ng/color": "^5.7.47",
44
44
  "@thi.ng/compare": "^2.4.22",
45
45
  "@thi.ng/math": "^5.11.30",
46
46
  "@thi.ng/pixel": "^7.5.2",
47
47
  "@thi.ng/pixel-convolve": "^1.1.2",
48
48
  "@thi.ng/pixel-dominant-colors": "^2.0.2",
49
- "@thi.ng/transducers": "^9.6.0",
49
+ "@thi.ng/transducers": "^9.6.1",
50
50
  "@thi.ng/vectors": "^8.3.3"
51
51
  },
52
52
  "devDependencies": {
@@ -107,5 +107,5 @@
107
107
  "status": "beta",
108
108
  "year": 2024
109
109
  },
110
- "gitHead": "63a8b97642b9c481bb623e8d53e6250749a2cbfb\n"
110
+ "gitHead": "9ec4a5584ed6e59f2b74f4ca9726daef7e766974\n"
111
111
  }