@thi.ng/pixel-analysis 0.1.0
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 +18 -0
- package/LICENSE +201 -0
- package/README.md +97 -0
- package/analyze-colors.d.ts +11 -0
- package/analyze-colors.js +95 -0
- package/analyze-features.d.ts +10 -0
- package/analyze-features.js +35 -0
- package/api.d.ts +102 -0
- package/api.js +0 -0
- package/hues.d.ts +43 -0
- package/hues.js +57 -0
- package/index.d.ts +5 -0
- package/index.js +4 -0
- package/package.json +110 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
- **Last updated**: 2025-05-28T12:02:40Z
|
|
4
|
+
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
|
+
|
|
6
|
+
All notable changes to this project will be documented in this file.
|
|
7
|
+
Only versions published since **2022-01-01** are listed here.
|
|
8
|
+
Please consult the Git history for older version information.
|
|
9
|
+
See [Conventional Commits](https://conventionalcommits.org/) for commit guidelines.
|
|
10
|
+
|
|
11
|
+
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
12
|
+
and/or version bumps of transitive dependencies.
|
|
13
|
+
|
|
14
|
+
## [0.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/pixel-analysis@0.1.0) (2025-05-28)
|
|
15
|
+
|
|
16
|
+
#### 🚀 Features
|
|
17
|
+
|
|
18
|
+
- import as new pkg ([587b4da](https://github.com/thi-ng/umbrella/commit/587b4da))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
<!-- This file is generated - DO NOT EDIT! -->
|
|
2
|
+
<!-- Please see: https://github.com/thi-ng/umbrella/blob/develop/CONTRIBUTING.md#changes-to-readme-files -->
|
|
3
|
+
# 
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@thi.ng/pixel-analysis)
|
|
6
|
+

|
|
7
|
+
[](https://mastodon.thi.ng/@toxi)
|
|
8
|
+
|
|
9
|
+
> [!NOTE]
|
|
10
|
+
> This is one of 208 standalone projects, maintained as part
|
|
11
|
+
> of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
|
|
12
|
+
> and anti-framework.
|
|
13
|
+
>
|
|
14
|
+
> 🚀 Please help me to work full-time on these projects by [sponsoring me on
|
|
15
|
+
> GitHub](https://github.com/sponsors/postspectacular). Thank you! ❤️
|
|
16
|
+
|
|
17
|
+
- [About](#about)
|
|
18
|
+
- [Status](#status)
|
|
19
|
+
- [Installation](#installation)
|
|
20
|
+
- [Dependencies](#dependencies)
|
|
21
|
+
- [API](#api)
|
|
22
|
+
- [Authors](#authors)
|
|
23
|
+
- [License](#license)
|
|
24
|
+
|
|
25
|
+
## About
|
|
26
|
+
|
|
27
|
+
Image color & feature analysis utilities. This is a support package for [@thi.ng/pixel](https://github.com/thi-ng/umbrella/tree/develop/packages/pixel).
|
|
28
|
+
|
|
29
|
+
## Status
|
|
30
|
+
|
|
31
|
+
**BETA** - possibly breaking changes forthcoming
|
|
32
|
+
|
|
33
|
+
[Search or submit any issues for this package](https://github.com/thi-ng/umbrella/issues?q=%5Bpixel-analysis%5D+in%3Atitle)
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
yarn add @thi.ng/pixel-analysis
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
ESM import:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import * as pa from "@thi.ng/pixel-analysis";
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Browser ESM import:
|
|
48
|
+
|
|
49
|
+
```html
|
|
50
|
+
<script type="module" src="https://esm.run/@thi.ng/pixel-analysis"></script>
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
[JSDelivr documentation](https://www.jsdelivr.com/)
|
|
54
|
+
|
|
55
|
+
For Node.js REPL:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
const pa = await import("@thi.ng/pixel-analysis");
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 1.29 KB
|
|
62
|
+
|
|
63
|
+
## Dependencies
|
|
64
|
+
|
|
65
|
+
- [@thi.ng/color](https://github.com/thi-ng/umbrella/tree/develop/packages/color)
|
|
66
|
+
- [@thi.ng/compare](https://github.com/thi-ng/umbrella/tree/develop/packages/compare)
|
|
67
|
+
- [@thi.ng/math](https://github.com/thi-ng/umbrella/tree/develop/packages/math)
|
|
68
|
+
- [@thi.ng/pixel](https://github.com/thi-ng/umbrella/tree/develop/packages/pixel)
|
|
69
|
+
- [@thi.ng/pixel-convolve](https://github.com/thi-ng/umbrella/tree/develop/packages/pixel-convolve)
|
|
70
|
+
- [@thi.ng/pixel-dominant-colors](https://github.com/thi-ng/umbrella/tree/develop/packages/pixel-dominant-colors)
|
|
71
|
+
- [@thi.ng/transducers](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers)
|
|
72
|
+
- [@thi.ng/vectors](https://github.com/thi-ng/umbrella/tree/develop/packages/vectors)
|
|
73
|
+
|
|
74
|
+
## API
|
|
75
|
+
|
|
76
|
+
[Generated API docs](https://docs.thi.ng/umbrella/pixel-analysis/)
|
|
77
|
+
|
|
78
|
+
TODO
|
|
79
|
+
|
|
80
|
+
## Authors
|
|
81
|
+
|
|
82
|
+
- [Karsten Schmidt](https://thi.ng)
|
|
83
|
+
|
|
84
|
+
If this project contributes to an academic publication, please cite it as:
|
|
85
|
+
|
|
86
|
+
```bibtex
|
|
87
|
+
@misc{thing-pixel-analysis,
|
|
88
|
+
title = "@thi.ng/pixel-analysis",
|
|
89
|
+
author = "Karsten Schmidt",
|
|
90
|
+
note = "https://thi.ng/pixel-analysis",
|
|
91
|
+
year = 2024
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## License
|
|
96
|
+
|
|
97
|
+
© 2024 - 2025 Karsten Schmidt // Apache License 2.0
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { FloatBuffer } from "@thi.ng/pixel/float";
|
|
2
|
+
import { IntBuffer } from "@thi.ng/pixel/int";
|
|
3
|
+
import type { AnalysisOpts, AnalyzedImage } from "./api.js";
|
|
4
|
+
/**
|
|
5
|
+
* Performs a set of image/color analyses on provided pixel buffer.
|
|
6
|
+
*
|
|
7
|
+
* @param img
|
|
8
|
+
* @param opts
|
|
9
|
+
*/
|
|
10
|
+
export declare const analyzeColors: (img: FloatBuffer | IntBuffer, opts?: Partial<AnalysisOpts>) => AnalyzedImage;
|
|
11
|
+
//# sourceMappingURL=analyze-colors.d.ts.map
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { contrast } from "@thi.ng/color/contrast";
|
|
2
|
+
import { css } from "@thi.ng/color/css/css";
|
|
3
|
+
import { hsv } from "@thi.ng/color/hsv/hsv";
|
|
4
|
+
import { luminanceSrgb } from "@thi.ng/color/luminance-rgb";
|
|
5
|
+
import { oklch } from "@thi.ng/color/oklch/oklch";
|
|
6
|
+
import { srgb } from "@thi.ng/color/srgb/srgb";
|
|
7
|
+
import { compareByKey } from "@thi.ng/compare/keys";
|
|
8
|
+
import { compareNumDesc } from "@thi.ng/compare/numeric";
|
|
9
|
+
import { fit } from "@thi.ng/math/fit";
|
|
10
|
+
import { dominantColors } from "@thi.ng/pixel-dominant-colors";
|
|
11
|
+
import { FloatBuffer } from "@thi.ng/pixel/float";
|
|
12
|
+
import { FLOAT_GRAY } from "@thi.ng/pixel/format/float-gray";
|
|
13
|
+
import { FLOAT_HSVA } from "@thi.ng/pixel/format/float-hsva";
|
|
14
|
+
import { FLOAT_RGBA } from "@thi.ng/pixel/format/float-rgba";
|
|
15
|
+
import { IntBuffer } from "@thi.ng/pixel/int";
|
|
16
|
+
import { map } from "@thi.ng/transducers/map";
|
|
17
|
+
import { max } from "@thi.ng/transducers/max";
|
|
18
|
+
import { minMax } from "@thi.ng/transducers/min-max";
|
|
19
|
+
import { permutations } from "@thi.ng/transducers/permutations";
|
|
20
|
+
import { pluck } from "@thi.ng/transducers/pluck";
|
|
21
|
+
import { reduce } from "@thi.ng/transducers/reduce";
|
|
22
|
+
import { transduce } from "@thi.ng/transducers/transduce";
|
|
23
|
+
import { dot } from "@thi.ng/vectors/dot";
|
|
24
|
+
import { warmIntensityHsv } from "./hues.js";
|
|
25
|
+
const analyzeColors = (img, opts) => {
|
|
26
|
+
let $img = img.format !== FLOAT_RGBA ? img.as(FLOAT_RGBA) : img;
|
|
27
|
+
if (opts?.size) {
|
|
28
|
+
const size = ~~opts.size;
|
|
29
|
+
let w = $img.width;
|
|
30
|
+
let h = $img.height;
|
|
31
|
+
[w, h] = w > h ? [size, ~~Math.max(1, h / w * size)] : [~~Math.max(1, w / h * size), size];
|
|
32
|
+
$img = $img.resize(w, h);
|
|
33
|
+
}
|
|
34
|
+
const imgGray = $img.as(FLOAT_GRAY);
|
|
35
|
+
const imgHsv = $img.as(FLOAT_HSVA);
|
|
36
|
+
const colors = dominantColors($img, opts?.numColors ?? 4).sort(
|
|
37
|
+
compareByKey("area", compareNumDesc)
|
|
38
|
+
);
|
|
39
|
+
const colorAreas = colors.map((x) => x.area);
|
|
40
|
+
const dominantLuma = colors.map((x) => luminanceSrgb(x.color));
|
|
41
|
+
const dominantSrgb = colors.map((x) => srgb(x.color));
|
|
42
|
+
const dominantHsv = dominantSrgb.map((x) => hsv(x));
|
|
43
|
+
const dominantOklch = dominantSrgb.map((x) => oklch(x));
|
|
44
|
+
const dominantCss = dominantSrgb.map((x) => css(x));
|
|
45
|
+
const hueRange = transduce(pluck(0), minMax(), dominantHsv);
|
|
46
|
+
const satRange = transduce(pluck(1), minMax(), dominantHsv);
|
|
47
|
+
const chromaRange = transduce(pluck(1), minMax(), dominantOklch);
|
|
48
|
+
const lumaRange = reduce(minMax(), dominantLuma);
|
|
49
|
+
const lumaRangeImg = reduce(minMax(), imgGray.data);
|
|
50
|
+
const weightedLuma = dot(dominantLuma, colorAreas);
|
|
51
|
+
const weightedChroma = dot(
|
|
52
|
+
dominantOklch.map((x) => x[1]),
|
|
53
|
+
colorAreas
|
|
54
|
+
);
|
|
55
|
+
const weightedSat = dot(
|
|
56
|
+
dominantHsv.map((x) => x[1]),
|
|
57
|
+
colorAreas
|
|
58
|
+
);
|
|
59
|
+
const colorContrast = fit(
|
|
60
|
+
transduce(
|
|
61
|
+
map((pair) => contrast(...pair)),
|
|
62
|
+
max(),
|
|
63
|
+
permutations(dominantSrgb, dominantSrgb)
|
|
64
|
+
),
|
|
65
|
+
1,
|
|
66
|
+
21,
|
|
67
|
+
0,
|
|
68
|
+
1
|
|
69
|
+
);
|
|
70
|
+
return {
|
|
71
|
+
img: $img,
|
|
72
|
+
imgGray,
|
|
73
|
+
imgHsv,
|
|
74
|
+
css: dominantCss,
|
|
75
|
+
srgb: dominantSrgb,
|
|
76
|
+
hsv: dominantHsv,
|
|
77
|
+
oklch: dominantOklch,
|
|
78
|
+
area: colorAreas,
|
|
79
|
+
hueRange,
|
|
80
|
+
satRange,
|
|
81
|
+
chromaRange,
|
|
82
|
+
lumaRange,
|
|
83
|
+
lumaRangeImg,
|
|
84
|
+
warmth: warmIntensityHsv(imgHsv, opts?.minSat),
|
|
85
|
+
contrast: lumaRange[1] - lumaRange[0],
|
|
86
|
+
contrastImg: lumaRangeImg[1] - lumaRangeImg[0],
|
|
87
|
+
colorContrast,
|
|
88
|
+
weightedSat,
|
|
89
|
+
weightedLuma,
|
|
90
|
+
weightedChroma
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
export {
|
|
94
|
+
analyzeColors
|
|
95
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type FloatBuffer } from "@thi.ng/pixel/float";
|
|
2
|
+
import type { IntBuffer } from "@thi.ng/pixel/int";
|
|
3
|
+
export declare const analyzeFeatures: (img: FloatBuffer | IntBuffer) => {
|
|
4
|
+
imgEdge: FloatBuffer;
|
|
5
|
+
imgSobelX: FloatBuffer;
|
|
6
|
+
imgSobelY: FloatBuffer;
|
|
7
|
+
scoreEdge: number;
|
|
8
|
+
scoreSobel: number;
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=analyze-features.d.ts.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {
|
|
2
|
+
convolveImage,
|
|
3
|
+
EDGE5,
|
|
4
|
+
SOBEL_X,
|
|
5
|
+
SOBEL_Y
|
|
6
|
+
} from "@thi.ng/pixel-convolve/convolve";
|
|
7
|
+
import {} from "@thi.ng/pixel/float";
|
|
8
|
+
import { FLOAT_GRAY } from "@thi.ng/pixel/format/float-gray";
|
|
9
|
+
import { FLOAT_GRAY_RANGE } from "@thi.ng/pixel/format/float-gray-range";
|
|
10
|
+
const FMT_EDGE = FLOAT_GRAY_RANGE(-24, 24);
|
|
11
|
+
const FMT_SOBEL = FLOAT_GRAY_RANGE(-4, 4);
|
|
12
|
+
const analyzeFeatures = (img) => {
|
|
13
|
+
const $img = img.format !== FLOAT_GRAY ? img.as(FLOAT_GRAY) : img;
|
|
14
|
+
const { width, height } = $img;
|
|
15
|
+
const numPixels = width * height;
|
|
16
|
+
const imgEdge = convolveImage($img, { kernel: EDGE5 });
|
|
17
|
+
imgEdge.format = FMT_EDGE;
|
|
18
|
+
const imgSobelX = convolveImage($img, { kernel: SOBEL_X });
|
|
19
|
+
imgSobelX.format = FMT_SOBEL;
|
|
20
|
+
const imgSobelY = convolveImage($img, { kernel: SOBEL_Y });
|
|
21
|
+
imgSobelY.format = FMT_SOBEL;
|
|
22
|
+
const magSq = (img2) => img2.data.reduce((acc, x) => acc + x * x);
|
|
23
|
+
const scoreEdge = Math.sqrt(magSq(imgEdge)) / numPixels;
|
|
24
|
+
const scoreSobel = Math.sqrt(magSq(imgSobelX) + magSq(imgSobelY)) / numPixels;
|
|
25
|
+
return {
|
|
26
|
+
imgEdge,
|
|
27
|
+
imgSobelX,
|
|
28
|
+
imgSobelY,
|
|
29
|
+
scoreEdge,
|
|
30
|
+
scoreSobel
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
export {
|
|
34
|
+
analyzeFeatures
|
|
35
|
+
};
|
package/api.d.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { HSV, Oklch, SRGB } from "@thi.ng/color";
|
|
2
|
+
import type { FloatBuffer } from "@thi.ng/pixel";
|
|
3
|
+
export interface AnalysisOpts {
|
|
4
|
+
/**
|
|
5
|
+
* Max. number of dominant colors.
|
|
6
|
+
*/
|
|
7
|
+
numColors: number;
|
|
8
|
+
/**
|
|
9
|
+
* Max. image size (longest side)
|
|
10
|
+
*/
|
|
11
|
+
size: number;
|
|
12
|
+
/**
|
|
13
|
+
* Min. saturation to consider for computing {@link warmIntensity}.
|
|
14
|
+
*/
|
|
15
|
+
minSat: number;
|
|
16
|
+
}
|
|
17
|
+
export interface AnalyzedImage {
|
|
18
|
+
/**
|
|
19
|
+
* Input image, possibly converted to float RGBA & resized.
|
|
20
|
+
*/
|
|
21
|
+
img: FloatBuffer;
|
|
22
|
+
/**
|
|
23
|
+
* Float grayscale version of {@link AnalyzedImage.img}.
|
|
24
|
+
*/
|
|
25
|
+
imgGray: FloatBuffer;
|
|
26
|
+
/**
|
|
27
|
+
* Float HSV version of {@link AnalyzedImage.img}.
|
|
28
|
+
*/
|
|
29
|
+
imgHsv: FloatBuffer;
|
|
30
|
+
/**
|
|
31
|
+
* Dominant colors as CSS (hex) strings
|
|
32
|
+
*/
|
|
33
|
+
css: string[];
|
|
34
|
+
/**
|
|
35
|
+
* Dominant colors as SRGB
|
|
36
|
+
*/
|
|
37
|
+
srgb: SRGB[];
|
|
38
|
+
/**
|
|
39
|
+
* Dominant colors as HSV
|
|
40
|
+
*/
|
|
41
|
+
hsv: HSV[];
|
|
42
|
+
/**
|
|
43
|
+
* Dominant colors as Oklch
|
|
44
|
+
*/
|
|
45
|
+
oklch: Oklch[];
|
|
46
|
+
/**
|
|
47
|
+
* Normalized areas of dominant color clusters
|
|
48
|
+
*/
|
|
49
|
+
area: number[];
|
|
50
|
+
/**
|
|
51
|
+
* Min/max HSV hue range of dominant colors
|
|
52
|
+
*/
|
|
53
|
+
hueRange: [number, number];
|
|
54
|
+
/**
|
|
55
|
+
* Min/max HSV saturation range of dominant colors
|
|
56
|
+
*/
|
|
57
|
+
satRange: [number, number];
|
|
58
|
+
/**
|
|
59
|
+
* Min/max Oklch chroma range of dominant colors
|
|
60
|
+
*/
|
|
61
|
+
chromaRange: [number, number];
|
|
62
|
+
/**
|
|
63
|
+
* Min/max luminance range of dominant colors (obtained from SRGB)
|
|
64
|
+
*/
|
|
65
|
+
lumaRange: [number, number];
|
|
66
|
+
/**
|
|
67
|
+
* Min/max luminance range of entire grayscale image (obtained from SRGB)
|
|
68
|
+
*/
|
|
69
|
+
lumaRangeImg: [number, number];
|
|
70
|
+
/**
|
|
71
|
+
* Normalized warmth, i.e. the area-weighted intensity of "warm" colors in
|
|
72
|
+
* the image (see {@link warmIntensity} and {@link AnalysisOpts.minSat}).
|
|
73
|
+
*/
|
|
74
|
+
warmth: number;
|
|
75
|
+
/**
|
|
76
|
+
* Luminance contrast of dominant colors (i.e. delta of
|
|
77
|
+
* {@link AnalyzedImage.lumaRange}).
|
|
78
|
+
*/
|
|
79
|
+
contrast: number;
|
|
80
|
+
/**
|
|
81
|
+
* Luminance contrast of entire grayscale image (i.e. delta of
|
|
82
|
+
* {@link AnalyzedImage.lumaRangeImg}).
|
|
83
|
+
*/
|
|
84
|
+
contrastImg: number;
|
|
85
|
+
/**
|
|
86
|
+
* Max. normalized WCAG color contrast of dominant colors.
|
|
87
|
+
*/
|
|
88
|
+
colorContrast: number;
|
|
89
|
+
/**
|
|
90
|
+
* Average luminance of dominant colors, weighted by area.
|
|
91
|
+
*/
|
|
92
|
+
weightedLuma: number;
|
|
93
|
+
/**
|
|
94
|
+
* Average HSV saturation of dominant colors, weighted by area.
|
|
95
|
+
*/
|
|
96
|
+
weightedSat: number;
|
|
97
|
+
/**
|
|
98
|
+
* Average Oklch chroma of dominant colors, weighted by area.
|
|
99
|
+
*/
|
|
100
|
+
weightedChroma: number;
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=api.d.ts.map
|
package/api.js
ADDED
|
File without changes
|
package/hues.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { FloatBuffer } from "@thi.ng/pixel/float";
|
|
2
|
+
/**
|
|
3
|
+
* Iterator yielding HSV pixel values/colors matching given hue range and
|
|
4
|
+
* minimum saturation.
|
|
5
|
+
*
|
|
6
|
+
* @param img
|
|
7
|
+
* @param minHue
|
|
8
|
+
* @param maxHue
|
|
9
|
+
* @param minSat
|
|
10
|
+
*/
|
|
11
|
+
export declare function selectHueRangeHsv(img: FloatBuffer, minHue: number, maxHue: number, minSat: number): Generator<Float32Array<ArrayBufferLike>, void, unknown>;
|
|
12
|
+
/**
|
|
13
|
+
* Similar to {@link selectHueRangeHsv}, but only returns a count of HSV pixel
|
|
14
|
+
* values/colors matching given hue range and minimum saturation (all
|
|
15
|
+
* normalized).
|
|
16
|
+
*
|
|
17
|
+
* @param img
|
|
18
|
+
* @param minHue
|
|
19
|
+
* @param maxHue
|
|
20
|
+
* @param minSat
|
|
21
|
+
*/
|
|
22
|
+
export declare const countHueRangeHsv: (img: FloatBuffer, minHue: number, maxHue: number, minSat: number) => number;
|
|
23
|
+
/**
|
|
24
|
+
* Takes a list of hue ranges and computes the area-weighted mean intensity of
|
|
25
|
+
* matching pixels in the given image. Also see {@link warmIntensityHsv}.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* Intensity here means the product of HSV `saturation * brightness`.
|
|
29
|
+
*
|
|
30
|
+
* @param img
|
|
31
|
+
* @param hues
|
|
32
|
+
* @param minSat
|
|
33
|
+
*/
|
|
34
|
+
export declare const hueRangeIntensityHsv: (img: FloatBuffer, hues: [number, number][], minSat?: number) => number;
|
|
35
|
+
/**
|
|
36
|
+
* Syntax sugar for {@link hueRangeIntensityHsv} to compute the area-weighted mean
|
|
37
|
+
* intensity of pixels in the yellow/orange/red hue ranges.
|
|
38
|
+
*
|
|
39
|
+
* @param img
|
|
40
|
+
* @param minSat
|
|
41
|
+
*/
|
|
42
|
+
export declare const warmIntensityHsv: (img: FloatBuffer, minSat?: number) => number;
|
|
43
|
+
//# sourceMappingURL=hues.d.ts.map
|
package/hues.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { FLOAT_HSVA } from "@thi.ng/pixel/format/float-hsva";
|
|
2
|
+
import { map } from "@thi.ng/transducers/map";
|
|
3
|
+
import { mapcat } from "@thi.ng/transducers/mapcat";
|
|
4
|
+
import { mean } from "@thi.ng/transducers/mean";
|
|
5
|
+
import { transduce } from "@thi.ng/transducers/transduce";
|
|
6
|
+
function* selectHueRangeHsv(img, minHue, maxHue, minSat) {
|
|
7
|
+
if (img.format !== FLOAT_HSVA) img = img.as(FLOAT_HSVA);
|
|
8
|
+
const {
|
|
9
|
+
data,
|
|
10
|
+
stride: [stride]
|
|
11
|
+
} = img;
|
|
12
|
+
const pred = minHue <= maxHue ? (i) => data[i] >= minHue && data[i] < maxHue : (i) => data[i] >= minHue || data[i] < maxHue;
|
|
13
|
+
for (let i = 0, n = data.length; i < n; i += stride) {
|
|
14
|
+
if (data[i + 1] >= minSat && pred(i)) yield data.subarray(i, i + 4);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const countHueRangeHsv = (img, minHue, maxHue, minSat) => {
|
|
18
|
+
if (img.format !== FLOAT_HSVA) img = img.as(FLOAT_HSVA);
|
|
19
|
+
const {
|
|
20
|
+
data,
|
|
21
|
+
stride: [stride]
|
|
22
|
+
} = img;
|
|
23
|
+
const pred = minHue <= maxHue ? (i) => data[i] >= minHue && data[i] < maxHue : (i) => data[i] >= minHue || data[i] < maxHue;
|
|
24
|
+
let count = 0;
|
|
25
|
+
for (let i = 0, n = data.length; i < n; i += stride) {
|
|
26
|
+
if (data[i + 1] >= minSat && pred(i)) count++;
|
|
27
|
+
}
|
|
28
|
+
return count;
|
|
29
|
+
};
|
|
30
|
+
const hueRangeIntensityHsv = (img, hues, minSat = 0.2) => {
|
|
31
|
+
if (img.format !== FLOAT_HSVA) img = img.as(FLOAT_HSVA);
|
|
32
|
+
const selected = [
|
|
33
|
+
...mapcat((range) => selectHueRangeHsv(img, ...range, minSat), hues)
|
|
34
|
+
];
|
|
35
|
+
const area = selected.length / (img.data.length / 4);
|
|
36
|
+
const intensity = transduce(
|
|
37
|
+
map((x) => x[1] * x[2]),
|
|
38
|
+
mean(),
|
|
39
|
+
selected
|
|
40
|
+
);
|
|
41
|
+
return intensity * area;
|
|
42
|
+
};
|
|
43
|
+
const warmIntensityHsv = (img, minSat) => hueRangeIntensityHsv(
|
|
44
|
+
img,
|
|
45
|
+
[
|
|
46
|
+
[15 / 360, 35 / 360],
|
|
47
|
+
[35 / 360, 55 / 360],
|
|
48
|
+
[345 / 360, 15 / 360]
|
|
49
|
+
],
|
|
50
|
+
minSat
|
|
51
|
+
);
|
|
52
|
+
export {
|
|
53
|
+
countHueRangeHsv,
|
|
54
|
+
hueRangeIntensityHsv,
|
|
55
|
+
selectHueRangeHsv,
|
|
56
|
+
warmIntensityHsv
|
|
57
|
+
};
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@thi.ng/pixel-analysis",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Image color & feature analysis utilities",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"module": "./index.js",
|
|
7
|
+
"typings": "./index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/thi-ng/umbrella.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://thi.ng/pixel-analysis",
|
|
14
|
+
"funding": [
|
|
15
|
+
{
|
|
16
|
+
"type": "github",
|
|
17
|
+
"url": "https://github.com/sponsors/postspectacular"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"type": "patreon",
|
|
21
|
+
"url": "https://patreon.com/thing_umbrella"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"type": "liberapay",
|
|
25
|
+
"url": "https://liberapay.com/thi.ng"
|
|
26
|
+
}
|
|
27
|
+
],
|
|
28
|
+
"author": "Karsten Schmidt (https://thi.ng)",
|
|
29
|
+
"license": "Apache-2.0",
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
32
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
33
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
34
|
+
"clean": "bun ../../tools/src/clean-package.ts",
|
|
35
|
+
"doc": "typedoc --options ../../typedoc.json --out doc src/index.ts",
|
|
36
|
+
"doc:readme": "bun ../../tools/src/module-stats.ts && bun ../../tools/src/readme.ts",
|
|
37
|
+
"pub": "yarn npm publish --access public",
|
|
38
|
+
"test": "bun test",
|
|
39
|
+
"tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@thi.ng/color": "^5.7.38",
|
|
43
|
+
"@thi.ng/compare": "^2.4.20",
|
|
44
|
+
"@thi.ng/math": "^5.11.28",
|
|
45
|
+
"@thi.ng/pixel": "^7.5.0",
|
|
46
|
+
"@thi.ng/pixel-convolve": "^1.1.0",
|
|
47
|
+
"@thi.ng/pixel-dominant-colors": "^1.1.46",
|
|
48
|
+
"@thi.ng/transducers": "^9.3.1",
|
|
49
|
+
"@thi.ng/vectors": "^8.2.1"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"esbuild": "^0.25.5",
|
|
53
|
+
"typedoc": "^0.28.5",
|
|
54
|
+
"typescript": "^5.8.3"
|
|
55
|
+
},
|
|
56
|
+
"keywords": [
|
|
57
|
+
"analysis",
|
|
58
|
+
"area",
|
|
59
|
+
"color",
|
|
60
|
+
"contrast",
|
|
61
|
+
"dominant",
|
|
62
|
+
"edge",
|
|
63
|
+
"gradient",
|
|
64
|
+
"grayscale",
|
|
65
|
+
"hsv",
|
|
66
|
+
"luminance",
|
|
67
|
+
"oklch",
|
|
68
|
+
"rgb",
|
|
69
|
+
"saturation",
|
|
70
|
+
"temperature",
|
|
71
|
+
"typescript"
|
|
72
|
+
],
|
|
73
|
+
"publishConfig": {
|
|
74
|
+
"access": "public"
|
|
75
|
+
},
|
|
76
|
+
"browser": {
|
|
77
|
+
"process": false,
|
|
78
|
+
"setTimeout": false
|
|
79
|
+
},
|
|
80
|
+
"engines": {
|
|
81
|
+
"node": ">=18"
|
|
82
|
+
},
|
|
83
|
+
"files": [
|
|
84
|
+
"./*.js",
|
|
85
|
+
"./*.d.ts"
|
|
86
|
+
],
|
|
87
|
+
"exports": {
|
|
88
|
+
".": {
|
|
89
|
+
"default": "./index.js"
|
|
90
|
+
},
|
|
91
|
+
"./analyze-colors": {
|
|
92
|
+
"default": "./analyze-colors.js"
|
|
93
|
+
},
|
|
94
|
+
"./analyze-features": {
|
|
95
|
+
"default": "./analyze-features.js"
|
|
96
|
+
},
|
|
97
|
+
"./api": {
|
|
98
|
+
"default": "./api.js"
|
|
99
|
+
},
|
|
100
|
+
"./hues": {
|
|
101
|
+
"default": "./hues.js"
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
"thi.ng": {
|
|
105
|
+
"parent": "@thi.ng/pixel",
|
|
106
|
+
"status": "beta",
|
|
107
|
+
"year": 2024
|
|
108
|
+
},
|
|
109
|
+
"gitHead": "61c3833b7ef7d044621454b5ea4af885d39f065e\n"
|
|
110
|
+
}
|