@zag-js/color-picker 0.9.2 → 0.10.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.
@@ -0,0 +1,23 @@
1
+ import { Color } from "@zag-js/color-utils"
2
+ import { ExtendedColorChannel } from "../color-picker.types"
3
+
4
+ export function getChannelInputValue(color: Color, channel: ExtendedColorChannel) {
5
+ switch (channel) {
6
+ case "hex":
7
+ return color.toString("hex")
8
+ case "css":
9
+ return color.toString("css")
10
+ default:
11
+ return color.getChannelValue(channel).toString()
12
+ }
13
+ }
14
+
15
+ export function getChannelInputRange(color: Color, channel: ExtendedColorChannel) {
16
+ switch (channel) {
17
+ case "hex":
18
+ case "css":
19
+ return undefined
20
+ default:
21
+ return color.getChannelRange(channel)
22
+ }
23
+ }
@@ -0,0 +1,84 @@
1
+ import { ColorChannel } from "@zag-js/color-utils"
2
+ import { Style } from "@zag-js/types"
3
+ import { MachineContext } from "../color-picker.types"
4
+ import {
5
+ generateHSB_B,
6
+ generateHSB_H,
7
+ generateHSB_S,
8
+ generateHSL_H,
9
+ generateHSL_L,
10
+ generateHSL_S,
11
+ generateRGB_B,
12
+ generateRGB_G,
13
+ generateRGB_R,
14
+ } from "./generate-format-background"
15
+
16
+ export function getColorAreaGradient(ctx: MachineContext, xChannel: ColorChannel, yChannel: ColorChannel) {
17
+ const value = ctx.valueAsColor
18
+
19
+ const { zChannel } = value.getColorSpaceAxes({ xChannel, yChannel })
20
+ const zValue = value.getChannelValue(zChannel)
21
+
22
+ const { minValue: zMin, maxValue: zMax } = value.getChannelRange(zChannel)
23
+ const orientation: [string, string] = ["top", ctx.dir === "rtl" ? "left" : "right"]
24
+
25
+ let dir = false
26
+ let background = { areaStyles: {} as Style, areaGradientStyles: {} as Style }
27
+
28
+ let alphaValue = (zValue - zMin) / (zMax - zMin)
29
+ let isHSL = value.getColorSpace() === "hsl"
30
+
31
+ switch (zChannel) {
32
+ case "red": {
33
+ dir = xChannel === "green"
34
+ background = generateRGB_R(orientation, dir, zValue)
35
+ break
36
+ }
37
+
38
+ case "green": {
39
+ dir = xChannel === "red"
40
+ background = generateRGB_G(orientation, dir, zValue)
41
+ break
42
+ }
43
+
44
+ case "blue": {
45
+ dir = xChannel === "red"
46
+ background = generateRGB_B(orientation, dir, zValue)
47
+ break
48
+ }
49
+
50
+ case "hue": {
51
+ dir = xChannel !== "saturation"
52
+ if (isHSL) {
53
+ background = generateHSL_H(orientation, dir, zValue)
54
+ } else {
55
+ background = generateHSB_H(orientation, dir, zValue)
56
+ }
57
+ break
58
+ }
59
+
60
+ case "saturation": {
61
+ dir = xChannel === "hue"
62
+ if (isHSL) {
63
+ background = generateHSL_S(orientation, dir, alphaValue)
64
+ } else {
65
+ background = generateHSB_S(orientation, dir, alphaValue)
66
+ }
67
+ break
68
+ }
69
+
70
+ case "brightness": {
71
+ dir = xChannel === "hue"
72
+ background = generateHSB_B(orientation, dir, alphaValue)
73
+ break
74
+ }
75
+
76
+ case "lightness": {
77
+ dir = xChannel === "hue"
78
+ background = generateHSL_L(orientation, dir, zValue)
79
+ break
80
+ }
81
+ }
82
+
83
+ return background
84
+ }
@@ -0,0 +1,43 @@
1
+ import { ColorChannelProps, MachineContext } from "../color-picker.types"
2
+
3
+ function getSliderBgDirection(orientation: "vertical" | "horizontal", dir: "ltr" | "rtl") {
4
+ if (orientation === "vertical") {
5
+ return "top"
6
+ } else if (dir === "ltr") {
7
+ return "right"
8
+ } else {
9
+ return "left"
10
+ }
11
+ }
12
+
13
+ export const getSliderBgImage = (ctx: MachineContext, props: Required<ColorChannelProps>) => {
14
+ const { channel } = props
15
+
16
+ const dir = getSliderBgDirection(props.orientation, ctx.dir!)
17
+ const value = ctx.valueAsColor
18
+
19
+ const { minValue, maxValue } = ctx.valueAsColor.getChannelRange(channel)
20
+
21
+ switch (channel) {
22
+ case "hue":
23
+ 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%)`
24
+ case "lightness": {
25
+ let start = ctx.valueAsColor.withChannelValue(channel, minValue).toString("css")
26
+ let middle = value.withChannelValue(channel, (maxValue - minValue) / 2).toString("css")
27
+ let end = value.withChannelValue(channel, maxValue).toString("css")
28
+ return `linear-gradient(to ${dir}, ${start}, ${middle}, ${end})`
29
+ }
30
+ case "saturation":
31
+ case "brightness":
32
+ case "red":
33
+ case "green":
34
+ case "blue":
35
+ case "alpha": {
36
+ let start = value.withChannelValue(channel, minValue).toString("css")
37
+ let end = value.withChannelValue(channel, maxValue).toString("css")
38
+ return `linear-gradient(to ${dir}, ${start}, ${end})`
39
+ }
40
+ default:
41
+ throw new Error("Unknown color channel: " + channel)
42
+ }
43
+ }