@zag-js/color-picker 0.0.0-dev-20230414151834

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 (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -0
  3. package/dist/chunk-2ERX54SV.mjs +25 -0
  4. package/dist/chunk-3XH465XT.mjs +42 -0
  5. package/dist/chunk-75JXJMB5.mjs +22 -0
  6. package/dist/chunk-ACSV4TXB.mjs +35 -0
  7. package/dist/chunk-J3XCPS5T.mjs +365 -0
  8. package/dist/chunk-JNBNPPMF.mjs +24 -0
  9. package/dist/chunk-KLLIOTK6.mjs +55 -0
  10. package/dist/chunk-KO4TBUXB.mjs +74 -0
  11. package/dist/chunk-OY3B7NXA.mjs +132 -0
  12. package/dist/chunk-ZBNUATFY.mjs +344 -0
  13. package/dist/color-picker.anatomy.d.ts +6 -0
  14. package/dist/color-picker.anatomy.js +47 -0
  15. package/dist/color-picker.anatomy.mjs +8 -0
  16. package/dist/color-picker.connect.d.ts +27 -0
  17. package/dist/color-picker.connect.js +731 -0
  18. package/dist/color-picker.connect.mjs +14 -0
  19. package/dist/color-picker.dom.d.ts +62 -0
  20. package/dist/color-picker.dom.js +59 -0
  21. package/dist/color-picker.dom.mjs +6 -0
  22. package/dist/color-picker.machine.d.ts +8 -0
  23. package/dist/color-picker.machine.js +456 -0
  24. package/dist/color-picker.machine.mjs +9 -0
  25. package/dist/color-picker.types.d.ts +88 -0
  26. package/dist/color-picker.types.js +18 -0
  27. package/dist/color-picker.types.mjs +0 -0
  28. package/dist/index.d.ts +6 -0
  29. package/dist/index.js +1066 -0
  30. package/dist/index.mjs +18 -0
  31. package/dist/utils/generate-format-background.d.ts +68 -0
  32. package/dist/utils/generate-format-background.js +164 -0
  33. package/dist/utils/generate-format-background.mjs +22 -0
  34. package/dist/utils/get-channel-details.d.ts +23 -0
  35. package/dist/utils/get-channel-details.js +79 -0
  36. package/dist/utils/get-channel-details.mjs +6 -0
  37. package/dist/utils/get-channel-display-color.d.ts +5 -0
  38. package/dist/utils/get-channel-display-color.js +48 -0
  39. package/dist/utils/get-channel-display-color.mjs +6 -0
  40. package/dist/utils/get-channel-input-value.d.ts +10 -0
  41. package/dist/utils/get-channel-input-value.js +50 -0
  42. package/dist/utils/get-channel-input-value.mjs +8 -0
  43. package/dist/utils/get-color-area-gradient.d.ts +11 -0
  44. package/dist/utils/get-color-area-gradient.js +209 -0
  45. package/dist/utils/get-color-area-gradient.mjs +7 -0
  46. package/dist/utils/get-slider-background.d.ts +8 -0
  47. package/dist/utils/get-slider-background.js +66 -0
  48. package/dist/utils/get-slider-background.mjs +6 -0
  49. package/package.json +65 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Chakra UI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # @zag-js/color-picker
2
+
3
+ Core logic for the color-picker widget implemented as a state machine
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ yarn add @zag-js/color-picker
9
+ # or
10
+ npm i @zag-js/color-picker
11
+ ```
12
+
13
+ ## Contribution
14
+
15
+ Yes please! See the [contributing guidelines](https://github.com/chakra-ui/zag/blob/main/CONTRIBUTING.md) for details.
16
+
17
+ ## Licence
18
+
19
+ This project is licensed under the terms of the [MIT license](https://github.com/chakra-ui/zag/blob/main/LICENSE).
@@ -0,0 +1,25 @@
1
+ // src/utils/get-channel-input-value.ts
2
+ function getChannelInputValue(color, channel) {
3
+ switch (channel) {
4
+ case "hex":
5
+ return color.toString("hex");
6
+ case "css":
7
+ return color.toString("css");
8
+ default:
9
+ return color.getChannelValue(channel).toString();
10
+ }
11
+ }
12
+ function getChannelInputRange(color, channel) {
13
+ switch (channel) {
14
+ case "hex":
15
+ case "css":
16
+ return void 0;
17
+ default:
18
+ return color.getChannelRange(channel);
19
+ }
20
+ }
21
+
22
+ export {
23
+ getChannelInputValue,
24
+ getChannelInputRange
25
+ };
@@ -0,0 +1,42 @@
1
+ // src/utils/get-slider-background.ts
2
+ function getSliderBgDirection(orientation, dir) {
3
+ if (orientation === "vertical") {
4
+ return "top";
5
+ } else if (dir === "ltr") {
6
+ return "right";
7
+ } else {
8
+ return "left";
9
+ }
10
+ }
11
+ var getSliderBgImage = (ctx, props) => {
12
+ const { channel } = props;
13
+ const dir = getSliderBgDirection(props.orientation, ctx.dir);
14
+ const value = ctx.valueAsColor;
15
+ const { minValue, maxValue } = ctx.valueAsColor.getChannelRange(channel);
16
+ switch (channel) {
17
+ case "hue":
18
+ return `linear-gradient(to ${dir}, rgb(255, 0, 0) 0%, rgb(255, 255, 0) 17%, rgb(0, 255, 0) 33%, rgb(0, 255, 255) 50%, rgb(0, 0, 255) 67%, rgb(255, 0, 255) 83%, rgb(255, 0, 0) 100%)`;
19
+ case "lightness": {
20
+ let start = ctx.valueAsColor.withChannelValue(channel, minValue).toString("css");
21
+ let middle = value.withChannelValue(channel, (maxValue - minValue) / 2).toString("css");
22
+ let end = value.withChannelValue(channel, maxValue).toString("css");
23
+ return `linear-gradient(to ${dir}, ${start}, ${middle}, ${end})`;
24
+ }
25
+ case "saturation":
26
+ case "brightness":
27
+ case "red":
28
+ case "green":
29
+ case "blue":
30
+ case "alpha": {
31
+ let start = value.withChannelValue(channel, minValue).toString("css");
32
+ let end = value.withChannelValue(channel, maxValue).toString("css");
33
+ return `linear-gradient(to ${dir}, ${start}, ${end})`;
34
+ }
35
+ default:
36
+ throw new Error("Unknown color channel: " + channel);
37
+ }
38
+ };
39
+
40
+ export {
41
+ getSliderBgImage
42
+ };
@@ -0,0 +1,22 @@
1
+ // src/color-picker.anatomy.ts
2
+ import { createAnatomy } from "@zag-js/anatomy";
3
+ var anatomy = createAnatomy("color-picker", [
4
+ "area",
5
+ "areaThumb",
6
+ "areaGradient",
7
+ "channelSliderTrack",
8
+ "channelSliderTrackBg",
9
+ "channelSliderThumb",
10
+ "channelInput",
11
+ "swatch",
12
+ "swatchBg",
13
+ "content",
14
+ "label",
15
+ "eyeDropTrigger"
16
+ ]);
17
+ var parts = anatomy.build();
18
+
19
+ export {
20
+ anatomy,
21
+ parts
22
+ };
@@ -0,0 +1,35 @@
1
+ // src/color-picker.dom.ts
2
+ import { getRelativePointPercent } from "@zag-js/dom-event";
3
+ import { createScope, queryAll } from "@zag-js/dom-query";
4
+ var dom = createScope({
5
+ getAreaId: (ctx) => `color-picker:${ctx.id}:area`,
6
+ getAreaGradientId: (ctx) => `color-picker:${ctx.id}:area-gradient`,
7
+ getAreaThumbId: (ctx) => `color-picker:${ctx.id}:area-thumb`,
8
+ getChannelSliderTrackId: (ctx, channel) => `color-picker:${ctx.id}:slider-track:${channel}`,
9
+ getChannelInputId: (ctx, channel) => `color-picker:${ctx.id}:input:${channel}`,
10
+ getContentId: (ctx) => `color-picker:${ctx.id}:content`,
11
+ getChannelSliderThumbId: (ctx, channel) => `color-picker:${ctx.id}:thumb:${channel}`,
12
+ getContentEl: (ctx) => dom.queryById(ctx, dom.getContentId(ctx)),
13
+ getAreaThumbEl: (ctx) => dom.queryById(ctx, dom.getAreaThumbId(ctx)),
14
+ getChannelSliderThumbEl: (ctx, channel) => dom.queryById(ctx, dom.getChannelSliderThumbId(ctx, channel)),
15
+ getChannelInputEl: (ctx, channel) => dom.queryById(ctx, dom.getChannelInputId(ctx, channel)),
16
+ getAreaEl: (ctx) => dom.queryById(ctx, dom.getAreaId(ctx)),
17
+ getAreaValueFromPoint(ctx, point) {
18
+ const areaEl = dom.getAreaEl(ctx);
19
+ return getRelativePointPercent(point, areaEl);
20
+ },
21
+ getChannelSliderTrackEl: (ctx, channel) => {
22
+ return dom.queryById(ctx, dom.getChannelSliderTrackId(ctx, channel));
23
+ },
24
+ getChannelSliderValueFromPoint(ctx, point, channel) {
25
+ const areaEl = dom.getChannelSliderTrackEl(ctx, channel);
26
+ return getRelativePointPercent(point, areaEl);
27
+ },
28
+ getChannelInputEls: (ctx) => {
29
+ return queryAll(dom.getContentEl(ctx), "input[data-channel]");
30
+ }
31
+ });
32
+
33
+ export {
34
+ dom
35
+ };
@@ -0,0 +1,365 @@
1
+ import {
2
+ getChannelDisplayColor
3
+ } from "./chunk-JNBNPPMF.mjs";
4
+ import {
5
+ getColorAreaGradient
6
+ } from "./chunk-KO4TBUXB.mjs";
7
+ import {
8
+ getSliderBgImage
9
+ } from "./chunk-3XH465XT.mjs";
10
+ import {
11
+ parts
12
+ } from "./chunk-75JXJMB5.mjs";
13
+ import {
14
+ getChannelInputRange,
15
+ getChannelInputValue
16
+ } from "./chunk-2ERX54SV.mjs";
17
+ import {
18
+ dom
19
+ } from "./chunk-ACSV4TXB.mjs";
20
+ import {
21
+ getChannelDetails
22
+ } from "./chunk-KLLIOTK6.mjs";
23
+
24
+ // src/color-picker.connect.ts
25
+ import { normalizeColor } from "@zag-js/color-utils";
26
+ import { getEventKey, getEventStep, getNativeEvent, isLeftClick, isModifiedEvent } from "@zag-js/dom-event";
27
+ import { dataAttr } from "@zag-js/dom-query";
28
+ function connect(state, send, normalize) {
29
+ const valueAsColor = state.context.valueAsColor;
30
+ const isDisabled = state.context.disabled;
31
+ const isInteractive = state.context.isInteractive;
32
+ const isDragging = state.matches("dragging");
33
+ const channels = valueAsColor.getColorChannels();
34
+ return {
35
+ isDragging,
36
+ value: state.context.value,
37
+ valueAsColor,
38
+ channels,
39
+ setColor(value) {
40
+ send({ type: "VALUE.SET", value: normalizeColor(value), src: "set-color" });
41
+ },
42
+ setChannelValue(channel, value) {
43
+ const color = valueAsColor.withChannelValue(channel, value);
44
+ send({ type: "VALUE.SET", value: color, src: "set-channel" });
45
+ },
46
+ setFormat(format) {
47
+ const value = valueAsColor.toFormat(format);
48
+ send({ type: "VALUE.SET", value, src: "set-format" });
49
+ },
50
+ contentProps: normalize.element({
51
+ ...parts.content.attrs,
52
+ id: dom.getContentId(state.context)
53
+ }),
54
+ getAreaProps(props) {
55
+ const { xChannel, yChannel } = props;
56
+ const { areaStyles } = getColorAreaGradient(state.context, xChannel, yChannel);
57
+ return normalize.element({
58
+ ...parts.area.attrs,
59
+ id: dom.getAreaId(state.context),
60
+ role: "group",
61
+ onPointerDown(event) {
62
+ if (!isInteractive)
63
+ return;
64
+ const evt = getNativeEvent(event);
65
+ if (!isLeftClick(evt) || isModifiedEvent(evt))
66
+ return;
67
+ const point = { x: evt.clientX, y: evt.clientY };
68
+ const channel = { xChannel, yChannel };
69
+ send({ type: "AREA.POINTER_DOWN", point, channel, id: "area" });
70
+ },
71
+ style: {
72
+ position: "relative",
73
+ touchAction: "none",
74
+ forcedColorAdjust: "none",
75
+ ...areaStyles
76
+ }
77
+ });
78
+ },
79
+ getAreaGradientProps(props) {
80
+ const { xChannel, yChannel } = props;
81
+ const { areaGradientStyles } = getColorAreaGradient(state.context, xChannel, yChannel);
82
+ return normalize.element({
83
+ ...parts.areaGradient.attrs,
84
+ id: dom.getAreaGradientId(state.context),
85
+ style: {
86
+ position: "relative",
87
+ touchAction: "none",
88
+ forcedColorAdjust: "none",
89
+ ...areaGradientStyles
90
+ }
91
+ });
92
+ },
93
+ getAreaThumbProps(props) {
94
+ const { xChannel, yChannel } = props;
95
+ const { getThumbPosition } = getChannelDetails(valueAsColor, xChannel, yChannel);
96
+ const { x, y } = getThumbPosition();
97
+ const channel = { xChannel, yChannel };
98
+ return normalize.element({
99
+ ...parts.areaThumb.attrs,
100
+ id: dom.getAreaThumbId(state.context),
101
+ tabIndex: isDisabled ? void 0 : 0,
102
+ "data-disabled": dataAttr(isDisabled),
103
+ role: "presentation",
104
+ style: {
105
+ position: "absolute",
106
+ left: `${x * 100}%`,
107
+ top: `${y * 100}%`,
108
+ transform: "translate(-50%, -50%)",
109
+ touchAction: "none",
110
+ forcedColorAdjust: "none",
111
+ background: valueAsColor.withChannelValue("alpha", 1).toString("css")
112
+ },
113
+ onBlur() {
114
+ send("AREA.BLUR");
115
+ },
116
+ onKeyDown(event) {
117
+ if (!isInteractive)
118
+ return;
119
+ const step = getEventStep(event);
120
+ const keyMap = {
121
+ ArrowUp() {
122
+ send({ type: "AREA.ARROW_UP", channel, step });
123
+ },
124
+ ArrowDown() {
125
+ send({ type: "AREA.ARROW_DOWN", channel, step });
126
+ },
127
+ ArrowLeft() {
128
+ send({ type: "AREA.ARROW_LEFT", channel, step });
129
+ },
130
+ ArrowRight() {
131
+ send({ type: "AREA.ARROW_RIGHT", channel, step });
132
+ },
133
+ PageUp() {
134
+ send({ type: "AREA.PAGE_UP", channel, step });
135
+ },
136
+ PageDown() {
137
+ send({ type: "AREA.PAGE_DOWN", channel, step });
138
+ }
139
+ };
140
+ const exec = keyMap[getEventKey(event, state.context)];
141
+ if (exec) {
142
+ exec(event);
143
+ event.preventDefault();
144
+ }
145
+ }
146
+ });
147
+ },
148
+ getChannelSliderTrackProps(props) {
149
+ const { orientation = "horizontal", channel } = props;
150
+ return normalize.element({
151
+ ...parts.channelSliderTrack.attrs,
152
+ id: dom.getChannelSliderTrackId(state.context, channel),
153
+ role: "group",
154
+ "data-channel": channel,
155
+ "data-orientation": orientation,
156
+ onPointerDown(event) {
157
+ if (!isInteractive)
158
+ return;
159
+ const evt = getNativeEvent(event);
160
+ if (!isLeftClick(evt) || isModifiedEvent(evt))
161
+ return;
162
+ const point = { x: evt.clientX, y: evt.clientY };
163
+ send({ type: "CHANNEL_SLIDER.POINTER_DOWN", channel, point, id: channel, orientation });
164
+ },
165
+ style: {
166
+ position: "relative",
167
+ touchAction: "none",
168
+ forcedColorAdjust: "none",
169
+ backgroundImage: getSliderBgImage(state.context, { orientation, channel })
170
+ }
171
+ });
172
+ },
173
+ getChannelSliderBackgroundProps(props) {
174
+ const { orientation = "horizontal", channel } = props;
175
+ return normalize.element({
176
+ ...parts.channelSliderTrackBg.attrs,
177
+ "data-orientation": orientation,
178
+ "data-channel": channel,
179
+ style: {
180
+ position: "absolute",
181
+ backgroundColor: "#fff",
182
+ backgroundImage: [
183
+ "linear-gradient(-45deg,#0000 75.5%,#bcbcbc 75.5%)",
184
+ "linear-gradient(45deg,#0000 75.5%,#bcbcbc 75.5%)",
185
+ "linear-gradient(-45deg,#bcbcbc 25.5%,#0000 25.5%)",
186
+ "linear-gradient(45deg,#bcbcbc 25.5%,#0000 25.5%)"
187
+ ].join(","),
188
+ backgroundSize: "16px 16px",
189
+ backgroundPosition: "-2px -2px,-2px 6px,6px -10px,-10px -2px",
190
+ inset: 0,
191
+ zIndex: -1
192
+ }
193
+ });
194
+ },
195
+ getChannelSliderThumbProps(props) {
196
+ const { orientation = "horizontal", channel } = props;
197
+ const { minValue, maxValue, step: stepValue } = valueAsColor.getChannelRange(channel);
198
+ const channelValue = valueAsColor.getChannelValue(channel);
199
+ const offset = (channelValue - minValue) / (maxValue - minValue);
200
+ const placementStyles = orientation === "horizontal" ? { left: `${offset * 100}%`, top: "50%" } : { top: `${offset * 100}%`, left: "50%" };
201
+ return normalize.element({
202
+ ...parts.channelSliderThumb.attrs,
203
+ id: dom.getChannelSliderThumbId(state.context, channel),
204
+ role: "slider",
205
+ "aria-label": channel,
206
+ tabIndex: isDisabled ? void 0 : 0,
207
+ "data-channel": channel,
208
+ "data-disabled": dataAttr(isDisabled),
209
+ "data-orientation": orientation,
210
+ "aria-disabled": dataAttr(isDisabled),
211
+ "aria-orientation": orientation,
212
+ "aria-valuemax": maxValue,
213
+ "aria-valuemin": minValue,
214
+ "aria-valuenow": channelValue,
215
+ style: {
216
+ forcedColorAdjust: "none",
217
+ position: "absolute",
218
+ background: getChannelDisplayColor(valueAsColor, channel).toString("css"),
219
+ ...placementStyles
220
+ },
221
+ onFocus() {
222
+ if (!isInteractive)
223
+ return;
224
+ send({ type: "CHANNEL_SLIDER.FOCUS", channel });
225
+ },
226
+ onBlur() {
227
+ if (!isInteractive)
228
+ return;
229
+ send({ type: "CHANNEL_SLIDER.BLUR", channel });
230
+ },
231
+ onKeyDown(event) {
232
+ if (!isInteractive)
233
+ return;
234
+ const step = getEventStep(event) * stepValue;
235
+ const keyMap = {
236
+ ArrowUp() {
237
+ send({ type: "CHANNEL_SLIDER.ARROW_UP", channel, step });
238
+ },
239
+ ArrowDown() {
240
+ send({ type: "CHANNEL_SLIDER.ARROW_DOWN", channel, step });
241
+ },
242
+ ArrowLeft() {
243
+ send({ type: "CHANNEL_SLIDER.ARROW_LEFT", channel, step });
244
+ },
245
+ ArrowRight() {
246
+ send({ type: "CHANNEL_SLIDER.ARROW_RIGHT", channel, step });
247
+ },
248
+ PageUp() {
249
+ send({ type: "CHANNEL_SLIDER.PAGE_UP", channel });
250
+ },
251
+ PageDown() {
252
+ send({ type: "CHANNEL_SLIDER.PAGE_DOWN", channel });
253
+ },
254
+ Home() {
255
+ send({ type: "CHANNEL_SLIDER.HOME", channel });
256
+ },
257
+ End() {
258
+ send({ type: "CHANNEL_SLIDER.END", channel });
259
+ }
260
+ };
261
+ const exec = keyMap[getEventKey(event, state.context)];
262
+ if (exec) {
263
+ exec(event);
264
+ event.preventDefault();
265
+ }
266
+ }
267
+ });
268
+ },
269
+ getChannelInputProps(props) {
270
+ const { channel } = props;
271
+ const isTextField = channel === "hex" || channel === "css";
272
+ const range = getChannelInputRange(valueAsColor, channel);
273
+ return normalize.input({
274
+ ...parts.channelInput.attrs,
275
+ type: isTextField ? "text" : "number",
276
+ "data-channel": channel,
277
+ "aria-label": channel,
278
+ disabled: isDisabled,
279
+ "data-disabled": dataAttr(isDisabled),
280
+ readOnly: state.context.readOnly,
281
+ id: dom.getChannelInputId(state.context, channel),
282
+ defaultValue: getChannelInputValue(valueAsColor, channel),
283
+ min: range?.minValue,
284
+ max: range?.maxValue,
285
+ step: range?.step,
286
+ onFocus() {
287
+ send({ type: "CHANNEL_INPUT.FOCUS", channel });
288
+ },
289
+ onChange(event) {
290
+ if (isTextField)
291
+ return;
292
+ const value = event.currentTarget.value;
293
+ send({ type: "CHANNEL_INPUT.CHANGE", channel, value, isTextField });
294
+ },
295
+ onBlur(event) {
296
+ const value = event.currentTarget.value;
297
+ send({ type: "CHANNEL_INPUT.BLUR", channel, value, isTextField });
298
+ },
299
+ onKeyDown(event) {
300
+ if (!isTextField)
301
+ return;
302
+ if (event.key === "Enter") {
303
+ const value = event.currentTarget.value;
304
+ send({ type: "CHANNEL_INPUT.CHANGE", channel, value, isTextField });
305
+ }
306
+ },
307
+ style: {
308
+ appearance: "none",
309
+ WebkitAppearance: "none",
310
+ MozAppearance: "textfield"
311
+ }
312
+ });
313
+ },
314
+ eyeDropperTriggerProps: normalize.button({
315
+ ...parts.eyeDropTrigger.attrs,
316
+ onClick() {
317
+ send("EYEDROPPER.CLICK");
318
+ }
319
+ }),
320
+ getSwatchBackgroundProps(props) {
321
+ const { value } = props;
322
+ const alpha = normalizeColor(value).getChannelValue("alpha");
323
+ return normalize.element({
324
+ ...parts.swatchBg.attrs,
325
+ "data-alpha": alpha,
326
+ style: {
327
+ width: "100%",
328
+ height: "100%",
329
+ background: "#fff",
330
+ backgroundImage: [
331
+ "linear-gradient(-45deg,#0000 75.5%,#bcbcbc 75.5%)",
332
+ "linear-gradient(45deg,#0000 75.5%,#bcbcbc 75.5%)",
333
+ "linear-gradient(-45deg,#bcbcbc 25.5%,#0000 25.5%)",
334
+ "linear-gradient(45deg,#bcbcbc 25.5%,#0000 25.5%)"
335
+ ].join(","),
336
+ backgroundPosition: "-2px -2px,-2px 6px,6px -10px,-10px -2px",
337
+ backgroundSize: "16px 16px",
338
+ position: "absolute",
339
+ inset: "0px",
340
+ zIndex: -1
341
+ }
342
+ });
343
+ },
344
+ getSwatchProps(props) {
345
+ const { value, readOnly } = props;
346
+ const color = normalizeColor(value).toFormat(valueAsColor.getColorSpace());
347
+ return normalize.element({
348
+ ...parts.swatch.attrs,
349
+ onClick() {
350
+ if (readOnly)
351
+ return;
352
+ send({ type: "VALUE.SET", value: color });
353
+ },
354
+ style: {
355
+ position: "relative",
356
+ background: color.toString("css")
357
+ }
358
+ });
359
+ }
360
+ };
361
+ }
362
+
363
+ export {
364
+ connect
365
+ };
@@ -0,0 +1,24 @@
1
+ // src/utils/get-channel-display-color.ts
2
+ import { parseColor } from "@zag-js/color-utils";
3
+ function getChannelDisplayColor(color, channel) {
4
+ switch (channel) {
5
+ case "hue":
6
+ return parseColor(`hsl(${color.getChannelValue("hue")}, 100%, 50%)`);
7
+ case "lightness":
8
+ case "brightness":
9
+ case "saturation":
10
+ case "red":
11
+ case "green":
12
+ case "blue":
13
+ return color.withChannelValue("alpha", 1);
14
+ case "alpha": {
15
+ return color;
16
+ }
17
+ default:
18
+ throw new Error("Unknown color channel: " + channel);
19
+ }
20
+ }
21
+
22
+ export {
23
+ getChannelDisplayColor
24
+ };
@@ -0,0 +1,55 @@
1
+ // src/utils/get-channel-details.ts
2
+ import { snapValueToStep, getPercentValue } from "@zag-js/numeric-range";
3
+ function getChannelDetails(color, xChannel, yChannel) {
4
+ const channels = color.getColorSpaceAxes({ xChannel, yChannel });
5
+ const xChannelRange = color.getChannelRange(channels.xChannel);
6
+ const yChannelRange = color.getChannelRange(channels.yChannel);
7
+ const { minValue: minValueX, maxValue: maxValueX, step: stepX, pageSize: pageSizeX } = xChannelRange;
8
+ const { minValue: minValueY, maxValue: maxValueY, step: stepY, pageSize: pageSizeY } = yChannelRange;
9
+ const xValue = color.getChannelValue(channels.xChannel);
10
+ const yValue = color.getChannelValue(channels.yChannel);
11
+ return {
12
+ channels,
13
+ xChannelStep: stepX,
14
+ yChannelStep: stepY,
15
+ xChannelPageStep: pageSizeX,
16
+ yChannelPageStep: pageSizeY,
17
+ xValue,
18
+ yValue,
19
+ getThumbPosition() {
20
+ let x = (xValue - minValueX) / (maxValueX - minValueX);
21
+ let y = 1 - (yValue - minValueY) / (maxValueY - minValueY);
22
+ return { x, y };
23
+ },
24
+ incrementX(stepSize) {
25
+ return xValue + stepSize > maxValueX ? maxValueX : snapValueToStep(xValue + stepSize, minValueX, maxValueX, stepX);
26
+ },
27
+ incrementY(stepSize) {
28
+ return yValue + stepSize > maxValueY ? maxValueY : snapValueToStep(yValue + stepSize, minValueY, maxValueY, stepY);
29
+ },
30
+ decrementX(stepSize) {
31
+ return snapValueToStep(xValue - stepSize, minValueX, maxValueX, stepX);
32
+ },
33
+ decrementY(stepSize) {
34
+ return snapValueToStep(yValue - stepSize, minValueY, maxValueY, stepY);
35
+ },
36
+ getColorFromPoint(x, y) {
37
+ let newXValue = getPercentValue(x, minValueX, maxValueX, stepX);
38
+ let newYValue = getPercentValue(1 - y, minValueY, maxValueY, stepY);
39
+ let newColor;
40
+ if (newXValue !== xValue) {
41
+ newXValue = snapValueToStep(newXValue, minValueX, maxValueX, stepX);
42
+ newColor = color.withChannelValue(channels.xChannel, newXValue);
43
+ }
44
+ if (newYValue !== yValue) {
45
+ newYValue = snapValueToStep(newYValue, minValueY, maxValueY, stepY);
46
+ newColor = (newColor || color).withChannelValue(channels.yChannel, newYValue);
47
+ }
48
+ return newColor;
49
+ }
50
+ };
51
+ }
52
+
53
+ export {
54
+ getChannelDetails
55
+ };
@@ -0,0 +1,74 @@
1
+ import {
2
+ generateHSB_B,
3
+ generateHSB_H,
4
+ generateHSB_S,
5
+ generateHSL_H,
6
+ generateHSL_L,
7
+ generateHSL_S,
8
+ generateRGB_B,
9
+ generateRGB_G,
10
+ generateRGB_R
11
+ } from "./chunk-OY3B7NXA.mjs";
12
+
13
+ // src/utils/get-color-area-gradient.ts
14
+ function getColorAreaGradient(ctx, xChannel, yChannel) {
15
+ const value = ctx.valueAsColor;
16
+ const { zChannel } = value.getColorSpaceAxes({ xChannel, yChannel });
17
+ const zValue = value.getChannelValue(zChannel);
18
+ const { minValue: zMin, maxValue: zMax } = value.getChannelRange(zChannel);
19
+ const orientation = ["top", ctx.dir === "rtl" ? "left" : "right"];
20
+ let dir = false;
21
+ let background = { areaStyles: {}, areaGradientStyles: {} };
22
+ let alphaValue = (zValue - zMin) / (zMax - zMin);
23
+ let isHSL = value.getColorSpace() === "hsl";
24
+ switch (zChannel) {
25
+ case "red": {
26
+ dir = xChannel === "green";
27
+ background = generateRGB_R(orientation, dir, zValue);
28
+ break;
29
+ }
30
+ case "green": {
31
+ dir = xChannel === "red";
32
+ background = generateRGB_G(orientation, dir, zValue);
33
+ break;
34
+ }
35
+ case "blue": {
36
+ dir = xChannel === "red";
37
+ background = generateRGB_B(orientation, dir, zValue);
38
+ break;
39
+ }
40
+ case "hue": {
41
+ dir = xChannel !== "saturation";
42
+ if (isHSL) {
43
+ background = generateHSL_H(orientation, dir, zValue);
44
+ } else {
45
+ background = generateHSB_H(orientation, dir, zValue);
46
+ }
47
+ break;
48
+ }
49
+ case "saturation": {
50
+ dir = xChannel === "hue";
51
+ if (isHSL) {
52
+ background = generateHSL_S(orientation, dir, alphaValue);
53
+ } else {
54
+ background = generateHSB_S(orientation, dir, alphaValue);
55
+ }
56
+ break;
57
+ }
58
+ case "brightness": {
59
+ dir = xChannel === "hue";
60
+ background = generateHSB_B(orientation, dir, alphaValue);
61
+ break;
62
+ }
63
+ case "lightness": {
64
+ dir = xChannel === "hue";
65
+ background = generateHSL_L(orientation, dir, zValue);
66
+ break;
67
+ }
68
+ }
69
+ return background;
70
+ }
71
+
72
+ export {
73
+ getColorAreaGradient
74
+ };