@zag-js/color-utils 0.20.0 → 0.22.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,139 @@
1
+ export const generateRGB_R = (orientation: [string, string], dir: boolean, zValue: number) => {
2
+ const maskImage = `linear-gradient(to ${orientation[Number(!dir)]}, transparent, #000)`
3
+ const result = {
4
+ areaStyles: {
5
+ backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(${zValue},0,0),rgb(${zValue},255,0))`,
6
+ },
7
+ areaGradientStyles: {
8
+ backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(${zValue},0,255),rgb(${zValue},255,255))`,
9
+ WebkitMaskImage: maskImage,
10
+ maskImage,
11
+ },
12
+ }
13
+ return result
14
+ }
15
+
16
+ export const generateRGB_G = (orientation: [string, string], dir: boolean, zValue: number) => {
17
+ const maskImage = `linear-gradient(to ${orientation[Number(!dir)]}, transparent, #000)`
18
+ const result = {
19
+ areaStyles: {
20
+ backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,${zValue},0),rgb(255,${zValue},0))`,
21
+ },
22
+ areaGradientStyles: {
23
+ backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,${zValue},255),rgb(255,${zValue},255))`,
24
+ WebkitMaskImage: maskImage,
25
+ maskImage,
26
+ },
27
+ }
28
+ return result
29
+ }
30
+
31
+ export const generateRGB_B = (orientation: [string, string], dir: boolean, zValue: number) => {
32
+ const maskImage = `linear-gradient(to ${orientation[Number(!dir)]}, transparent, #000)`
33
+ const result = {
34
+ areaStyles: {
35
+ backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,0,${zValue}),rgb(255,0,${zValue}))`,
36
+ },
37
+ areaGradientStyles: {
38
+ backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,255,${zValue}),rgb(255,255,${zValue}))`,
39
+ WebkitMaskImage: maskImage,
40
+ maskImage,
41
+ },
42
+ }
43
+ return result
44
+ }
45
+
46
+ export const generateHSL_H = (orientation: [string, string], dir: boolean, zValue: number) => {
47
+ const result = {
48
+ areaStyles: {},
49
+ areaGradientStyles: {
50
+ background: [
51
+ `linear-gradient(to ${
52
+ orientation[Number(dir)]
53
+ }, hsla(0,0%,0%,1) 0%, hsla(0,0%,0%,0) 50%, hsla(0,0%,100%,0) 50%, hsla(0,0%,100%,1) 100%)`,
54
+ `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,50%),hsla(0,0%,50%,0))`,
55
+ `hsl(${zValue}, 100%, 50%)`,
56
+ ].join(","),
57
+ },
58
+ }
59
+ return result
60
+ }
61
+
62
+ export const generateHSL_S = (orientation: [string, string], dir: boolean, alphaValue: number) => {
63
+ const result = {
64
+ areaStyles: {},
65
+ areaGradientStyles: {
66
+ background: [
67
+ `linear-gradient(to ${
68
+ orientation[Number(!dir)]
69
+ }, hsla(0,0%,0%,${alphaValue}) 0%, hsla(0,0%,0%,0) 50%, hsla(0,0%,100%,0) 50%, hsla(0,0%,100%,${alphaValue}) 100%)`,
70
+ `linear-gradient(to ${
71
+ orientation[Number(dir)]
72
+ },hsla(0,100%,50%,${alphaValue}),hsla(60,100%,50%,${alphaValue}),hsla(120,100%,50%,${alphaValue}),hsla(180,100%,50%,${alphaValue}),hsla(240,100%,50%,${alphaValue}),hsla(300,100%,50%,${alphaValue}),hsla(359,100%,50%,${alphaValue}))`,
73
+ "hsl(0, 0%, 50%)",
74
+ ].join(","),
75
+ },
76
+ }
77
+ return result
78
+ }
79
+
80
+ export const generateHSL_L = (orientation: [string, string], dir: boolean, zValue: number) => {
81
+ const result = {
82
+ areaStyles: {},
83
+ areaGradientStyles: {
84
+ backgroundImage: [
85
+ `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,${zValue}%),hsla(0,0%,${zValue}%,0))`,
86
+ `linear-gradient(to ${
87
+ orientation[Number(dir)]
88
+ },hsl(0,100%,${zValue}%),hsl(60,100%,${zValue}%),hsl(120,100%,${zValue}%),hsl(180,100%,${zValue}%),hsl(240,100%,${zValue}%),hsl(300,100%,${zValue}%),hsl(360,100%,${zValue}%))`,
89
+ ].join(","),
90
+ },
91
+ }
92
+ return result
93
+ }
94
+
95
+ export const generateHSB_H = (orientation: [string, string], dir: boolean, zValue: number) => {
96
+ const result = {
97
+ areaStyles: {},
98
+ areaGradientStyles: {
99
+ background: [
100
+ `linear-gradient(to ${orientation[Number(dir)]},hsl(0,0%,0%),hsla(0,0%,0%,0))`,
101
+ `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,100%),hsla(0,0%,100%,0))`,
102
+ `hsl(${zValue}, 100%, 50%)`,
103
+ ].join(","),
104
+ },
105
+ }
106
+ return result
107
+ }
108
+
109
+ export const generateHSB_S = (orientation: [string, string], dir: boolean, alphaValue: number) => {
110
+ const result = {
111
+ areaStyles: {},
112
+ areaGradientStyles: {
113
+ background: [
114
+ `linear-gradient(to ${orientation[Number(!dir)]},hsla(0,0%,0%,${alphaValue}),hsla(0,0%,0%,0))`,
115
+ `linear-gradient(to ${
116
+ orientation[Number(dir)]
117
+ },hsla(0,100%,50%,${alphaValue}),hsla(60,100%,50%,${alphaValue}),hsla(120,100%,50%,${alphaValue}),hsla(180,100%,50%,${alphaValue}),hsla(240,100%,50%,${alphaValue}),hsla(300,100%,50%,${alphaValue}),hsla(359,100%,50%,${alphaValue}))`,
118
+ `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,0%),hsl(0,0%,100%))`,
119
+ ].join(","),
120
+ },
121
+ }
122
+ return result
123
+ }
124
+
125
+ export const generateHSB_B = (orientation: [string, string], dir: boolean, alphaValue: number) => {
126
+ const result = {
127
+ areaStyles: {},
128
+ areaGradientStyles: {
129
+ background: [
130
+ `linear-gradient(to ${orientation[Number(!dir)]},hsla(0,0%,100%,${alphaValue}),hsla(0,0%,100%,0))`,
131
+ `linear-gradient(to ${
132
+ orientation[Number(dir)]
133
+ },hsla(0,100%,50%,${alphaValue}),hsla(60,100%,50%,${alphaValue}),hsla(120,100%,50%,${alphaValue}),hsla(180,100%,50%,${alphaValue}),hsla(240,100%,50%,${alphaValue}),hsla(300,100%,50%,${alphaValue}),hsla(359,100%,50%,${alphaValue}))`,
134
+ "#000",
135
+ ].join(","),
136
+ },
137
+ }
138
+ return result
139
+ }
package/src/color.ts CHANGED
@@ -5,9 +5,13 @@ export abstract class Color implements ColorType {
5
5
  abstract toString(format: ColorFormat | "css"): string
6
6
  abstract clone(): ColorType
7
7
  abstract getChannelRange(channel: ColorChannel): ColorChannelRange
8
- abstract getColorSpace(): ColorFormat
8
+ abstract getColorFormat(): ColorFormat
9
9
  abstract getColorChannels(): [ColorChannel, ColorChannel, ColorChannel]
10
10
 
11
+ hasColorChannel(channel: ColorChannel): boolean {
12
+ return this.getColorChannels().includes(channel)
13
+ }
14
+
11
15
  toHexInt(): number {
12
16
  return this.toFormat("rgb").toHexInt()
13
17
  }
package/src/hsb-color.ts CHANGED
@@ -113,7 +113,7 @@ export class HSBColor extends Color {
113
113
  }
114
114
  }
115
115
 
116
- getColorSpace(): ColorFormat {
116
+ getColorFormat(): ColorFormat {
117
117
  return "hsb"
118
118
  }
119
119
 
package/src/hsl-color.ts CHANGED
@@ -108,7 +108,7 @@ export class HSLColor extends Color {
108
108
  }
109
109
  }
110
110
 
111
- getColorSpace(): ColorFormat {
111
+ getColorFormat(): ColorFormat {
112
112
  return "hsl"
113
113
  }
114
114
 
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { getColorAreaGradient } from "./area-gradient"
1
2
  export { Color } from "./color"
2
- export { parseColor, normalizeColor } from "./parse-color"
3
- export type { ColorChannel, ColorFormat, ColorAxes, ColorChannelRange, ColorType } from "./types"
3
+ export { normalizeColor, parseColor } from "./parse-color"
4
+ export type { ColorAxes, ColorChannel, ColorChannelRange, ColorFormat, ColorType } from "./types"
@@ -0,0 +1,14 @@
1
+ const nativeColors /* @__PURE__ */ =
2
+ "aliceblue:f0f8ff,antiquewhite:faebd7,aqua:00ffff,aquamarine:7fffd4,azure:f0ffff,beige:f5f5dc,bisque:ffe4c4,black:000000,blanchedalmond:ffebcd,blue:0000ff,blueviolet:8a2be2,brown:a52a2a,burlywood:deb887,cadetblue:5f9ea0,chartreuse:7fff00,chocolate:d2691e,coral:ff7f50,cornflowerblue:6495ed,cornsilk:fff8dc,crimson:dc143c,cyan:00ffff,darkblue:00008b,darkcyan:008b8b,darkgoldenrod:b8860b,darkgray:a9a9a9,darkgreen:006400,darkkhaki:bdb76b,darkmagenta:8b008b,darkolivegreen:556b2f,darkorange:ff8c00,darkorchid:9932cc,darkred:8b0000,darksalmon:e9967a,darkseagreen:8fbc8f,darkslateblue:483d8b,darkslategray:2f4f4f,darkturquoise:00ced1,darkviolet:9400d3,deeppink:ff1493,deepskyblue:00bfff,dimgray:696969,dodgerblue:1e90ff,firebrick:b22222,floralwhite:fffaf0,forestgreen:228b22,fuchsia:ff00ff,gainsboro:dcdcdc,ghostwhite:f8f8ff,gold:ffd700,goldenrod:daa520,gray:808080,green:008000,greenyellow:adff2f,honeydew:f0fff0,hotpink:ff69b4,indianred:cd5c5c,indigo:4b0082,ivory:fffff0,khaki:f0e68c,lavender:e6e6fa,lavenderblush:fff0f5,lawngreen:7cfc00,lemonchiffon:fffacd,lightblue:add8e6,lightcoral:f08080,lightcyan:e0ffff,lightgoldenrodyellow:fafad2,lightgrey:d3d3d3,lightgreen:90ee90,lightpink:ffb6c1,lightsalmon:ffa07a,lightseagreen:20b2aa,lightskyblue:87cefa,lightslategray:778899,lightsteelblue:b0c4de,lightyellow:ffffe0,lime:00ff00,limegreen:32cd32,linen:faf0e6,magenta:ff00ff,maroon:800000,mediumaquamarine:66cdaa,mediumblue:0000cd,mediumorchid:ba55d3,mediumpurple:9370d8,mediumseagreen:3cb371,mediumslateblue:7b68ee,mediumspringgreen:00fa9a,mediumturquoise:48d1cc,mediumvioletred:c71585,midnightblue:191970,mintcream:f5fffa,mistyrose:ffe4e1,moccasin:ffe4b5,navajowhite:ffdead,navy:000080,oldlace:fdf5e6,olive:808000,olivedrab:6b8e23,orange:ffa500,orangered:ff4500,orchid:da70d6,palegoldenrod:eee8aa,palegreen:98fb98,paleturquoise:afeeee,palevioletred:d87093,papayawhip:ffefd5,peachpuff:ffdab9,peru:cd853f,pink:ffc0cb,plum:dda0dd,powderblue:b0e0e6,purple:800080,red:ff0000,rosybrown:bc8f8f,royalblue:4169e1,saddlebrown:8b4513,salmon:fa8072,sandybrown:f4a460,seagreen:2e8b57,seashell:fff5ee,sienna:a0522d,silver:c0c0c0,skyblue:87ceeb,slateblue:6a5acd,slategray:708090,snow:fffafa,springgreen:00ff7f,steelblue:4682b4,tan:d2b48c,teal:008080,thistle:d8bfd8,tomato:ff6347,turquoise:40e0d0,violet:ee82ee,wheat:f5deb3,white:ffffff,whitesmoke:f5f5f5,yellow:ffff00,yellowgreen:9acd32"
3
+
4
+ const makeMap = (str: string) => {
5
+ const map = new Map<string, string>()
6
+ const list = str.split(",")
7
+ for (let i = 0; i < list.length; i++) {
8
+ const [key, val] = list[i].split(":")
9
+ map.set(key, `#${val}`)
10
+ }
11
+ return map
12
+ }
13
+
14
+ export const nativeColorMap /* @__PURE__ */ = makeMap(nativeColors)
@@ -1,20 +1,25 @@
1
1
  import { HSBColor } from "./hsb-color"
2
2
  import { HSLColor } from "./hsl-color"
3
+ import { nativeColorMap } from "./native-color"
3
4
  import { RGBColor } from "./rgb-color"
4
5
  import type { ColorType } from "./types"
5
6
 
6
- export function parseColor(value: string): ColorType {
7
- let result = RGBColor.parse(value) || HSBColor.parse(value) || HSLColor.parse(value)
7
+ export const parseColor = (value: string): ColorType => {
8
+ if (nativeColorMap.has(value)) {
9
+ return parseColor(nativeColorMap.get(value)!)
10
+ }
11
+
12
+ const result = RGBColor.parse(value) || HSBColor.parse(value) || HSLColor.parse(value)
13
+
8
14
  if (!result) {
9
- throw new Error("Invalid color value: " + value)
15
+ const error = new Error("Invalid color value: " + value)
16
+ Error.captureStackTrace?.(error, parseColor)
17
+ throw error
10
18
  }
19
+
11
20
  return result
12
21
  }
13
22
 
14
- export function normalizeColor(v: string | ColorType) {
15
- if (typeof v === "string") {
16
- return parseColor(v)
17
- } else {
18
- return v
19
- }
23
+ export const normalizeColor = (v: string | ColorType) => {
24
+ return typeof v === "string" ? parseColor(v) : v
20
25
  }
package/src/rgb-color.ts CHANGED
@@ -196,7 +196,7 @@ export class RGBColor extends Color {
196
196
  }
197
197
  }
198
198
 
199
- getColorSpace(): ColorFormat {
199
+ getColorFormat(): ColorFormat {
200
200
  return "rgb"
201
201
  }
202
202
 
package/src/types.ts CHANGED
@@ -39,7 +39,7 @@ export interface ColorType {
39
39
  /**
40
40
  * Returns the color space, 'rgb', 'hsb' or 'hsl', for the current color.
41
41
  */
42
- getColorSpace(): ColorFormat
42
+ getColorFormat(): ColorFormat
43
43
  /**
44
44
  * Returns the color space axes, xChannel, yChannel, zChannel.
45
45
  */