@zag-js/color-picker 0.10.4 → 0.11.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.
Files changed (38) hide show
  1. package/dist/index.d.mts +150 -0
  2. package/dist/index.d.ts +150 -4
  3. package/dist/index.js +1086 -8
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.mjs +1069 -3
  6. package/dist/index.mjs.map +1 -0
  7. package/package.json +11 -11
  8. package/dist/color-picker.anatomy.d.ts +0 -3
  9. package/dist/color-picker.anatomy.js +0 -24
  10. package/dist/color-picker.anatomy.mjs +0 -19
  11. package/dist/color-picker.connect.d.ts +0 -44
  12. package/dist/color-picker.connect.js +0 -372
  13. package/dist/color-picker.connect.mjs +0 -368
  14. package/dist/color-picker.dom.d.ts +0 -46
  15. package/dist/color-picker.dom.js +0 -37
  16. package/dist/color-picker.dom.mjs +0 -33
  17. package/dist/color-picker.machine.d.ts +0 -3
  18. package/dist/color-picker.machine.js +0 -340
  19. package/dist/color-picker.machine.mjs +0 -336
  20. package/dist/color-picker.types.d.ts +0 -99
  21. package/dist/utils/generate-format-background.d.ts +0 -66
  22. package/dist/utils/generate-format-background.js +0 -133
  23. package/dist/utils/generate-format-background.mjs +0 -121
  24. package/dist/utils/get-channel-details.d.ts +0 -19
  25. package/dist/utils/get-channel-details.js +0 -57
  26. package/dist/utils/get-channel-details.mjs +0 -53
  27. package/dist/utils/get-channel-display-color.d.ts +0 -3
  28. package/dist/utils/get-channel-display-color.js +0 -26
  29. package/dist/utils/get-channel-display-color.mjs +0 -22
  30. package/dist/utils/get-channel-input-value.d.ts +0 -5
  31. package/dist/utils/get-channel-input-value.js +0 -26
  32. package/dist/utils/get-channel-input-value.mjs +0 -21
  33. package/dist/utils/get-color-area-gradient.d.ts +0 -7
  34. package/dist/utils/get-color-area-gradient.js +0 -65
  35. package/dist/utils/get-color-area-gradient.mjs +0 -61
  36. package/dist/utils/get-slider-background.d.ts +0 -2
  37. package/dist/utils/get-slider-background.js +0 -43
  38. package/dist/utils/get-slider-background.mjs +0 -39
@@ -1,340 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const colorUtils = require('@zag-js/color-utils');
6
- const core = require('@zag-js/core');
7
- const domEvent = require('@zag-js/dom-event');
8
- const domQuery = require('@zag-js/dom-query');
9
- const numericRange = require('@zag-js/numeric-range');
10
- const textSelection = require('@zag-js/text-selection');
11
- const utils = require('@zag-js/utils');
12
- const colorPicker_dom = require('./color-picker.dom.js');
13
- const getChannelDetails = require('./utils/get-channel-details.js');
14
- const getChannelInputValue = require('./utils/get-channel-input-value.js');
15
-
16
- function machine(userContext) {
17
- const ctx = utils.compact(userContext);
18
- return core.createMachine(
19
- {
20
- id: "color-picker",
21
- initial: "idle",
22
- context: {
23
- dir: "ltr",
24
- activeId: null,
25
- activeChannel: null,
26
- activeOrientation: null,
27
- value: "#D9D9D9",
28
- ...ctx,
29
- valueAsColor: colorUtils.parseColor(ctx.value || "#D9D9D9")
30
- },
31
- computed: {
32
- isRtl: (ctx2) => ctx2.dir === "rtl",
33
- isInteractive: (ctx2) => !(ctx2.disabled || ctx2.readOnly)
34
- },
35
- on: {
36
- "VALUE.SET": {
37
- actions: ["setValue"]
38
- }
39
- },
40
- created: ["setValueAsColor"],
41
- watch: {
42
- value: ["setValueAsColor", "syncChannelInputs", "invokeOnChange"]
43
- },
44
- states: {
45
- idle: {
46
- on: {
47
- "EYEDROPPER.CLICK": {
48
- actions: ["openEyeDropper"]
49
- },
50
- "AREA.POINTER_DOWN": {
51
- target: "dragging",
52
- actions: ["setActiveChannel", "setAreaColorFromPoint", "focusAreaThumb"]
53
- },
54
- "CHANNEL_SLIDER.POINTER_DOWN": {
55
- target: "dragging",
56
- actions: ["setActiveChannel", "setChannelColorFromPoint", "focusChannelThumb"]
57
- },
58
- "CHANNEL_INPUT.FOCUS": {
59
- target: "focused",
60
- actions: ["setActiveChannel"]
61
- },
62
- "CHANNEL_INPUT.CHANGE": {
63
- actions: ["setChannelColorFromInput"]
64
- }
65
- }
66
- },
67
- focused: {
68
- on: {
69
- "AREA.POINTER_DOWN": {
70
- target: "dragging",
71
- actions: ["setActiveChannel", "setAreaColorFromPoint", "focusAreaThumb"]
72
- },
73
- "CHANNEL_SLIDER.POINTER_DOWN": {
74
- target: "dragging",
75
- actions: ["setActiveChannel", "setChannelColorFromPoint", "focusChannelThumb"]
76
- },
77
- "AREA.ARROW_LEFT": {
78
- actions: ["decrementXChannel"]
79
- },
80
- "AREA.ARROW_RIGHT": {
81
- actions: ["incrementXChannel"]
82
- },
83
- "AREA.ARROW_UP": {
84
- actions: ["incrementYChannel"]
85
- },
86
- "AREA.ARROW_DOWN": {
87
- actions: ["decrementYChannel"]
88
- },
89
- "AREA.PAGE_UP": {
90
- actions: ["incrementXChannel"]
91
- },
92
- "AREA.PAGE_DOWN": {
93
- actions: ["decrementXChannel"]
94
- },
95
- "CHANNEL_SLIDER.ARROW_LEFT": {
96
- actions: ["decrementChannel"]
97
- },
98
- "CHANNEL_SLIDER.ARROW_RIGHT": {
99
- actions: ["incrementChannel"]
100
- },
101
- "CHANNEL_SLIDER.ARROW_UP": {
102
- actions: ["incrementChannel"]
103
- },
104
- "CHANNEL_SLIDER.ARROW_DOWN": {
105
- actions: ["decrementChannel"]
106
- },
107
- "CHANNEL_SLIDER.PAGE_UP": {
108
- actions: ["incrementChannel"]
109
- },
110
- "CHANNEL_SLIDER.PAGE_DOWN": {
111
- actions: ["decrementChannel"]
112
- },
113
- "CHANNEL_SLIDER.HOME": {
114
- actions: ["setChannelToMin"]
115
- },
116
- "CHANNEL_SLIDER.END": {
117
- actions: ["setChannelToMax"]
118
- },
119
- "CHANNEL_INPUT.FOCUS": {
120
- actions: ["setActiveChannel"]
121
- },
122
- "CHANNEL_INPUT.CHANGE": {
123
- actions: ["setChannelColorFromInput"]
124
- },
125
- "CHANNEL_INPUT.BLUR": [
126
- {
127
- guard: "isTextField",
128
- target: "idle",
129
- actions: ["setChannelColorFromInput"]
130
- },
131
- { target: "idle" }
132
- ],
133
- "CHANNEL_SLIDER.BLUR": {
134
- target: "idle"
135
- },
136
- "AREA.BLUR": {
137
- target: "idle"
138
- }
139
- }
140
- },
141
- dragging: {
142
- exit: ["clearActiveChannel"],
143
- activities: ["trackPointerMove", "disableTextSelection"],
144
- on: {
145
- "AREA.POINTER_MOVE": {
146
- actions: ["setAreaColorFromPoint"]
147
- },
148
- "AREA.POINTER_UP": {
149
- target: "focused",
150
- actions: ["invokeOnChangeEnd"]
151
- },
152
- "CHANNEL_SLIDER.POINTER_MOVE": {
153
- actions: ["setChannelColorFromPoint"]
154
- },
155
- "CHANNEL_SLIDER.POINTER_UP": {
156
- target: "focused",
157
- actions: ["invokeOnChangeEnd"]
158
- }
159
- }
160
- }
161
- }
162
- },
163
- {
164
- guards: {
165
- isTextField: (_ctx, evt) => !!evt.isTextField
166
- },
167
- activities: {
168
- trackPointerMove(ctx2, _evt, { send }) {
169
- return domEvent.trackPointerMove(colorPicker_dom.dom.getDoc(ctx2), {
170
- onPointerMove({ point }) {
171
- const type = ctx2.activeId === "area" ? "AREA.POINTER_MOVE" : "CHANNEL_SLIDER.POINTER_MOVE";
172
- send({ type, point });
173
- },
174
- onPointerUp() {
175
- const type = ctx2.activeId === "area" ? "AREA.POINTER_UP" : "CHANNEL_SLIDER.POINTER_UP";
176
- send({ type });
177
- }
178
- });
179
- },
180
- disableTextSelection(ctx2) {
181
- return textSelection.disableTextSelection({ doc: colorPicker_dom.dom.getDoc(ctx2), target: colorPicker_dom.dom.getContentEl(ctx2) });
182
- }
183
- },
184
- actions: {
185
- openEyeDropper(ctx2) {
186
- const isSupported = "EyeDropper" in colorPicker_dom.dom.getWin(ctx2);
187
- if (!isSupported)
188
- return;
189
- const win = colorPicker_dom.dom.getWin(ctx2);
190
- const picker = new win.EyeDropper();
191
- picker.open().then(({ sRGBHex }) => {
192
- const format = ctx2.valueAsColor.getColorSpace();
193
- const color = colorUtils.parseColor(sRGBHex).toFormat(format);
194
- setColor(ctx2, color);
195
- ctx2.onChangeEnd?.({ value: ctx2.value, valueAsColor: color });
196
- }).catch(() => void 0);
197
- },
198
- setActiveChannel(ctx2, evt) {
199
- ctx2.activeId = evt.id;
200
- if (evt.channel) {
201
- ctx2.activeChannel = evt.channel;
202
- }
203
- if (evt.orientation) {
204
- ctx2.activeOrientation = evt.orientation;
205
- }
206
- },
207
- clearActiveChannel(ctx2) {
208
- ctx2.activeChannel = null;
209
- ctx2.activeId = null;
210
- ctx2.activeOrientation = null;
211
- },
212
- setAreaColorFromPoint(ctx2, evt) {
213
- const { xChannel, yChannel } = evt.channel || ctx2.activeChannel;
214
- const percent = colorPicker_dom.dom.getAreaValueFromPoint(ctx2, evt.point);
215
- const { getColorFromPoint } = getChannelDetails.getChannelDetails(ctx2.valueAsColor, xChannel, yChannel);
216
- const color = getColorFromPoint(percent.x, percent.y);
217
- if (!color)
218
- return;
219
- setColor(ctx2, color);
220
- },
221
- setChannelColorFromPoint(ctx2, evt) {
222
- const channel = evt.channel || ctx2.activeId;
223
- const percent = colorPicker_dom.dom.getChannelSliderValueFromPoint(ctx2, evt.point, channel);
224
- const { minValue, maxValue, step } = ctx2.valueAsColor.getChannelRange(channel);
225
- const orientation = ctx2.activeOrientation || "horizontal";
226
- const point = orientation === "horizontal" ? percent.x : percent.y;
227
- const channelValue = numericRange.getPercentValue(point, minValue, maxValue, step);
228
- const value = numericRange.snapValueToStep(channelValue - step, minValue, maxValue, step);
229
- const newColor = ctx2.valueAsColor.withChannelValue(channel, value);
230
- setColor(ctx2, newColor);
231
- },
232
- setValue(ctx2, evt) {
233
- setColor(ctx2, evt.value);
234
- },
235
- setValueAsColor(ctx2) {
236
- try {
237
- const color = colorUtils.parseColor(ctx2.value);
238
- if (color.isEqual(ctx2.valueAsColor))
239
- return;
240
- ctx2.valueAsColor = color;
241
- } catch {
242
- }
243
- },
244
- syncChannelInputs(ctx2) {
245
- const inputs = colorPicker_dom.dom.getChannelInputEls(ctx2);
246
- inputs.forEach((input) => {
247
- const channel = input.getAttribute("data-channel");
248
- if (!channel)
249
- return;
250
- const value = getChannelInputValue.getChannelInputValue(ctx2.valueAsColor, channel);
251
- input.value = value.toString();
252
- });
253
- },
254
- setChannelColorFromInput(ctx2, evt) {
255
- const { channel, isTextField, value } = evt;
256
- try {
257
- const format = ctx2.valueAsColor.getColorSpace();
258
- const newColor = isTextField ? colorUtils.parseColor(value).toFormat(format) : ctx2.valueAsColor.withChannelValue(channel, value);
259
- setColor(ctx2, newColor);
260
- } catch {
261
- const input = colorPicker_dom.dom.getChannelInputEl(ctx2, channel);
262
- if (!input)
263
- return;
264
- input.value = getChannelInputValue.getChannelInputValue(ctx2.valueAsColor, channel);
265
- }
266
- },
267
- incrementChannel(ctx2, evt) {
268
- const { minValue, maxValue, step } = ctx2.valueAsColor.getChannelRange(evt.channel);
269
- const channelValue = ctx2.valueAsColor.getChannelValue(evt.channel);
270
- const value = numericRange.snapValueToStep(channelValue + evt.step, minValue, maxValue, step);
271
- const newColor = ctx2.valueAsColor.withChannelValue(evt.channel, numericRange.clampValue(value, minValue, maxValue));
272
- setColor(ctx2, newColor);
273
- },
274
- decrementChannel(ctx2, evt) {
275
- const { minValue, maxValue, step } = ctx2.valueAsColor.getChannelRange(evt.channel);
276
- const channelValue = ctx2.valueAsColor.getChannelValue(evt.channel);
277
- const value = numericRange.snapValueToStep(channelValue - evt.step, minValue, maxValue, step);
278
- const newColor = ctx2.valueAsColor.withChannelValue(evt.channel, numericRange.clampValue(value, minValue, maxValue));
279
- setColor(ctx2, newColor);
280
- },
281
- incrementXChannel(ctx2, evt) {
282
- const { xChannel, yChannel } = evt.channel;
283
- const { incrementX } = getChannelDetails.getChannelDetails(ctx2.valueAsColor, xChannel, yChannel);
284
- const newColor = ctx2.valueAsColor.withChannelValue(xChannel, incrementX(evt.step));
285
- setColor(ctx2, newColor);
286
- },
287
- decrementXChannel(ctx2, evt) {
288
- const { xChannel, yChannel } = evt.channel;
289
- const { decrementX } = getChannelDetails.getChannelDetails(ctx2.valueAsColor, xChannel, yChannel);
290
- const newColor = ctx2.valueAsColor.withChannelValue(xChannel, decrementX(evt.step));
291
- setColor(ctx2, newColor);
292
- },
293
- incrementYChannel(ctx2, evt) {
294
- const { xChannel, yChannel } = evt.channel;
295
- const { incrementY } = getChannelDetails.getChannelDetails(ctx2.valueAsColor, xChannel, yChannel);
296
- const newColor = ctx2.valueAsColor.withChannelValue(yChannel, incrementY(evt.step));
297
- setColor(ctx2, newColor);
298
- },
299
- decrementYChannel(ctx2, evt) {
300
- const { xChannel, yChannel } = evt.channel;
301
- const { decrementY } = getChannelDetails.getChannelDetails(ctx2.valueAsColor, xChannel, yChannel);
302
- const newColor = ctx2.valueAsColor.withChannelValue(yChannel, decrementY(evt.step));
303
- setColor(ctx2, newColor);
304
- },
305
- setChannelToMax(ctx2, evt) {
306
- const { maxValue } = ctx2.valueAsColor.getChannelRange(evt.channel);
307
- const newColor = ctx2.valueAsColor.withChannelValue(evt.channel, maxValue);
308
- setColor(ctx2, newColor);
309
- },
310
- setChannelToMin(ctx2, evt) {
311
- const { minValue } = ctx2.valueAsColor.getChannelRange(evt.channel);
312
- const newColor = ctx2.valueAsColor.withChannelValue(evt.channel, minValue);
313
- setColor(ctx2, newColor);
314
- },
315
- invokeOnChangeEnd(ctx2) {
316
- ctx2.onChangeEnd?.({ value: ctx2.value, valueAsColor: ctx2.valueAsColor });
317
- },
318
- invokeOnChange(ctx2) {
319
- ctx2.onChange?.({ value: ctx2.value, valueAsColor: ctx2.valueAsColor });
320
- },
321
- focusAreaThumb(ctx2) {
322
- domQuery.raf(() => {
323
- colorPicker_dom.dom.getAreaThumbEl(ctx2)?.focus({ preventScroll: true });
324
- });
325
- },
326
- focusChannelThumb(ctx2, evt) {
327
- domQuery.raf(() => {
328
- colorPicker_dom.dom.getChannelSliderThumbEl(ctx2, evt.channel)?.focus({ preventScroll: true });
329
- });
330
- }
331
- }
332
- }
333
- );
334
- }
335
- const setColor = (ctx, color) => {
336
- ctx.value = color.toString("css");
337
- ctx.valueAsColor = color;
338
- };
339
-
340
- exports.machine = machine;
@@ -1,336 +0,0 @@
1
- import { parseColor } from '@zag-js/color-utils';
2
- import { createMachine } from '@zag-js/core';
3
- import { trackPointerMove } from '@zag-js/dom-event';
4
- import { raf } from '@zag-js/dom-query';
5
- import { getPercentValue, snapValueToStep, clampValue } from '@zag-js/numeric-range';
6
- import { disableTextSelection } from '@zag-js/text-selection';
7
- import { compact } from '@zag-js/utils';
8
- import { dom } from './color-picker.dom.mjs';
9
- import { getChannelDetails } from './utils/get-channel-details.mjs';
10
- import { getChannelInputValue } from './utils/get-channel-input-value.mjs';
11
-
12
- function machine(userContext) {
13
- const ctx = compact(userContext);
14
- return createMachine(
15
- {
16
- id: "color-picker",
17
- initial: "idle",
18
- context: {
19
- dir: "ltr",
20
- activeId: null,
21
- activeChannel: null,
22
- activeOrientation: null,
23
- value: "#D9D9D9",
24
- ...ctx,
25
- valueAsColor: parseColor(ctx.value || "#D9D9D9")
26
- },
27
- computed: {
28
- isRtl: (ctx2) => ctx2.dir === "rtl",
29
- isInteractive: (ctx2) => !(ctx2.disabled || ctx2.readOnly)
30
- },
31
- on: {
32
- "VALUE.SET": {
33
- actions: ["setValue"]
34
- }
35
- },
36
- created: ["setValueAsColor"],
37
- watch: {
38
- value: ["setValueAsColor", "syncChannelInputs", "invokeOnChange"]
39
- },
40
- states: {
41
- idle: {
42
- on: {
43
- "EYEDROPPER.CLICK": {
44
- actions: ["openEyeDropper"]
45
- },
46
- "AREA.POINTER_DOWN": {
47
- target: "dragging",
48
- actions: ["setActiveChannel", "setAreaColorFromPoint", "focusAreaThumb"]
49
- },
50
- "CHANNEL_SLIDER.POINTER_DOWN": {
51
- target: "dragging",
52
- actions: ["setActiveChannel", "setChannelColorFromPoint", "focusChannelThumb"]
53
- },
54
- "CHANNEL_INPUT.FOCUS": {
55
- target: "focused",
56
- actions: ["setActiveChannel"]
57
- },
58
- "CHANNEL_INPUT.CHANGE": {
59
- actions: ["setChannelColorFromInput"]
60
- }
61
- }
62
- },
63
- focused: {
64
- on: {
65
- "AREA.POINTER_DOWN": {
66
- target: "dragging",
67
- actions: ["setActiveChannel", "setAreaColorFromPoint", "focusAreaThumb"]
68
- },
69
- "CHANNEL_SLIDER.POINTER_DOWN": {
70
- target: "dragging",
71
- actions: ["setActiveChannel", "setChannelColorFromPoint", "focusChannelThumb"]
72
- },
73
- "AREA.ARROW_LEFT": {
74
- actions: ["decrementXChannel"]
75
- },
76
- "AREA.ARROW_RIGHT": {
77
- actions: ["incrementXChannel"]
78
- },
79
- "AREA.ARROW_UP": {
80
- actions: ["incrementYChannel"]
81
- },
82
- "AREA.ARROW_DOWN": {
83
- actions: ["decrementYChannel"]
84
- },
85
- "AREA.PAGE_UP": {
86
- actions: ["incrementXChannel"]
87
- },
88
- "AREA.PAGE_DOWN": {
89
- actions: ["decrementXChannel"]
90
- },
91
- "CHANNEL_SLIDER.ARROW_LEFT": {
92
- actions: ["decrementChannel"]
93
- },
94
- "CHANNEL_SLIDER.ARROW_RIGHT": {
95
- actions: ["incrementChannel"]
96
- },
97
- "CHANNEL_SLIDER.ARROW_UP": {
98
- actions: ["incrementChannel"]
99
- },
100
- "CHANNEL_SLIDER.ARROW_DOWN": {
101
- actions: ["decrementChannel"]
102
- },
103
- "CHANNEL_SLIDER.PAGE_UP": {
104
- actions: ["incrementChannel"]
105
- },
106
- "CHANNEL_SLIDER.PAGE_DOWN": {
107
- actions: ["decrementChannel"]
108
- },
109
- "CHANNEL_SLIDER.HOME": {
110
- actions: ["setChannelToMin"]
111
- },
112
- "CHANNEL_SLIDER.END": {
113
- actions: ["setChannelToMax"]
114
- },
115
- "CHANNEL_INPUT.FOCUS": {
116
- actions: ["setActiveChannel"]
117
- },
118
- "CHANNEL_INPUT.CHANGE": {
119
- actions: ["setChannelColorFromInput"]
120
- },
121
- "CHANNEL_INPUT.BLUR": [
122
- {
123
- guard: "isTextField",
124
- target: "idle",
125
- actions: ["setChannelColorFromInput"]
126
- },
127
- { target: "idle" }
128
- ],
129
- "CHANNEL_SLIDER.BLUR": {
130
- target: "idle"
131
- },
132
- "AREA.BLUR": {
133
- target: "idle"
134
- }
135
- }
136
- },
137
- dragging: {
138
- exit: ["clearActiveChannel"],
139
- activities: ["trackPointerMove", "disableTextSelection"],
140
- on: {
141
- "AREA.POINTER_MOVE": {
142
- actions: ["setAreaColorFromPoint"]
143
- },
144
- "AREA.POINTER_UP": {
145
- target: "focused",
146
- actions: ["invokeOnChangeEnd"]
147
- },
148
- "CHANNEL_SLIDER.POINTER_MOVE": {
149
- actions: ["setChannelColorFromPoint"]
150
- },
151
- "CHANNEL_SLIDER.POINTER_UP": {
152
- target: "focused",
153
- actions: ["invokeOnChangeEnd"]
154
- }
155
- }
156
- }
157
- }
158
- },
159
- {
160
- guards: {
161
- isTextField: (_ctx, evt) => !!evt.isTextField
162
- },
163
- activities: {
164
- trackPointerMove(ctx2, _evt, { send }) {
165
- return trackPointerMove(dom.getDoc(ctx2), {
166
- onPointerMove({ point }) {
167
- const type = ctx2.activeId === "area" ? "AREA.POINTER_MOVE" : "CHANNEL_SLIDER.POINTER_MOVE";
168
- send({ type, point });
169
- },
170
- onPointerUp() {
171
- const type = ctx2.activeId === "area" ? "AREA.POINTER_UP" : "CHANNEL_SLIDER.POINTER_UP";
172
- send({ type });
173
- }
174
- });
175
- },
176
- disableTextSelection(ctx2) {
177
- return disableTextSelection({ doc: dom.getDoc(ctx2), target: dom.getContentEl(ctx2) });
178
- }
179
- },
180
- actions: {
181
- openEyeDropper(ctx2) {
182
- const isSupported = "EyeDropper" in dom.getWin(ctx2);
183
- if (!isSupported)
184
- return;
185
- const win = dom.getWin(ctx2);
186
- const picker = new win.EyeDropper();
187
- picker.open().then(({ sRGBHex }) => {
188
- const format = ctx2.valueAsColor.getColorSpace();
189
- const color = parseColor(sRGBHex).toFormat(format);
190
- setColor(ctx2, color);
191
- ctx2.onChangeEnd?.({ value: ctx2.value, valueAsColor: color });
192
- }).catch(() => void 0);
193
- },
194
- setActiveChannel(ctx2, evt) {
195
- ctx2.activeId = evt.id;
196
- if (evt.channel) {
197
- ctx2.activeChannel = evt.channel;
198
- }
199
- if (evt.orientation) {
200
- ctx2.activeOrientation = evt.orientation;
201
- }
202
- },
203
- clearActiveChannel(ctx2) {
204
- ctx2.activeChannel = null;
205
- ctx2.activeId = null;
206
- ctx2.activeOrientation = null;
207
- },
208
- setAreaColorFromPoint(ctx2, evt) {
209
- const { xChannel, yChannel } = evt.channel || ctx2.activeChannel;
210
- const percent = dom.getAreaValueFromPoint(ctx2, evt.point);
211
- const { getColorFromPoint } = getChannelDetails(ctx2.valueAsColor, xChannel, yChannel);
212
- const color = getColorFromPoint(percent.x, percent.y);
213
- if (!color)
214
- return;
215
- setColor(ctx2, color);
216
- },
217
- setChannelColorFromPoint(ctx2, evt) {
218
- const channel = evt.channel || ctx2.activeId;
219
- const percent = dom.getChannelSliderValueFromPoint(ctx2, evt.point, channel);
220
- const { minValue, maxValue, step } = ctx2.valueAsColor.getChannelRange(channel);
221
- const orientation = ctx2.activeOrientation || "horizontal";
222
- const point = orientation === "horizontal" ? percent.x : percent.y;
223
- const channelValue = getPercentValue(point, minValue, maxValue, step);
224
- const value = snapValueToStep(channelValue - step, minValue, maxValue, step);
225
- const newColor = ctx2.valueAsColor.withChannelValue(channel, value);
226
- setColor(ctx2, newColor);
227
- },
228
- setValue(ctx2, evt) {
229
- setColor(ctx2, evt.value);
230
- },
231
- setValueAsColor(ctx2) {
232
- try {
233
- const color = parseColor(ctx2.value);
234
- if (color.isEqual(ctx2.valueAsColor))
235
- return;
236
- ctx2.valueAsColor = color;
237
- } catch {
238
- }
239
- },
240
- syncChannelInputs(ctx2) {
241
- const inputs = dom.getChannelInputEls(ctx2);
242
- inputs.forEach((input) => {
243
- const channel = input.getAttribute("data-channel");
244
- if (!channel)
245
- return;
246
- const value = getChannelInputValue(ctx2.valueAsColor, channel);
247
- input.value = value.toString();
248
- });
249
- },
250
- setChannelColorFromInput(ctx2, evt) {
251
- const { channel, isTextField, value } = evt;
252
- try {
253
- const format = ctx2.valueAsColor.getColorSpace();
254
- const newColor = isTextField ? parseColor(value).toFormat(format) : ctx2.valueAsColor.withChannelValue(channel, value);
255
- setColor(ctx2, newColor);
256
- } catch {
257
- const input = dom.getChannelInputEl(ctx2, channel);
258
- if (!input)
259
- return;
260
- input.value = getChannelInputValue(ctx2.valueAsColor, channel);
261
- }
262
- },
263
- incrementChannel(ctx2, evt) {
264
- const { minValue, maxValue, step } = ctx2.valueAsColor.getChannelRange(evt.channel);
265
- const channelValue = ctx2.valueAsColor.getChannelValue(evt.channel);
266
- const value = snapValueToStep(channelValue + evt.step, minValue, maxValue, step);
267
- const newColor = ctx2.valueAsColor.withChannelValue(evt.channel, clampValue(value, minValue, maxValue));
268
- setColor(ctx2, newColor);
269
- },
270
- decrementChannel(ctx2, evt) {
271
- const { minValue, maxValue, step } = ctx2.valueAsColor.getChannelRange(evt.channel);
272
- const channelValue = ctx2.valueAsColor.getChannelValue(evt.channel);
273
- const value = snapValueToStep(channelValue - evt.step, minValue, maxValue, step);
274
- const newColor = ctx2.valueAsColor.withChannelValue(evt.channel, clampValue(value, minValue, maxValue));
275
- setColor(ctx2, newColor);
276
- },
277
- incrementXChannel(ctx2, evt) {
278
- const { xChannel, yChannel } = evt.channel;
279
- const { incrementX } = getChannelDetails(ctx2.valueAsColor, xChannel, yChannel);
280
- const newColor = ctx2.valueAsColor.withChannelValue(xChannel, incrementX(evt.step));
281
- setColor(ctx2, newColor);
282
- },
283
- decrementXChannel(ctx2, evt) {
284
- const { xChannel, yChannel } = evt.channel;
285
- const { decrementX } = getChannelDetails(ctx2.valueAsColor, xChannel, yChannel);
286
- const newColor = ctx2.valueAsColor.withChannelValue(xChannel, decrementX(evt.step));
287
- setColor(ctx2, newColor);
288
- },
289
- incrementYChannel(ctx2, evt) {
290
- const { xChannel, yChannel } = evt.channel;
291
- const { incrementY } = getChannelDetails(ctx2.valueAsColor, xChannel, yChannel);
292
- const newColor = ctx2.valueAsColor.withChannelValue(yChannel, incrementY(evt.step));
293
- setColor(ctx2, newColor);
294
- },
295
- decrementYChannel(ctx2, evt) {
296
- const { xChannel, yChannel } = evt.channel;
297
- const { decrementY } = getChannelDetails(ctx2.valueAsColor, xChannel, yChannel);
298
- const newColor = ctx2.valueAsColor.withChannelValue(yChannel, decrementY(evt.step));
299
- setColor(ctx2, newColor);
300
- },
301
- setChannelToMax(ctx2, evt) {
302
- const { maxValue } = ctx2.valueAsColor.getChannelRange(evt.channel);
303
- const newColor = ctx2.valueAsColor.withChannelValue(evt.channel, maxValue);
304
- setColor(ctx2, newColor);
305
- },
306
- setChannelToMin(ctx2, evt) {
307
- const { minValue } = ctx2.valueAsColor.getChannelRange(evt.channel);
308
- const newColor = ctx2.valueAsColor.withChannelValue(evt.channel, minValue);
309
- setColor(ctx2, newColor);
310
- },
311
- invokeOnChangeEnd(ctx2) {
312
- ctx2.onChangeEnd?.({ value: ctx2.value, valueAsColor: ctx2.valueAsColor });
313
- },
314
- invokeOnChange(ctx2) {
315
- ctx2.onChange?.({ value: ctx2.value, valueAsColor: ctx2.valueAsColor });
316
- },
317
- focusAreaThumb(ctx2) {
318
- raf(() => {
319
- dom.getAreaThumbEl(ctx2)?.focus({ preventScroll: true });
320
- });
321
- },
322
- focusChannelThumb(ctx2, evt) {
323
- raf(() => {
324
- dom.getChannelSliderThumbEl(ctx2, evt.channel)?.focus({ preventScroll: true });
325
- });
326
- }
327
- }
328
- }
329
- );
330
- }
331
- const setColor = (ctx, color) => {
332
- ctx.value = color.toString("css");
333
- ctx.valueAsColor = color;
334
- };
335
-
336
- export { machine };