@electrovir/color 0.0.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-CC0 ADDED
@@ -0,0 +1,121 @@
1
+ CC0 1.0 Universal
2
+
3
+ Creative Commons Legal Code
4
+
5
+ CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
6
+ LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
7
+ ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
8
+ INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
9
+ REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
10
+ PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
11
+ THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
12
+ HEREUNDER.
13
+
14
+ Statement of Purpose
15
+
16
+ The laws of most jurisdictions throughout the world automatically confer
17
+ exclusive Copyright and Related Rights (defined below) upon the creator
18
+ and subsequent owner(s) (each and all, an "owner") of an original work of
19
+ authorship and/or a database (each, a "Work").
20
+
21
+ Certain owners wish to permanently relinquish those rights to a Work for
22
+ the purpose of contributing to a commons of creative, cultural and
23
+ scientific works ("Commons") that the public can reliably and without fear
24
+ of later claims of infringement build upon, modify, incorporate in other
25
+ works, reuse and redistribute as freely as possible in any form whatsoever
26
+ and for any purposes, including without limitation commercial purposes.
27
+ These owners may contribute to the Commons to promote the ideal of a free
28
+ culture and the further production of creative, cultural and scientific
29
+ works, or to gain reputation or greater distribution for their Work in
30
+ part through the use and efforts of others.
31
+
32
+ For these and/or other purposes and motivations, and without any
33
+ expectation of additional consideration or compensation, the person
34
+ associating CC0 with a Work (the "Affirmer"), to the extent that he or she
35
+ is an owner of Copyright and Related Rights in the Work, voluntarily
36
+ elects to apply CC0 to the Work and publicly distribute the Work under its
37
+ terms, with knowledge of his or her Copyright and Related Rights in the
38
+ Work and the meaning and intended legal effect of CC0 on those rights.
39
+
40
+ 1. Copyright and Related Rights. A Work made available under CC0 may be
41
+ protected by copyright and related or neighboring rights ("Copyright and
42
+ Related Rights"). Copyright and Related Rights include, but are not
43
+ limited to, the following:
44
+
45
+ i. the right to reproduce, adapt, distribute, perform, display,
46
+ communicate, and translate a Work;
47
+ ii. moral rights retained by the original author(s) and/or performer(s);
48
+ iii. publicity and privacy rights pertaining to a person's image or
49
+ likeness depicted in a Work;
50
+ iv. rights protecting against unfair competition in regards to a Work,
51
+ subject to the limitations in paragraph 4(a), below;
52
+ v. rights protecting the extraction, dissemination, use and reuse of data
53
+ in a Work;
54
+ vi. database rights (such as those arising under Directive 96/9/EC of the
55
+ European Parliament and of the Council of 11 March 1996 on the legal
56
+ protection of databases, and under any national implementation
57
+ thereof, including any amended or successor version of such
58
+ directive); and
59
+ vii. other similar, equivalent or corresponding rights throughout the
60
+ world based on applicable law or treaty, and any national
61
+ implementations thereof.
62
+
63
+ 2. Waiver. To the greatest extent permitted by, but not in contravention
64
+ of, applicable law, Affirmer hereby overtly, fully, permanently,
65
+ irrevocably and unconditionally waives, abandons, and surrenders all of
66
+ Affirmer's Copyright and Related Rights and associated claims and causes
67
+ of action, whether now known or unknown (including existing as well as
68
+ future claims and causes of action), in the Work (i) in all territories
69
+ worldwide, (ii) for the maximum duration provided by applicable law or
70
+ treaty (including future time extensions), (iii) in any current or future
71
+ medium and for any number of copies, and (iv) for any purpose whatsoever,
72
+ including without limitation commercial, advertising or promotional
73
+ purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
74
+ member of the public at large and to the detriment of Affirmer's heirs and
75
+ successors, fully intending that such Waiver shall not be subject to
76
+ revocation, rescission, cancellation, termination, or any other legal or
77
+ equitable action to disrupt the quiet enjoyment of the Work by the public
78
+ as contemplated by Affirmer's express Statement of Purpose.
79
+
80
+ 3. Public License Fallback. Should any part of the Waiver for any reason
81
+ be judged legally invalid or ineffective under applicable law, then the
82
+ Waiver shall be preserved to the maximum extent permitted taking into
83
+ account Affirmer's express Statement of Purpose. In addition, to the
84
+ extent the Waiver is so judged Affirmer hereby grants to each affected
85
+ person a royalty-free, non transferable, non sublicensable, non exclusive,
86
+ irrevocable and unconditional license to exercise Affirmer's Copyright and
87
+ Related Rights in the Work (i) in all territories worldwide, (ii) for the
88
+ maximum duration provided by applicable law or treaty (including future
89
+ time extensions), (iii) in any current or future medium and for any number
90
+ of copies, and (iv) for any purpose whatsoever, including without
91
+ limitation commercial, advertising or promotional purposes (the
92
+ "License"). The License shall be deemed effective as of the date CC0 was
93
+ applied by Affirmer to the Work. Should any part of the License for any
94
+ reason be judged legally invalid or ineffective under applicable law, such
95
+ partial invalidity or ineffectiveness shall not invalidate the remainder
96
+ of the License, and in such case Affirmer hereby affirms that he or she
97
+ will not (i) exercise any of his or her remaining Copyright and Related
98
+ Rights in the Work or (ii) assert any associated claims and causes of
99
+ action with respect to the Work, in either case contrary to Affirmer's
100
+ express Statement of Purpose.
101
+
102
+ 4. Limitations and Disclaimers.
103
+
104
+ a. No trademark or patent rights held by Affirmer are waived, abandoned,
105
+ surrendered, licensed or otherwise affected by this document.
106
+ b. Affirmer offers the Work as-is and makes no representations or
107
+ warranties of any kind concerning the Work, express, implied,
108
+ statutory or otherwise, including without limitation warranties of
109
+ title, merchantability, fitness for a particular purpose, non
110
+ infringement, or the absence of latent or other defects, accuracy, or
111
+ the present or absence of errors, whether or not discoverable, all to
112
+ the greatest extent permissible under applicable law.
113
+ c. Affirmer disclaims responsibility for clearing rights of other persons
114
+ that may apply to the Work or any use thereof, including without
115
+ limitation any person's Copyright and Related Rights in the Work.
116
+ Further, Affirmer disclaims responsibility for obtaining any necessary
117
+ consents, permissions or other rights required for any use of the
118
+ Work.
119
+ d. Affirmer understands and acknowledges that Creative Commons is not a
120
+ party to this document and has no duty or obligation with respect to
121
+ this CC0 or use of the Work.
package/LICENSE-MIT ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 electrovir
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # @electrovir/color
2
+
3
+ A color package that wraps [the `culori` package](https://www.npmjs.com/package/culori) with an _extremely_ simple API for color string parsing and color space/model conversion. It is primarily built for https://electrovir.github.io/color-picker-app.
4
+
5
+ - Conversion examples: https://electrovir.github.io/color
6
+ - Reference docs: https://electrovir.github.io/color/docs
7
+
8
+ ## Install
9
+
10
+ ```sh
11
+ npm i @electrovir/color
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ All functionality is accessed through the [`Color`](https://electrovir.github.io/color/docs/classes/Color.html) class:
17
+
18
+ <!-- example-link: src/readme-examples/color.example.ts -->
19
+
20
+ ```TypeScript
21
+ import {Color} from '@electrovir/color';
22
+
23
+ /** Create a new color instance by parsing any valid CSS color string. */
24
+ const color = new Color('#123');
25
+
26
+ /** Update an individual color format's coordinate */
27
+ color.set({
28
+ hsl: {
29
+ h: 100,
30
+ },
31
+ });
32
+
33
+ /** Access the color values in multiple formats. */
34
+ console.info(color.hex); // #1c3311
35
+ console.info(color.rgb); // {r: 28, g: 51, b: 17}
36
+ console.info(color.lab); // {l: 18.6, a: -15, b: 18}
37
+
38
+ /** Express the color value as a valid CSS string. */
39
+ console.info(color.toCss().hsl); // hsl(100 50 13.3)
40
+ ```
41
+
42
+ ### Supported color formats / spaces
43
+
44
+ - [RGB](https://developer.mozilla.org/docs/Web/CSS/color_value/rgb)
45
+ - [HSL](https://developer.mozilla.org/docs/Web/CSS/color_value/hsl)
46
+ - [HWB](https://developer.mozilla.org/docs/Web/CSS/color_value/hwb)
47
+ - [LAB](https://developer.mozilla.org/docs/Web/CSS/color_value/lab)
48
+ - [LCH](https://developer.mozilla.org/docs/Web/CSS/color_value/lch)
49
+ - [Oklab](https://developer.mozilla.org/docs/Web/CSS/color_value/oklab)
50
+ - [Oklch](https://developer.mozilla.org/docs/Web/CSS/color_value/oklch)
@@ -0,0 +1,254 @@
1
+ import { type PartialWithUndefined, type UnionToIntersection, type Values } from '@augment-vir/common';
2
+ /**
3
+ * A hex color string.
4
+ *
5
+ * @category Internal
6
+ */
7
+ export type HexColor = `#${string}`;
8
+ /**
9
+ * An individual definition for a single color format coordinate (like `h` or `s` or `r` or `g`).
10
+ *
11
+ * @category Internal
12
+ */
13
+ export type ColorCoordinateDefinition = {
14
+ min: number;
15
+ max: number;
16
+ } & PartialWithUndefined<{
17
+ /**
18
+ * The number of digits to round this coordinate to.
19
+ *
20
+ * @default 0
21
+ */
22
+ digits?: number | undefined;
23
+ /**
24
+ * A factor to multiple the values in the `Color` class by.
25
+ *
26
+ * @default 1
27
+ */
28
+ factor?: number | undefined;
29
+ suffix?: string;
30
+ }>;
31
+ /**
32
+ * A single color format definition.
33
+ *
34
+ * @category Internal
35
+ */
36
+ export type ColorFormatDefinition<ColorSpace extends string = string> = {
37
+ coords: Record<string, ColorCoordinateDefinition>;
38
+ /** This name for this color to be used in `Colorjs.to()`. Defaults to the color format key. */
39
+ colorSpace: ColorSpace;
40
+ };
41
+ /**
42
+ * All raw supported color formats.
43
+ *
44
+ * @category Internal
45
+ */
46
+ export declare const rawColorFormats: {
47
+ readonly rgb: {
48
+ readonly coords: {
49
+ readonly r: {
50
+ readonly min: 0;
51
+ readonly max: 255;
52
+ readonly factor: 255;
53
+ };
54
+ readonly g: {
55
+ readonly min: 0;
56
+ readonly max: 255;
57
+ readonly factor: 255;
58
+ };
59
+ readonly b: {
60
+ readonly min: 0;
61
+ readonly max: 255;
62
+ readonly factor: 255;
63
+ };
64
+ };
65
+ readonly colorSpace: "rgb";
66
+ };
67
+ readonly hsl: {
68
+ readonly coords: {
69
+ readonly h: {
70
+ readonly min: 0;
71
+ readonly max: 360;
72
+ };
73
+ readonly s: {
74
+ readonly min: 0;
75
+ readonly max: 100;
76
+ readonly factor: 100;
77
+ readonly digits: 1;
78
+ };
79
+ readonly l: {
80
+ readonly min: 0;
81
+ readonly max: 100;
82
+ readonly factor: 100;
83
+ readonly digits: 1;
84
+ };
85
+ };
86
+ readonly colorSpace: "rgb";
87
+ };
88
+ readonly hwb: {
89
+ readonly coords: {
90
+ readonly h: {
91
+ readonly min: 0;
92
+ readonly max: 360;
93
+ };
94
+ readonly w: {
95
+ readonly min: 0;
96
+ readonly max: 100;
97
+ readonly factor: 100;
98
+ readonly digits: 1;
99
+ };
100
+ readonly b: {
101
+ readonly min: 0;
102
+ readonly max: 100;
103
+ readonly factor: 100;
104
+ readonly digits: 1;
105
+ };
106
+ };
107
+ readonly colorSpace: "rgb";
108
+ };
109
+ readonly lab: {
110
+ readonly coords: {
111
+ readonly l: {
112
+ readonly min: 0;
113
+ readonly max: 100;
114
+ readonly digits: 1;
115
+ };
116
+ readonly a: {
117
+ readonly min: -128;
118
+ readonly max: 127;
119
+ };
120
+ readonly b: {
121
+ readonly min: -128;
122
+ readonly max: 127;
123
+ };
124
+ };
125
+ readonly colorSpace: "lab";
126
+ };
127
+ readonly lch: {
128
+ readonly coords: {
129
+ readonly l: {
130
+ readonly min: 0;
131
+ readonly max: 100;
132
+ readonly digits: 1;
133
+ };
134
+ readonly c: {
135
+ readonly min: 0;
136
+ readonly max: 230;
137
+ };
138
+ readonly h: {
139
+ readonly min: 0;
140
+ readonly max: 360;
141
+ };
142
+ };
143
+ readonly colorSpace: "lab";
144
+ };
145
+ readonly oklab: {
146
+ readonly coords: {
147
+ readonly l: {
148
+ readonly min: 0;
149
+ readonly max: 1;
150
+ readonly digits: 3;
151
+ };
152
+ readonly a: {
153
+ readonly min: -0.5;
154
+ readonly max: 0.5;
155
+ readonly digits: 3;
156
+ };
157
+ readonly b: {
158
+ readonly min: -0.5;
159
+ readonly max: 0.5;
160
+ readonly digits: 3;
161
+ };
162
+ };
163
+ readonly colorSpace: "oklab";
164
+ };
165
+ readonly oklch: {
166
+ readonly coords: {
167
+ readonly l: {
168
+ readonly min: 0;
169
+ readonly max: 1;
170
+ readonly digits: 3;
171
+ };
172
+ readonly c: {
173
+ readonly min: 0;
174
+ readonly max: 0.4;
175
+ readonly digits: 3;
176
+ };
177
+ readonly h: {
178
+ readonly min: 0;
179
+ readonly max: 360;
180
+ readonly digits: 1;
181
+ };
182
+ };
183
+ readonly colorSpace: "oklab";
184
+ };
185
+ };
186
+ /**
187
+ * All supported color format names. This can be used as an enum.
188
+ *
189
+ * @category Internal
190
+ */
191
+ export declare const ColorFormatName: { [FormatName in ColorFormatName]: FormatName; };
192
+ /**
193
+ * All supported color format names. This can be used as an enum.
194
+ *
195
+ * @category Internal
196
+ */
197
+ export type ColorFormatName = keyof typeof rawColorFormats;
198
+ /**
199
+ * List the color coordinate keys (like `r` or `g` or `b`) for each supported color format in a
200
+ * union.
201
+ *
202
+ * @category Internal
203
+ */
204
+ export type ColorCoordsByFormat = {
205
+ [FormatName in ColorFormatName]: keyof (typeof rawColorFormats)[FormatName]['coords'];
206
+ };
207
+ /**
208
+ * Type for {@link colorFormats}.
209
+ *
210
+ * @category Internal
211
+ */
212
+ export type ColorFormats = Readonly<{
213
+ [FormatName in ColorFormatName]: ColorFormatDefinition<'colorSpace' extends keyof (typeof rawColorFormats)[FormatName] ? Extract<(typeof rawColorFormats)[FormatName], {
214
+ colorSpace: any;
215
+ }>['colorSpace'] : FormatName>;
216
+ }>;
217
+ /**
218
+ * All supported color formats.
219
+ *
220
+ * @category Color Format
221
+ */
222
+ export declare const colorFormats: ColorFormats;
223
+ /**
224
+ * All available color space names.
225
+ *
226
+ * @category Internal
227
+ */
228
+ export type ColorSpaceName = Values<typeof rawColorFormats>['colorSpace'];
229
+ /**
230
+ * All value types for all supported color formats.
231
+ *
232
+ * @category Internal
233
+ */
234
+ export type ColorValues = {
235
+ [FormatName in ColorFormatName]: Record<keyof (typeof rawColorFormats)[FormatName]['coords'], number>;
236
+ };
237
+ /**
238
+ * All color formats grouped by their color space.
239
+ *
240
+ * @category Color Format
241
+ */
242
+ export declare const colorSpaces: Record<"rgb" | "lab" | "oklab", Record<"rgb" | "hsl" | "hwb" | "lab" | "lch" | "oklab" | "oklch", ColorFormatDefinition<string>>>;
243
+ /**
244
+ * All color format names in an array.
245
+ *
246
+ * @category Internal
247
+ */
248
+ export declare const colorFormatNames: ColorFormatName[];
249
+ /**
250
+ * All possible coordinate names for all supported color formats in a union.
251
+ *
252
+ * @category Internal
253
+ */
254
+ export type ColorCoordinateName = keyof UnionToIntersection<Values<typeof rawColorFormats>['coords']>;
@@ -0,0 +1,175 @@
1
+ import { getObjectTypedEntries, getObjectTypedKeys, getOrSet, mapObjectValues, } from '@augment-vir/common';
2
+ /**
3
+ * All raw supported color formats.
4
+ *
5
+ * @category Internal
6
+ */
7
+ export const rawColorFormats = {
8
+ rgb: {
9
+ coords: {
10
+ r: {
11
+ min: 0,
12
+ max: 255,
13
+ factor: 255,
14
+ },
15
+ g: {
16
+ min: 0,
17
+ max: 255,
18
+ factor: 255,
19
+ },
20
+ b: {
21
+ min: 0,
22
+ max: 255,
23
+ factor: 255,
24
+ },
25
+ },
26
+ colorSpace: 'rgb',
27
+ },
28
+ hsl: {
29
+ coords: {
30
+ h: {
31
+ min: 0,
32
+ max: 360,
33
+ },
34
+ s: {
35
+ min: 0,
36
+ max: 100,
37
+ factor: 100,
38
+ digits: 1,
39
+ },
40
+ l: {
41
+ min: 0,
42
+ max: 100,
43
+ factor: 100,
44
+ digits: 1,
45
+ },
46
+ },
47
+ colorSpace: 'rgb',
48
+ },
49
+ hwb: {
50
+ coords: {
51
+ h: {
52
+ min: 0,
53
+ max: 360,
54
+ },
55
+ w: {
56
+ min: 0,
57
+ max: 100,
58
+ factor: 100,
59
+ digits: 1,
60
+ },
61
+ b: {
62
+ min: 0,
63
+ max: 100,
64
+ factor: 100,
65
+ digits: 1,
66
+ },
67
+ },
68
+ colorSpace: 'rgb',
69
+ },
70
+ lab: {
71
+ coords: {
72
+ l: {
73
+ min: 0,
74
+ max: 100,
75
+ digits: 1,
76
+ },
77
+ a: {
78
+ min: -128,
79
+ max: 127,
80
+ },
81
+ b: {
82
+ min: -128,
83
+ max: 127,
84
+ },
85
+ },
86
+ colorSpace: 'lab',
87
+ },
88
+ lch: {
89
+ coords: {
90
+ l: {
91
+ min: 0,
92
+ max: 100,
93
+ digits: 1,
94
+ },
95
+ c: {
96
+ min: 0,
97
+ max: 230,
98
+ },
99
+ h: {
100
+ min: 0,
101
+ max: 360,
102
+ },
103
+ },
104
+ colorSpace: 'lab',
105
+ },
106
+ oklab: {
107
+ coords: {
108
+ l: {
109
+ min: 0,
110
+ max: 1,
111
+ digits: 3,
112
+ },
113
+ a: {
114
+ min: -0.5,
115
+ max: 0.5,
116
+ digits: 3,
117
+ },
118
+ b: {
119
+ min: -0.5,
120
+ max: 0.5,
121
+ digits: 3,
122
+ },
123
+ },
124
+ colorSpace: 'oklab',
125
+ },
126
+ oklch: {
127
+ coords: {
128
+ l: {
129
+ min: 0,
130
+ max: 1,
131
+ digits: 3,
132
+ },
133
+ c: {
134
+ min: 0,
135
+ max: 0.4,
136
+ digits: 3,
137
+ },
138
+ h: {
139
+ min: 0,
140
+ max: 360,
141
+ digits: 1,
142
+ },
143
+ },
144
+ colorSpace: 'oklab',
145
+ },
146
+ };
147
+ /**
148
+ * All supported color format names. This can be used as an enum.
149
+ *
150
+ * @category Internal
151
+ */
152
+ export const ColorFormatName = mapObjectValues(rawColorFormats, (colorName) => colorName);
153
+ /**
154
+ * All supported color formats.
155
+ *
156
+ * @category Color Format
157
+ */
158
+ export const colorFormats = rawColorFormats;
159
+ /**
160
+ * All color formats grouped by their color space.
161
+ *
162
+ * @category Color Format
163
+ */
164
+ export const colorSpaces = getObjectTypedEntries(colorFormats).reduce((accum, [colorFormatName, colorFormatDefinition,]) => {
165
+ getOrSet(accum, colorFormatDefinition.colorSpace, () => {
166
+ return {};
167
+ })[colorFormatName] = colorFormatDefinition;
168
+ return accum;
169
+ }, {});
170
+ /**
171
+ * All color format names in an array.
172
+ *
173
+ * @category Internal
174
+ */
175
+ export const colorFormatNames = getObjectTypedKeys(colorFormats);