precise-colors 0.9.7 → 0.9.8

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/dist/index.d.ts CHANGED
@@ -1,133 +1,423 @@
1
+ /**
2
+ * CMYK color model (Cyan, Magenta, Yellow, Key/Black).
3
+ * @property {number} c - Cyan `[0..100]`
4
+ * @property {number} m - Magenta `[0..100]`
5
+ * @property {number} y - Yellow `[0..100]`
6
+ * @property {number} k - Key/Black `[0..100]`
7
+ * @see {@link https://en.wikipedia.org/wiki/CMYK_color_model|Wikipedia}
8
+ */
1
9
  interface Cmyk {
2
- /** [0..100] */
10
+ /** Cyan `[0..100]` */
3
11
  readonly c: number;
4
- /** [0..100] */
12
+ /** Magenta `[0..100]` */
5
13
  readonly m: number;
6
- /** [0..100] */
14
+ /** Yellow `[0..100]` */
7
15
  readonly y: number;
8
- /** [0..100] */
16
+ /** Key/Black `[0..100]` */
9
17
  readonly k: number;
10
18
  }
19
+ /**
20
+ * Apple 16-bit RGB color.
21
+ * @property {number} r16 - Red `[0..65535]`
22
+ * @property {number} g16 - Green `[0..65535]`
23
+ * @property {number} b16 - Blue `[0..65535]`
24
+ * @see {@link https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ConvertRGBtoHTMLColor.html|Apple RGB}
25
+ */
11
26
  interface Apple {
27
+ /** Red `[0..65535]` */
12
28
  readonly r16: number;
29
+ /** Green `[0..65535]` */
13
30
  readonly g16: number;
31
+ /** Blue `[0..65535]` */
14
32
  readonly b16: number;
15
33
  }
34
+ /**
35
+ * sRGB color model (8-bit per channel).
36
+ * @property {number} r - Red `[0..255]`
37
+ * @property {number} g - Green `[0..255]`
38
+ * @property {number} b - Blue `[0..255]`
39
+ * @see {@link https://www.color.org/srgb.pdf|ICC sRGB Specification}
40
+ */
16
41
  interface Rgb {
17
- /** [0..255] */
42
+ /** Red `[0..255]` */
18
43
  readonly r: number;
19
- /** [0..255] */
44
+ /** Green `[0..255]` */
20
45
  readonly g: number;
21
- /** [0..255] */
46
+ /** Blue `[0..255]` */
22
47
  readonly b: number;
23
48
  }
49
+ /**
50
+ * HSL cylindrical color model (Hue, Saturation, Lightness).
51
+ * @property {number} h - Hue `[0..360]`
52
+ * @property {number} s - Saturation `[0..100]`
53
+ * @property {number} l - Lightness `[0..100]`
54
+ * @see {@link https://www.w3.org/TR/css-color-4/#the-hsl-notation|W3C CSS Color 4}
55
+ */
24
56
  interface Hsl {
57
+ /** Hue angle `[0..360]` */
25
58
  readonly h: number;
59
+ /** Saturation `[0..100]` */
26
60
  readonly s: number;
61
+ /** Lightness `[0..100]` */
27
62
  readonly l: number;
28
63
  }
64
+ /**
65
+ * HSV/HSB cylindrical color model (Hue, Saturation, Value/Brightness).
66
+ * @property {number} h - Hue `[0..360]`
67
+ * @property {number} s - Saturation `[0..100]`
68
+ * @property {number} v - Value `[0..100]`
69
+ * @see {@link https://en.wikipedia.org/wiki/HSL_and_HSV|Wikipedia}
70
+ */
29
71
  interface Hsv {
72
+ /** Hue angle `[0..360]` */
30
73
  readonly h: number;
74
+ /** Saturation `[0..100]` */
31
75
  readonly s: number;
76
+ /** Value/Brightness `[0..100]` */
32
77
  readonly v: number;
33
78
  }
79
+ /**
80
+ * HCG color model (Hue, Chroma, Grayness).
81
+ * Derived from the Munsell color system.
82
+ * @property {number} h - Hue `[0..360]`
83
+ * @property {number} c - Chroma `[0..100]`
84
+ * @property {number} g - Grayness `[0..100]`
85
+ * @see {@link https://github.com/d3/d3-hcg|d3-hcg}
86
+ */
34
87
  interface Hcg {
88
+ /** Hue angle `[0..360]` */
35
89
  readonly h: number;
90
+ /** Chroma `[0..100]` */
36
91
  readonly c: number;
92
+ /** Grayness `[0..100]` */
37
93
  readonly g: number;
38
94
  }
95
+ /**
96
+ * HWB color model (Hue, Whiteness, Blackness).
97
+ * @property {number} h - Hue `[0..360]`
98
+ * @property {number} w - Whiteness `[0..100]`
99
+ * @property {number} b - Blackness `[0..100]`
100
+ * @see {@link https://www.w3.org/TR/css-color-4/#the-hwb-notation|W3C CSS Color 4}
101
+ */
39
102
  interface Hwb {
40
- /** Hue [0..360] */
103
+ /** Hue angle `[0..360]` */
41
104
  readonly h: number;
42
- /** Whiteness [0..100] */
105
+ /** Whiteness `[0..100]` */
43
106
  readonly w: number;
44
- /** Blackness [0..100] */
107
+ /** Blackness `[0..100]` */
45
108
  readonly b: number;
46
109
  }
110
+ /**
111
+ * CIE L*a*b* perceptual color space (1976).
112
+ * a* and b* are theoretically unbounded but clamped to `[-128..127]` for 8-bit storage.
113
+ * @property {number} l - Lightness `[0..100]`
114
+ * @property {number} a - Green-Red axis `[-128..127]`
115
+ * @property {number} b - Blue-Yellow axis `[-128..127]`
116
+ * @see {@link https://en.wikipedia.org/wiki/CIELAB_color_space|Wikipedia}
117
+ * @see {@link https://www.w3.org/TR/css-color-4/#lab-colors|W3C CSS Color 4}
118
+ */
47
119
  interface Lab {
120
+ /** Lightness `[0..100]` */
48
121
  readonly l: number;
122
+ /** Green (-) to Red (+) axis. Clamped `[-128..127]`, theoretical `~[-430..+172]` */
49
123
  readonly a: number;
124
+ /** Blue (-) to Yellow (+) axis. Clamped `[-128..127]`, theoretically unbounded */
50
125
  readonly b: number;
51
126
  }
127
+ /**
128
+ * CIE XYZ tristimulus color space with D65 illuminant.
129
+ * @property {number} x - X tristimulus `[0..95.047]`
130
+ * @property {number} y - Y luminance `[0..100]`
131
+ * @property {number} z - Z tristimulus `[0..108.883]`
132
+ * @see {@link https://en.wikipedia.org/wiki/CIE_1931_color_space|Wikipedia CIE 1931}
133
+ * @see {@link https://en.wikipedia.org/wiki/Standard_illuminant#Illuminant_series_D|D65 Illuminant}
134
+ */
52
135
  interface Xyz {
136
+ /** X tristimulus `[0..95.047]` for D65 white */
53
137
  readonly x: number;
138
+ /** Y tristimulus (luminance) `[0..100]` */
54
139
  readonly y: number;
140
+ /** Z tristimulus `[0..108.883]` for D65 white */
55
141
  readonly z: number;
56
142
  }
143
+ /**
144
+ * XYZ values from Lab conversion (named Lyz to avoid confusion).
145
+ * Uses D65 illuminant reference white.
146
+ * @property {number} l - X tristimulus `[0..95.047]`
147
+ * @property {number} y - Y luminance `[0..100]`
148
+ * @property {number} z - Z tristimulus `[0..108.883]`
149
+ * @see {@link https://en.wikipedia.org/wiki/Standard_illuminant#Illuminant_series_D|D65 Illuminant}
150
+ */
57
151
  interface Lyz {
152
+ /** X tristimulus `[0..95.047]` for D65 */
58
153
  readonly l: number;
154
+ /** Y tristimulus `[0..100]` */
59
155
  readonly y: number;
156
+ /** Z tristimulus `[0..108.883]` for D65 */
60
157
  readonly z: number;
61
158
  }
159
+ /**
160
+ * CIE LCH cylindrical color space (Lightness, Chroma, Hue).
161
+ * Polar representation of L*a*b*.
162
+ * @property {number} l - Lightness `[0..100]`
163
+ * @property {number} c - Chroma `[0..~230]`
164
+ * @property {number} h - Hue `[0..360]`
165
+ * @see {@link https://en.wikipedia.org/wiki/CIELAB_color_space#Cylindrical_model|Wikipedia}
166
+ * @see {@link https://www.w3.org/TR/css-color-4/#lch-colors|W3C CSS Color 4}
167
+ */
62
168
  interface Lch {
169
+ /** Lightness `[0..100]` */
63
170
  readonly l: number;
171
+ /** Chroma `[0..~230]`. Theoretically unbounded, sRGB max ~131 */
64
172
  readonly c: number;
173
+ /** Hue angle `[0..360]` */
65
174
  readonly h: number;
66
175
  }
67
- /** rounds number to amount of places after `.` */
176
+ /**
177
+ * Rounds a number to specified decimal places.
178
+ * Uses exponential notation to avoid floating-point errors
179
+ * (e.g., `1.005 * 100 = 100.49999...` but `roundTo(1.005, 2) = 1.01`).
180
+ * @param num - Number to round
181
+ * @param places - Decimal places `[0..]`
182
+ * @returns Rounded number
183
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed|Number.toFixed} for string output
184
+ */
68
185
  declare function roundTo(num: number, places: number): number;
186
+ /**
187
+ * Modulo operation that always returns positive result.
188
+ * @param x - Dividend
189
+ * @param n - Divisor
190
+ * @returns `x mod n`, always positive
191
+ */
69
192
  declare function modulo(x: number, n: number): number;
70
193
  /**
71
- * rgb2css converts {@link Rgb} to a string for css. Calls `Math.round` for each of
72
- * the Rgb fields.
73
- * @param {Rbg} rgb - [0..255].
74
- * @returns {string} `"rgb(r,g,b)"` representation of passed {@link Rgb} instance.
194
+ * Converts {@link Rgb} to CSS rgb() string.
195
+ * @param rgb - {@link Rgb} color, r/g/b `[0..255]`
196
+ * @returns CSS string `"rgb(r,g,b)"` (rounded)
75
197
  */
76
198
  declare function rgb2css(rgb: Rgb): string;
77
199
  /**
78
- * rgb2str converts {@link Rgb} to a rounded `"0..255,0.255,0.255"` string.
79
- * @param {Rgb} rgb
80
- * @returns {string} example: `"0,127,255"`
200
+ * Converts {@link Rgb} to comma-separated string.
201
+ * @param rgb - {@link Rgb} color, r/g/b `[0..255]`
202
+ * @returns String `"r,g,b"` (rounded)
81
203
  */
82
204
  declare function rgb2str(rgb: Rgb): string;
83
205
  /**
84
- * Converts {@link Rgb} and `alpha` to a string for css. Calls `Math.round` for each of
85
- * the Rgb fields. Rounds `alpha` to max 2 places after `.`.
86
- * @param {Rbg} rgb - [0..255].
87
- * @param {number} alpha [0..1]
88
- * @returns {string} `"rgb(r,g,b,a)"` representation of passed {@link Rgb} instance and `alpha`.
206
+ * Converts {@link Rgb} and alpha to CSS rgba() string.
207
+ * @param rgb - {@link Rgb} color, r/g/b `[0..255]`
208
+ * @param alpha - Alpha `[0..1]`
209
+ * @returns CSS string `"rgba(r,g,b,a)"` (rounded)
89
210
  */
90
211
  declare function rgba2css(rgb: Rgb, alpha: number): string;
91
- /** returns `hsl(h,s,l)` expression */
212
+ /**
213
+ * Converts {@link Hsl} to CSS hsl() string.
214
+ * @param hsl - {@link Hsl} color, h `[0..360]`, s/l `[0..100]`
215
+ * @returns CSS string `"hsl(hdeg,s%,l%)"` (rounded to 2 decimals)
216
+ */
92
217
  declare function hsl2css(hsl: Hsl): string;
93
- /** returns `hwb(h,w,b)` expression */
218
+ /**
219
+ * Converts {@link Hwb} to CSS hwb() string.
220
+ * @param hwb - {@link Hwb} color, h `[0..360]`, w/b `[0..100]`
221
+ * @returns CSS string `"hwb(hdeg,w%,b%)"` (rounded to 2 decimals)
222
+ */
94
223
  declare function hwb2css(hwb: Hwb): string;
95
- /** rgb expects [0..255] for each field */
224
+ /**
225
+ * Converts {@link Rgb} to 6-digit hex string (without #).
226
+ * @param rgb - {@link Rgb} color, r/g/b `[0..255]`
227
+ * @returns Hex string `"rrggbb"` (rounded)
228
+ */
96
229
  declare function rgb2hex(rgb: Rgb): string;
97
- /** gray expects [0..100] */
230
+ /**
231
+ * Converts gray value to 6-digit hex string.
232
+ * @param gray - Gray level `[0..100]`
233
+ * @returns Hex string `"gggggg"` (rounded)
234
+ */
98
235
  declare function gray2hex(gray: number): string;
236
+ /**
237
+ * Parses hex string to {@link Rgb}.
238
+ * @param input - Hex string (6 digits, without #)
239
+ * @returns {@link Rgb} color, r/g/b `[0..255]`
240
+ */
99
241
  declare function hex2rgb(input: string): Rgb;
100
- /** expects h [0..360], s [0..100], l [0..100]. Returns rgb[0..255], doesn't perform Math.round on the result. */
242
+ /**
243
+ * Converts {@link Hsl} to {@link Rgb}.
244
+ * @param hsl - {@link Hsl} color, h `[0..360]`, s/l `[0..100]`
245
+ * @returns {@link Rgb} color, r/g/b `[0..255]` (not rounded)
246
+ */
101
247
  declare function hsl2rgb(hsl: Hsl): Rgb;
102
- /** expects h [0..360], s [0..100], l [0..100]. Returns rgb[0..255], doesn't perform Math.round on the result. */
248
+ /**
249
+ * Converts {@link Hsl} to {@link Hsv}.
250
+ * @param hsl - {@link Hsl} color, h `[0..360]`, s/l `[0..100]`
251
+ * @returns {@link Hsv} color, h `[0..360]`, s/v `[0..100]`
252
+ */
103
253
  declare function hsl2hsv(hsl: Hsl): Hsv;
254
+ /**
255
+ * Converts {@link Hsl} to {@link Hcg}.
256
+ * @param hsl - {@link Hsl} color, h `[0..360]`, s/l `[0..100]`
257
+ * @returns {@link Hcg} color, h `[0..360]`, c/g `[0..100]`
258
+ */
104
259
  declare function hsl2hcg(hsl: Hsl): Hcg;
260
+ /**
261
+ * Converts {@link Hsv} to {@link Rgb}.
262
+ * @param hsv - {@link Hsv} color, h `[0..360]`, s/v `[0..100]`
263
+ * @returns {@link Rgb} color, r/g/b `[0..255]` (not rounded)
264
+ */
105
265
  declare function hsv2rgb(hsv: Hsv): Rgb;
266
+ /**
267
+ * Converts {@link Hsv} to {@link Hsl}.
268
+ * @param hsv - {@link Hsv} color, h `[0..360]`, s/v `[0..100]`
269
+ * @returns {@link Hsl} color, h `[0..360]`, s/l `[0..100]`
270
+ */
106
271
  declare function hsv2hsl(hsv: Hsv): Hsl;
272
+ /**
273
+ * Converts {@link Hsv} to {@link Hcg}.
274
+ * @param hsv - {@link Hsv} color, h `[0..360]`, s/v `[0..100]`
275
+ * @returns {@link Hcg} color, h `[0..360]`, c/g `[0..100]`
276
+ */
107
277
  declare function hsv2hcg(hsv: Hsv): Hcg;
278
+ /**
279
+ * Converts {@link Apple} 16-bit RGB to 8-bit {@link Rgb}.
280
+ * @param rgb16 - {@link Apple} color, r16/g16/b16 `[0..65535]`
281
+ * @returns {@link Rgb} color, r/g/b `[0..255]`
282
+ */
108
283
  declare function apple2rgb(rgb16: Apple): Rgb;
284
+ /**
285
+ * Converts {@link Cmyk} to {@link Rgb}.
286
+ * @param cmyk - {@link Cmyk} color, c/m/y/k `[0..100]`
287
+ * @returns {@link Rgb} color, r/g/b `[0..255]`
288
+ */
109
289
  declare function cmyk2rgb(cmyk: Cmyk): Rgb;
290
+ /**
291
+ * Converts {@link Rgb} to {@link Cmyk}.
292
+ * @param rgb - {@link Rgb} color, r/g/b `[0..255]`
293
+ * @returns {@link Cmyk} color, c/m/y/k `[0..100]`
294
+ */
110
295
  declare function rgb2cmyk(rgb: Rgb): Cmyk;
296
+ /**
297
+ * Converts {@link Rgb} to {@link Hsl}.
298
+ * @param rgb - {@link Rgb} color, r/g/b `[0..255]`
299
+ * @returns {@link Hsl} color, h `[0..360]`, s/l `[0..100]`
300
+ */
111
301
  declare function rgb2hsl(rgb: Rgb): Hsl;
302
+ /**
303
+ * Converts {@link Rgb} to {@link Hwb}.
304
+ * @param rgb - {@link Rgb} color, r/g/b `[0..255]`
305
+ * @returns {@link Hwb} color, h `[0..360]`, w/b `[0..100]`
306
+ */
112
307
  declare function rgb2hwb(rgb: Rgb): Hwb;
308
+ /**
309
+ * Converts {@link Hwb} to {@link Rgb}.
310
+ * @param hwb - {@link Hwb} color, h `[0..360]`, w/b `[0..100]`
311
+ * @returns {@link Rgb} color, r/g/b `[0..255]` (not rounded)
312
+ */
113
313
  declare function hwb2rgb(hwb: Hwb): Rgb;
314
+ /**
315
+ * Converts {@link Hwb} to {@link Hcg}.
316
+ * @param hwb - {@link Hwb} color, h `[0..360]`, w/b `[0..100]`
317
+ * @returns {@link Hcg} color, h `[0..360]`, c/g `[0..100]`
318
+ */
114
319
  declare function hwb2hcg(hwb: Hwb): Hcg;
320
+ /**
321
+ * Converts {@link Hcg} to {@link Rgb}.
322
+ * @param hcg - {@link Hcg} color, h `[0..360]`, c/g `[0..100]`
323
+ * @returns {@link Rgb} color, r/g/b `[0..255]` (not rounded)
324
+ */
115
325
  declare function hcg2rgb(hcg: Hcg): Rgb;
326
+ /**
327
+ * Converts {@link Hcg} to {@link Hsv}.
328
+ * @param hcg - {@link Hcg} color, h `[0..360]`, c/g `[0..100]`
329
+ * @returns {@link Hsv} color, h `[0..360]`, s/v `[0..100]`
330
+ */
116
331
  declare function hcg2hsv(hcg: Hcg): Hsv;
332
+ /**
333
+ * Converts {@link Hcg} to {@link Hsl}.
334
+ * @param hcg - {@link Hcg} color, h `[0..360]`, c/g `[0..100]`
335
+ * @returns {@link Hsl} color, h `[0..360]`, s/l `[0..100]`
336
+ */
117
337
  declare function hcg2hsl(hcg: Hcg): Hsl;
338
+ /**
339
+ * Converts {@link Hcg} to {@link Hwb}.
340
+ * @param hcg - {@link Hcg} color, h `[0..360]`, c/g `[0..100]`
341
+ * @returns {@link Hwb} color, h `[0..360]`, w/b `[0..100]`
342
+ */
118
343
  declare function hcg2hwb(hcg: Hcg): Hwb;
344
+ /**
345
+ * Converts gray to {@link Rgb}.
346
+ * @param gray - Gray level `[0..100]`
347
+ * @returns {@link Rgb} color, r/g/b `[0..255]`
348
+ */
119
349
  declare function gray2rgb(gray: number): Rgb;
350
+ /**
351
+ * Converts gray to {@link Hsl}.
352
+ * @param gray - Gray level `[0..100]`
353
+ * @returns {@link Hsl} color, h=0, s=0, l=gray
354
+ */
120
355
  declare function gray2hsl(gray: number): Hsl;
356
+ /**
357
+ * Converts gray to {@link Hsv}.
358
+ * @param gray - Gray level `[0..100]`
359
+ * @returns {@link Hsv} color, h=0, s=0, v=gray
360
+ */
121
361
  declare function gray2hsv(gray: number): Hsv;
362
+ /**
363
+ * Converts gray to {@link Hwb}.
364
+ * @param gray - Gray level `[0..100]`
365
+ * @returns {@link Hwb} color, h=0, w=gray, b=100-gray
366
+ */
122
367
  declare function gray2hwb(gray: number): Hwb;
368
+ /**
369
+ * Converts gray to {@link Cmyk}.
370
+ * @param gray - Gray level `[0..100]`
371
+ * @returns {@link Cmyk} color, c=0, m=0, y=0, k=gray
372
+ */
123
373
  declare function gray2cmyk(gray: number): Cmyk;
374
+ /**
375
+ * Converts gray to {@link Lab}.
376
+ * @param gray - Gray level `[0..100]`
377
+ * @returns {@link Lab} color, l=gray, a=0, b=0
378
+ */
124
379
  declare function gray2lab(gray: number): Lab;
380
+ /**
381
+ * Converts {@link Rgb} to {@link Lab}.
382
+ * @param rgb - {@link Rgb} color, r/g/b `[0..255]`
383
+ * @returns {@link Lab} color, l `[0..100]`, a/b `[-128..127]`
384
+ */
125
385
  declare function rgb2lab(rgb: Rgb): Lab;
386
+ /**
387
+ * Converts {@link Lab} to {@link Lyz} (XYZ values).
388
+ * @param lab - {@link Lab} color, l `[0..100]`, a/b `[-128..127]`
389
+ * @returns {@link Lyz} color, l `[0..95.047]`, y `[0..100]`, z `[0..108.883]`
390
+ */
126
391
  declare function lab2lyz(lab: Lab): Lyz;
392
+ /**
393
+ * Converts {@link Lab} to {@link Lch}.
394
+ * @param lab - {@link Lab} color, l `[0..100]`, a/b `[-128..127]`
395
+ * @returns {@link Lch} color, l `[0..100]`, c `[0..~230]`, h `[0..360]`
396
+ */
127
397
  declare function lab2lch(lab: Lab): Lch;
398
+ /**
399
+ * Converts {@link Lch} to {@link Lab}.
400
+ * @param lch - {@link Lch} color, l `[0..100]`, c `[0..~230]`, h `[0..360]`
401
+ * @returns {@link Lab} color, l `[0..100]`, a/b `[-128..127]`
402
+ */
128
403
  declare function lch2lab(lch: Lch): Lab;
404
+ /**
405
+ * Converts {@link Xyz} to {@link Rgb}.
406
+ * @param xyz - {@link Xyz} color, x `[0..95.047]`, y `[0..100]`, z `[0..108.883]`
407
+ * @returns {@link Rgb} color, r/g/b `[0..255]`
408
+ */
129
409
  declare function xyz2rgb(xyz: Xyz): Rgb;
410
+ /**
411
+ * Converts {@link Xyz} to {@link Lab}.
412
+ * @param xyz - {@link Xyz} color, x `[0..95.047]`, y `[0..100]`, z `[0..108.883]`
413
+ * @returns {@link Lab} color, l `[0..100]`, a/b `[-128..127]`
414
+ */
130
415
  declare function xyz2lab(xyz: Xyz): Lab;
416
+ /**
417
+ * Converts {@link Rgb} to {@link Xyz}.
418
+ * @param rgb - {@link Rgb} color, r/g/b `[0..255]`
419
+ * @returns {@link Xyz} color, x `[0..95.047]`, y `[0..100]`, z `[0..108.883]`
420
+ */
131
421
  declare function rgb2xyz(rgb: Rgb): Xyz;
132
422
 
133
423
  export { type Apple, type Cmyk, type Hcg, type Hsl, type Hsv, type Hwb, type Lab, type Lch, type Lyz, type Rgb, type Xyz, apple2rgb, cmyk2rgb, gray2cmyk, gray2hex, gray2hsl, gray2hsv, gray2hwb, gray2lab, gray2rgb, hcg2hsl, hcg2hsv, hcg2hwb, hcg2rgb, hex2rgb, hsl2css, hsl2hcg, hsl2hsv, hsl2rgb, hsv2hcg, hsv2hsl, hsv2rgb, hwb2css, hwb2hcg, hwb2rgb, lab2lch, lab2lyz, lch2lab, modulo, rgb2cmyk, rgb2css, rgb2hex, rgb2hsl, rgb2hwb, rgb2lab, rgb2str, rgb2xyz, rgba2css, roundTo, xyz2lab, xyz2rgb };
package/dist/index.js CHANGED
@@ -472,49 +472,47 @@ function gray2lab(gray) {
472
472
  b: 0
473
473
  };
474
474
  }
475
+ var CIE_E = 216 / 24389;
476
+ var CIE_K = 24389 / 27;
477
+ var SRGB_TO_XYZ = {
478
+ xr: 0.4124564,
479
+ xg: 0.3575761,
480
+ xb: 0.1804375,
481
+ yr: 0.2126729,
482
+ yg: 0.7151522,
483
+ yb: 0.072175,
484
+ zr: 0.0193339,
485
+ zg: 0.119192,
486
+ zb: 0.9503041
487
+ };
488
+ var D65 = { x: 95.047, y: 100, z: 108.883 };
475
489
  function rgb2lab(rgb) {
476
490
  let r = rgb.r / 255, g = rgb.g / 255, b = rgb.b / 255;
477
- let x = 0, y = 0, z = 0;
478
491
  r = r > 0.04045 ? Math.pow((r + 0.055) / 1.055, 2.4) : r / 12.92;
479
492
  g = g > 0.04045 ? Math.pow((g + 0.055) / 1.055, 2.4) : g / 12.92;
480
493
  b = b > 0.04045 ? Math.pow((b + 0.055) / 1.055, 2.4) : b / 12.92;
481
- x = (r * 0.4124 + g * 0.3576 + b * 0.1805) / 0.95047;
482
- y = (r * 0.2126 + g * 0.7152 + b * 0.0722) / 1;
483
- z = (r * 0.0193 + g * 0.1192 + b * 0.9505) / 1.08883;
484
- x = x > 8856e-6 ? Math.pow(x, 1 / 3) : 7.787 * x + 16 / 116;
485
- y = y > 8856e-6 ? Math.pow(y, 1 / 3) : 7.787 * y + 16 / 116;
486
- z = z > 8856e-6 ? Math.pow(z, 1 / 3) : 7.787 * z + 16 / 116;
494
+ let x = (r * SRGB_TO_XYZ.xr + g * SRGB_TO_XYZ.xg + b * SRGB_TO_XYZ.xb) / (D65.x / 100);
495
+ let y = (r * SRGB_TO_XYZ.yr + g * SRGB_TO_XYZ.yg + b * SRGB_TO_XYZ.yb) / (D65.y / 100);
496
+ let z = (r * SRGB_TO_XYZ.zr + g * SRGB_TO_XYZ.zg + b * SRGB_TO_XYZ.zb) / (D65.z / 100);
497
+ x = x > CIE_E ? Math.pow(x, 1 / 3) : (CIE_K * x + 16) / 116;
498
+ y = y > CIE_E ? Math.pow(y, 1 / 3) : (CIE_K * y + 16) / 116;
499
+ z = z > CIE_E ? Math.pow(z, 1 / 3) : (CIE_K * z + 16) / 116;
487
500
  return { l: 116 * y - 16, a: 500 * (x - y), b: 200 * (y - z) };
488
501
  }
489
502
  function lab2lyz(lab) {
490
503
  let y = (lab.l + 16) / 116;
491
504
  let x = lab.a / 500 + y;
492
505
  let z = y - lab.b / 200;
493
- const x2 = Math.pow(x, 3);
494
- const y2 = Math.pow(y, 3);
495
- const z2 = Math.pow(z, 3);
496
- if (x2 > 8856e-6) {
497
- x = x2;
498
- } else {
499
- x = (x - 16 / 116) / 7.787;
500
- }
501
- if (y2 > 8856e-6) {
502
- y = y2;
503
- } else {
504
- y = (y - 16 / 116) / 7.787;
505
- }
506
- if (z2 > 8856e-6) {
507
- z = z2;
508
- } else {
509
- z = (z - 16 / 116) / 7.787;
510
- }
511
- x *= 95.047;
512
- y *= 100;
513
- z *= 108.883;
506
+ const x3 = Math.pow(x, 3);
507
+ const y3 = Math.pow(y, 3);
508
+ const z3 = Math.pow(z, 3);
509
+ x = x3 > CIE_E ? x3 : (116 * x - 16) / CIE_K;
510
+ y = y3 > CIE_E ? y3 : (116 * y - 16) / CIE_K;
511
+ z = z3 > CIE_E ? z3 : (116 * z - 16) / CIE_K;
514
512
  return {
515
- l: x,
516
- y,
517
- z
513
+ l: x * D65.x,
514
+ y: y * D65.y,
515
+ z: z * D65.z
518
516
  };
519
517
  }
520
518
  function lab2lch(lab) {
@@ -573,24 +571,12 @@ function xyz2rgb(xyz) {
573
571
  };
574
572
  }
575
573
  function xyz2lab(xyz) {
576
- let x = xyz.x / 95.047;
577
- let y = xyz.y / 100;
578
- let z = xyz.z / 108.883;
579
- if (x > 8856e-6) {
580
- x = Math.pow(x, 1 / 3);
581
- } else {
582
- x = 7.787 * x + 16 / 116;
583
- }
584
- if (y > 8856e-6) {
585
- y = Math.pow(y, 1 / 3);
586
- } else {
587
- y = 7.787 * y + 16 / 116;
588
- }
589
- if (z > 8856e-6) {
590
- z = Math.pow(z, 1 / 3);
591
- } else {
592
- z = 7.787 * z + 16 / 116;
593
- }
574
+ let x = xyz.x / D65.x;
575
+ let y = xyz.y / D65.y;
576
+ let z = xyz.z / D65.z;
577
+ x = x > CIE_E ? Math.pow(x, 1 / 3) : (CIE_K * x + 16) / 116;
578
+ y = y > CIE_E ? Math.pow(y, 1 / 3) : (CIE_K * y + 16) / 116;
579
+ z = z > CIE_E ? Math.pow(z, 1 / 3) : (CIE_K * z + 16) / 116;
594
580
  return {
595
581
  l: 116 * y - 16,
596
582
  a: 500 * (x - y),
@@ -598,11 +584,11 @@ function xyz2lab(xyz) {
598
584
  };
599
585
  }
600
586
  function rgb2xyz(rgb) {
601
- const [var_R, var_G, var_B] = [rgb.r, rgb.g, rgb.b].map((x) => x / 255).map((x) => x > 0.04045 ? Math.pow((x + 0.055) / 1.055, 2.4) : x / 12.92).map((x) => x * 100);
587
+ const [r, g, b] = [rgb.r, rgb.g, rgb.b].map((x) => x / 255).map((x) => x > 0.04045 ? Math.pow((x + 0.055) / 1.055, 2.4) : x / 12.92).map((x) => x * 100);
602
588
  return {
603
- x: var_R * 0.412453 + var_G * 0.35758 + var_B * 0.180423,
604
- y: var_R * 0.212671 + var_G * 0.71516 + var_B * 0.072169,
605
- z: var_R * 0.019334 + var_G * 0.119193 + var_B * 0.950227
589
+ x: r * SRGB_TO_XYZ.xr + g * SRGB_TO_XYZ.xg + b * SRGB_TO_XYZ.xb,
590
+ y: r * SRGB_TO_XYZ.yr + g * SRGB_TO_XYZ.yg + b * SRGB_TO_XYZ.yb,
591
+ z: r * SRGB_TO_XYZ.zr + g * SRGB_TO_XYZ.zg + b * SRGB_TO_XYZ.zb
606
592
  };
607
593
  }
608
594
  export {