@amplib/color 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/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 2024-2026 Another Machine
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,69 @@
1
+ # @amplib/color
2
+
3
+ Color analysis utilities.
4
+
5
+ ```ts
6
+ import { toPerceptual, fromPerceptual } from "@amplib/color";
7
+
8
+ toPerceptual(210); // 256.22 — display blue is much further round than it looks
9
+ fromPerceptual(180); // 172.88 — the display hue that reads as halfway to cyan
10
+ ```
11
+
12
+ Divide a hue wheel into six equal slices in HSV and the slices do not look
13
+ equal. Divide it in oklch and they do. That is the entire purpose of this
14
+ package: a bijection between the hue you write down and the hue people see.
15
+
16
+ ```ts
17
+ // Six hues that actually look evenly spaced.
18
+ const spaced = Array.from({ length: 6 }, (_, i) => fromPerceptual(i * 60));
19
+ // 328.0, 33.8, 68.9, 172.9, 198.9, 274.6 — not 0, 60, 120, 180, 240, 300
20
+ ```
21
+
22
+ Note where that list starts. Perceptual 0° is display **328°**, not 0° — the
23
+ angle vision reads as the origin of the wheel sits in magenta, some way before
24
+ display red. The two wheels do not share a zero, so there is no offset you
25
+ could apply instead of the mapping.
26
+
27
+ ## Modules
28
+
29
+ | Module | Description |
30
+ | ---------------------- | ---------------------------------------------------- |
31
+ | `toPerceptual` | Display (HSV) hue → perceptual (oklch) hue |
32
+ | `fromPerceptual` | Perceptual (oklch) hue → display (HSV) hue |
33
+ | `worstRoundTripError` | Measured disagreement between the two directions |
34
+
35
+ ## Design
36
+
37
+ **The wheel is severely non-linear, and not in the direction most descriptions
38
+ suggest.** Measured as perceptual degrees per display degree:
39
+
40
+ | red | orange | yellow | green | cyan | blue | deep blue | magenta |
41
+ | ----- | ------ | ------ | ----- | ----- | ----- | --------- | ------- |
42
+ | 0.21× | 1.76× | 1.29× | 0.05× | 2.23× | 0.14× | 0.01× | 0.94× |
43
+
44
+ Green and deep blue barely move — a wide sweep of display hue is almost one
45
+ perceptual color — while cyan and orange stretch. Over two hundredfold between
46
+ the extremes.
47
+
48
+ **The round-trip is measured, not assumed.** The forward table samples oklch
49
+ once per display degree; the inverse searches it backwards at twenty times that
50
+ resolution, because the deep-blue zone squeezes roughly 16 display degrees into
51
+ half a perceptual degree and would otherwise collapse to a single bin. The two
52
+ are built by different methods and only approximately invert each other, so
53
+ `worstRoundTripError()` measures the disagreement and `npm test` fails the
54
+ build above 1.5°.
55
+
56
+ Raw oklch hue also steps *backwards* by a fraction of a degree around display
57
+ 231–240°, which would make the mapping non-invertible exactly there. Every
58
+ backward run is replaced with a straight interpolation across it.
59
+
60
+ **The error check is exported, not run on import.** A library that asserts at
61
+ load time charges every consumer for the check whether they want it or not, and
62
+ still cannot fail a build when it matters.
63
+
64
+ **Hue only, saturation and lightness untouched.** Full-saturation, full-value
65
+ HSV is the sampling line, so this answers "which angle" and says nothing about
66
+ how light or vivid the result is. Anything needing the other two axes wants a
67
+ full color-space conversion, not this.
68
+
69
+ Matrix constants from Björn Ottosson's oklab reference implementation (2020).
@@ -0,0 +1,36 @@
1
+ /**
2
+ * A bijective mapping between display hue and perceptual hue.
3
+ *
4
+ * HSV hue is what a camera reports and what people use to name a color on a
5
+ * screen. Oklch hue — atan2(b, a) of oklab — is the angle human vision reads
6
+ * as evenly spaced. Both run 0–360°, but the mapping between them is severely
7
+ * non-linear, and not in the direction most descriptions of it suggest.
8
+ * Measured as perceptual degrees per display degree:
9
+ *
10
+ * red 0.21× orange 1.76× yellow 1.29× green 0.05×
11
+ * cyan 2.23× blue 0.14× deep blue 0.01× magenta 0.94×
12
+ *
13
+ * Green and deep blue barely move at all — a wide sweep of display hue is
14
+ * almost one perceptual color — while cyan and orange stretch. The spread
15
+ * between the extremes is over two hundredfold.
16
+ *
17
+ * That is the whole reason this file exists. Divide a hue wheel into equal
18
+ * slices in HSV and the slices do not look equal; divide it in oklch and they
19
+ * do.
20
+ */
21
+ /** Display (HSV) hue → perceptual (oklch) hue. */
22
+ declare function toPerceptual(hue: number): number;
23
+ /** Perceptual (oklch) hue → display (HSV) hue. */
24
+ declare function fromPerceptual(hue: number): number;
25
+ /**
26
+ * Worst round-trip error in degrees, sampled every `step` degrees.
27
+ *
28
+ * The two tables are built by different methods and only approximately invert
29
+ * each other, so this is the number that says whether they still agree. It is
30
+ * exported rather than run on import: a library that asserts at load time
31
+ * costs every consumer the check whether or not they care, and cannot fail the
32
+ * build when it matters. See `npm test`.
33
+ */
34
+ declare function worstRoundTripError(step?: number): number;
35
+
36
+ export { fromPerceptual, toPerceptual, worstRoundTripError };
package/dist/index.js ADDED
@@ -0,0 +1,143 @@
1
+ // src/huePerception.ts
2
+ function linearize(channel) {
3
+ return channel <= 0.04045 ? channel / 12.92 : Math.pow((channel + 0.055) / 1.055, 2.4);
4
+ }
5
+ function hsvToLinearRgb(hue) {
6
+ const x = 1 - Math.abs(hue / 60 % 2 - 1);
7
+ let r, g, b;
8
+ if (hue < 60) {
9
+ r = 1;
10
+ g = x;
11
+ b = 0;
12
+ } else if (hue < 120) {
13
+ r = x;
14
+ g = 1;
15
+ b = 0;
16
+ } else if (hue < 180) {
17
+ r = 0;
18
+ g = 1;
19
+ b = x;
20
+ } else if (hue < 240) {
21
+ r = 0;
22
+ g = x;
23
+ b = 1;
24
+ } else if (hue < 300) {
25
+ r = x;
26
+ g = 0;
27
+ b = 1;
28
+ } else {
29
+ r = 1;
30
+ g = 0;
31
+ b = x;
32
+ }
33
+ return [linearize(r), linearize(g), linearize(b)];
34
+ }
35
+ function linearRgbToOklchHue(r, g, b) {
36
+ const l = 0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b;
37
+ const m = 0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b;
38
+ const s = 0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b;
39
+ const lRoot = Math.cbrt(l);
40
+ const mRoot = Math.cbrt(m);
41
+ const sRoot = Math.cbrt(s);
42
+ const a = 1.9779984951 * lRoot - 2.428592205 * mRoot + 0.4505937099 * sRoot;
43
+ const bAxis = 0.0259040371 * lRoot + 0.7827717662 * mRoot - 0.808675766 * sRoot;
44
+ let hue = Math.atan2(bAxis, a) * (180 / Math.PI);
45
+ if (hue < 0) hue += 360;
46
+ return hue;
47
+ }
48
+ var forward = new Float64Array(360);
49
+ for (let hue = 0; hue < 360; hue++) {
50
+ const [r, g, b] = hsvToLinearRgb(hue);
51
+ forward[hue] = linearRgbToOklchHue(r, g, b);
52
+ }
53
+ for (let i = 0; i < 360; i++) {
54
+ let delta = forward[(i + 1) % 360] - forward[i];
55
+ if (delta < 0) delta += 360;
56
+ if (delta > 180) {
57
+ const start = forward[i];
58
+ let j = i + 1;
59
+ while (j < i + 360) {
60
+ let ahead = forward[j % 360] - start;
61
+ if (ahead < 0) ahead += 360;
62
+ if (ahead > 0 && ahead <= 180) break;
63
+ j++;
64
+ }
65
+ let span = forward[j % 360] - start;
66
+ if (span < 0) span += 360;
67
+ const steps = j - i;
68
+ for (let k = 1; k < steps; k++) {
69
+ forward[(i + k) % 360] = (start + span * k / steps) % 360;
70
+ }
71
+ i = j - 1;
72
+ }
73
+ }
74
+ var INVERSE_RESOLUTION = 7200;
75
+ var inverse = new Float64Array(INVERSE_RESOLUTION);
76
+ for (let index = 0; index < INVERSE_RESOLUTION; index++) {
77
+ const degrees = index * (360 / INVERSE_RESOLUTION);
78
+ let found = false;
79
+ for (let hue = 0; hue < 360; hue++) {
80
+ const start = forward[hue];
81
+ const end = forward[(hue + 1) % 360];
82
+ let delta = end - start;
83
+ if (delta < 0) delta += 360;
84
+ if (delta < 1e-9 || delta > 180) continue;
85
+ let offset = degrees - start;
86
+ if (offset < 0) offset += 360;
87
+ if (offset < delta) {
88
+ inverse[index] = hue + offset / delta;
89
+ found = true;
90
+ break;
91
+ }
92
+ }
93
+ if (!found) {
94
+ let best = 0;
95
+ let bestDistance = 360;
96
+ for (let hue = 0; hue < 360; hue++) {
97
+ let distance = Math.abs(forward[hue] - degrees);
98
+ if (distance > 180) distance = 360 - distance;
99
+ if (distance < bestDistance) {
100
+ bestDistance = distance;
101
+ best = hue;
102
+ }
103
+ }
104
+ inverse[index] = best;
105
+ }
106
+ }
107
+ function lerpAngle(from, to, t) {
108
+ let delta = to - from;
109
+ if (delta > 180) delta -= 360;
110
+ if (delta < -180) delta += 360;
111
+ return ((from + t * delta) % 360 + 360) % 360;
112
+ }
113
+ function toPerceptual(hue) {
114
+ const wrapped = (hue % 360 + 360) % 360;
115
+ const index = Math.floor(wrapped) % 360;
116
+ return lerpAngle(forward[index], forward[(index + 1) % 360], wrapped - index);
117
+ }
118
+ function fromPerceptual(hue) {
119
+ const wrapped = (hue % 360 + 360) % 360;
120
+ const scaled = wrapped * (INVERSE_RESOLUTION / 360);
121
+ const index = Math.floor(scaled) % INVERSE_RESOLUTION;
122
+ return lerpAngle(
123
+ inverse[index],
124
+ inverse[(index + 1) % INVERSE_RESOLUTION],
125
+ scaled - index
126
+ );
127
+ }
128
+ function worstRoundTripError(step = 0.25) {
129
+ let worst = 0;
130
+ for (let hue = 0; hue < 360; hue += step) {
131
+ const returned = fromPerceptual(toPerceptual(hue));
132
+ let error = Math.abs(returned - hue);
133
+ if (error > 180) error = 360 - error;
134
+ if (error > worst) worst = error;
135
+ }
136
+ return worst;
137
+ }
138
+ export {
139
+ fromPerceptual,
140
+ toPerceptual,
141
+ worstRoundTripError
142
+ };
143
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/huePerception.ts"],"sourcesContent":["/**\n * A bijective mapping between display hue and perceptual hue.\n *\n * HSV hue is what a camera reports and what people use to name a color on a\n * screen. Oklch hue — atan2(b, a) of oklab — is the angle human vision reads\n * as evenly spaced. Both run 0–360°, but the mapping between them is severely\n * non-linear, and not in the direction most descriptions of it suggest.\n * Measured as perceptual degrees per display degree:\n *\n * red 0.21× orange 1.76× yellow 1.29× green 0.05×\n * cyan 2.23× blue 0.14× deep blue 0.01× magenta 0.94×\n *\n * Green and deep blue barely move at all — a wide sweep of display hue is\n * almost one perceptual color — while cyan and orange stretch. The spread\n * between the extremes is over two hundredfold.\n *\n * That is the whole reason this file exists. Divide a hue wheel into equal\n * slices in HSV and the slices do not look equal; divide it in oklch and they\n * do.\n */\n\n/** sRGB gamma decode. */\nfunction linearize(channel: number): number {\n return channel <= 0.04045\n ? channel / 12.92\n : Math.pow((channel + 0.055) / 1.055, 2.4);\n}\n\n/** HSV(hue, 1, 1) → linear sRGB. */\nfunction hsvToLinearRgb(hue: number): [number, number, number] {\n const x = 1 - Math.abs(((hue / 60) % 2) - 1);\n let r: number, g: number, b: number;\n if (hue < 60) {\n r = 1;\n g = x;\n b = 0;\n } else if (hue < 120) {\n r = x;\n g = 1;\n b = 0;\n } else if (hue < 180) {\n r = 0;\n g = 1;\n b = x;\n } else if (hue < 240) {\n r = 0;\n g = x;\n b = 1;\n } else if (hue < 300) {\n r = x;\n g = 0;\n b = 1;\n } else {\n r = 1;\n g = 0;\n b = x;\n }\n return [linearize(r), linearize(g), linearize(b)];\n}\n\n/**\n * Linear sRGB → oklch hue in degrees.\n * Matrix constants from Björn Ottosson's reference implementation (2020).\n */\nfunction linearRgbToOklchHue(r: number, g: number, b: number): number {\n const l = 0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b;\n const m = 0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b;\n const s = 0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b;\n const lRoot = Math.cbrt(l);\n const mRoot = Math.cbrt(m);\n const sRoot = Math.cbrt(s);\n const a = 1.9779984951 * lRoot - 2.428592205 * mRoot + 0.4505937099 * sRoot;\n const bAxis =\n 0.0259040371 * lRoot + 0.7827717662 * mRoot - 0.808675766 * sRoot;\n let hue = Math.atan2(bAxis, a) * (180 / Math.PI);\n if (hue < 0) hue += 360;\n return hue;\n}\n\n/** Forward table: display hue → perceptual hue, one entry per degree. */\nconst forward = new Float64Array(360);\nfor (let hue = 0; hue < 360; hue++) {\n const [r, g, b] = hsvToLinearRgb(hue);\n forward[hue] = linearRgbToOklchHue(r, g, b);\n}\n\n// Raw oklch hue is not quite monotonic in display hue: around display 231–240°\n// it steps backwards by a fraction of a degree. Left alone that makes the\n// mapping non-invertible right there, so every backward run is replaced with a\n// straight interpolation across it.\nfor (let i = 0; i < 360; i++) {\n let delta = forward[(i + 1) % 360] - forward[i];\n if (delta < 0) delta += 360;\n if (delta > 180) {\n const start = forward[i];\n let j = i + 1;\n while (j < i + 360) {\n let ahead = forward[j % 360] - start;\n if (ahead < 0) ahead += 360;\n if (ahead > 0 && ahead <= 180) break;\n j++;\n }\n let span = forward[j % 360] - start;\n if (span < 0) span += 360;\n const steps = j - i;\n for (let k = 1; k < steps; k++) {\n forward[(i + k) % 360] = (start + (span * k) / steps) % 360;\n }\n i = j - 1;\n }\n}\n\n/**\n * Inverse table: perceptual hue → display hue.\n *\n * 7200 entries rather than 360 because the deep-blue zone squeezes roughly 16\n * display degrees into half a perceptual degree. At one entry per degree that\n * whole region would collapse to a single bin; at 0.05° per bin the worst\n * round-trip error is about 0.8°.\n */\nconst INVERSE_RESOLUTION = 7200;\nconst inverse = new Float64Array(INVERSE_RESOLUTION);\nfor (let index = 0; index < INVERSE_RESOLUTION; index++) {\n const degrees = index * (360 / INVERSE_RESOLUTION);\n let found = false;\n for (let hue = 0; hue < 360; hue++) {\n const start = forward[hue];\n const end = forward[(hue + 1) % 360];\n let delta = end - start;\n if (delta < 0) delta += 360;\n if (delta < 1e-9 || delta > 180) continue;\n let offset = degrees - start;\n if (offset < 0) offset += 360;\n if (offset < delta) {\n inverse[index] = hue + offset / delta;\n found = true;\n break;\n }\n }\n if (!found) {\n let best = 0;\n let bestDistance = 360;\n for (let hue = 0; hue < 360; hue++) {\n let distance = Math.abs(forward[hue] - degrees);\n if (distance > 180) distance = 360 - distance;\n if (distance < bestDistance) {\n bestDistance = distance;\n best = hue;\n }\n }\n inverse[index] = best;\n }\n}\n\n/** Interpolate between two angles along the shorter arc. */\nfunction lerpAngle(from: number, to: number, t: number): number {\n let delta = to - from;\n if (delta > 180) delta -= 360;\n if (delta < -180) delta += 360;\n return (((from + t * delta) % 360) + 360) % 360;\n}\n\n/** Display (HSV) hue → perceptual (oklch) hue. */\nexport function toPerceptual(hue: number): number {\n const wrapped = ((hue % 360) + 360) % 360;\n const index = Math.floor(wrapped) % 360;\n return lerpAngle(forward[index], forward[(index + 1) % 360], wrapped - index);\n}\n\n/** Perceptual (oklch) hue → display (HSV) hue. */\nexport function fromPerceptual(hue: number): number {\n const wrapped = ((hue % 360) + 360) % 360;\n const scaled = wrapped * (INVERSE_RESOLUTION / 360);\n const index = Math.floor(scaled) % INVERSE_RESOLUTION;\n return lerpAngle(\n inverse[index],\n inverse[(index + 1) % INVERSE_RESOLUTION],\n scaled - index\n );\n}\n\n/**\n * Worst round-trip error in degrees, sampled every `step` degrees.\n *\n * The two tables are built by different methods and only approximately invert\n * each other, so this is the number that says whether they still agree. It is\n * exported rather than run on import: a library that asserts at load time\n * costs every consumer the check whether or not they care, and cannot fail the\n * build when it matters. See `npm test`.\n */\nexport function worstRoundTripError(step = 0.25): number {\n let worst = 0;\n for (let hue = 0; hue < 360; hue += step) {\n const returned = fromPerceptual(toPerceptual(hue));\n let error = Math.abs(returned - hue);\n if (error > 180) error = 360 - error;\n if (error > worst) worst = error;\n }\n return worst;\n}\n"],"mappings":";AAsBA,SAAS,UAAU,SAAyB;AAC1C,SAAO,WAAW,UACd,UAAU,QACV,KAAK,KAAK,UAAU,SAAS,OAAO,GAAG;AAC7C;AAGA,SAAS,eAAe,KAAuC;AAC7D,QAAM,IAAI,IAAI,KAAK,IAAM,MAAM,KAAM,IAAK,CAAC;AAC3C,MAAI,GAAW,GAAW;AAC1B,MAAI,MAAM,IAAI;AACZ,QAAI;AACJ,QAAI;AACJ,QAAI;AAAA,EACN,WAAW,MAAM,KAAK;AACpB,QAAI;AACJ,QAAI;AACJ,QAAI;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Q,KAAK,IAAI;AAAA,EACnB;AACF;AAGA,SAAS,UAAU,MAAc,IAAY,GAAmB;AAC9D,MAAI,QAAQ,KAAK;AACjB,MAAI,QAAQ,IAAK,UAAS;AAC1B,MAAI,QAAQ,KAAM,UAAS;AAC3B,WAAU,OAAO,IAAI,SAAS,MAAO,OAAO;AAC9C;AAGO,SAAS,aAAa,KAAqB;AAChD,QAAM,WAAY,MAAM,MAAO,OAAO;AACtC,QAAM,QAAQ,KAAK,MAAM,OAAO,IAAI;AACpC,SAAO,UAAU,QAAQ,KAAK,GAAG,SAAS,QAAQ,KAAK,GAAG,GAAG,UAAU,KAAK;AAC9E;AAGO,SAAS,eAAe,KAAqB;AAClD,QAAM,WAAY,MAAM,MAAO,OAAO;AACtC,QAAM,SAAS,WAAW,qBAAqB;AAC/C,QAAM,QAAQ,KAAK,MAAM,MAAM,IAAI;AACnC,SAAO;AAAA,IACL,QAAQ,KAAK;AAAA,IACb,SAAS,QAAQ,KAAK,kBAAkB;AAAA,IACxC,SAAS;AAAA,EACX;AACF;AAWO,SAAS,oBAAoB,OAAO,MAAc;AACvD,MAAI,QAAQ;AACZ,WAAS,MAAM,GAAG,MAAM,KAAK,OAAO,MAAM;AACxC,UAAM,WAAW,eAAe,aAAa,GAAG,CAAC;AACjD,QAAI,QAAQ,KAAK,IAAI,WAAW,GAAG;AACnC,QAAI,QAAQ,IAAK,SAAQ,MAAM;AAC/B,QAAI,QAAQ,MAAO,SAAQ;AAAA,EAC7B;AACA,SAAO;AACT;","names":[]}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@amplib/color",
3
+ "description": "Color analysis utilities",
4
+ "version": "0.1.0",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "homepage": "https://amplib.app/color",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/another-machine/public-library.git",
17
+ "directory": "packages/amplib-color"
18
+ },
19
+ "tsup": {
20
+ "entry": [
21
+ "src/index.ts"
22
+ ],
23
+ "clean": true,
24
+ "dts": true,
25
+ "format": "esm",
26
+ "minify": false,
27
+ "sourcemap": true,
28
+ "splitting": false
29
+ },
30
+ "scripts": {
31
+ "build": "tsup",
32
+ "watch": "tsup --watch",
33
+ "test": "tsx test/run.ts",
34
+ "prepack": "npm run build"
35
+ },
36
+ "author": "Another Machine",
37
+ "license": "Apache-2.0",
38
+ "type": "module",
39
+ "devDependencies": {
40
+ "tsx": "^4.19.2"
41
+ }
42
+ }