@proyecto-viviana/solid-stately 0.2.4 → 0.3.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 +21 -0
- package/README.md +30 -283
- package/dist/autocomplete/createAutocompleteState.d.ts +3 -2
- package/dist/autocomplete/index.d.ts +1 -1
- package/dist/calendar/createCalendarState.d.ts +22 -7
- package/dist/calendar/createDateFieldState.d.ts +30 -7
- package/dist/calendar/createDatePickerState.d.ts +60 -0
- package/dist/calendar/createRangeCalendarState.d.ts +27 -8
- package/dist/calendar/createTimeFieldState.d.ts +34 -7
- package/dist/calendar/index.d.ts +7 -6
- package/dist/checkbox/createCheckboxGroupState.d.ts +12 -3
- package/dist/checkbox/index.d.ts +1 -1
- package/dist/collections/ListCollection.d.ts +1 -1
- package/dist/collections/createListState.d.ts +8 -8
- package/dist/collections/createMenuState.d.ts +7 -7
- package/dist/collections/createSelectionState.d.ts +5 -5
- package/dist/collections/index.d.ts +5 -5
- package/dist/collections/types.d.ts +23 -12
- package/dist/color/Color.d.ts +1 -1
- package/dist/color/createColorAreaState.d.ts +4 -2
- package/dist/color/createColorFieldState.d.ts +38 -12
- package/dist/color/createColorSliderState.d.ts +9 -2
- package/dist/color/createColorWheelState.d.ts +13 -2
- package/dist/color/getColorChannels.d.ts +20 -0
- package/dist/color/index.d.ts +6 -6
- package/dist/color/types.d.ts +4 -4
- package/dist/combobox/createComboBoxState.d.ts +27 -13
- package/dist/combobox/index.d.ts +1 -1
- package/dist/data/createAsyncList.d.ts +111 -0
- package/dist/data/createListData.d.ts +65 -0
- package/dist/data/createTreeData.d.ts +65 -0
- package/dist/data/index.d.ts +3 -0
- package/dist/datepicker/index.d.ts +8 -0
- package/dist/disclosure/createDisclosureState.d.ts +2 -2
- package/dist/disclosure/index.d.ts +1 -1
- package/dist/dnd/createDragState.d.ts +2 -2
- package/dist/dnd/createDraggableCollectionState.d.ts +2 -2
- package/dist/dnd/createDropState.d.ts +2 -2
- package/dist/dnd/createDroppableCollectionState.d.ts +3 -3
- package/dist/dnd/index.d.ts +5 -5
- package/dist/dnd/types.d.ts +16 -16
- package/dist/form/createFormValidationState.d.ts +3 -3
- package/dist/form/index.d.ts +1 -1
- package/dist/grid/createGridState.d.ts +2 -2
- package/dist/grid/index.d.ts +2 -2
- package/dist/grid/types.d.ts +17 -11
- package/dist/index.d.ts +31 -25
- package/dist/index.js +7990 -6264
- package/dist/index.js.map +1 -7
- package/dist/menu/index.d.ts +8 -0
- package/dist/numberfield/createNumberFieldState.d.ts +2 -2
- package/dist/numberfield/index.d.ts +1 -1
- package/dist/overlays/createOverlayTriggerState.d.ts +2 -2
- package/dist/overlays/index.d.ts +1 -1
- package/dist/radio/createRadioGroupState.d.ts +13 -4
- package/dist/radio/index.d.ts +1 -1
- package/dist/searchfield/createSearchFieldState.d.ts +2 -2
- package/dist/searchfield/index.d.ts +2 -2
- package/dist/select/createSelectState.d.ts +20 -3
- package/dist/select/index.d.ts +1 -1
- package/dist/selection/index.d.ts +11 -0
- package/dist/slider/createSliderState.d.ts +3 -3
- package/dist/slider/index.d.ts +2 -2
- package/dist/steplist/createStepListState.d.ts +48 -0
- package/dist/steplist/index.d.ts +1 -0
- package/dist/table/TableCollection.d.ts +3 -3
- package/dist/table/createTableColumnResizeState.d.ts +54 -0
- package/dist/table/createTableState.d.ts +2 -2
- package/dist/table/index.d.ts +5 -3
- package/dist/table/types.d.ts +10 -10
- package/dist/tabs/createTabListState.d.ts +5 -5
- package/dist/tabs/index.d.ts +1 -1
- package/dist/textfield/createTextFieldState.d.ts +2 -2
- package/dist/textfield/index.d.ts +1 -1
- package/dist/toast/createToastState.d.ts +15 -3
- package/dist/toast/index.d.ts +1 -1
- package/dist/toggle/createToggleGroupState.d.ts +45 -0
- package/dist/toggle/createToggleState.d.ts +2 -2
- package/dist/toggle/index.d.ts +2 -1
- package/dist/tooltip/createTooltipTriggerState.d.ts +2 -2
- package/dist/tooltip/index.d.ts +1 -1
- package/dist/tree/TreeCollection.d.ts +5 -4
- package/dist/tree/createTreeState.d.ts +2 -2
- package/dist/tree/index.d.ts +3 -3
- package/dist/tree/types.d.ts +19 -13
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/reactivity.d.ts +1 -1
- package/package.json +30 -30
- package/src/autocomplete/createAutocompleteState.d.ts +19 -17
- package/src/autocomplete/createAutocompleteState.ts +25 -32
- package/src/autocomplete/index.d.ts +6 -2
- package/src/autocomplete/index.ts +1 -1
- package/src/calendar/createCalendarState.d.ts +139 -117
- package/src/calendar/createCalendarState.ts +221 -74
- package/src/calendar/createDateFieldState.d.ts +130 -95
- package/src/calendar/createDateFieldState.ts +286 -151
- package/src/calendar/createDatePickerState.ts +347 -0
- package/src/calendar/createRangeCalendarState.d.ts +154 -129
- package/src/calendar/createRangeCalendarState.ts +293 -79
- package/src/calendar/createTimeFieldState.d.ts +81 -79
- package/src/calendar/createTimeFieldState.ts +187 -108
- package/src/calendar/index.d.ts +80 -7
- package/src/calendar/index.ts +21 -12
- package/src/checkbox/createCheckboxGroupState.d.ts +57 -55
- package/src/checkbox/createCheckboxGroupState.ts +57 -30
- package/src/checkbox/index.d.ts +6 -2
- package/src/checkbox/index.ts +1 -1
- package/src/collections/ListCollection.d.ts +25 -22
- package/src/collections/ListCollection.ts +152 -146
- package/src/collections/createListState.d.ts +73 -58
- package/src/collections/createListState.ts +267 -264
- package/src/collections/createMenuState.d.ts +36 -31
- package/src/collections/createMenuState.ts +112 -106
- package/src/collections/createSelectionState.d.ts +78 -65
- package/src/collections/createSelectionState.ts +340 -336
- package/src/collections/index.d.ts +38 -6
- package/src/collections/index.ts +41 -46
- package/src/collections/types.d.ts +101 -101
- package/src/collections/types.ts +181 -169
- package/src/color/Color.d.ts +20 -5
- package/src/color/Color.ts +1054 -951
- package/src/color/createColorAreaState.d.ts +67 -65
- package/src/color/createColorAreaState.ts +301 -293
- package/src/color/createColorFieldState.d.ts +46 -44
- package/src/color/createColorFieldState.ts +428 -292
- package/src/color/createColorSliderState.d.ts +58 -56
- package/src/color/createColorSliderState.ts +272 -241
- package/src/color/createColorWheelState.d.ts +42 -40
- package/src/color/createColorWheelState.ts +248 -211
- package/src/color/getColorChannels.ts +36 -0
- package/src/color/index.d.ts +36 -7
- package/src/color/index.ts +44 -47
- package/src/color/types.d.ts +68 -63
- package/src/color/types.ts +119 -127
- package/src/combobox/createComboBoxState.d.ts +107 -105
- package/src/combobox/createComboBoxState.ts +790 -703
- package/src/combobox/index.d.ts +10 -2
- package/src/combobox/index.ts +13 -13
- package/src/data/createAsyncList.ts +403 -0
- package/src/data/createListData.ts +298 -0
- package/src/data/createTreeData.ts +646 -0
- package/src/data/index.ts +25 -0
- package/src/datepicker/index.ts +32 -0
- package/src/disclosure/createDisclosureState.d.ts +47 -43
- package/src/disclosure/createDisclosureState.ts +9 -23
- package/src/disclosure/index.d.ts +10 -2
- package/src/disclosure/index.ts +1 -1
- package/src/dnd/createDragState.d.ts +51 -42
- package/src/dnd/createDragState.ts +151 -153
- package/src/dnd/createDraggableCollectionState.d.ts +52 -41
- package/src/dnd/createDraggableCollectionState.ts +158 -165
- package/src/dnd/createDropState.d.ts +64 -45
- package/src/dnd/createDropState.ts +202 -212
- package/src/dnd/createDroppableCollectionState.d.ts +88 -59
- package/src/dnd/createDroppableCollectionState.ts +354 -357
- package/src/dnd/index.d.ts +54 -6
- package/src/dnd/index.ts +63 -76
- package/src/dnd/types.d.ts +167 -156
- package/src/dnd/types.ts +316 -317
- package/src/form/createFormValidationState.d.ts +47 -45
- package/src/form/createFormValidationState.ts +356 -389
- package/src/form/index.d.ts +16 -2
- package/src/form/index.ts +15 -15
- package/src/grid/createGridState.d.ts +7 -4
- package/src/grid/createGridState.ts +44 -51
- package/src/grid/index.d.ts +3 -3
- package/src/grid/index.ts +2 -8
- package/src/grid/types.d.ts +136 -129
- package/src/grid/types.ts +24 -17
- package/src/index.d.ts +310 -26
- package/src/index.ts +87 -79
- package/src/menu/index.ts +17 -0
- package/src/numberfield/createNumberFieldState.d.ts +56 -54
- package/src/numberfield/createNumberFieldState.ts +422 -383
- package/src/numberfield/index.d.ts +6 -2
- package/src/numberfield/index.ts +5 -5
- package/src/overlays/createOverlayTriggerState.d.ts +22 -20
- package/src/overlays/createOverlayTriggerState.ts +66 -67
- package/src/overlays/index.d.ts +6 -2
- package/src/overlays/index.ts +5 -5
- package/src/radio/createRadioGroupState.d.ts +54 -52
- package/src/radio/createRadioGroupState.ts +66 -30
- package/src/radio/index.d.ts +7 -2
- package/src/radio/index.ts +1 -1
- package/src/searchfield/createSearchFieldState.d.ts +16 -14
- package/src/searchfield/createSearchFieldState.ts +56 -62
- package/src/searchfield/index.d.ts +3 -3
- package/src/searchfield/index.ts +2 -5
- package/src/select/createSelectState.d.ts +63 -61
- package/src/select/createSelectState.ts +290 -181
- package/src/select/index.d.ts +2 -2
- package/src/select/index.ts +1 -5
- package/src/selection/index.ts +27 -0
- package/src/slider/createSliderState.d.ts +60 -60
- package/src/slider/createSliderState.ts +233 -211
- package/src/slider/index.d.ts +3 -3
- package/src/slider/index.ts +2 -6
- package/src/ssr/index.d.ts +1 -1
- package/src/ssr/index.ts +2 -2
- package/src/steplist/createStepListState.ts +191 -0
- package/src/steplist/index.ts +5 -0
- package/src/table/TableCollection.d.ts +42 -40
- package/src/table/TableCollection.ts +61 -64
- package/src/table/createTableColumnResizeState.ts +242 -0
- package/src/table/createTableState.d.ts +7 -4
- package/src/table/createTableState.ts +10 -7
- package/src/table/index.d.ts +14 -4
- package/src/table/index.ts +10 -3
- package/src/table/types.d.ts +98 -92
- package/src/table/types.ts +17 -13
- package/src/tabs/createTabListState.d.ts +59 -57
- package/src/tabs/createTabListState.ts +58 -39
- package/src/tabs/index.d.ts +8 -2
- package/src/tabs/index.ts +1 -1
- package/src/textfield/createTextFieldState.d.ts +16 -14
- package/src/textfield/createTextFieldState.ts +7 -18
- package/src/textfield/index.d.ts +6 -2
- package/src/textfield/index.ts +1 -1
- package/src/toast/createToastState.d.ts +92 -81
- package/src/toast/createToastState.ts +56 -44
- package/src/toast/index.d.ts +12 -2
- package/src/toast/index.ts +1 -1
- package/src/toggle/createToggleGroupState.ts +117 -0
- package/src/toggle/createToggleState.d.ts +19 -19
- package/src/toggle/createToggleState.ts +2 -16
- package/src/toggle/index.d.ts +2 -2
- package/src/toggle/index.ts +6 -4
- package/src/tooltip/createTooltipTriggerState.d.ts +17 -17
- package/src/tooltip/createTooltipTriggerState.ts +184 -183
- package/src/tooltip/index.d.ts +7 -2
- package/src/tooltip/index.ts +6 -6
- package/src/tree/TreeCollection.d.ts +26 -23
- package/src/tree/TreeCollection.ts +215 -175
- package/src/tree/createTreeState.d.ts +7 -4
- package/src/tree/createTreeState.ts +390 -392
- package/src/tree/index.d.ts +10 -4
- package/src/tree/index.ts +13 -13
- package/src/tree/types.d.ts +131 -126
- package/src/tree/types.ts +183 -174
- package/src/utils/index.d.ts +2 -2
- package/src/utils/index.ts +1 -1
- package/src/utils/reactivity.d.ts +2 -2
- package/src/utils/reactivity.ts +3 -3
package/src/color/Color.ts
CHANGED
|
@@ -1,951 +1,1054 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Color class implementation.
|
|
3
|
-
* Based on @react-stately/color.
|
|
4
|
-
*
|
|
5
|
-
* Provides color manipulation, conversion, and formatting.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type {
|
|
9
|
-
Color,
|
|
10
|
-
ColorFormat,
|
|
11
|
-
ColorSpace,
|
|
12
|
-
ColorChannel,
|
|
13
|
-
ColorChannelRange,
|
|
14
|
-
ColorAxes,
|
|
15
|
-
RGBColor,
|
|
16
|
-
HSLColor,
|
|
17
|
-
HSBColor,
|
|
18
|
-
} from
|
|
19
|
-
|
|
20
|
-
// Channel ranges
|
|
21
|
-
const RGB_CHANNEL_RANGE: ColorChannelRange = {
|
|
22
|
-
minValue: 0,
|
|
23
|
-
maxValue: 255,
|
|
24
|
-
step: 1,
|
|
25
|
-
pageSize: 17,
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const ALPHA_CHANNEL_RANGE: ColorChannelRange = {
|
|
29
|
-
minValue: 0,
|
|
30
|
-
maxValue: 1,
|
|
31
|
-
step: 0.01,
|
|
32
|
-
pageSize: 0.1,
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const HUE_CHANNEL_RANGE: ColorChannelRange = {
|
|
36
|
-
minValue: 0,
|
|
37
|
-
maxValue: 360,
|
|
38
|
-
step: 1,
|
|
39
|
-
pageSize: 15,
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const PERCENT_CHANNEL_RANGE: ColorChannelRange = {
|
|
43
|
-
minValue: 0,
|
|
44
|
-
maxValue: 100,
|
|
45
|
-
step: 1,
|
|
46
|
-
pageSize: 10,
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
// Channel names (English only for now)
|
|
50
|
-
const CHANNEL_NAMES: Record<ColorChannel, string> = {
|
|
51
|
-
hue:
|
|
52
|
-
saturation:
|
|
53
|
-
brightness:
|
|
54
|
-
lightness:
|
|
55
|
-
red:
|
|
56
|
-
green:
|
|
57
|
-
blue:
|
|
58
|
-
alpha:
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
//
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
]
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
*
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
*
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
*
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
case
|
|
222
|
-
r =
|
|
223
|
-
g =
|
|
224
|
-
bl = p;
|
|
225
|
-
break;
|
|
226
|
-
case
|
|
227
|
-
r =
|
|
228
|
-
g = b;
|
|
229
|
-
bl =
|
|
230
|
-
break;
|
|
231
|
-
case
|
|
232
|
-
r = p;
|
|
233
|
-
g =
|
|
234
|
-
bl =
|
|
235
|
-
break;
|
|
236
|
-
case
|
|
237
|
-
r =
|
|
238
|
-
g =
|
|
239
|
-
bl = b;
|
|
240
|
-
break;
|
|
241
|
-
|
|
242
|
-
r =
|
|
243
|
-
g = p;
|
|
244
|
-
bl =
|
|
245
|
-
break;
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
*
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
if (
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
case
|
|
432
|
-
|
|
433
|
-
case
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
case
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
case
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
case
|
|
531
|
-
case
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
return
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
case
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
case
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
return
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
case
|
|
700
|
-
case
|
|
701
|
-
case
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
case
|
|
706
|
-
case
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
case
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
case
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
}
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
}
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
const
|
|
903
|
-
const
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
const
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
}
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Color class implementation.
|
|
3
|
+
* Based on @react-stately/color.
|
|
4
|
+
*
|
|
5
|
+
* Provides color manipulation, conversion, and formatting.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
Color,
|
|
10
|
+
ColorFormat,
|
|
11
|
+
ColorSpace,
|
|
12
|
+
ColorChannel,
|
|
13
|
+
ColorChannelRange,
|
|
14
|
+
ColorAxes,
|
|
15
|
+
RGBColor,
|
|
16
|
+
HSLColor,
|
|
17
|
+
HSBColor,
|
|
18
|
+
} from "./types";
|
|
19
|
+
|
|
20
|
+
// Channel ranges
|
|
21
|
+
const RGB_CHANNEL_RANGE: ColorChannelRange = {
|
|
22
|
+
minValue: 0,
|
|
23
|
+
maxValue: 255,
|
|
24
|
+
step: 1,
|
|
25
|
+
pageSize: 17,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const ALPHA_CHANNEL_RANGE: ColorChannelRange = {
|
|
29
|
+
minValue: 0,
|
|
30
|
+
maxValue: 1,
|
|
31
|
+
step: 0.01,
|
|
32
|
+
pageSize: 0.1,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const HUE_CHANNEL_RANGE: ColorChannelRange = {
|
|
36
|
+
minValue: 0,
|
|
37
|
+
maxValue: 360,
|
|
38
|
+
step: 1,
|
|
39
|
+
pageSize: 15,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const PERCENT_CHANNEL_RANGE: ColorChannelRange = {
|
|
43
|
+
minValue: 0,
|
|
44
|
+
maxValue: 100,
|
|
45
|
+
step: 1,
|
|
46
|
+
pageSize: 10,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// Channel names (English only for now)
|
|
50
|
+
const CHANNEL_NAMES: Record<ColorChannel, string> = {
|
|
51
|
+
hue: "Hue",
|
|
52
|
+
saturation: "Saturation",
|
|
53
|
+
brightness: "Brightness",
|
|
54
|
+
lightness: "Lightness",
|
|
55
|
+
red: "Red",
|
|
56
|
+
green: "Green",
|
|
57
|
+
blue: "Blue",
|
|
58
|
+
alpha: "Alpha",
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// React Stately uses OKLCH for color names so that lightness is perceptually
|
|
62
|
+
// consistent across hues.
|
|
63
|
+
const ORANGE_LIGHTNESS_THRESHOLD = 0.68;
|
|
64
|
+
const YELLOW_GREEN_LIGHTNESS_THRESHOLD = 0.85;
|
|
65
|
+
const MAX_DARK_LIGHTNESS = 0.55;
|
|
66
|
+
const GRAY_THRESHOLD = 0.001;
|
|
67
|
+
const OKLCH_HUES: Array<[number, string]> = [
|
|
68
|
+
[0, "pink"],
|
|
69
|
+
[15, "red"],
|
|
70
|
+
[48, "orange"],
|
|
71
|
+
[94, "yellow"],
|
|
72
|
+
[135, "green"],
|
|
73
|
+
[175, "cyan"],
|
|
74
|
+
[264, "blue"],
|
|
75
|
+
[284, "purple"],
|
|
76
|
+
[320, "magenta"],
|
|
77
|
+
[349, "pink"],
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Clamp a value to a range.
|
|
82
|
+
*/
|
|
83
|
+
function clamp(value: number, min: number, max: number): number {
|
|
84
|
+
return Math.min(Math.max(value, min), max);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Round to fixed decimal places.
|
|
89
|
+
*/
|
|
90
|
+
function toFixed(value: number, decimals: number): number {
|
|
91
|
+
return Math.round(value * Math.pow(10, decimals)) / Math.pow(10, decimals);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Convert RGB to HSL.
|
|
96
|
+
*/
|
|
97
|
+
function rgbToHsl(r: number, g: number, b: number): { h: number; s: number; l: number } {
|
|
98
|
+
r /= 255;
|
|
99
|
+
g /= 255;
|
|
100
|
+
b /= 255;
|
|
101
|
+
|
|
102
|
+
const max = Math.max(r, g, b);
|
|
103
|
+
const min = Math.min(r, g, b);
|
|
104
|
+
let h = 0;
|
|
105
|
+
let s = 0;
|
|
106
|
+
const l = (max + min) / 2;
|
|
107
|
+
|
|
108
|
+
if (max !== min) {
|
|
109
|
+
const d = max - min;
|
|
110
|
+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
111
|
+
|
|
112
|
+
switch (max) {
|
|
113
|
+
case r:
|
|
114
|
+
h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
|
|
115
|
+
break;
|
|
116
|
+
case g:
|
|
117
|
+
h = ((b - r) / d + 2) / 6;
|
|
118
|
+
break;
|
|
119
|
+
case b:
|
|
120
|
+
h = ((r - g) / d + 4) / 6;
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
h: Math.round(h * 360),
|
|
127
|
+
s: Math.round(s * 100),
|
|
128
|
+
l: Math.round(l * 100),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Convert HSL to RGB.
|
|
134
|
+
*/
|
|
135
|
+
function hslToRgb(h: number, s: number, l: number): { r: number; g: number; b: number } {
|
|
136
|
+
h /= 360;
|
|
137
|
+
s /= 100;
|
|
138
|
+
l /= 100;
|
|
139
|
+
|
|
140
|
+
let r: number, g: number, b: number;
|
|
141
|
+
|
|
142
|
+
if (s === 0) {
|
|
143
|
+
r = g = b = l;
|
|
144
|
+
} else {
|
|
145
|
+
const hue2rgb = (p: number, q: number, t: number) => {
|
|
146
|
+
if (t < 0) t += 1;
|
|
147
|
+
if (t > 1) t -= 1;
|
|
148
|
+
if (t < 1 / 6) return p + (q - p) * 6 * t;
|
|
149
|
+
if (t < 1 / 2) return q;
|
|
150
|
+
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
|
|
151
|
+
return p;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
155
|
+
const p = 2 * l - q;
|
|
156
|
+
r = hue2rgb(p, q, h + 1 / 3);
|
|
157
|
+
g = hue2rgb(p, q, h);
|
|
158
|
+
b = hue2rgb(p, q, h - 1 / 3);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
r: Math.round(r * 255),
|
|
163
|
+
g: Math.round(g * 255),
|
|
164
|
+
b: Math.round(b * 255),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Convert RGB to HSB.
|
|
170
|
+
*/
|
|
171
|
+
function rgbToHsb(r: number, g: number, b: number): { h: number; s: number; b: number } {
|
|
172
|
+
r /= 255;
|
|
173
|
+
g /= 255;
|
|
174
|
+
b /= 255;
|
|
175
|
+
|
|
176
|
+
const max = Math.max(r, g, b);
|
|
177
|
+
const min = Math.min(r, g, b);
|
|
178
|
+
let h = 0;
|
|
179
|
+
const v = max;
|
|
180
|
+
const d = max - min;
|
|
181
|
+
const s = max === 0 ? 0 : d / max;
|
|
182
|
+
|
|
183
|
+
if (max !== min) {
|
|
184
|
+
switch (max) {
|
|
185
|
+
case r:
|
|
186
|
+
h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
|
|
187
|
+
break;
|
|
188
|
+
case g:
|
|
189
|
+
h = ((b - r) / d + 2) / 6;
|
|
190
|
+
break;
|
|
191
|
+
case b:
|
|
192
|
+
h = ((r - g) / d + 4) / 6;
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
h: Math.round(h * 360),
|
|
199
|
+
s: Math.round(s * 100),
|
|
200
|
+
b: Math.round(v * 100),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Convert HSB to RGB.
|
|
206
|
+
*/
|
|
207
|
+
function hsbToRgb(h: number, s: number, b: number): { r: number; g: number; b: number } {
|
|
208
|
+
h /= 360;
|
|
209
|
+
s /= 100;
|
|
210
|
+
b /= 100;
|
|
211
|
+
|
|
212
|
+
let r: number, g: number, bl: number;
|
|
213
|
+
|
|
214
|
+
const i = Math.floor(h * 6);
|
|
215
|
+
const f = h * 6 - i;
|
|
216
|
+
const p = b * (1 - s);
|
|
217
|
+
const q = b * (1 - f * s);
|
|
218
|
+
const t = b * (1 - (1 - f) * s);
|
|
219
|
+
|
|
220
|
+
switch (i % 6) {
|
|
221
|
+
case 0:
|
|
222
|
+
r = b;
|
|
223
|
+
g = t;
|
|
224
|
+
bl = p;
|
|
225
|
+
break;
|
|
226
|
+
case 1:
|
|
227
|
+
r = q;
|
|
228
|
+
g = b;
|
|
229
|
+
bl = p;
|
|
230
|
+
break;
|
|
231
|
+
case 2:
|
|
232
|
+
r = p;
|
|
233
|
+
g = b;
|
|
234
|
+
bl = t;
|
|
235
|
+
break;
|
|
236
|
+
case 3:
|
|
237
|
+
r = p;
|
|
238
|
+
g = q;
|
|
239
|
+
bl = b;
|
|
240
|
+
break;
|
|
241
|
+
case 4:
|
|
242
|
+
r = t;
|
|
243
|
+
g = p;
|
|
244
|
+
bl = b;
|
|
245
|
+
break;
|
|
246
|
+
default:
|
|
247
|
+
r = b;
|
|
248
|
+
g = p;
|
|
249
|
+
bl = q;
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return {
|
|
254
|
+
r: Math.round(r * 255),
|
|
255
|
+
g: Math.round(g * 255),
|
|
256
|
+
b: Math.round(bl * 255),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function getOklchHueName(l: number, c: number, h: number): [string, number] {
|
|
261
|
+
if (c < GRAY_THRESHOLD) {
|
|
262
|
+
return ["gray", l];
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
for (let i = 0; i < OKLCH_HUES.length; i += 1) {
|
|
266
|
+
let [hue, hueName] = OKLCH_HUES[i];
|
|
267
|
+
const [nextHue, nextHueName] = OKLCH_HUES[i + 1] || [360, "pink"];
|
|
268
|
+
if (h >= hue && h < nextHue) {
|
|
269
|
+
if (hueName === "orange") {
|
|
270
|
+
if (l < ORANGE_LIGHTNESS_THRESHOLD) {
|
|
271
|
+
hueName = "brown";
|
|
272
|
+
} else {
|
|
273
|
+
l = l - ORANGE_LIGHTNESS_THRESHOLD + MAX_DARK_LIGHTNESS;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (h > hue + (nextHue - hue) / 2 && hueName !== nextHueName) {
|
|
278
|
+
hueName = `${hueName} ${nextHueName}`;
|
|
279
|
+
} else if (hueName === "yellow" && l < YELLOW_GREEN_LIGHTNESS_THRESHOLD) {
|
|
280
|
+
hueName = "yellow green";
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return [hueName.toLocaleLowerCase(), l];
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return ["pink", l];
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function getColorNameFromColor(color: Color): string {
|
|
291
|
+
let [l, c, h] = toOKLCH(color);
|
|
292
|
+
const alpha = color.getChannelValue("alpha");
|
|
293
|
+
|
|
294
|
+
if (l > 0.999) {
|
|
295
|
+
return alpha < 1 ? `white ${Math.round((1 - alpha) * 100)}% transparent` : "white";
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (l < 0.001) {
|
|
299
|
+
return alpha < 1 ? `black ${Math.round((1 - alpha) * 100)}% transparent` : "black";
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
let hue: string;
|
|
303
|
+
[hue, l] = getOklchHueName(l, c, h);
|
|
304
|
+
|
|
305
|
+
let chroma = "";
|
|
306
|
+
if (c <= 0.1 && c >= GRAY_THRESHOLD) {
|
|
307
|
+
chroma = l >= 0.7 ? "pale" : "grayish";
|
|
308
|
+
} else if (c >= 0.15) {
|
|
309
|
+
chroma = "vibrant";
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
let lightness = "";
|
|
313
|
+
if (l < 0.3) {
|
|
314
|
+
lightness = "very dark";
|
|
315
|
+
} else if (l < MAX_DARK_LIGHTNESS) {
|
|
316
|
+
lightness = "dark";
|
|
317
|
+
} else if (l < 0.7) {
|
|
318
|
+
lightness = "";
|
|
319
|
+
} else if (l < 0.85) {
|
|
320
|
+
lightness = "light";
|
|
321
|
+
} else {
|
|
322
|
+
lightness = "very light";
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const name = [lightness, chroma, hue].filter(Boolean).join(" ").replace(/\s+/g, " ").trim();
|
|
326
|
+
if (alpha < 1) {
|
|
327
|
+
return `${Math.round((1 - alpha) * 100)}% transparent ${name}`.trim();
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return name;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function getHueNameFromColor(color: Color): string {
|
|
334
|
+
const [l, c, h] = toOKLCH(color);
|
|
335
|
+
const [name] = getOklchHueName(l, c, h);
|
|
336
|
+
return name;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function toOKLCH(color: Color): [number, number, number] {
|
|
340
|
+
const rgb = color.toFormat("rgb");
|
|
341
|
+
let red = rgb.getChannelValue("red") / 255;
|
|
342
|
+
let green = rgb.getChannelValue("green") / 255;
|
|
343
|
+
let blue = rgb.getChannelValue("blue") / 255;
|
|
344
|
+
[red, green, blue] = linSRGB(red, green, blue);
|
|
345
|
+
const [x, y, z] = linSRGBToXYZ(red, green, blue);
|
|
346
|
+
const [l, a, b] = xyzToOKLab(x, y, z);
|
|
347
|
+
return okLabToOKLCH(l, a, b);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function okLabToOKLCH(l: number, a: number, b: number): [number, number, number] {
|
|
351
|
+
const hue = (Math.atan2(b, a) * 180) / Math.PI;
|
|
352
|
+
return [l, Math.sqrt(a ** 2 + b ** 2), hue >= 0 ? hue : hue + 360];
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function linSRGB(r: number, g: number, b: number): [number, number, number] {
|
|
356
|
+
return [linSRGBComponent(r), linSRGBComponent(g), linSRGBComponent(b)];
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function linSRGBComponent(value: number) {
|
|
360
|
+
const sign = value < 0 ? -1 : 1;
|
|
361
|
+
const abs = Math.abs(value);
|
|
362
|
+
|
|
363
|
+
if (abs <= 0.04045) {
|
|
364
|
+
return value / 12.92;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return sign * Math.pow((abs + 0.055) / 1.055, 2.4);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function linSRGBToXYZ(r: number, g: number, b: number): [number, number, number] {
|
|
371
|
+
const matrix = [
|
|
372
|
+
506752 / 1228815,
|
|
373
|
+
87881 / 245763,
|
|
374
|
+
12673 / 70218,
|
|
375
|
+
87098 / 409605,
|
|
376
|
+
175762 / 245763,
|
|
377
|
+
12673 / 175545,
|
|
378
|
+
7918 / 409605,
|
|
379
|
+
87881 / 737289,
|
|
380
|
+
1001167 / 1053270,
|
|
381
|
+
];
|
|
382
|
+
return multiplyMatrix(matrix, r, g, b);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function xyzToOKLab(x: number, y: number, z: number): [number, number, number] {
|
|
386
|
+
const xyzToLMS = [
|
|
387
|
+
0.819022437996703, 0.3619062600528904, -0.1288737815209879, 0.0329836539323885,
|
|
388
|
+
0.9292868615863434, 0.0361446663506424, 0.0481771893596242, 0.2642395317527308,
|
|
389
|
+
0.6335478284694309,
|
|
390
|
+
];
|
|
391
|
+
const lmsToOKLab = [
|
|
392
|
+
0.210454268309314, 0.7936177747023054, -0.0040720430116193, 1.9779985324311684,
|
|
393
|
+
-2.4285922420485799, 0.450593709617411, 0.0259040424655478, 0.7827717124575296,
|
|
394
|
+
-0.8086757549230774,
|
|
395
|
+
];
|
|
396
|
+
|
|
397
|
+
const [a, b, c] = multiplyMatrix(xyzToLMS, x, y, z);
|
|
398
|
+
return multiplyMatrix(lmsToOKLab, Math.cbrt(a), Math.cbrt(b), Math.cbrt(c));
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function multiplyMatrix(
|
|
402
|
+
matrix: number[],
|
|
403
|
+
x: number,
|
|
404
|
+
y: number,
|
|
405
|
+
z: number,
|
|
406
|
+
): [number, number, number] {
|
|
407
|
+
const a = matrix[0] * x + matrix[1] * y + matrix[2] * z;
|
|
408
|
+
const b = matrix[3] * x + matrix[4] * y + matrix[5] * z;
|
|
409
|
+
const c = matrix[6] * x + matrix[7] * y + matrix[8] * z;
|
|
410
|
+
return [a, b, c];
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* RGB Color implementation.
|
|
415
|
+
*/
|
|
416
|
+
class RGBColorImpl implements Color {
|
|
417
|
+
private red: number;
|
|
418
|
+
private green: number;
|
|
419
|
+
private blue: number;
|
|
420
|
+
private alpha: number;
|
|
421
|
+
|
|
422
|
+
constructor(red: number, green: number, blue: number, alpha: number = 1) {
|
|
423
|
+
this.red = clamp(Math.round(red), 0, 255);
|
|
424
|
+
this.green = clamp(Math.round(green), 0, 255);
|
|
425
|
+
this.blue = clamp(Math.round(blue), 0, 255);
|
|
426
|
+
this.alpha = clamp(toFixed(alpha, 2), 0, 1);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
toFormat(format: ColorFormat): Color {
|
|
430
|
+
switch (format) {
|
|
431
|
+
case "hex":
|
|
432
|
+
case "hexa":
|
|
433
|
+
case "rgb":
|
|
434
|
+
case "rgba":
|
|
435
|
+
return this.clone();
|
|
436
|
+
case "hsl":
|
|
437
|
+
case "hsla": {
|
|
438
|
+
const { h, s, l } = rgbToHsl(this.red, this.green, this.blue);
|
|
439
|
+
return new HSLColorImpl(h, s, l, this.alpha);
|
|
440
|
+
}
|
|
441
|
+
case "hsb":
|
|
442
|
+
case "hsba": {
|
|
443
|
+
const { h, s, b } = rgbToHsb(this.red, this.green, this.blue);
|
|
444
|
+
return new HSBColorImpl(h, s, b, this.alpha);
|
|
445
|
+
}
|
|
446
|
+
default:
|
|
447
|
+
throw new Error(`Unsupported format: ${format}`);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
toString(format?: ColorFormat | "css"): string {
|
|
452
|
+
const f = format ?? "css";
|
|
453
|
+
|
|
454
|
+
switch (f) {
|
|
455
|
+
case "hex":
|
|
456
|
+
return `#${this.red.toString(16).padStart(2, "0")}${this.green.toString(16).padStart(2, "0")}${this.blue.toString(16).padStart(2, "0")}`;
|
|
457
|
+
case "hexa":
|
|
458
|
+
return `#${this.red.toString(16).padStart(2, "0")}${this.green.toString(16).padStart(2, "0")}${this.blue.toString(16).padStart(2, "0")}${Math.round(
|
|
459
|
+
this.alpha * 255,
|
|
460
|
+
)
|
|
461
|
+
.toString(16)
|
|
462
|
+
.padStart(2, "0")}`;
|
|
463
|
+
case "rgb":
|
|
464
|
+
return `rgb(${this.red}, ${this.green}, ${this.blue})`;
|
|
465
|
+
case "rgba":
|
|
466
|
+
case "css":
|
|
467
|
+
return this.alpha === 1
|
|
468
|
+
? `rgb(${this.red}, ${this.green}, ${this.blue})`
|
|
469
|
+
: `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`;
|
|
470
|
+
default:
|
|
471
|
+
return this.toFormat(f as ColorFormat).toString(f);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
clone(): Color {
|
|
476
|
+
return new RGBColorImpl(this.red, this.green, this.blue, this.alpha);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
toHexInt(): number {
|
|
480
|
+
return (this.red << 16) | (this.green << 8) | this.blue;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
getChannelValue(channel: ColorChannel): number {
|
|
484
|
+
switch (channel) {
|
|
485
|
+
case "red":
|
|
486
|
+
return this.red;
|
|
487
|
+
case "green":
|
|
488
|
+
return this.green;
|
|
489
|
+
case "blue":
|
|
490
|
+
return this.blue;
|
|
491
|
+
case "alpha":
|
|
492
|
+
return this.alpha;
|
|
493
|
+
// Cross-color-space channels - convert to HSB
|
|
494
|
+
case "hue":
|
|
495
|
+
case "saturation":
|
|
496
|
+
case "brightness":
|
|
497
|
+
return this.toFormat("hsb").getChannelValue(channel);
|
|
498
|
+
case "lightness":
|
|
499
|
+
return this.toFormat("hsl").getChannelValue(channel);
|
|
500
|
+
default:
|
|
501
|
+
throw new Error(`Invalid channel: ${channel}`);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
withChannelValue(channel: ColorChannel, value: number): Color {
|
|
506
|
+
switch (channel) {
|
|
507
|
+
case "red":
|
|
508
|
+
return new RGBColorImpl(value, this.green, this.blue, this.alpha);
|
|
509
|
+
case "green":
|
|
510
|
+
return new RGBColorImpl(this.red, value, this.blue, this.alpha);
|
|
511
|
+
case "blue":
|
|
512
|
+
return new RGBColorImpl(this.red, this.green, value, this.alpha);
|
|
513
|
+
case "alpha":
|
|
514
|
+
return new RGBColorImpl(this.red, this.green, this.blue, value);
|
|
515
|
+
// Cross-color-space channels - convert, update, convert back
|
|
516
|
+
case "hue":
|
|
517
|
+
case "saturation":
|
|
518
|
+
case "brightness":
|
|
519
|
+
return this.toFormat("hsb").withChannelValue(channel, value).toFormat("rgb");
|
|
520
|
+
case "lightness":
|
|
521
|
+
return this.toFormat("hsl").withChannelValue(channel, value).toFormat("rgb");
|
|
522
|
+
default:
|
|
523
|
+
throw new Error(`Invalid channel: ${channel}`);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange {
|
|
528
|
+
switch (channel) {
|
|
529
|
+
case "red":
|
|
530
|
+
case "green":
|
|
531
|
+
case "blue":
|
|
532
|
+
return RGB_CHANNEL_RANGE;
|
|
533
|
+
case "alpha":
|
|
534
|
+
return ALPHA_CHANNEL_RANGE;
|
|
535
|
+
case "hue":
|
|
536
|
+
return HUE_CHANNEL_RANGE;
|
|
537
|
+
case "saturation":
|
|
538
|
+
case "brightness":
|
|
539
|
+
case "lightness":
|
|
540
|
+
return PERCENT_CHANNEL_RANGE;
|
|
541
|
+
default:
|
|
542
|
+
throw new Error(`Invalid channel: ${channel}`);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
getChannelName(channel: ColorChannel, _locale: string): string {
|
|
547
|
+
return CHANNEL_NAMES[channel] || channel;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions {
|
|
551
|
+
if (channel === "alpha") {
|
|
552
|
+
return { style: "percent" };
|
|
553
|
+
}
|
|
554
|
+
return { maximumFractionDigits: 0 };
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
formatChannelValue(channel: ColorChannel, locale: string): string {
|
|
558
|
+
const value = this.getChannelValue(channel);
|
|
559
|
+
const options = this.getChannelFormatOptions(channel);
|
|
560
|
+
return new Intl.NumberFormat(locale, options).format(value);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
getColorSpace(): ColorSpace {
|
|
564
|
+
return "rgb";
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
getColorSpaceAxes(xyChannels?: { xChannel?: ColorChannel; yChannel?: ColorChannel }): ColorAxes {
|
|
568
|
+
const xChannel = xyChannels?.xChannel ?? "red";
|
|
569
|
+
const yChannel = xyChannels?.yChannel ?? "green";
|
|
570
|
+
const channels: ColorChannel[] = ["red", "green", "blue"];
|
|
571
|
+
const zChannel = channels.find((c) => c !== xChannel && c !== yChannel) ?? "blue";
|
|
572
|
+
return { xChannel, yChannel, zChannel };
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
|
|
576
|
+
return ["red", "green", "blue"];
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
getColorName(_locale: string): string {
|
|
580
|
+
return getColorNameFromColor(this);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
getHueName(_locale: string): string {
|
|
584
|
+
return getHueNameFromColor(this);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* HSL Color implementation.
|
|
590
|
+
*/
|
|
591
|
+
class HSLColorImpl implements Color {
|
|
592
|
+
private hue: number;
|
|
593
|
+
private saturation: number;
|
|
594
|
+
private lightness: number;
|
|
595
|
+
private alpha: number;
|
|
596
|
+
|
|
597
|
+
constructor(hue: number, saturation: number, lightness: number, alpha: number = 1) {
|
|
598
|
+
this.hue = clamp(Math.round(hue) % 360, 0, 360);
|
|
599
|
+
this.saturation = clamp(Math.round(saturation), 0, 100);
|
|
600
|
+
this.lightness = clamp(Math.round(lightness), 0, 100);
|
|
601
|
+
this.alpha = clamp(toFixed(alpha, 2), 0, 1);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
toFormat(format: ColorFormat): Color {
|
|
605
|
+
switch (format) {
|
|
606
|
+
case "hsl":
|
|
607
|
+
case "hsla":
|
|
608
|
+
return this.clone();
|
|
609
|
+
case "hex":
|
|
610
|
+
case "hexa":
|
|
611
|
+
case "rgb":
|
|
612
|
+
case "rgba": {
|
|
613
|
+
const { r, g, b } = hslToRgb(this.hue, this.saturation, this.lightness);
|
|
614
|
+
return new RGBColorImpl(r, g, b, this.alpha);
|
|
615
|
+
}
|
|
616
|
+
case "hsb":
|
|
617
|
+
case "hsba": {
|
|
618
|
+
const { r, g, b } = hslToRgb(this.hue, this.saturation, this.lightness);
|
|
619
|
+
const hsb = rgbToHsb(r, g, b);
|
|
620
|
+
return new HSBColorImpl(hsb.h, hsb.s, hsb.b, this.alpha);
|
|
621
|
+
}
|
|
622
|
+
default:
|
|
623
|
+
throw new Error(`Unsupported format: ${format}`);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
toString(format?: ColorFormat | "css"): string {
|
|
628
|
+
const f = format ?? "css";
|
|
629
|
+
|
|
630
|
+
switch (f) {
|
|
631
|
+
case "hsl":
|
|
632
|
+
return `hsl(${this.hue}, ${this.saturation}%, ${this.lightness}%)`;
|
|
633
|
+
case "hsla":
|
|
634
|
+
case "css":
|
|
635
|
+
return this.alpha === 1
|
|
636
|
+
? `hsl(${this.hue}, ${this.saturation}%, ${this.lightness}%)`
|
|
637
|
+
: `hsla(${this.hue}, ${this.saturation}%, ${this.lightness}%, ${this.alpha})`;
|
|
638
|
+
default:
|
|
639
|
+
return this.toFormat(f as ColorFormat).toString(f);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
clone(): Color {
|
|
644
|
+
return new HSLColorImpl(this.hue, this.saturation, this.lightness, this.alpha);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
toHexInt(): number {
|
|
648
|
+
return this.toFormat("rgb").toHexInt();
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
getChannelValue(channel: ColorChannel): number {
|
|
652
|
+
switch (channel) {
|
|
653
|
+
case "hue":
|
|
654
|
+
return this.hue;
|
|
655
|
+
case "saturation":
|
|
656
|
+
return this.saturation;
|
|
657
|
+
case "lightness":
|
|
658
|
+
return this.lightness;
|
|
659
|
+
case "alpha":
|
|
660
|
+
return this.alpha;
|
|
661
|
+
// Cross-color-space channels
|
|
662
|
+
case "red":
|
|
663
|
+
case "green":
|
|
664
|
+
case "blue":
|
|
665
|
+
return this.toFormat("rgb").getChannelValue(channel);
|
|
666
|
+
case "brightness":
|
|
667
|
+
return this.toFormat("hsb").getChannelValue(channel);
|
|
668
|
+
default:
|
|
669
|
+
throw new Error(`Invalid channel: ${channel}`);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
withChannelValue(channel: ColorChannel, value: number): Color {
|
|
674
|
+
switch (channel) {
|
|
675
|
+
case "hue":
|
|
676
|
+
return new HSLColorImpl(value, this.saturation, this.lightness, this.alpha);
|
|
677
|
+
case "saturation":
|
|
678
|
+
return new HSLColorImpl(this.hue, value, this.lightness, this.alpha);
|
|
679
|
+
case "lightness":
|
|
680
|
+
return new HSLColorImpl(this.hue, this.saturation, value, this.alpha);
|
|
681
|
+
case "alpha":
|
|
682
|
+
return new HSLColorImpl(this.hue, this.saturation, this.lightness, value);
|
|
683
|
+
// Cross-color-space channels
|
|
684
|
+
case "red":
|
|
685
|
+
case "green":
|
|
686
|
+
case "blue":
|
|
687
|
+
return this.toFormat("rgb").withChannelValue(channel, value).toFormat("hsl");
|
|
688
|
+
case "brightness":
|
|
689
|
+
return this.toFormat("hsb").withChannelValue(channel, value).toFormat("hsl");
|
|
690
|
+
default:
|
|
691
|
+
throw new Error(`Invalid channel: ${channel}`);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange {
|
|
696
|
+
switch (channel) {
|
|
697
|
+
case "hue":
|
|
698
|
+
return HUE_CHANNEL_RANGE;
|
|
699
|
+
case "saturation":
|
|
700
|
+
case "lightness":
|
|
701
|
+
case "brightness":
|
|
702
|
+
return PERCENT_CHANNEL_RANGE;
|
|
703
|
+
case "alpha":
|
|
704
|
+
return ALPHA_CHANNEL_RANGE;
|
|
705
|
+
case "red":
|
|
706
|
+
case "green":
|
|
707
|
+
case "blue":
|
|
708
|
+
return RGB_CHANNEL_RANGE;
|
|
709
|
+
default:
|
|
710
|
+
throw new Error(`Invalid channel: ${channel}`);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
getChannelName(channel: ColorChannel, _locale: string): string {
|
|
715
|
+
return CHANNEL_NAMES[channel] || channel;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions {
|
|
719
|
+
if (channel === "alpha") {
|
|
720
|
+
return { style: "percent" };
|
|
721
|
+
}
|
|
722
|
+
if (channel === "hue") {
|
|
723
|
+
return { style: "unit", unit: "degree", unitDisplay: "narrow", maximumFractionDigits: 0 };
|
|
724
|
+
}
|
|
725
|
+
return { style: "percent", maximumFractionDigits: 0 };
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
formatChannelValue(channel: ColorChannel, locale: string): string {
|
|
729
|
+
const value = this.getChannelValue(channel);
|
|
730
|
+
const options = this.getChannelFormatOptions(channel);
|
|
731
|
+
if (channel === "saturation" || channel === "lightness") {
|
|
732
|
+
return new Intl.NumberFormat(locale, options).format(value / 100);
|
|
733
|
+
}
|
|
734
|
+
return new Intl.NumberFormat(locale, options).format(value);
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
getColorSpace(): ColorSpace {
|
|
738
|
+
return "hsl";
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
getColorSpaceAxes(xyChannels?: { xChannel?: ColorChannel; yChannel?: ColorChannel }): ColorAxes {
|
|
742
|
+
const xChannel = xyChannels?.xChannel ?? "saturation";
|
|
743
|
+
const yChannel = xyChannels?.yChannel ?? "lightness";
|
|
744
|
+
const channels: ColorChannel[] = ["hue", "saturation", "lightness"];
|
|
745
|
+
const zChannel = channels.find((c) => c !== xChannel && c !== yChannel) ?? "hue";
|
|
746
|
+
return { xChannel, yChannel, zChannel };
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
|
|
750
|
+
return ["hue", "saturation", "lightness"];
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
getColorName(_locale: string): string {
|
|
754
|
+
return getColorNameFromColor(this);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
getHueName(_locale: string): string {
|
|
758
|
+
return getHueNameFromColor(this);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/**
|
|
763
|
+
* HSB Color implementation.
|
|
764
|
+
*/
|
|
765
|
+
class HSBColorImpl implements Color {
|
|
766
|
+
private hue: number;
|
|
767
|
+
private saturation: number;
|
|
768
|
+
private brightness: number;
|
|
769
|
+
private alpha: number;
|
|
770
|
+
|
|
771
|
+
constructor(hue: number, saturation: number, brightness: number, alpha: number = 1) {
|
|
772
|
+
this.hue = clamp(Math.round(hue) % 360, 0, 360);
|
|
773
|
+
this.saturation = clamp(Math.round(saturation), 0, 100);
|
|
774
|
+
this.brightness = clamp(Math.round(brightness), 0, 100);
|
|
775
|
+
this.alpha = clamp(toFixed(alpha, 2), 0, 1);
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
toFormat(format: ColorFormat): Color {
|
|
779
|
+
switch (format) {
|
|
780
|
+
case "hsb":
|
|
781
|
+
case "hsba":
|
|
782
|
+
return this.clone();
|
|
783
|
+
case "hex":
|
|
784
|
+
case "hexa":
|
|
785
|
+
case "rgb":
|
|
786
|
+
case "rgba": {
|
|
787
|
+
const { r, g, b } = hsbToRgb(this.hue, this.saturation, this.brightness);
|
|
788
|
+
return new RGBColorImpl(r, g, b, this.alpha);
|
|
789
|
+
}
|
|
790
|
+
case "hsl":
|
|
791
|
+
case "hsla": {
|
|
792
|
+
const { r, g, b } = hsbToRgb(this.hue, this.saturation, this.brightness);
|
|
793
|
+
const hsl = rgbToHsl(r, g, b);
|
|
794
|
+
return new HSLColorImpl(hsl.h, hsl.s, hsl.l, this.alpha);
|
|
795
|
+
}
|
|
796
|
+
default:
|
|
797
|
+
throw new Error(`Unsupported format: ${format}`);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
toString(format?: ColorFormat | "css"): string {
|
|
802
|
+
const f = format ?? "css";
|
|
803
|
+
|
|
804
|
+
switch (f) {
|
|
805
|
+
case "hsb":
|
|
806
|
+
return `hsb(${this.hue}, ${this.saturation}%, ${this.brightness}%)`;
|
|
807
|
+
case "hsba":
|
|
808
|
+
case "css":
|
|
809
|
+
// HSB is not a standard CSS format, convert to RGB
|
|
810
|
+
return this.toFormat("rgba").toString("css");
|
|
811
|
+
default:
|
|
812
|
+
return this.toFormat(f as ColorFormat).toString(f);
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
clone(): Color {
|
|
817
|
+
return new HSBColorImpl(this.hue, this.saturation, this.brightness, this.alpha);
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
toHexInt(): number {
|
|
821
|
+
return this.toFormat("rgb").toHexInt();
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
getChannelValue(channel: ColorChannel): number {
|
|
825
|
+
switch (channel) {
|
|
826
|
+
case "hue":
|
|
827
|
+
return this.hue;
|
|
828
|
+
case "saturation":
|
|
829
|
+
return this.saturation;
|
|
830
|
+
case "brightness":
|
|
831
|
+
return this.brightness;
|
|
832
|
+
case "alpha":
|
|
833
|
+
return this.alpha;
|
|
834
|
+
// Cross-color-space channels
|
|
835
|
+
case "red":
|
|
836
|
+
case "green":
|
|
837
|
+
case "blue":
|
|
838
|
+
return this.toFormat("rgb").getChannelValue(channel);
|
|
839
|
+
case "lightness":
|
|
840
|
+
return this.toFormat("hsl").getChannelValue(channel);
|
|
841
|
+
default:
|
|
842
|
+
throw new Error(`Invalid channel: ${channel}`);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
withChannelValue(channel: ColorChannel, value: number): Color {
|
|
847
|
+
switch (channel) {
|
|
848
|
+
case "hue":
|
|
849
|
+
return new HSBColorImpl(value, this.saturation, this.brightness, this.alpha);
|
|
850
|
+
case "saturation":
|
|
851
|
+
return new HSBColorImpl(this.hue, value, this.brightness, this.alpha);
|
|
852
|
+
case "brightness":
|
|
853
|
+
return new HSBColorImpl(this.hue, this.saturation, value, this.alpha);
|
|
854
|
+
case "alpha":
|
|
855
|
+
return new HSBColorImpl(this.hue, this.saturation, this.brightness, value);
|
|
856
|
+
// Cross-color-space channels
|
|
857
|
+
case "red":
|
|
858
|
+
case "green":
|
|
859
|
+
case "blue":
|
|
860
|
+
return this.toFormat("rgb").withChannelValue(channel, value).toFormat("hsb");
|
|
861
|
+
case "lightness":
|
|
862
|
+
return this.toFormat("hsl").withChannelValue(channel, value).toFormat("hsb");
|
|
863
|
+
default:
|
|
864
|
+
throw new Error(`Invalid channel: ${channel}`);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange {
|
|
869
|
+
switch (channel) {
|
|
870
|
+
case "hue":
|
|
871
|
+
return HUE_CHANNEL_RANGE;
|
|
872
|
+
case "saturation":
|
|
873
|
+
case "brightness":
|
|
874
|
+
case "lightness":
|
|
875
|
+
return PERCENT_CHANNEL_RANGE;
|
|
876
|
+
case "alpha":
|
|
877
|
+
return ALPHA_CHANNEL_RANGE;
|
|
878
|
+
case "red":
|
|
879
|
+
case "green":
|
|
880
|
+
case "blue":
|
|
881
|
+
return RGB_CHANNEL_RANGE;
|
|
882
|
+
default:
|
|
883
|
+
throw new Error(`Invalid channel: ${channel}`);
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
getChannelName(channel: ColorChannel, _locale: string): string {
|
|
888
|
+
return CHANNEL_NAMES[channel] || channel;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions {
|
|
892
|
+
if (channel === "alpha") {
|
|
893
|
+
return { style: "percent" };
|
|
894
|
+
}
|
|
895
|
+
if (channel === "hue") {
|
|
896
|
+
return { style: "unit", unit: "degree", unitDisplay: "narrow", maximumFractionDigits: 0 };
|
|
897
|
+
}
|
|
898
|
+
return { style: "percent", maximumFractionDigits: 0 };
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
formatChannelValue(channel: ColorChannel, locale: string): string {
|
|
902
|
+
const value = this.getChannelValue(channel);
|
|
903
|
+
const options = this.getChannelFormatOptions(channel);
|
|
904
|
+
if (channel === "saturation" || channel === "brightness") {
|
|
905
|
+
return new Intl.NumberFormat(locale, options).format(value / 100);
|
|
906
|
+
}
|
|
907
|
+
return new Intl.NumberFormat(locale, options).format(value);
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
getColorSpace(): ColorSpace {
|
|
911
|
+
return "hsb";
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
getColorSpaceAxes(xyChannels?: { xChannel?: ColorChannel; yChannel?: ColorChannel }): ColorAxes {
|
|
915
|
+
const xChannel = xyChannels?.xChannel ?? "saturation";
|
|
916
|
+
const yChannel = xyChannels?.yChannel ?? "brightness";
|
|
917
|
+
const channels: ColorChannel[] = ["hue", "saturation", "brightness"];
|
|
918
|
+
const zChannel = channels.find((c) => c !== xChannel && c !== yChannel) ?? "hue";
|
|
919
|
+
return { xChannel, yChannel, zChannel };
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
|
|
923
|
+
return ["hue", "saturation", "brightness"];
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
getColorName(_locale: string): string {
|
|
927
|
+
return getColorNameFromColor(this);
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
getHueName(_locale: string): string {
|
|
931
|
+
return getHueNameFromColor(this);
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
/**
|
|
936
|
+
* Parse a color string into a Color object.
|
|
937
|
+
*/
|
|
938
|
+
export function parseColor(value: string): Color {
|
|
939
|
+
const trimmed = value.trim().toLowerCase();
|
|
940
|
+
|
|
941
|
+
// Hex format
|
|
942
|
+
if (trimmed.startsWith("#")) {
|
|
943
|
+
const hex = trimmed.slice(1);
|
|
944
|
+
if (!/^[0-9a-f]+$/i.test(hex)) {
|
|
945
|
+
throw new Error(`Invalid hex color: ${value}`);
|
|
946
|
+
}
|
|
947
|
+
if (hex.length === 3) {
|
|
948
|
+
const r = parseInt(hex[0] + hex[0], 16);
|
|
949
|
+
const g = parseInt(hex[1] + hex[1], 16);
|
|
950
|
+
const b = parseInt(hex[2] + hex[2], 16);
|
|
951
|
+
return new RGBColorImpl(r, g, b);
|
|
952
|
+
}
|
|
953
|
+
if (hex.length === 4) {
|
|
954
|
+
const r = parseInt(hex[0] + hex[0], 16);
|
|
955
|
+
const g = parseInt(hex[1] + hex[1], 16);
|
|
956
|
+
const b = parseInt(hex[2] + hex[2], 16);
|
|
957
|
+
const a = parseInt(hex[3] + hex[3], 16) / 255;
|
|
958
|
+
return new RGBColorImpl(r, g, b, a);
|
|
959
|
+
}
|
|
960
|
+
if (hex.length === 6) {
|
|
961
|
+
const r = parseInt(hex.slice(0, 2), 16);
|
|
962
|
+
const g = parseInt(hex.slice(2, 4), 16);
|
|
963
|
+
const b = parseInt(hex.slice(4, 6), 16);
|
|
964
|
+
return new RGBColorImpl(r, g, b);
|
|
965
|
+
}
|
|
966
|
+
if (hex.length === 8) {
|
|
967
|
+
const r = parseInt(hex.slice(0, 2), 16);
|
|
968
|
+
const g = parseInt(hex.slice(2, 4), 16);
|
|
969
|
+
const b = parseInt(hex.slice(4, 6), 16);
|
|
970
|
+
const a = parseInt(hex.slice(6, 8), 16) / 255;
|
|
971
|
+
return new RGBColorImpl(r, g, b, a);
|
|
972
|
+
}
|
|
973
|
+
throw new Error(`Invalid hex color: ${value}`);
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
// RGB/RGBA format
|
|
977
|
+
const rgbMatch = trimmed.match(
|
|
978
|
+
/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+))?\s*\)$/,
|
|
979
|
+
);
|
|
980
|
+
if (rgbMatch) {
|
|
981
|
+
const r = parseInt(rgbMatch[1], 10);
|
|
982
|
+
const g = parseInt(rgbMatch[2], 10);
|
|
983
|
+
const b = parseInt(rgbMatch[3], 10);
|
|
984
|
+
const a = rgbMatch[4] !== undefined ? parseFloat(rgbMatch[4]) : 1;
|
|
985
|
+
return new RGBColorImpl(r, g, b, a);
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
// HSL/HSLA format
|
|
989
|
+
const hslMatch = trimmed.match(
|
|
990
|
+
/^hsla?\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*(?:,\s*([\d.]+))?\s*\)$/,
|
|
991
|
+
);
|
|
992
|
+
if (hslMatch) {
|
|
993
|
+
const h = parseInt(hslMatch[1], 10);
|
|
994
|
+
const s = parseInt(hslMatch[2], 10);
|
|
995
|
+
const l = parseInt(hslMatch[3], 10);
|
|
996
|
+
const a = hslMatch[4] !== undefined ? parseFloat(hslMatch[4]) : 1;
|
|
997
|
+
return new HSLColorImpl(h, s, l, a);
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
// HSB/HSBA format
|
|
1001
|
+
const hsbMatch = trimmed.match(
|
|
1002
|
+
/^hsba?\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*(?:,\s*([\d.]+))?\s*\)$/,
|
|
1003
|
+
);
|
|
1004
|
+
if (hsbMatch) {
|
|
1005
|
+
const h = parseInt(hsbMatch[1], 10);
|
|
1006
|
+
const s = parseInt(hsbMatch[2], 10);
|
|
1007
|
+
const b = parseInt(hsbMatch[3], 10);
|
|
1008
|
+
const a = hsbMatch[4] !== undefined ? parseFloat(hsbMatch[4]) : 1;
|
|
1009
|
+
return new HSBColorImpl(h, s, b, a);
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
throw new Error(`Invalid color format: ${value}`);
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
/**
|
|
1016
|
+
* Create an RGB color.
|
|
1017
|
+
*/
|
|
1018
|
+
export function createRGBColor(red: number, green: number, blue: number, alpha: number = 1): Color {
|
|
1019
|
+
return new RGBColorImpl(red, green, blue, alpha);
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
/**
|
|
1023
|
+
* Create an HSL color.
|
|
1024
|
+
*/
|
|
1025
|
+
export function createHSLColor(
|
|
1026
|
+
hue: number,
|
|
1027
|
+
saturation: number,
|
|
1028
|
+
lightness: number,
|
|
1029
|
+
alpha: number = 1,
|
|
1030
|
+
): Color {
|
|
1031
|
+
return new HSLColorImpl(hue, saturation, lightness, alpha);
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
/**
|
|
1035
|
+
* Create an HSB color.
|
|
1036
|
+
*/
|
|
1037
|
+
export function createHSBColor(
|
|
1038
|
+
hue: number,
|
|
1039
|
+
saturation: number,
|
|
1040
|
+
brightness: number,
|
|
1041
|
+
alpha: number = 1,
|
|
1042
|
+
): Color {
|
|
1043
|
+
return new HSBColorImpl(hue, saturation, brightness, alpha);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
/**
|
|
1047
|
+
* Normalize a color value (string or Color) to a Color object.
|
|
1048
|
+
*/
|
|
1049
|
+
export function normalizeColor(value: string | Color): Color {
|
|
1050
|
+
if (typeof value === "string") {
|
|
1051
|
+
return parseColor(value);
|
|
1052
|
+
}
|
|
1053
|
+
return value;
|
|
1054
|
+
}
|