@colordx/core 0.1.0 → 0.2.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/README.md +243 -0
- package/dist/colordx-Bxv5vPj2.d.cts +120 -0
- package/dist/colordx-Bxv5vPj2.d.ts +120 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +16 -110
- package/dist/index.d.ts +16 -110
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/plugins/a11y.cjs +1 -1
- package/dist/plugins/a11y.cjs.map +1 -1
- package/dist/plugins/a11y.d.cts +1 -1
- package/dist/plugins/a11y.d.ts +1 -1
- package/dist/plugins/a11y.mjs +1 -1
- package/dist/plugins/a11y.mjs.map +1 -1
- package/dist/plugins/cmyk.cjs +2 -0
- package/dist/plugins/cmyk.cjs.map +1 -0
- package/dist/plugins/cmyk.d.cts +11 -0
- package/dist/plugins/cmyk.d.ts +11 -0
- package/dist/plugins/cmyk.mjs +2 -0
- package/dist/plugins/cmyk.mjs.map +1 -0
- package/dist/plugins/delta.cjs +2 -0
- package/dist/plugins/delta.cjs.map +1 -0
- package/dist/plugins/delta.d.cts +10 -0
- package/dist/plugins/delta.d.ts +10 -0
- package/dist/plugins/delta.mjs +2 -0
- package/dist/plugins/delta.mjs.map +1 -0
- package/dist/plugins/harmonies.d.cts +1 -1
- package/dist/plugins/harmonies.d.ts +1 -1
- package/dist/plugins/lab.cjs +2 -0
- package/dist/plugins/lab.cjs.map +1 -0
- package/dist/plugins/lab.d.cts +11 -0
- package/dist/plugins/lab.d.ts +11 -0
- package/dist/plugins/lab.mjs +2 -0
- package/dist/plugins/lab.mjs.map +1 -0
- package/dist/plugins/lch.cjs +2 -0
- package/dist/plugins/lch.cjs.map +1 -0
- package/dist/plugins/lch.d.cts +11 -0
- package/dist/plugins/lch.d.ts +11 -0
- package/dist/plugins/lch.mjs +2 -0
- package/dist/plugins/lch.mjs.map +1 -0
- package/dist/plugins/minify.cjs +2 -0
- package/dist/plugins/minify.cjs.map +1 -0
- package/dist/plugins/minify.d.cts +18 -0
- package/dist/plugins/minify.d.ts +18 -0
- package/dist/plugins/minify.mjs +2 -0
- package/dist/plugins/minify.mjs.map +1 -0
- package/dist/plugins/mix.d.cts +1 -1
- package/dist/plugins/mix.d.ts +1 -1
- package/dist/plugins/names.cjs +1 -1
- package/dist/plugins/names.cjs.map +1 -1
- package/dist/plugins/names.d.cts +1 -1
- package/dist/plugins/names.d.ts +1 -1
- package/dist/plugins/names.mjs +1 -1
- package/dist/plugins/names.mjs.map +1 -1
- package/package.json +7 -1
package/README.md
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# @colordx/core
|
|
2
|
+
|
|
3
|
+
**[Try it on colordx.dev](https://colordx.dev)**
|
|
4
|
+
|
|
5
|
+
A modern color manipulation library built for the CSS Color 4 era. Drop-in upgrade from [colord](https://github.com/omgovich/colord) with first-class support for **OKLCH**, **OKLab**, and a cleaner plugin system.
|
|
6
|
+
|
|
7
|
+
## Why colordx?
|
|
8
|
+
|
|
9
|
+
[colord](https://github.com/omgovich/colord) is a great library, but it was designed around CSS Color 3. Modern CSS uses `oklch()` and `oklab()` — color spaces that are perceptually uniform and supported natively in all modern browsers. With colord, you need plugins for those. With colordx, they're built in.
|
|
10
|
+
|
|
11
|
+
| Feature | colord | colordx |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| `hex`, `rgb`, `hsl`, `hsv`, `hwb` | ✅ core | ✅ core |
|
|
14
|
+
| `oklch`, `oklab` | ❌ not available | ✅ core |
|
|
15
|
+
| `lab` (CIE), `lch` (CIE), `xyz`, `cmyk` | plugin | plugin |
|
|
16
|
+
| `delta()` (CIEDE2000) | plugin | plugin |
|
|
17
|
+
| `names`, `a11y`, `harmonies`, `mix`, `minify` | plugin | plugin |
|
|
18
|
+
| Bundle size (gzipped) | ~2.1 KB | ~3.4 KB |
|
|
19
|
+
| `getFormat()` | ✅ | ✅ |
|
|
20
|
+
|
|
21
|
+
The extra ~1.3 KB buys you OKLab + OKLCH math in core — no plugin import needed for the color spaces that modern CSS actually uses.
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @colordx/core
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { colordx } from '@colordx/core';
|
|
33
|
+
|
|
34
|
+
colordx('#ff0000').toOklch() // { l: 0.6279, c: 0.2577, h: 29.23, a: 1 }
|
|
35
|
+
colordx('#ff0000').toOklchString() // 'oklch(0.6279 0.2577 29.23)'
|
|
36
|
+
colordx('#ff0000').lighten(0.1).toHex() // '#ff3333'
|
|
37
|
+
colordx('oklch(0.5 0.2 240)').toHex() // '#0055c2'
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## API
|
|
41
|
+
|
|
42
|
+
All methods are immutable — they return a new `Colordx` instance.
|
|
43
|
+
|
|
44
|
+
### Parsing
|
|
45
|
+
|
|
46
|
+
Accepts any CSS color string or color object:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
colordx('#ff0000')
|
|
50
|
+
colordx('#f00')
|
|
51
|
+
colordx('rgb(255, 0, 0)')
|
|
52
|
+
colordx('rgba(255, 0, 0, 0.5)')
|
|
53
|
+
colordx('hsl(0, 100%, 50%)')
|
|
54
|
+
colordx('hwb(0 0% 0%)')
|
|
55
|
+
colordx('oklab(0.6279 0.2249 0.1257)')
|
|
56
|
+
colordx('oklch(0.6279 0.2577 29.23)')
|
|
57
|
+
colordx({ r: 255, g: 0, b: 0, a: 1 })
|
|
58
|
+
colordx({ h: 0, s: 100, l: 50, a: 1 })
|
|
59
|
+
colordx({ h: 0, s: 100, v: 100, a: 1 })
|
|
60
|
+
colordx({ h: 0, w: 0, b: 0, a: 1 })
|
|
61
|
+
colordx({ l: 0.6279, a: 0.2249, b: 0.1257, alpha: 1 }) // OKLab
|
|
62
|
+
colordx({ l: 0.6279, c: 0.2577, h: 29.23, a: 1 }) // OKLch
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Conversion
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
.toRgb() // { r: 255, g: 0, b: 0, a: 1 }
|
|
69
|
+
.toRgbString() // 'rgb(255, 0, 0)'
|
|
70
|
+
.toHex() // '#ff0000'
|
|
71
|
+
.toHsl() // { h: 0, s: 100, l: 50, a: 1 }
|
|
72
|
+
.toHslString() // 'hsl(0, 100%, 50%)'
|
|
73
|
+
.toHsv() // { h: 0, s: 100, v: 100, a: 1 }
|
|
74
|
+
.toHwb() // { h: 0, w: 0, b: 0, a: 1 }
|
|
75
|
+
.toHwbString() // 'hwb(0 0% 0%)'
|
|
76
|
+
.toOklab() // { l: 0.6279, a: 0.2249, b: 0.1257, alpha: 1 }
|
|
77
|
+
.toOklabString() // 'oklab(0.6279 0.2249 0.1257)'
|
|
78
|
+
.toOklch() // { l: 0.6279, c: 0.2577, h: 29.23, a: 1 }
|
|
79
|
+
.toOklchString() // 'oklch(0.6279 0.2577 29.23)'
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Manipulation
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
.lighten(0.1) // increase lightness by 10%
|
|
86
|
+
.darken(0.1) // decrease lightness by 10%
|
|
87
|
+
.saturate(0.1) // increase saturation by 10%
|
|
88
|
+
.desaturate(0.1) // decrease saturation by 10%
|
|
89
|
+
.grayscale() // fully desaturate
|
|
90
|
+
.invert() // invert RGB channels
|
|
91
|
+
.rotate(30) // rotate hue by 30°
|
|
92
|
+
.mix('#0000ff', 0.5) // mix with another color
|
|
93
|
+
.alpha(0.5) // set alpha
|
|
94
|
+
.hue(120) // set hue
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Getters
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
.isValid() // true if input was parseable
|
|
101
|
+
.alpha() // get alpha (0–1)
|
|
102
|
+
.hue() // get hue (0–360)
|
|
103
|
+
.brightness() // perceived brightness (0–1)
|
|
104
|
+
.luminance() // relative luminance (0–1, WCAG)
|
|
105
|
+
.isDark() // brightness < 0.5
|
|
106
|
+
.isLight() // brightness >= 0.5
|
|
107
|
+
.contrast('#fff') // WCAG contrast ratio
|
|
108
|
+
.isEqual('#f00') // exact RGB equality
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Utilities
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
import { getFormat, random } from '@colordx/core';
|
|
115
|
+
|
|
116
|
+
getFormat('#ff0000') // 'hex'
|
|
117
|
+
getFormat('oklch(0.5 0.2 240)') // 'lch'
|
|
118
|
+
getFormat({ r: 255, g: 0, b: 0, a: 1 }) // 'rgb'
|
|
119
|
+
getFormat('notacolor') // undefined
|
|
120
|
+
|
|
121
|
+
random() // random Colordx instance
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Gamut
|
|
125
|
+
|
|
126
|
+
OKLCH and OKLab can describe colors outside the sRGB gamut. colordx includes standalone utilities for checking and mapping:
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
import { inGamutSrgb, toGamutSrgb } from '@colordx/core';
|
|
130
|
+
|
|
131
|
+
// Check: is this color displayable in sRGB?
|
|
132
|
+
inGamutSrgb('#ff0000') // true — hex is always sRGB
|
|
133
|
+
inGamutSrgb('oklch(0.6279 0.2577 29.23)') // true — red
|
|
134
|
+
inGamutSrgb('oklch(0.5 0.4 180)') // false — too much cyan chroma
|
|
135
|
+
|
|
136
|
+
// Map: reduce chroma until in-gamut (preserves lightness and hue)
|
|
137
|
+
toGamutSrgb('oklch(0.5 0.4 180)') // → Colordx at the sRGB boundary
|
|
138
|
+
toGamutSrgb('#ff0000') // → unchanged, already in sRGB
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
`inGamutSrgb` is always `true` for sRGB-bounded inputs (hex, rgb, hsl, hsv, hwb).
|
|
142
|
+
`toGamutSrgb` uses a binary search on chroma following the [CSS Color 4 gamut mapping algorithm](https://www.w3.org/TR/css-color-4/#css-gamut-mapping).
|
|
143
|
+
|
|
144
|
+
## Plugins
|
|
145
|
+
|
|
146
|
+
Opt-in plugins for less common color spaces and utilities:
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
import { extend } from '@colordx/core';
|
|
150
|
+
import lab from '@colordx/core/plugins/lab'; // toLab(), toXyz()
|
|
151
|
+
import lch from '@colordx/core/plugins/lch'; // toLch(), toLchString()
|
|
152
|
+
import cmyk from '@colordx/core/plugins/cmyk'; // toCmyk(), toCmykString()
|
|
153
|
+
import delta from '@colordx/core/plugins/delta'; // delta() — CIEDE2000
|
|
154
|
+
import names from '@colordx/core/plugins/names'; // toName(), parse CSS color names
|
|
155
|
+
import a11y from '@colordx/core/plugins/a11y'; // isReadable(), minReadable()
|
|
156
|
+
import harmonies from '@colordx/core/plugins/harmonies'; // harmonies()
|
|
157
|
+
import mix from '@colordx/core/plugins/mix'; // tint(), shade(), tone(), palette()
|
|
158
|
+
import minify from '@colordx/core/plugins/minify'; // minify()
|
|
159
|
+
|
|
160
|
+
extend([lab, lch, cmyk, delta, names, a11y, harmonies, mix, minify]);
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Migrating from colord
|
|
164
|
+
|
|
165
|
+
The API is intentionally compatible. Most code works unchanged:
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
// Before
|
|
169
|
+
import { colord } from 'colord';
|
|
170
|
+
const c = colord('#ff0000');
|
|
171
|
+
|
|
172
|
+
// After
|
|
173
|
+
import { colordx } from '@colordx/core';
|
|
174
|
+
const c = colordx('#ff0000');
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### What's the same
|
|
178
|
+
|
|
179
|
+
All core manipulation and conversion methods have identical signatures:
|
|
180
|
+
`.toHex()`, `.toRgb()`, `.toRgbString()`, `.toHsl()`, `.toHslString()`, `.toHsv()`, `.toHwb()`, `.toHwbString()`, `.lighten()`, `.darken()`, `.saturate()`, `.desaturate()`, `.grayscale()`, `.invert()`, `.rotate()`, `.mix()`, `.alpha()`, `.hue()`, `.brightness()`, `.luminance()`, `.isDark()`, `.isLight()`, `.contrast()`, `.isEqual()`, `getFormat()`, `random()`
|
|
181
|
+
|
|
182
|
+
### What changed
|
|
183
|
+
|
|
184
|
+
**OKLCH and OKLab are now core** — no plugin needed:
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
// colord (requires plugin — not available)
|
|
188
|
+
// colordx
|
|
189
|
+
colordx('#ff0000').toOklch()
|
|
190
|
+
colordx('#ff0000').toOklchString()
|
|
191
|
+
colordx('oklch(0.5 0.2 240)').toHex()
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
**CIE Lab, LCH, XYZ, CMYK moved to plugins:**
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
// colord
|
|
198
|
+
import { colord } from 'colord';
|
|
199
|
+
import labPlugin from 'colord/plugins/lab';
|
|
200
|
+
import lchPlugin from 'colord/plugins/lch';
|
|
201
|
+
import xyzPlugin from 'colord/plugins/xyz';
|
|
202
|
+
import cmykPlugin from 'colord/plugins/cmyk';
|
|
203
|
+
colordExtend([labPlugin, lchPlugin, xyzPlugin, cmykPlugin]);
|
|
204
|
+
|
|
205
|
+
// colordx
|
|
206
|
+
import { extend } from '@colordx/core';
|
|
207
|
+
import lab from '@colordx/core/plugins/lab';
|
|
208
|
+
import lch from '@colordx/core/plugins/lch';
|
|
209
|
+
import cmyk from '@colordx/core/plugins/cmyk';
|
|
210
|
+
extend([lab, lch, cmyk]);
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
**`delta()` moved to a plugin:**
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
// colord
|
|
217
|
+
import deltaPlugin from 'colord/plugins/lab';
|
|
218
|
+
colordExtend([deltaPlugin]);
|
|
219
|
+
colord('#ff0000').delta('#00ff00');
|
|
220
|
+
|
|
221
|
+
// colordx
|
|
222
|
+
import delta from '@colordx/core/plugins/delta';
|
|
223
|
+
extend([delta]);
|
|
224
|
+
colordx('#ff0000').delta('#00ff00');
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
**`getFormat()` import path:**
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
// colord
|
|
231
|
+
import { getFormat } from 'colord';
|
|
232
|
+
|
|
233
|
+
// colordx
|
|
234
|
+
import { getFormat } from '@colordx/core';
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### HSL/HSV precision
|
|
238
|
+
|
|
239
|
+
colordx returns higher precision HSL/HSV values than colord. If your code does exact equality checks on `.toHsl()` output, use `toBeCloseTo` or round the values.
|
|
240
|
+
|
|
241
|
+
## License
|
|
242
|
+
|
|
243
|
+
MIT
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
interface RgbColor {
|
|
2
|
+
r: number;
|
|
3
|
+
g: number;
|
|
4
|
+
b: number;
|
|
5
|
+
a: number;
|
|
6
|
+
}
|
|
7
|
+
interface HslColor {
|
|
8
|
+
h: number;
|
|
9
|
+
s: number;
|
|
10
|
+
l: number;
|
|
11
|
+
a: number;
|
|
12
|
+
}
|
|
13
|
+
interface HsvColor {
|
|
14
|
+
h: number;
|
|
15
|
+
s: number;
|
|
16
|
+
v: number;
|
|
17
|
+
a: number;
|
|
18
|
+
}
|
|
19
|
+
interface HwbColor {
|
|
20
|
+
h: number;
|
|
21
|
+
w: number;
|
|
22
|
+
b: number;
|
|
23
|
+
a: number;
|
|
24
|
+
}
|
|
25
|
+
/** CIE LAB (D50) */
|
|
26
|
+
interface LabColor {
|
|
27
|
+
l: number;
|
|
28
|
+
a: number;
|
|
29
|
+
b: number;
|
|
30
|
+
alpha: number;
|
|
31
|
+
}
|
|
32
|
+
/** CIE LCH (D50) */
|
|
33
|
+
interface LchColor {
|
|
34
|
+
l: number;
|
|
35
|
+
c: number;
|
|
36
|
+
h: number;
|
|
37
|
+
a: number;
|
|
38
|
+
}
|
|
39
|
+
/** CIE XYZ (D65) */
|
|
40
|
+
interface XyzColor {
|
|
41
|
+
x: number;
|
|
42
|
+
y: number;
|
|
43
|
+
z: number;
|
|
44
|
+
a: number;
|
|
45
|
+
}
|
|
46
|
+
interface CmykColor {
|
|
47
|
+
c: number;
|
|
48
|
+
m: number;
|
|
49
|
+
y: number;
|
|
50
|
+
k: number;
|
|
51
|
+
a: number;
|
|
52
|
+
}
|
|
53
|
+
/** Oklab — perceptually uniform, D65 */
|
|
54
|
+
interface OklabColor {
|
|
55
|
+
l: number;
|
|
56
|
+
a: number;
|
|
57
|
+
b: number;
|
|
58
|
+
alpha: number;
|
|
59
|
+
}
|
|
60
|
+
/** Oklch — perceptually uniform, polar form of Oklab */
|
|
61
|
+
interface OklchColor {
|
|
62
|
+
l: number;
|
|
63
|
+
c: number;
|
|
64
|
+
h: number;
|
|
65
|
+
a: number;
|
|
66
|
+
}
|
|
67
|
+
/** CSS Color 4 Display-P3 */
|
|
68
|
+
interface P3Color {
|
|
69
|
+
r: number;
|
|
70
|
+
g: number;
|
|
71
|
+
b: number;
|
|
72
|
+
a: number;
|
|
73
|
+
}
|
|
74
|
+
type AnyColor = string | RgbColor | HslColor | HsvColor | HwbColor | LabColor | LchColor | XyzColor | CmykColor | OklabColor | OklchColor | P3Color;
|
|
75
|
+
type ColorParser<T = AnyColor> = (input: T) => RgbColor | null;
|
|
76
|
+
type ColorFormat = 'hex' | 'rgb' | 'hsl' | 'hsv' | 'hwb' | 'lab' | 'lch' | 'xyz' | 'cmyk' | 'name';
|
|
77
|
+
|
|
78
|
+
declare class Colordx {
|
|
79
|
+
private readonly _rgb;
|
|
80
|
+
private readonly _valid;
|
|
81
|
+
constructor(input: AnyColor);
|
|
82
|
+
isValid(): boolean;
|
|
83
|
+
toRgb(): RgbColor;
|
|
84
|
+
toRgbString(): string;
|
|
85
|
+
toHex(): string;
|
|
86
|
+
toHsl(): HslColor;
|
|
87
|
+
toHslString(): string;
|
|
88
|
+
toHsv(): HsvColor;
|
|
89
|
+
toHwb(): HwbColor;
|
|
90
|
+
toHwbString(): string;
|
|
91
|
+
toOklab(): OklabColor;
|
|
92
|
+
toOklabString(): string;
|
|
93
|
+
toOklch(): OklchColor;
|
|
94
|
+
toOklchString(): string;
|
|
95
|
+
brightness(): number;
|
|
96
|
+
luminance(): number;
|
|
97
|
+
isDark(): boolean;
|
|
98
|
+
isLight(): boolean;
|
|
99
|
+
alpha(): number;
|
|
100
|
+
alpha(value: number): Colordx;
|
|
101
|
+
hue(): number;
|
|
102
|
+
hue(value: number): Colordx;
|
|
103
|
+
lighten(amount?: number): Colordx;
|
|
104
|
+
darken(amount?: number): Colordx;
|
|
105
|
+
saturate(amount?: number): Colordx;
|
|
106
|
+
desaturate(amount?: number): Colordx;
|
|
107
|
+
grayscale(): Colordx;
|
|
108
|
+
invert(): Colordx;
|
|
109
|
+
rotate(amount?: number): Colordx;
|
|
110
|
+
mix(color: AnyColor, ratio?: number): Colordx;
|
|
111
|
+
contrast(color?: AnyColor): number;
|
|
112
|
+
isEqual(color: AnyColor): boolean;
|
|
113
|
+
toString(): string;
|
|
114
|
+
}
|
|
115
|
+
type Plugin = (ColordxClass: typeof Colordx, parsers: ColorParser[]) => void;
|
|
116
|
+
declare const colordx: (input: AnyColor) => Colordx;
|
|
117
|
+
declare const extend: (plugins: Plugin[]) => void;
|
|
118
|
+
declare const random: () => Colordx;
|
|
119
|
+
|
|
120
|
+
export { type AnyColor as A, type ColorFormat as C, type HslColor as H, type LabColor as L, type OklabColor as O, type P3Color as P, type RgbColor as R, type XyzColor as X, Colordx as a, type CmykColor as b, type ColorParser as c, type HsvColor as d, type HwbColor as e, type LchColor as f, type OklchColor as g, type Plugin as h, colordx as i, extend as j, random as r };
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
interface RgbColor {
|
|
2
|
+
r: number;
|
|
3
|
+
g: number;
|
|
4
|
+
b: number;
|
|
5
|
+
a: number;
|
|
6
|
+
}
|
|
7
|
+
interface HslColor {
|
|
8
|
+
h: number;
|
|
9
|
+
s: number;
|
|
10
|
+
l: number;
|
|
11
|
+
a: number;
|
|
12
|
+
}
|
|
13
|
+
interface HsvColor {
|
|
14
|
+
h: number;
|
|
15
|
+
s: number;
|
|
16
|
+
v: number;
|
|
17
|
+
a: number;
|
|
18
|
+
}
|
|
19
|
+
interface HwbColor {
|
|
20
|
+
h: number;
|
|
21
|
+
w: number;
|
|
22
|
+
b: number;
|
|
23
|
+
a: number;
|
|
24
|
+
}
|
|
25
|
+
/** CIE LAB (D50) */
|
|
26
|
+
interface LabColor {
|
|
27
|
+
l: number;
|
|
28
|
+
a: number;
|
|
29
|
+
b: number;
|
|
30
|
+
alpha: number;
|
|
31
|
+
}
|
|
32
|
+
/** CIE LCH (D50) */
|
|
33
|
+
interface LchColor {
|
|
34
|
+
l: number;
|
|
35
|
+
c: number;
|
|
36
|
+
h: number;
|
|
37
|
+
a: number;
|
|
38
|
+
}
|
|
39
|
+
/** CIE XYZ (D65) */
|
|
40
|
+
interface XyzColor {
|
|
41
|
+
x: number;
|
|
42
|
+
y: number;
|
|
43
|
+
z: number;
|
|
44
|
+
a: number;
|
|
45
|
+
}
|
|
46
|
+
interface CmykColor {
|
|
47
|
+
c: number;
|
|
48
|
+
m: number;
|
|
49
|
+
y: number;
|
|
50
|
+
k: number;
|
|
51
|
+
a: number;
|
|
52
|
+
}
|
|
53
|
+
/** Oklab — perceptually uniform, D65 */
|
|
54
|
+
interface OklabColor {
|
|
55
|
+
l: number;
|
|
56
|
+
a: number;
|
|
57
|
+
b: number;
|
|
58
|
+
alpha: number;
|
|
59
|
+
}
|
|
60
|
+
/** Oklch — perceptually uniform, polar form of Oklab */
|
|
61
|
+
interface OklchColor {
|
|
62
|
+
l: number;
|
|
63
|
+
c: number;
|
|
64
|
+
h: number;
|
|
65
|
+
a: number;
|
|
66
|
+
}
|
|
67
|
+
/** CSS Color 4 Display-P3 */
|
|
68
|
+
interface P3Color {
|
|
69
|
+
r: number;
|
|
70
|
+
g: number;
|
|
71
|
+
b: number;
|
|
72
|
+
a: number;
|
|
73
|
+
}
|
|
74
|
+
type AnyColor = string | RgbColor | HslColor | HsvColor | HwbColor | LabColor | LchColor | XyzColor | CmykColor | OklabColor | OklchColor | P3Color;
|
|
75
|
+
type ColorParser<T = AnyColor> = (input: T) => RgbColor | null;
|
|
76
|
+
type ColorFormat = 'hex' | 'rgb' | 'hsl' | 'hsv' | 'hwb' | 'lab' | 'lch' | 'xyz' | 'cmyk' | 'name';
|
|
77
|
+
|
|
78
|
+
declare class Colordx {
|
|
79
|
+
private readonly _rgb;
|
|
80
|
+
private readonly _valid;
|
|
81
|
+
constructor(input: AnyColor);
|
|
82
|
+
isValid(): boolean;
|
|
83
|
+
toRgb(): RgbColor;
|
|
84
|
+
toRgbString(): string;
|
|
85
|
+
toHex(): string;
|
|
86
|
+
toHsl(): HslColor;
|
|
87
|
+
toHslString(): string;
|
|
88
|
+
toHsv(): HsvColor;
|
|
89
|
+
toHwb(): HwbColor;
|
|
90
|
+
toHwbString(): string;
|
|
91
|
+
toOklab(): OklabColor;
|
|
92
|
+
toOklabString(): string;
|
|
93
|
+
toOklch(): OklchColor;
|
|
94
|
+
toOklchString(): string;
|
|
95
|
+
brightness(): number;
|
|
96
|
+
luminance(): number;
|
|
97
|
+
isDark(): boolean;
|
|
98
|
+
isLight(): boolean;
|
|
99
|
+
alpha(): number;
|
|
100
|
+
alpha(value: number): Colordx;
|
|
101
|
+
hue(): number;
|
|
102
|
+
hue(value: number): Colordx;
|
|
103
|
+
lighten(amount?: number): Colordx;
|
|
104
|
+
darken(amount?: number): Colordx;
|
|
105
|
+
saturate(amount?: number): Colordx;
|
|
106
|
+
desaturate(amount?: number): Colordx;
|
|
107
|
+
grayscale(): Colordx;
|
|
108
|
+
invert(): Colordx;
|
|
109
|
+
rotate(amount?: number): Colordx;
|
|
110
|
+
mix(color: AnyColor, ratio?: number): Colordx;
|
|
111
|
+
contrast(color?: AnyColor): number;
|
|
112
|
+
isEqual(color: AnyColor): boolean;
|
|
113
|
+
toString(): string;
|
|
114
|
+
}
|
|
115
|
+
type Plugin = (ColordxClass: typeof Colordx, parsers: ColorParser[]) => void;
|
|
116
|
+
declare const colordx: (input: AnyColor) => Colordx;
|
|
117
|
+
declare const extend: (plugins: Plugin[]) => void;
|
|
118
|
+
declare const random: () => Colordx;
|
|
119
|
+
|
|
120
|
+
export { type AnyColor as A, type ColorFormat as C, type HslColor as H, type LabColor as L, type OklabColor as O, type P3Color as P, type RgbColor as R, type XyzColor as X, Colordx as a, type CmykColor as b, type ColorParser as c, type HsvColor as d, type HwbColor as e, type LchColor as f, type OklchColor as g, type Plugin as h, colordx as i, extend as j, random as r };
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var b=(o,r,t)=>Math.min(Math.max(o,r),t),l=(o,r=0)=>Math.round(o*10**r)/10**r;var m=o=>typeof o=="number"&&!Number.isNaN(o)&&Number.isFinite(o),C=o=>typeof o=="object"&&o!==null&&!Array.isArray(o),f=(o,r)=>r.every(t=>t in o);var H=o=>parseInt(o.padEnd(2,o),16),v=o=>{if(typeof o!="string")return null;let r=o.trim().replace(/^#/,"");if(!/^[0-9a-f]{3,8}$/i.test(r))return null;if(r.length===3||r.length===4){let[t,e,n,s="f"]=r.split("");return {r:H(t),g:H(e),b:H(n),a:l(H(s)/255,2)}}return r.length===6||r.length===8?{r:parseInt(r.slice(0,2),16),g:parseInt(r.slice(2,4),16),b:parseInt(r.slice(4,6),16),a:r.length===8?l(parseInt(r.slice(6,8),16)/255,2):1}:null},k=({r:o,g:r,b:t,a:e})=>{let n=a=>b(Math.round(a),0,255).toString(16).padStart(2,"0"),s=`#${n(o)}${n(r)}${n(t)}`;return e<1?s+n(e*255):s};var y=o=>({h:(o.h%360+360)%360,s:b(o.s,0,100),l:b(o.l,0,100),a:b(l(o.a,2),0,1)}),j=({r:o,g:r,b:t,a:e})=>{let n=o/255,s=r/255,a=t/255,u=Math.max(n,s,a),c=Math.min(n,s,a),g=(u+c)/2,i=0,h=0;if(u!==c){let p=u-c;switch(h=g>.5?p/(2-u-c):p/(u+c),u){case n:i=((s-a)/p+(s<a?6:0))/6;break;case s:i=((a-n)/p+2)/6;break;case a:i=((n-s)/p+4)/6;break}}return y({h:l(i*360,2),s:l(h*100,2),l:l(g*100,2),a:e})},d=({h:o,s:r,l:t,a:e})=>{let n=r/100,s=t/100,a=s<.5?s*(1+n):s+n-s*n,u=2*s-a,c=o/360,g=i=>(i<0&&(i+=1),i>1&&(i-=1),i<1/6?u+(a-u)*6*i:i<1/2?a:i<2/3?u+(a-u)*(2/3-i)*6:u);return {r:l(g(c+1/3)*255),g:l(g(c)*255),b:l(g(c-1/3)*255),a:e}},$=o=>{if(!C(o)||!f(o,["h","s","l"]))return null;let{h:r,s:t,l:e,a:n=1}=o;return !m(r)||!m(t)||!m(e)||!m(n)?null:d(y({h:r,s:t,l:e,a:n}))},M=o=>{if(typeof o!="string")return null;let r=o.match(/^hsla?\(\s*([\d.]+)\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%(?:\s*,\s*([\d.]+))?\s*\)$/i);return r?d(y({h:parseFloat(r[1]),s:parseFloat(r[2]),l:parseFloat(r[3]),a:r[4]!==void 0?parseFloat(r[4]):1})):null};var T=o=>({h:(o.h%360+360)%360,s:b(o.s,0,100),v:b(o.v,0,100),a:b(l(o.a,2),0,1)}),O=({r:o,g:r,b:t,a:e})=>{let n=o/255,s=r/255,a=t/255,u=Math.max(n,s,a),c=Math.min(n,s,a),g=u-c,i=u,h=u===0?0:g/u,p=0;if(u!==c)switch(u){case n:p=((s-a)/g+(s<a?6:0))/6;break;case s:p=((a-n)/g+2)/6;break;case a:p=((n-s)/g+4)/6;break}return T({h:l(p*360,2),s:l(h*100,2),v:l(i*100,2),a:e})},I=({h:o,s:r,v:t,a:e})=>{let n=r/100,s=t/100,a=Math.floor(o/60%6),u=o/60-Math.floor(o/60),c=s*(1-n),g=s*(1-u*n),i=s*(1-(1-u)*n),[h,p,N]=[[s,i,c,c,g,s],[g,s,s,i,c,c],[c,c,g,s,s,i]].map(_=>l(_[a]*255));return {r:h,g:p,b:N,a:e}},A=o=>{if(!C(o)||!f(o,["h","s","v"]))return null;let{h:r,s:t,v:e,a:n=1}=o;return !m(r)||!m(t)||!m(e)||!m(n)?null:I(T({h:r,s:t,v:e,a:n}))};var P=o=>({r:b(l(o.r),0,255),g:b(l(o.g),0,255),b:b(l(o.b),0,255),a:b(l(o.a,2),0,1)}),F=o=>{if(!C(o)||!f(o,["r","g","b"]))return null;let{r,g:t,b:e,a:n=1}=o;return !m(r)||!m(t)||!m(e)||!m(n)?null:P({r,g:t,b:e,a:n})},S=o=>{if(typeof o!="string")return null;let r=o.match(/^rgba?\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)(?:\s*,\s*([\d.]+))?\s*\)$/i);return r?P({r:parseFloat(r[1]),g:parseFloat(r[2]),b:parseFloat(r[3]),a:r[4]!==void 0?parseFloat(r[4]):1}):null};var K=[v,S,M,F,$,A],R=[...K],w=o=>{for(let r of R){let t=r(o);if(t)return t}return null};var x=class o{_rgb;constructor(r){this._rgb=w(r)??{r:0,g:0,b:0,a:1};}isValid(){return w(this._rgb)!==null}toRgb(){return {...this._rgb}}toRgbString(){let{r,g:t,b:e,a:n}=this._rgb;return n<1?`rgba(${r}, ${t}, ${e}, ${n})`:`rgb(${r}, ${t}, ${e})`}toHex(){return k(this._rgb)}toHsl(){return j(this._rgb)}toHslString(){let{h:r,s:t,l:e,a:n}=this.toHsl();return n<1?`hsla(${r}, ${t}%, ${e}%, ${n})`:`hsl(${r}, ${t}%, ${e}%)`}toHsv(){return O(this._rgb)}brightness(){let{r,g:t,b:e}=this._rgb;return l((r*299+t*587+e*114)/255e3,2)}luminance(){let r=s=>{let a=s/255;return a<=.03928?a/12.92:((a+.055)/1.055)**2.4},{r:t,g:e,b:n}=this._rgb;return l(.2126*r(t)+.7152*r(e)+.0722*r(n),4)}isDark(){return this.brightness()<.5}isLight(){return this.brightness()>=.5}alpha(r){return r===void 0?this._rgb.a:new o({...this._rgb,a:b(r,0,1)})}hue(r){let t=this.toHsl();return r===void 0?t.h:new o(d({...t,h:r}))}lighten(r=.1){let t=this.toHsl();return new o(d({...t,l:b(t.l+r*100,0,100)}))}darken(r=.1){return this.lighten(-r)}saturate(r=.1){let t=this.toHsl();return new o(d({...t,s:b(t.s+r*100,0,100)}))}desaturate(r=.1){return this.saturate(-r)}grayscale(){return this.desaturate(1)}invert(){let{r,g:t,b:e,a:n}=this._rgb;return new o({r:255-r,g:255-t,b:255-e,a:n})}rotate(r=15){return this.hue(this.hue()+r)}mix(r,t=.5){let e=new o(r).toRgb(),n=this._rgb,s=b(t,0,1);return new o({r:l(n.r*(1-s)+e.r*s),g:l(n.g*(1-s)+e.g*s),b:l(n.b*(1-s)+e.b*s),a:l(n.a*(1-s)+e.a*s,2)})}contrast(r="#fff"){let t=this.luminance(),e=new o(r).luminance(),n=Math.max(t,e),s=Math.min(t,e);return l((n+.05)/(s+.05),2)}isEqual(r){let t=new o(r).toRgb(),e=this._rgb;return e.r===t.r&&e.g===t.g&&e.b===t.b&&e.a===t.a}toString(){return this.toHex()}},L=o=>new x(o),q=o=>{o.forEach(r=>r(x,R));},E=()=>new x({r:Math.round(Math.random()*255),g:Math.round(Math.random()*255),b:Math.round(Math.random()*255),a:1});exports.Colordx=x;exports.colordx=L;exports.extend=q;exports.random=E;//# sourceMappingURL=index.cjs.map
|
|
1
|
+
'use strict';var u=(o,r,n)=>Math.min(Math.max(o,r),n),a=(o,r=0)=>parseFloat(o.toFixed(r));var C=o=>o>=0&&o<360?o:(o%360+360)%360,k={deg:1,grad:.9,turn:360,rad:360/(2*Math.PI)},i=o=>typeof o=="number"&&!Number.isNaN(o)&&Number.isFinite(o),d=o=>typeof o=="object"&&o!==null&&!Array.isArray(o),f=(o,r)=>r.every(n=>n in o);var O=o=>parseInt(o.padEnd(2,o),16),E=o=>{if(typeof o!="string")return null;let r=o.trim().replace(/^#/,"");if(!/^[0-9a-f]{3,8}$/i.test(r))return null;if(r.length===3||r.length===4){let[n,e,t,s="f"]=r.split("");return {r:O(n),g:O(e),b:O(t),a:a(O(s)/255,2)}}return r.length===6||r.length===8?{r:parseInt(r.slice(0,2),16),g:parseInt(r.slice(2,4),16),b:parseInt(r.slice(4,6),16),a:r.length===8?a(parseInt(r.slice(6,8),16)/255,2):1}:null},K=({r:o,g:r,b:n,a:e})=>{let t=l=>u(Math.round(l),0,255).toString(16).padStart(2,"0"),s=`#${t(o)}${t(r)}${t(n)}`;return e<1?s+t(e*255):s};var T=o=>({h:C(o.h),s:u(o.s,0,100),l:u(o.l,0,100),a:u(a(o.a,2),0,1)}),G=({r:o,g:r,b:n,a:e})=>{let t=o/255,s=r/255,l=n/255,b=Math.max(t,s,l),c=Math.min(t,s,l),h=(b+c)/2,m=0,p=0;if(b!==c){let g=b-c;switch(p=h>.5?g/(2-b-c):g/(b+c),b){case t:m=((s-l)/g+(s<l?6:0))/6;break;case s:m=((l-t)/g+2)/6;break;case l:m=((t-s)/g+4)/6;break}}return T({h:a(m*360,2),s:a(p*100,2),l:a(h*100,2),a:e})},w=({h:o,s:r,l:n,a:e})=>{let t=r/100,s=n/100,l=s<.5?s*(1+t):s+t-s*t,b=2*s-l,c=o/360,h=m=>(m<0&&(m+=1),m>1&&(m-=1),m<1/6?b+(l-b)*6*m:m<1/2?l:m<2/3?b+(l-b)*(2/3-m)*6:b);return {r:a(h(c+1/3)*255),g:a(h(c)*255),b:a(h(c-1/3)*255),a:e}},z=o=>{if(!d(o)||!f(o,["h","s","l"]))return null;let{h:r,s:n,l:e,a:t=1}=o;return !i(r)||!i(n)||!i(e)||!i(t)?null:w(T({h:r,s:n,l:e,a:t}))},q=o=>{if(typeof o!="string")return null;let r=o.match(/^hsla?\(\s*([\d.]+)\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%(?:\s*,\s*([\d.]+))?\s*\)$/i);return r?w(T({h:parseFloat(r[1]),s:parseFloat(r[2]),l:parseFloat(r[3]),a:r[4]!==void 0?parseFloat(r[4]):1})):null};var B=o=>({h:C(o.h),s:u(o.s,0,100),v:u(o.v,0,100),a:u(a(o.a,2),0,1)}),N=({r:o,g:r,b:n,a:e})=>{let t=o/255,s=r/255,l=n/255,b=Math.max(t,s,l),c=Math.min(t,s,l),h=b-c,m=b,p=b===0?0:h/b,g=0;if(b!==c)switch(b){case t:g=((s-l)/h+(s<l?6:0))/6;break;case s:g=((l-t)/h+2)/6;break;case l:g=((t-s)/h+4)/6;break}return B({h:a(g*360,2),s:a(p*100,2),v:a(m*100,2),a:e})},_=({h:o,s:r,v:n,a:e})=>{let t=r/100,s=n/100,l=Math.floor(o/60%6),b=o/60-Math.floor(o/60),c=s*(1-t),h=s*(1-b*t),m=s*(1-(1-b)*t),[p,g,v]=[[s,h,c,c,m,s],[m,s,s,h,c,c],[c,c,m,s,s,h]].map(j=>a(j[l]*255));return {r:p,g,b:v,a:e}},D=o=>{if(!d(o)||!f(o,["h","s","v"]))return null;let{h:r,s:n,v:e,a:t=1}=o;return !i(r)||!i(n)||!i(e)||!i(t)?null:_(B({h:r,s:n,v:e,a:t}))};var S=o=>({h:C(o.h),w:u(o.w,0,100),b:u(o.b,0,100),a:u(a(o.a,2),0,1)}),U=o=>({h:a(o.h),w:a(o.w),b:a(o.b),a:a(o.a,3)}),V=o=>{let{h:r}=N(o);return S({h:r,w:Math.min(o.r,o.g,o.b)/255*100,b:100-Math.max(o.r,o.g,o.b)/255*100,a:o.a})},W=({h:o,w:r,b:n,a:e})=>{let t=n===100?0:100-r/(100-n)*100;return _({h:o,s:t,v:100-n,a:e})},X=o=>{if(!d(o)||!f(o,["h","w","b"]))return null;let{h:r,w:n,b:e,a:t=1}=o;return !i(r)||!i(n)||!i(e)||!i(t)?null:W(S({h:r,w:n,b:e,a:t}))},gr=/^hwb\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,J=o=>{if(typeof o!="string")return null;let r=gr.exec(o);if(!r)return null;let n=r[2]?.toLowerCase()??"deg",e=Number(r[1])*(k[n]??1),t=r[5]===void 0?1:Number(r[5])/(r[6]?100:1);return W(S({h:e,w:Number(r[3]),b:Number(r[4]),a:t}))};var P=o=>{let r=o/255;return r<=.04045?r/12.92:((r+.055)/1.055)**2.4},A=o=>o<=.0031308?12.92*o:1.055*o**(1/2.4)-.055,$=({r:o,g:r,b:n,a:e})=>{let t=P(o),s=P(r),l=P(n),b=.4122214708*t+.5363325363*s+.0514459929*l,c=.2119034982*t+.6806995451*s+.1073969566*l,h=.0883024619*t+.2817188376*s+.6299787005*l,m=Math.cbrt(b),p=Math.cbrt(c),g=Math.cbrt(h);return {l:.2104542553*m+.793617785*p-.0040720468*g,a:1.9779984951*m-2.428592205*p+.4505937099*g,b:.0259040371*m+.7827717662*p-.808675766*g,alpha:e}},M=({l:o,a:r,b:n,alpha:e})=>{let t=o+.3963377774*r+.2158037573*n,s=o-.1055613458*r-.0638541728*n,l=o-.0894841775*r-1.291485548*n,b=t**3,c=s**3,h=l**3,m=4.0767416613*b-3.3077115904*c+.2309699287*h,p=-1.2684380041*b+2.6097574007*c-.3413193963*h,g=-0.0041960865*b-.7034186145*c+1.7076147009*h;return {r:u(a(A(u(m,0,1))*255),0,255),g:u(a(A(u(p,0,1))*255),0,255),b:u(a(A(u(g,0,1))*255),0,255),a:u(e,0,1)}},Q=o=>{if(!d(o)||!f(o,["l","a","b"])||"r"in o||"x"in o||"c"in o||"h"in o)return null;!("alpha"in o)&&"a"in o&&o.a;let{l:r,a:n,b:e,alpha:t=1}=o;return !i(r)||!i(n)||!i(e)||!i(t)||r>1?null:M({l:r,a:n,b:e,alpha:u(t,0,1)})},pr=/^oklab\(\s*([+-]?\d*\.?\d+)(%?)\s+([+-]?\d*\.?\d+)(%?)\s+([+-]?\d*\.?\d+)(%?)\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,Y=o=>{if(typeof o!="string")return null;let r=pr.exec(o);if(!r)return null;let n=r[2]?Number(r[1])/100:Number(r[1]),e=r[4]?Number(r[3])*.004:Number(r[3]),t=r[6]?Number(r[5])*.004:Number(r[5]),s=r[7]===void 0?1:Number(r[7])/(r[8]?100:1);return M({l:n,a:e,b:t,alpha:u(s,0,1)})};var dr=({l:o,a:r,b:n,alpha:e})=>{let t=Math.sqrt(r**2+n**2),s=Math.atan2(n,r)*180/Math.PI;return {l:o,c:t,h:C(s),a:e}},fr=({l:o,c:r,h:n,a:e})=>({l:o,a:r*Math.cos(n*Math.PI/180),b:r*Math.sin(n*Math.PI/180),alpha:e}),Z=o=>dr($(o)),rr=o=>M(fr(o)),or=o=>{if(!d(o)||!f(o,["l","c","h"]))return null;let{l:r,c:n,h:e,a:t=1}=o;return !i(r)||!i(n)||!i(e)||!i(t)||r>1?null:rr({l:r,c:n,h:C(Number(e)),a:u(t,0,1)})},Cr=/^oklch\(\s*([+-]?\d*\.?\d+)(%?)\s+([+-]?\d*\.?\d+)(%?)\s+([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,nr=o=>{if(typeof o!="string")return null;let r=Cr.exec(o);if(!r)return null;let n=r[2]?Number(r[1])/100:Number(r[1]),e=r[4]?Number(r[3])*.004:Number(r[3]),t=r[6]?.toLowerCase()??"deg",s=Number(r[5])*(k[t]??1),l=r[7]===void 0?1:Number(r[7])/(r[8]?100:1);return rr({l:n,c:e,h:C(s),a:u(l,0,1)})};var tr=o=>({r:u(a(o.r),0,255),g:u(a(o.g),0,255),b:u(a(o.b),0,255),a:u(a(o.a,2),0,1)}),er=o=>{if(!d(o)||!f(o,["r","g","b"]))return null;let{r,g:n,b:e,a:t=1}=o;return !i(r)||!i(n)||!i(e)||!i(t)?null:tr({r,g:n,b:e,a:t})},sr=o=>{if(typeof o!="string")return null;let r=o.match(/^rgba?\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)(?:\s*,\s*([\d.]+))?\s*\)$/i);return r?tr({r:parseFloat(r[1]),g:parseFloat(r[2]),b:parseFloat(r[3]),a:r[4]!==void 0?parseFloat(r[4]):1}):null};var lr=[[E,"hex"],[sr,"rgb"],[q,"hsl"],[J,"hwb"],[nr,"lch"],[Y,"lab"]],ar=[[er,"rgb"],[z,"hsl"],[D,"hsv"],[X,"hwb"],[Q,"lab"],[or,"lch"]],xr=[...lr,...ar],br=xr.map(([o])=>o),y=[...br],ur=o=>{for(let r of y){let n=r(o);if(n)return n}return null},kr=o=>{let r=typeof o=="string"?lr:ar;for(let[n,e]of r)if(n(o))return e;for(let n=br.length;n<y.length;n++)if(y[n](o))return "name"};var x=class o{_rgb;_valid;constructor(r){let n=ur(r);this._valid=n!==null,this._rgb=n??{r:0,g:0,b:0,a:1};}isValid(){return this._valid}toRgb(){return {...this._rgb}}toRgbString(){let{r,g:n,b:e,a:t}=this._rgb;return t<1?`rgba(${r}, ${n}, ${e}, ${t})`:`rgb(${r}, ${n}, ${e})`}toHex(){return K(this._rgb)}toHsl(){return G(this._rgb)}toHslString(){let{h:r,s:n,l:e,a:t}=this.toHsl();return t<1?`hsla(${r}, ${n}%, ${e}%, ${t})`:`hsl(${r}, ${n}%, ${e}%)`}toHsv(){return N(this._rgb)}toHwb(){return U(V(this._rgb))}toHwbString(){let{h:r,w:n,b:e,a:t}=this.toHwb();return t<1?`hwb(${r} ${n}% ${e}% / ${t})`:`hwb(${r} ${n}% ${e}%)`}toOklab(){return $(this._rgb)}toOklabString(){let{l:r,a:n,b:e,alpha:t}=this.toOklab(),s=a(r,4),l=a(n,4),b=a(e,4);return t<1?`oklab(${s} ${l} ${b} / ${t})`:`oklab(${s} ${l} ${b})`}toOklch(){return Z(this._rgb)}toOklchString(){let{l:r,c:n,h:e,a:t}=this.toOklch(),s=a(r,4),l=a(n,4),b=a(e,2);return t<1?`oklch(${s} ${l} ${b} / ${t})`:`oklch(${s} ${l} ${b})`}brightness(){let{r,g:n,b:e}=this._rgb;return a((r*299+n*587+e*114)/255e3,2)}luminance(){let r=s=>{let l=s/255;return l<=.03928?l/12.92:((l+.055)/1.055)**2.4},{r:n,g:e,b:t}=this._rgb;return a(.2126*r(n)+.7152*r(e)+.0722*r(t),4)}isDark(){return this.brightness()<.5}isLight(){return this.brightness()>=.5}alpha(r){return r===void 0?this._rgb.a:new o({...this._rgb,a:u(r,0,1)})}hue(r){let n=this.toHsl();return r===void 0?n.h:new o(w({...n,h:r}))}lighten(r=.1){let n=this.toHsl();return new o(w({...n,l:u(n.l+r*100,0,100)}))}darken(r=.1){return this.lighten(-r)}saturate(r=.1){let n=this.toHsl();return new o(w({...n,s:u(n.s+r*100,0,100)}))}desaturate(r=.1){return this.saturate(-r)}grayscale(){return this.desaturate(1)}invert(){let{r,g:n,b:e,a:t}=this._rgb;return new o({r:255-r,g:255-n,b:255-e,a:t})}rotate(r=15){return this.hue(this.hue()+r)}mix(r,n=.5){let e=new o(r).toRgb(),t=this._rgb,s=u(n,0,1);return new o({r:a(t.r*(1-s)+e.r*s),g:a(t.g*(1-s)+e.g*s),b:a(t.b*(1-s)+e.b*s),a:a(t.a*(1-s)+e.a*s,2)})}contrast(r="#fff"){let n=this.luminance(),e=new o(r).luminance(),t=Math.max(n,e),s=Math.min(n,e);return a((t+.05)/(s+.05),2)}isEqual(r){let n=new o(r).toRgb(),e=this._rgb;return e.r===n.r&&e.g===n.g&&e.b===n.b&&e.a===n.a}toString(){return this.toHex()}},wr=o=>new x(o),Hr=o=>{o.forEach(r=>r(x,y));},yr=()=>new x({r:Math.round(Math.random()*255),g:Math.round(Math.random()*255),b:Math.round(Math.random()*255),a:1});var L=(o,r,n)=>{let e=o+.3963377774*r+.2158037573*n,t=o-.1055613458*r-.0638541728*n,s=o-.0894841775*r-1.291485548*n,l=e**3,b=t**3,c=s**3;return [4.0767416613*l-3.3077115904*b+.2309699287*c,-1.2684380041*l+2.6097574007*b-.3413193963*c,-0.0041960865*l-.7034186145*b+1.7076147009*c]},Rr=/^oklch\(\s*([+-]?\d*\.?\d+)(%?)\s+([+-]?\d*\.?\d+)(%?)\s+([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,Or=/^oklab\(\s*([+-]?\d*\.?\d+)(%?)\s+([+-]?\d*\.?\d+)(%?)\s+([+-]?\d*\.?\d+)(%?)\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,cr=o=>{if(typeof o=="object"&&o!==null){let r=o;if("l"in r&&"a"in r&&"b"in r&&"alpha"in r&&!("c"in r)&&!("h"in r)&&!("r"in r)){let n=o;if(typeof n.l=="number"&&typeof n.a=="number"&&typeof n.b=="number"&&typeof n.alpha=="number")return {l:n.l,a:n.a,b:n.b,alpha:n.alpha}}if("l"in r&&"c"in r&&"h"in r&&!("r"in r)&&!("x"in r)&&!("alpha"in r)){let n=o;if(typeof n.l=="number"&&typeof n.c=="number"&&typeof n.h=="number"){let e=n.h*Math.PI/180;return {l:n.l,a:n.c*Math.cos(e),b:n.c*Math.sin(e),alpha:typeof n.a=="number"?n.a:1}}}return null}if(typeof o=="string"){let r=Rr.exec(o);if(r){let n=r[2]?Number(r[1])/100:Number(r[1]),e=r[4]?Number(r[3])*.004:Number(r[3]),t=r[6]?.toLowerCase()??"deg",l=Number(r[5])*(k[t]??1)*Math.PI/180,b=r[7]===void 0?1:Number(r[7])/(r[8]?100:1);return {l:n,a:e*Math.cos(l),b:e*Math.sin(l),alpha:b}}if(r=Or.exec(o),r){let n=r[2]?Number(r[1])/100:Number(r[1]),e=r[4]?Number(r[3])*.004:Number(r[3]),t=r[6]?Number(r[5])*.004:Number(r[5]),s=r[7]===void 0?1:Number(r[7])/(r[8]?100:1);return {l:n,a:e,b:t,alpha:s}}return null}return null},H=.002,F=(o,r,n)=>o>=-H&&o<=1+H&&r>=-H&&r<=1+H&&n>=-H&&n<=1+H,Nr=o=>{let r=cr(o);if(r===null)return true;let[n,e,t]=L(r.l,r.a,r.b);return F(n,e,t)},$r=o=>{let r=cr(o);if(r===null)return new x(o);let{l:n,a:e,b:t,alpha:s}=r,[l,b,c]=L(n,e,t);if(F(l,b,c))return new x(o);let h=Math.atan2(t,e),m=0,p=Math.sqrt(e**2+t**2);for(let I=0;I<24;I++){let R=(m+p)/2,[mr,ir,hr]=L(n,R*Math.cos(h),R*Math.sin(h));F(mr,ir,hr)?m=R:p=R;}let g=(m+p)/2,v=(h*180/Math.PI+360)%360,j={l:u(n,0,1),c:g,h:v,a:u(s,0,1)};return new x(j)};exports.Colordx=x;exports.colordx=wr;exports.extend=Hr;exports.getFormat=kr;exports.inGamutSrgb=Nr;exports.random=yr;exports.toGamutSrgb=$r;//# sourceMappingURL=index.cjs.map
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|