@electrovir/color 1.1.0 → 1.2.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.
|
@@ -180,6 +180,24 @@ export declare const rawColorFormats: {
|
|
|
180
180
|
* @enum
|
|
181
181
|
*/
|
|
182
182
|
export declare const ColorFormatName: { [Key in ColorFormatName]: Key; };
|
|
183
|
+
/**
|
|
184
|
+
* {@link ColorFormatName} combined with hex and named CSS color representation.
|
|
185
|
+
*
|
|
186
|
+
* @category Internal
|
|
187
|
+
* @enum
|
|
188
|
+
*/
|
|
189
|
+
export declare const ColorSyntaxName: {
|
|
190
|
+
readonly hex: "hex";
|
|
191
|
+
readonly name: "name";
|
|
192
|
+
readonly rgb: "rgb";
|
|
193
|
+
readonly hsl: "hsl";
|
|
194
|
+
readonly hwb: "hwb";
|
|
195
|
+
readonly lab: "lab";
|
|
196
|
+
readonly lch: "lch";
|
|
197
|
+
readonly oklab: "oklab";
|
|
198
|
+
readonly oklch: "oklch";
|
|
199
|
+
};
|
|
200
|
+
export type ColorSyntaxName = Values<typeof ColorSyntaxName>;
|
|
183
201
|
/**
|
|
184
202
|
* All supported color format names.
|
|
185
203
|
*
|
|
@@ -240,6 +258,12 @@ export type ColorValue = {
|
|
|
240
258
|
* @category Color Format
|
|
241
259
|
*/
|
|
242
260
|
export declare const colorFormatsBySpace: { [ColorSpace in ColorSpaceName]: { [ColorFormat in ColorFormatName]: ColorFormatDefinition<ColorSpace, ColorFormat>; }; };
|
|
261
|
+
/**
|
|
262
|
+
* Determines the color syntax / format in use based on the given CSS color string.
|
|
263
|
+
*
|
|
264
|
+
* @category Util
|
|
265
|
+
*/
|
|
266
|
+
export declare function getColorSyntaxFromCssString(cssString: string): ColorSyntaxName;
|
|
243
267
|
/**
|
|
244
268
|
* All possible coordinate names for all supported color formats in a union.
|
|
245
269
|
*
|
|
@@ -151,6 +151,17 @@ export const rawColorFormats = {
|
|
|
151
151
|
* @enum
|
|
152
152
|
*/
|
|
153
153
|
export const ColorFormatName = mapObjectValues(rawColorFormats, (colorName) => colorName);
|
|
154
|
+
/**
|
|
155
|
+
* {@link ColorFormatName} combined with hex and named CSS color representation.
|
|
156
|
+
*
|
|
157
|
+
* @category Internal
|
|
158
|
+
* @enum
|
|
159
|
+
*/
|
|
160
|
+
export const ColorSyntaxName = {
|
|
161
|
+
...ColorFormatName,
|
|
162
|
+
hex: 'hex',
|
|
163
|
+
name: 'name',
|
|
164
|
+
};
|
|
154
165
|
/**
|
|
155
166
|
* All supported color formats.
|
|
156
167
|
*
|
|
@@ -187,3 +198,37 @@ export const colorFormatsBySpace = getObjectTypedEntries(colorFormats).reduce((a
|
|
|
187
198
|
})[colorFormatName] = colorFormatDefinition;
|
|
188
199
|
return accum;
|
|
189
200
|
}, {});
|
|
201
|
+
/**
|
|
202
|
+
* Determines the color syntax / format in use based on the given CSS color string.
|
|
203
|
+
*
|
|
204
|
+
* @category Util
|
|
205
|
+
*/
|
|
206
|
+
export function getColorSyntaxFromCssString(cssString) {
|
|
207
|
+
if (cssString.startsWith('rgb')) {
|
|
208
|
+
return ColorSyntaxName.rgb;
|
|
209
|
+
}
|
|
210
|
+
else if (cssString.startsWith('hsl')) {
|
|
211
|
+
return ColorSyntaxName.hsl;
|
|
212
|
+
}
|
|
213
|
+
else if (cssString.startsWith('hwb')) {
|
|
214
|
+
return ColorSyntaxName.hwb;
|
|
215
|
+
}
|
|
216
|
+
else if (cssString.startsWith('oklab')) {
|
|
217
|
+
return ColorSyntaxName.oklab;
|
|
218
|
+
}
|
|
219
|
+
else if (cssString.startsWith('oklch')) {
|
|
220
|
+
return ColorSyntaxName.oklch;
|
|
221
|
+
}
|
|
222
|
+
else if (cssString.startsWith('lab')) {
|
|
223
|
+
return ColorSyntaxName.lab;
|
|
224
|
+
}
|
|
225
|
+
else if (cssString.startsWith('lch')) {
|
|
226
|
+
return ColorSyntaxName.lch;
|
|
227
|
+
}
|
|
228
|
+
else if (cssString.startsWith('#')) {
|
|
229
|
+
return ColorSyntaxName.hex;
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
return ColorSyntaxName.name;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type PartialWithUndefined } from '@augment-vir/common';
|
|
2
2
|
import { type RequireExactlyOne } from 'type-fest';
|
|
3
|
-
import { ColorFormatName, type ColorCoordsByFormat, type ColorValue, type HexColor } from './color-formats.js';
|
|
3
|
+
import { ColorFormatName, ColorSyntaxName, type ColorCoordsByFormat, type ColorValue, type HexColor } from './color-formats.js';
|
|
4
4
|
/**
|
|
5
5
|
* An update to an existing {@link Color} instance. Used in {@link Color.set}.
|
|
6
6
|
*
|
|
@@ -9,8 +9,8 @@ import { ColorFormatName, type ColorCoordsByFormat, type ColorValue, type HexCol
|
|
|
9
9
|
export type ColorUpdate = RequireExactlyOne<{
|
|
10
10
|
[FormatName in ColorFormatName]: PartialWithUndefined<Record<ColorCoordsByFormat[FormatName], number>>;
|
|
11
11
|
} & {
|
|
12
|
-
hex: HexColor;
|
|
13
|
-
name: string;
|
|
12
|
+
[ColorSyntaxName.hex]: HexColor;
|
|
13
|
+
[ColorSyntaxName.name]: string;
|
|
14
14
|
}>;
|
|
15
15
|
/**
|
|
16
16
|
* The values of all supported color formats for a given {@link Color} instance. Accessed via
|
|
@@ -19,7 +19,8 @@ export type ColorUpdate = RequireExactlyOne<{
|
|
|
19
19
|
* @category Internal
|
|
20
20
|
*/
|
|
21
21
|
export type AllColorsValues = ColorValue & {
|
|
22
|
-
hex: HexColor;
|
|
22
|
+
[ColorSyntaxName.hex]: HexColor;
|
|
23
|
+
[ColorSyntaxName.name]: string;
|
|
23
24
|
names: string[];
|
|
24
25
|
};
|
|
25
26
|
/**
|
|
@@ -46,6 +47,7 @@ export declare class Color {
|
|
|
46
47
|
/** All current color values. These are updated whenever {@link Color.set} is called. */
|
|
47
48
|
protected readonly _allColors: {
|
|
48
49
|
names: string[];
|
|
50
|
+
name: string;
|
|
49
51
|
hex: HexColor;
|
|
50
52
|
rgb: {
|
|
51
53
|
r: number;
|
|
@@ -112,18 +114,27 @@ export declare class Color {
|
|
|
112
114
|
/**
|
|
113
115
|
* Converts the values for each supported color format into a padded string for easy display
|
|
114
116
|
* purposes.
|
|
117
|
+
*
|
|
118
|
+
* @see `.toCss()`
|
|
115
119
|
*/
|
|
116
|
-
toFormattedStrings(): Record<
|
|
120
|
+
toFormattedStrings(): Record<ColorSyntaxName | 'names', string>;
|
|
117
121
|
/**
|
|
118
122
|
* Converts the values for each supported color format in a CSS string that can be directly used
|
|
119
123
|
* in any modern CSS code.
|
|
124
|
+
*
|
|
125
|
+
* @see `.toFormattedStrings()`
|
|
120
126
|
*/
|
|
121
|
-
toCss(): Record<
|
|
127
|
+
toCss(): Record<ColorSyntaxName, string>;
|
|
122
128
|
/**
|
|
123
129
|
* The current color expressed as hardcoded CSS color keywords. If no CSS color keywords match
|
|
124
130
|
* the current color, this array will be empty.
|
|
125
131
|
*/
|
|
126
132
|
get names(): string[];
|
|
133
|
+
/**
|
|
134
|
+
* The current color expressed as a single CSS color name string. If there is no color name that
|
|
135
|
+
* matches the current color, this will be an empty string.
|
|
136
|
+
*/
|
|
137
|
+
get name(): string;
|
|
127
138
|
/** The current color expressed as an RGB hex string. */
|
|
128
139
|
get hex(): HexColor;
|
|
129
140
|
/** The current color expressed as its RGB coordinate values. */
|
|
@@ -2,7 +2,7 @@ import { assert, assertWrap, check } from '@augment-vir/assert';
|
|
|
2
2
|
import { copyThroughJson, filterMap, getEnumValues, getObjectTypedEntries, getObjectTypedKeys, joinWithFinalConjunction, mapObjectValues, round, } from '@augment-vir/common';
|
|
3
3
|
import colorNames from 'color-name';
|
|
4
4
|
import { clampGamut, converter, formatHex, parse } from 'culori';
|
|
5
|
-
import { ColorFormatName, colorFormats, } from './color-formats.js';
|
|
5
|
+
import { ColorFormatName, colorFormats, ColorSyntaxName, } from './color-formats.js';
|
|
6
6
|
import { maxColorNameLength } from './color-name-length.js';
|
|
7
7
|
/**
|
|
8
8
|
* A `Color` class with state and the following features:
|
|
@@ -37,38 +37,39 @@ export class Color {
|
|
|
37
37
|
/** All current color values. These are updated whenever {@link Color.set} is called. */
|
|
38
38
|
_allColors = {
|
|
39
39
|
names: ['black'],
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
[ColorSyntaxName.name]: 'black',
|
|
41
|
+
[ColorSyntaxName.hex]: '#000000',
|
|
42
|
+
[ColorSyntaxName.rgb]: {
|
|
42
43
|
r: 0,
|
|
43
44
|
g: 0,
|
|
44
45
|
b: 0,
|
|
45
46
|
},
|
|
46
|
-
hsl: {
|
|
47
|
+
[ColorSyntaxName.hsl]: {
|
|
47
48
|
h: 0,
|
|
48
49
|
s: 0,
|
|
49
50
|
l: 0,
|
|
50
51
|
},
|
|
51
|
-
hwb: {
|
|
52
|
+
[ColorSyntaxName.hwb]: {
|
|
52
53
|
h: 0,
|
|
53
54
|
w: 0,
|
|
54
55
|
b: 0,
|
|
55
56
|
},
|
|
56
|
-
lab: {
|
|
57
|
+
[ColorSyntaxName.lab]: {
|
|
57
58
|
l: 0,
|
|
58
59
|
a: 0,
|
|
59
60
|
b: 0,
|
|
60
61
|
},
|
|
61
|
-
lch: {
|
|
62
|
+
[ColorSyntaxName.lch]: {
|
|
62
63
|
l: 0,
|
|
63
64
|
c: 0,
|
|
64
65
|
h: 0,
|
|
65
66
|
},
|
|
66
|
-
oklab: {
|
|
67
|
+
[ColorSyntaxName.oklab]: {
|
|
67
68
|
l: 0,
|
|
68
69
|
a: 0,
|
|
69
70
|
b: 0,
|
|
70
71
|
},
|
|
71
|
-
oklch: {
|
|
72
|
+
[ColorSyntaxName.oklch]: {
|
|
72
73
|
l: 0,
|
|
73
74
|
c: 0,
|
|
74
75
|
h: 0,
|
|
@@ -147,8 +148,9 @@ export class Color {
|
|
|
147
148
|
}
|
|
148
149
|
});
|
|
149
150
|
});
|
|
150
|
-
this._allColors.hex = formatHex(this.#internalColor);
|
|
151
|
+
this._allColors[ColorSyntaxName.hex] = formatHex(this.#internalColor);
|
|
151
152
|
this._allColors.names = findMatchingColorNames(this.rgb);
|
|
153
|
+
this._allColors[ColorSyntaxName.name] = this._allColors.names[0] || '';
|
|
152
154
|
}
|
|
153
155
|
/**
|
|
154
156
|
* Create a string that can be serialized into a new {@link Color} instance which will exactly
|
|
@@ -164,6 +166,8 @@ export class Color {
|
|
|
164
166
|
/**
|
|
165
167
|
* Converts the values for each supported color format into a padded string for easy display
|
|
166
168
|
* purposes.
|
|
169
|
+
*
|
|
170
|
+
* @see `.toCss()`
|
|
167
171
|
*/
|
|
168
172
|
toFormattedStrings() {
|
|
169
173
|
const colorFormatStrings = mapObjectValues(colorFormats, (colorFormatName) => {
|
|
@@ -171,14 +175,17 @@ export class Color {
|
|
|
171
175
|
return coordValues.map((coordValue) => String(coordValue).padStart(6, ' ')).join(' ');
|
|
172
176
|
});
|
|
173
177
|
return {
|
|
174
|
-
hex: this.hex,
|
|
178
|
+
[ColorSyntaxName.hex]: this.hex,
|
|
175
179
|
...colorFormatStrings,
|
|
176
180
|
names: this.names.join(', ').padEnd(maxColorNameLength, ' '),
|
|
181
|
+
name: (this.names[0] || '').padEnd(maxColorNameLength, ' '),
|
|
177
182
|
};
|
|
178
183
|
}
|
|
179
184
|
/**
|
|
180
185
|
* Converts the values for each supported color format in a CSS string that can be directly used
|
|
181
186
|
* in any modern CSS code.
|
|
187
|
+
*
|
|
188
|
+
* @see `.toFormattedStrings()`
|
|
182
189
|
*/
|
|
183
190
|
toCss() {
|
|
184
191
|
const colorFormatStrings = mapObjectValues(colorFormats, (colorFormatName) => {
|
|
@@ -186,7 +193,7 @@ export class Color {
|
|
|
186
193
|
return `${colorFormatName}(${coordValues.join(' ')})`;
|
|
187
194
|
});
|
|
188
195
|
return {
|
|
189
|
-
hex: this.hex,
|
|
196
|
+
[ColorSyntaxName.hex]: this.hex,
|
|
190
197
|
...colorFormatStrings,
|
|
191
198
|
name: this.names[0] || '',
|
|
192
199
|
};
|
|
@@ -198,37 +205,44 @@ export class Color {
|
|
|
198
205
|
get names() {
|
|
199
206
|
return copyThroughJson(this._allColors.names);
|
|
200
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* The current color expressed as a single CSS color name string. If there is no color name that
|
|
210
|
+
* matches the current color, this will be an empty string.
|
|
211
|
+
*/
|
|
212
|
+
get name() {
|
|
213
|
+
return this._allColors.names[0] || '';
|
|
214
|
+
}
|
|
201
215
|
/** The current color expressed as an RGB hex string. */
|
|
202
216
|
get hex() {
|
|
203
|
-
return copyThroughJson(this._allColors.hex);
|
|
217
|
+
return copyThroughJson(this._allColors[ColorSyntaxName.hex]);
|
|
204
218
|
}
|
|
205
219
|
/** The current color expressed as its RGB coordinate values. */
|
|
206
220
|
get rgb() {
|
|
207
|
-
return copyThroughJson(this._allColors.rgb);
|
|
221
|
+
return copyThroughJson(this._allColors[ColorSyntaxName.rgb]);
|
|
208
222
|
}
|
|
209
223
|
/** The current color expressed as its HSL coordinate values. */
|
|
210
224
|
get hsl() {
|
|
211
|
-
return copyThroughJson(this._allColors.hsl);
|
|
225
|
+
return copyThroughJson(this._allColors[ColorSyntaxName.hsl]);
|
|
212
226
|
}
|
|
213
227
|
/** The current color expressed as its HWB coordinate values. */
|
|
214
228
|
get hwb() {
|
|
215
|
-
return copyThroughJson(this._allColors.hwb);
|
|
229
|
+
return copyThroughJson(this._allColors[ColorSyntaxName.hwb]);
|
|
216
230
|
}
|
|
217
231
|
/** The current color expressed as its LAB coordinate values. */
|
|
218
232
|
get lab() {
|
|
219
|
-
return copyThroughJson(this._allColors.lab);
|
|
233
|
+
return copyThroughJson(this._allColors[ColorSyntaxName.lab]);
|
|
220
234
|
}
|
|
221
235
|
/** The current color expressed as its LCH coordinate values. */
|
|
222
236
|
get lch() {
|
|
223
|
-
return copyThroughJson(this._allColors.lch);
|
|
237
|
+
return copyThroughJson(this._allColors[ColorSyntaxName.lch]);
|
|
224
238
|
}
|
|
225
239
|
/** The current color expressed as its Oklab coordinate values. */
|
|
226
240
|
get oklab() {
|
|
227
|
-
return copyThroughJson(this._allColors.oklab);
|
|
241
|
+
return copyThroughJson(this._allColors[ColorSyntaxName.oklab]);
|
|
228
242
|
}
|
|
229
243
|
/** The current color expressed as its Oklch coordinate values. */
|
|
230
244
|
get oklch() {
|
|
231
|
-
return copyThroughJson(this._allColors.oklch);
|
|
245
|
+
return copyThroughJson(this._allColors[ColorSyntaxName.oklch]);
|
|
232
246
|
}
|
|
233
247
|
}
|
|
234
248
|
function findMatchingColorNames(rgb) {
|