colorizr 3.0.0-0 → 3.0.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 +148 -166
- package/dist/index.d.mts +51 -16
- package/dist/index.d.ts +51 -16
- package/dist/index.js +133 -87
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +133 -87
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/colorizr.ts +19 -3
- package/src/convert.ts +1 -1
- package/src/converters/hex2hsl.ts +2 -2
- package/src/converters/hex2oklab.ts +2 -2
- package/src/converters/hex2oklch.ts +2 -2
- package/src/converters/hex2rgb.ts +2 -2
- package/src/converters/oklab2rgb.ts +3 -2
- package/src/converters/rgb2oklab.ts +7 -7
- package/src/format-css.ts +2 -2
- package/src/invert.ts +6 -2
- package/src/modules/constants.ts +10 -0
- package/src/name.ts +4 -0
- package/src/palette.ts +14 -6
- package/src/parse-css.ts +10 -10
- package/src/random.ts +4 -3
- package/src/scheme.ts +37 -13
- package/src/swatch.ts +96 -56
- package/src/transparentize.ts +2 -2
- package/src/types/index.ts +0 -11
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Colorizr
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/colorizr) [](https://bundlephobia.com/result?p=colorizr) [](https://github.com/gilbarbara/colorizr/actions/workflows/main.yml) [](https://www.npmjs.com/package/colorizr) [](https://bundlephobia.com/result?p=colorizr) [](https://github.com/gilbarbara/colorizr/actions/workflows/main.yml) [](https://sonarcloud.io/summary/new_code?id=gilbarbara_colorizr) [](https://sonarcloud.io/summary/new_code?id=gilbarbara_colorizr)
|
|
4
4
|
|
|
5
5
|
Color conversion, generation, manipulation, comparison, and analysis.
|
|
6
6
|
|
|
@@ -16,18 +16,20 @@ Color conversion, generation, manipulation, comparison, and analysis.
|
|
|
16
16
|
**Install**
|
|
17
17
|
|
|
18
18
|
```bash
|
|
19
|
-
npm
|
|
19
|
+
npm i colorizr
|
|
20
20
|
```
|
|
21
21
|
|
|
22
22
|
## Usage
|
|
23
23
|
|
|
24
|
+
You can use all the functions standalone:
|
|
25
|
+
|
|
24
26
|
```typescript
|
|
25
27
|
import { luminance } from 'colorizr';
|
|
26
28
|
|
|
27
29
|
const lux = luminance('#ff0044'); // 0.2168
|
|
28
30
|
```
|
|
29
31
|
|
|
30
|
-
Or you can create an instance to use all the methods for the
|
|
32
|
+
Or you can create an instance to use all the methods for the same color:
|
|
31
33
|
|
|
32
34
|
```typescript
|
|
33
35
|
import Colorizr from 'Colorizr';
|
|
@@ -44,10 +46,10 @@ colorInstance.opacity; // 1
|
|
|
44
46
|
|
|
45
47
|
- [Info](#Info)
|
|
46
48
|
- [Manipulators](#Manipulators)
|
|
47
|
-
- [Utilities](#Utilities)
|
|
48
49
|
- [Converters](#Converters)
|
|
49
50
|
- [Generators](#Generators)
|
|
50
51
|
- [Comparison](#Comparison)
|
|
52
|
+
- [Utilities](#Utilities)
|
|
51
53
|
- [Validators](#Validators)
|
|
52
54
|
- [Class](#Class)
|
|
53
55
|
|
|
@@ -79,7 +81,7 @@ Get the name of a color. Return the hex code if it can't be named.
|
|
|
79
81
|
```typescript
|
|
80
82
|
import { name } from 'colorizr';
|
|
81
83
|
|
|
82
|
-
name('#ffc0cb'
|
|
84
|
+
name('#ffc0cb'); // pink
|
|
83
85
|
name('rgb(176 224 230)'); // 'powderblue'
|
|
84
86
|
name('hsl(344 100 50)'); // #ff0044
|
|
85
87
|
```
|
|
@@ -90,29 +92,31 @@ Get the opacity of a color.
|
|
|
90
92
|
```typescript
|
|
91
93
|
import { opacity } from 'colorizr';
|
|
92
94
|
|
|
93
|
-
opacity('#ff0044'); 1
|
|
94
|
-
opacity('rgb(255 0 68 / 90%)'); 0.9
|
|
95
|
-
opacity('hsl(344 100 50 / 60%)'); 0.6
|
|
95
|
+
opacity('#ff0044'); // 1
|
|
96
|
+
opacity('rgb(255 0 68 / 90%)'); // 0.9
|
|
97
|
+
opacity('hsl(344 100 50 / 60%)'); // 0.6
|
|
96
98
|
```
|
|
97
99
|
|
|
98
100
|
### Manipulators
|
|
99
101
|
|
|
100
|
-
**lighten(input: string,
|
|
102
|
+
**lighten(input: string, amount: number): string**
|
|
101
103
|
Get a color with increased lightness.
|
|
102
104
|
|
|
103
105
|
```typescript
|
|
104
106
|
import { lighten } from 'colorizr';
|
|
105
107
|
|
|
106
108
|
lighten('#ff0044', 10); // #ff3369
|
|
109
|
+
lighten('hsl(136 100% 50%)', 10); // hsl(136 100% 60%)
|
|
107
110
|
```
|
|
108
111
|
|
|
109
|
-
**darken(input: string,
|
|
112
|
+
**darken(input: string, amount: number): string**
|
|
110
113
|
Get a color with decreased lightness.
|
|
111
114
|
|
|
112
115
|
```typescript
|
|
113
116
|
import { darken } from 'colorizr';
|
|
114
117
|
|
|
115
118
|
darken('#ff0044', 10); // #cc0036
|
|
119
|
+
darken('oklch(47.642% 0.29956 274.93693)', 10); // oklch(40.377% 0.25281 275.46794)
|
|
116
120
|
```
|
|
117
121
|
|
|
118
122
|
**saturate(input: string, amount: number): string**
|
|
@@ -132,6 +136,7 @@ Get a color with decreased saturation.
|
|
|
132
136
|
import { desaturate } from 'colorizr';
|
|
133
137
|
|
|
134
138
|
desaturate('#ff0044', 10); // #f20d4a
|
|
139
|
+
desaturate('oklab(70.622% 0.1374 0.14283)', 10); // oklab(69.021% 0.12176 0.13721)
|
|
135
140
|
```
|
|
136
141
|
|
|
137
142
|
**invert(input: string): string**
|
|
@@ -159,8 +164,8 @@ Add opacity to a color
|
|
|
159
164
|
```typescript
|
|
160
165
|
import { opacify } from 'colorizr';
|
|
161
166
|
|
|
162
|
-
opacify('hsl(344, 100, 50)',
|
|
163
|
-
opacify('#ff0044',
|
|
167
|
+
opacify('hsl(344, 100, 50)', 0.9); // hsl(344 100 50 / 90%)
|
|
168
|
+
opacify('#ff0044', 0.8); // #ff0044cc
|
|
164
169
|
```
|
|
165
170
|
|
|
166
171
|
**transparentize(input: string, alpha: number, format?: ColorType): string**
|
|
@@ -169,110 +174,8 @@ Increase/decrease the color opacity.
|
|
|
169
174
|
```typescript
|
|
170
175
|
import { transparentize } from 'colorizr';
|
|
171
176
|
|
|
172
|
-
transparentize('hsl(344, 100, 50)', 10); //
|
|
173
|
-
transparentize('#ff0044', 50, 'hsl'); //
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
### Utilities
|
|
177
|
-
|
|
178
|
-
**addAlphaToHex(input: string, alpha: number): string**
|
|
179
|
-
Add an alpha value to a hex string
|
|
180
|
-
|
|
181
|
-
```typescript
|
|
182
|
-
import { addAlphaToHex } from 'colorizr';
|
|
183
|
-
|
|
184
|
-
addAlphaToHex('#ff0044', 0.9); // '#ff0044e6'
|
|
185
|
-
addAlphaToHex('#ff0044cc', 0.9); // '#ff0044e6'
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
**convertAlphaToHex(input: number): string**
|
|
189
|
-
Convert an alpha value to a hex value.
|
|
190
|
-
|
|
191
|
-
```typescript
|
|
192
|
-
import { convertAlphaToHex } from 'colorizr';
|
|
193
|
-
|
|
194
|
-
convertAlphaToHex(0.5); // '80'
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
**extractAlphaFromHex(input: string): number**
|
|
198
|
-
Extract the alpha value from a hex string
|
|
199
|
-
|
|
200
|
-
```typescript
|
|
201
|
-
import { extractAlphaFromHex } from 'colorizr';
|
|
202
|
-
|
|
203
|
-
convertAlphaToHex('#ff004480'); // 0.5
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
**extractColorParts(input: string): ExtractColorPartsReturn**
|
|
207
|
-
Extract the color parts from a CSS color string.
|
|
208
|
-
Hex colors are not supported.
|
|
209
|
-
|
|
210
|
-
```typescript
|
|
211
|
-
type ExtractColorPartsReturn = {
|
|
212
|
-
alpha?: number;
|
|
213
|
-
model: 'hsl' | 'oklab' | 'oklch' | 'rgb';
|
|
214
|
-
} & Record<string, number>;
|
|
215
|
-
|
|
216
|
-
extractColorParts('rgb(255 0 68)') // { model: 'rgb', r: 255, g: 0, b: 68 }
|
|
217
|
-
extractColorParts('hsl(344 100% 50% / 90%)') // { alpha: 0.9, model: 'hsl', h: 344, g: 100, l: 50 }
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
**formatCSS(input: HSL | RGB, options?: FormatOptions): string**
|
|
221
|
-
Get a css string from a color object.
|
|
222
|
-
|
|
223
|
-
```typescript
|
|
224
|
-
import { formatCSS } from 'colorizr';
|
|
225
|
-
|
|
226
|
-
formatCSS({ h: 344, s: 100, l: 50 }, { format: 'rgb' }); // 'rgb(255, 0, 68)'
|
|
227
|
-
formatCSS({ r: 255, g: 0, b: 68 }, { alpha: 0.5, format: 'hsl' }); // 'hsla(344, 100%, 50%, 0.5)'
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
**formatHex(input: string): string**
|
|
231
|
-
Format a short hex string of 3 (or 4) digits into 6 (or 8) digits.
|
|
232
|
-
|
|
233
|
-
```typescript
|
|
234
|
-
import { formatHex } from 'colorizr';
|
|
235
|
-
|
|
236
|
-
formatHex('#07e'); // '#0077ee'
|
|
237
|
-
formatHex('#f058'); // '#ff005588'
|
|
238
|
-
```
|
|
239
|
-
**parseCSS(input: string, format?: ColorType): string | HSL | RGB**
|
|
240
|
-
Parse a css string to hex, HSL, OKLAB, OKLCH, or RGB.
|
|
241
|
-
If the format isn't set, it will return the same format as the input.
|
|
242
|
-
|
|
243
|
-
```typescript
|
|
244
|
-
import { parseCSS } from 'colorizr';
|
|
245
|
-
|
|
246
|
-
parseCSS('hsl(344 100% 50%)'); // { h: 344, l: 50, s: 100 }
|
|
247
|
-
parseCSS('#ff0044', 'hsl'); // { h: 344, l: 50, s: 100 }
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
**random(): string**
|
|
251
|
-
Get a random color.
|
|
252
|
-
|
|
253
|
-
```typescript
|
|
254
|
-
import { random } from 'colorizr';
|
|
255
|
-
|
|
256
|
-
random(); // '#b385e0'
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
**removeAlphaFromHex(input: string): string**
|
|
260
|
-
Remove the alpha value from a hex string
|
|
261
|
-
|
|
262
|
-
```typescript
|
|
263
|
-
import { removeAlphaFromHex } from 'colorizr';
|
|
264
|
-
|
|
265
|
-
removeAlphaFromHex('#ff0044cc'); // '#ff0044'
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
**textColor(input: string): string**
|
|
269
|
-
Get a contrasting color (black or white) for the input color.
|
|
270
|
-
|
|
271
|
-
```typescript
|
|
272
|
-
import { textColor } from 'colorizr';
|
|
273
|
-
|
|
274
|
-
textColor('#ff0044'); // #ffffff
|
|
275
|
-
textColor('#fff800'); // #000000
|
|
177
|
+
transparentize('hsl(344, 100, 50)', 10); // hsl(344 100 50 / 90%)
|
|
178
|
+
transparentize('#ff0044', 50, 'hsl'); // #ff004480
|
|
276
179
|
```
|
|
277
180
|
|
|
278
181
|
### Comparison
|
|
@@ -292,7 +195,7 @@ Get the color difference between the two colors.
|
|
|
292
195
|
```typescript
|
|
293
196
|
import { colorDifference } from 'colorizr';
|
|
294
197
|
|
|
295
|
-
colorDifference('hsl(0
|
|
198
|
+
colorDifference('hsl(0 0% 100%)', '#f04'); // 442
|
|
296
199
|
```
|
|
297
200
|
|
|
298
201
|
**compare(left: string, right: string): Analysis**
|
|
@@ -321,7 +224,7 @@ Get the WCAG contrast ratio between two colors.
|
|
|
321
224
|
```typescript
|
|
322
225
|
import { contrast } from 'colorizr';
|
|
323
226
|
|
|
324
|
-
contrast('hsl(0
|
|
227
|
+
contrast('hsl(0 0% 100%)', 'rgb(255 0 68)'); // 3.94
|
|
325
228
|
```
|
|
326
229
|
### Generators
|
|
327
230
|
|
|
@@ -344,7 +247,7 @@ Get a color scheme.
|
|
|
344
247
|
```typescript
|
|
345
248
|
import { scheme } from 'colorizr';
|
|
346
249
|
|
|
347
|
-
const complementary = scheme('rgb(255
|
|
250
|
+
const complementary = scheme('rgb(255 0 68)'); // ['#ff0044', '#00ffbb']
|
|
348
251
|
const triadic = scheme('#ff0044', 'triadic'); // ['#ff0044', '#44ff00', '#0044ff']
|
|
349
252
|
```
|
|
350
253
|
|
|
@@ -373,7 +276,7 @@ const colors = swatch('#ff0044');
|
|
|
373
276
|
### Converters
|
|
374
277
|
|
|
375
278
|
**convert(input: string, format: ColorType): string**
|
|
376
|
-
Convert a color string
|
|
279
|
+
Convert a color string to another format.
|
|
377
280
|
|
|
378
281
|
```typescript
|
|
379
282
|
import { convert } from 'colorizr';
|
|
@@ -418,7 +321,7 @@ import { hex2rgb } from 'colorizr';
|
|
|
418
321
|
hex2rgb('#ff0044'); // { r: 255, g: 0, b: 68 }
|
|
419
322
|
```
|
|
420
323
|
|
|
421
|
-
**hsl2hex(input: HSL |
|
|
324
|
+
**hsl2hex(input: HSL | ColorTuple): string**
|
|
422
325
|
Convert HSL to HEX.
|
|
423
326
|
|
|
424
327
|
```typescript
|
|
@@ -428,7 +331,7 @@ hsl2hex({ h: 344, s: 100, l: 50 }); // '#ff0044'
|
|
|
428
331
|
hsl2hex([344, 100, 50]); // '#ff0044'
|
|
429
332
|
```
|
|
430
333
|
|
|
431
|
-
**hsl2oklab(input: HSL |
|
|
334
|
+
**hsl2oklab(input: HSL | ColorTuple, precision?: number): LAB**
|
|
432
335
|
Convert HSL to OKLAB.
|
|
433
336
|
|
|
434
337
|
```typescript
|
|
@@ -438,7 +341,7 @@ hsl2oklab({ h: 344, s: 100, l: 50 }); // { l: 0.63269, a: 0.23887, b: 0.08648 }
|
|
|
438
341
|
hsl2oklab([344, 100, 50]); // { l: 0.63269, a: 0.23887, b: 0.08648 }
|
|
439
342
|
```
|
|
440
343
|
|
|
441
|
-
**hsl2oklch(input: HSL |
|
|
344
|
+
**hsl2oklch(input: HSL | ColorTuple, precision?: number): LCH**
|
|
442
345
|
Convert HSL to OKLCH.
|
|
443
346
|
|
|
444
347
|
```typescript
|
|
@@ -448,7 +351,7 @@ hsl2oklch({ h: 344, s: 100, l: 50 }); // { l: 0.63269, c: 0.25404, h: 19.90218 }
|
|
|
448
351
|
hsl2oklch([344, 100, 50]); // { l: 0.63269, c: 0.25404, h: 19.90218 }
|
|
449
352
|
```
|
|
450
353
|
|
|
451
|
-
**hsl2rgb(input: HSL |
|
|
354
|
+
**hsl2rgb(input: HSL | ColorTuple): RGB**
|
|
452
355
|
Convert HSL to RGB.
|
|
453
356
|
|
|
454
357
|
```typescript
|
|
@@ -458,7 +361,7 @@ hsl2rgb({ h: 344, s: 100, l: 50 }); // { r: 255, g: 0, b: 68 }
|
|
|
458
361
|
hsl2rgb([344, 100, 50]); // { r: 255, g: 0, b: 68 }
|
|
459
362
|
```
|
|
460
363
|
|
|
461
|
-
**oklab2hex(input: LAB |
|
|
364
|
+
**oklab2hex(input: LAB | ColorTuple): string**
|
|
462
365
|
Convert OKLAB to HEX.
|
|
463
366
|
|
|
464
367
|
```typescript
|
|
@@ -468,7 +371,7 @@ oklab2hex({ l: 0.63269, a: 0.23887, b: 0.08648 }); // '#ff0044'
|
|
|
468
371
|
oklab2hex([0.63269, 0.23887, 0.08648]); // '#ff0044'
|
|
469
372
|
```
|
|
470
373
|
|
|
471
|
-
**oklab2hsl(input: LAB |
|
|
374
|
+
**oklab2hsl(input: LAB | ColorTuple): HSL**
|
|
472
375
|
Convert OKLAB to HSL.
|
|
473
376
|
|
|
474
377
|
```typescript
|
|
@@ -478,7 +381,7 @@ oklab2hsl({ l: 0.63269, a: 0.23887, b: 0.08648 }); // { h: 344, s: 100, l: 50 }
|
|
|
478
381
|
oklab2hsl([0.63269, 0.23887, 0.08648]); // { h: 344, s: 100, l: 50 }
|
|
479
382
|
```
|
|
480
383
|
|
|
481
|
-
**oklab2oklch(input: LAB |
|
|
384
|
+
**oklab2oklch(input: LAB | ColorTuple, precision?: number): LCH**
|
|
482
385
|
Convert OKLAB to OKLCH.
|
|
483
386
|
|
|
484
387
|
```typescript
|
|
@@ -488,7 +391,7 @@ oklab2oklch({ l: 0.63269, a: 0.23887, b: 0.08648 }); // { l: 0.63269, c: 0.25404
|
|
|
488
391
|
oklab2oklch([0.63269, 0.23887, 0.08648]); // { l: 0.63269, c: 0.25404, h: 19.90218 }
|
|
489
392
|
```
|
|
490
393
|
|
|
491
|
-
**oklab2rgb(input: LAB |
|
|
394
|
+
**oklab2rgb(input: LAB | ColorTuple, precision: number = 0): RGB**
|
|
492
395
|
Convert OKLAB to RGB.
|
|
493
396
|
|
|
494
397
|
```typescript
|
|
@@ -498,7 +401,7 @@ oklab2rgb({ l: 0.63269, a: 0.23887, b: 0.08648 }); // { r: 255, g: 0, b: 68 }
|
|
|
498
401
|
oklab2rgb([0.63269, 0.23887, 0.08648]); // { r: 255, g: 0, b: 68 }
|
|
499
402
|
```
|
|
500
403
|
|
|
501
|
-
**oklch2hex(input: LCH |
|
|
404
|
+
**oklch2hex(input: LCH | ColorTuple): string**
|
|
502
405
|
Convert OKLCH to HEX.
|
|
503
406
|
|
|
504
407
|
```typescript
|
|
@@ -508,7 +411,7 @@ oklch2hex({ l: 0.63269, c: 0.25404, h: 19.90218 }); // '#ff0044'
|
|
|
508
411
|
oklch2hex([0.63269, 0.25404, 19.90218]); // '#ff0044'
|
|
509
412
|
```
|
|
510
413
|
|
|
511
|
-
**oklch2hsl(input: LCH |
|
|
414
|
+
**oklch2hsl(input: LCH | ColorTuple): HSL**
|
|
512
415
|
Convert OKLCH to HSL.
|
|
513
416
|
|
|
514
417
|
```typescript
|
|
@@ -518,7 +421,7 @@ oklch2hsl({ l: 0.63269, c: 0.25404, h: 19.90218 }); // { h: 344, s: 100, l: 50 }
|
|
|
518
421
|
oklch2hsl([0.63269, 0.25404, 19.90218]); // { h: 344, s: 100, l: 50 }
|
|
519
422
|
```
|
|
520
423
|
|
|
521
|
-
**oklch2oklab(input: LCH |
|
|
424
|
+
**oklch2oklab(input: LCH | ColorTuple, precision?: number): LAB**
|
|
522
425
|
Convert OKLCH to OKLAB.
|
|
523
426
|
|
|
524
427
|
```typescript
|
|
@@ -528,7 +431,7 @@ oklch2oklab({ l: 0.63269, c: 0.25404, h: 19.90218 }); // { l: 0.63269, a: 0.2388
|
|
|
528
431
|
oklch2oklab([0.63269, 0.25404, 19.90218]); // { l: 0.63269, a: 0.23887, b: 0.08648 }
|
|
529
432
|
```
|
|
530
433
|
|
|
531
|
-
**oklch2rgb(input: LCH |
|
|
434
|
+
**oklch2rgb(input: LCH | ColorTuple, precision: number = 0): RGB**
|
|
532
435
|
Convert OKLCH to RGB.
|
|
533
436
|
|
|
534
437
|
```typescript
|
|
@@ -538,7 +441,7 @@ oklch2rgb({ l: 0.63269, c: 0.25404, h: 19.90218 }); // { r: 255, g: 0, b: 68 }
|
|
|
538
441
|
oklch2rgb([0.63269, 0.25404, 19.90218]); // { r: 255, g: 0, b: 68 }
|
|
539
442
|
```
|
|
540
443
|
|
|
541
|
-
**rgb2hex(input: RGB |
|
|
444
|
+
**rgb2hex(input: RGB | ColorTuple): string**
|
|
542
445
|
Convert RGB to HEX.
|
|
543
446
|
|
|
544
447
|
```typescript
|
|
@@ -548,7 +451,7 @@ rgb2hex({ r: 255, g: 0, b: 68 }); // '#ff0044'
|
|
|
548
451
|
rgb2hex([255, 0, 68]); // '#ff0044'
|
|
549
452
|
```
|
|
550
453
|
|
|
551
|
-
**rgb2hsl(input: RGB |
|
|
454
|
+
**rgb2hsl(input: RGB | ColorTuple): HSL**
|
|
552
455
|
Convert RGB to HSL.
|
|
553
456
|
|
|
554
457
|
```typescript
|
|
@@ -558,7 +461,7 @@ rgb2hsl({ r: 255, g: 0, b: 68 }); // { h: 344, s: 100, l: 50 }
|
|
|
558
461
|
rgb2hsl([255, 0, 68]); // { h: 344, s: 100, l: 50 }
|
|
559
462
|
```
|
|
560
463
|
|
|
561
|
-
**rgb2oklab(input: RGB |
|
|
464
|
+
**rgb2oklab(input: RGB | ColorTuple, precision: number): LAB**
|
|
562
465
|
Convert RGB to OKLAB.
|
|
563
466
|
|
|
564
467
|
```typescript
|
|
@@ -568,7 +471,7 @@ rgb2oklab({ r: 255, g: 0, b: 68 }); // { l: 0.63269, a: 0.23887, b: 0.08648 }
|
|
|
568
471
|
rgb2oklab([255, 0, 68]); // { l: 0.63269, a: 0.23887, b: 0.08648 }
|
|
569
472
|
```
|
|
570
473
|
|
|
571
|
-
**rgb2oklch(input: RGB |
|
|
474
|
+
**rgb2oklch(input: RGB | ColorTuple, precision: number): LCH**
|
|
572
475
|
Convert RGB to OKLCH.
|
|
573
476
|
|
|
574
477
|
```typescript
|
|
@@ -578,6 +481,109 @@ rgb2oklch({ r: 255, g: 0, b: 68 }); // { l: 0.63269, c: 0.25404, h: 19.90218 }
|
|
|
578
481
|
rgb2oklch([255, 0, 68]); // { l: 0.63269, c: 0.25404, h: 19.90218 }
|
|
579
482
|
```
|
|
580
483
|
|
|
484
|
+
### Utilities
|
|
485
|
+
|
|
486
|
+
**addAlphaToHex(input: string, alpha: number): string**
|
|
487
|
+
Add an alpha value to a hex string
|
|
488
|
+
|
|
489
|
+
```typescript
|
|
490
|
+
import { addAlphaToHex } from 'colorizr';
|
|
491
|
+
|
|
492
|
+
addAlphaToHex('#ff0044', 0.9); // '#ff0044e6'
|
|
493
|
+
addAlphaToHex('#ff0044cc', 0.9); // '#ff0044e6'
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
**convertAlphaToHex(input: number): string**
|
|
497
|
+
Convert an alpha value to a hex value.
|
|
498
|
+
|
|
499
|
+
```typescript
|
|
500
|
+
import { convertAlphaToHex } from 'colorizr';
|
|
501
|
+
|
|
502
|
+
convertAlphaToHex(0.5); // '80'
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
**extractAlphaFromHex(input: string): number**
|
|
506
|
+
Extract the alpha value from a hex string
|
|
507
|
+
|
|
508
|
+
```typescript
|
|
509
|
+
import { extractAlphaFromHex } from 'colorizr';
|
|
510
|
+
|
|
511
|
+
convertAlphaToHex('#ff004480'); // 0.5
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
**extractColorParts(input: string): ExtractColorPartsReturn**
|
|
515
|
+
Extract the color parts from a CSS color string.
|
|
516
|
+
Hex colors are not supported.
|
|
517
|
+
|
|
518
|
+
```typescript
|
|
519
|
+
type ExtractColorPartsReturn = {
|
|
520
|
+
alpha?: number;
|
|
521
|
+
model: 'hsl' | 'oklab' | 'oklch' | 'rgb';
|
|
522
|
+
} & Record<string, number>;
|
|
523
|
+
|
|
524
|
+
extractColorParts('rgb(255 0 68)') // { model: 'rgb', r: 255, g: 0, b: 68 }
|
|
525
|
+
extractColorParts('hsl(344 100% 50% / 90%)') // { alpha: 0.9, model: 'hsl', h: 344, g: 100, l: 50 }
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
**formatCSS(input: HSL | RGB, options?: FormatOptions): string**
|
|
529
|
+
Get a css string from a color object.
|
|
530
|
+
|
|
531
|
+
```typescript
|
|
532
|
+
import { formatCSS } from 'colorizr';
|
|
533
|
+
|
|
534
|
+
formatCSS({ h: 344, s: 100, l: 50 }, { format: 'rgb' }); // 'rgb(255, 0, 68)'
|
|
535
|
+
formatCSS({ r: 255, g: 0, b: 68 }, { alpha: 0.5, format: 'hsl' }); // 'hsla(344, 100%, 50%, 0.5)'
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
**formatHex(input: string): string**
|
|
539
|
+
Format a short hex string of 3 (or 4) digits into 6 (or 8) digits.
|
|
540
|
+
|
|
541
|
+
```typescript
|
|
542
|
+
import { formatHex } from 'colorizr';
|
|
543
|
+
|
|
544
|
+
formatHex('#07e'); // '#0077ee'
|
|
545
|
+
formatHex('#f058'); // '#ff005588'
|
|
546
|
+
```
|
|
547
|
+
**parseCSS(input: string, format?: ColorType): string | HSL | RGB**
|
|
548
|
+
Parse a css string to hex, HSL, OKLAB, OKLCH, or RGB.
|
|
549
|
+
If the format isn't set, it will return the same format as the input.
|
|
550
|
+
|
|
551
|
+
```typescript
|
|
552
|
+
import { parseCSS } from 'colorizr';
|
|
553
|
+
|
|
554
|
+
parseCSS('hsl(344 100% 50%)'); // { h: 344, l: 50, s: 100 }
|
|
555
|
+
parseCSS('#ff0044', 'hsl'); // { h: 344, l: 50, s: 100 }
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
**random(type: ColorType = 'hex'): string**
|
|
559
|
+
Get a random color.
|
|
560
|
+
|
|
561
|
+
```typescript
|
|
562
|
+
import { random } from 'colorizr';
|
|
563
|
+
|
|
564
|
+
random(); // '#b385e0'
|
|
565
|
+
random('oklch') // oklab(86.876% -0.22518 0.1597)
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
**removeAlphaFromHex(input: string): string**
|
|
569
|
+
Remove the alpha value from a hex string
|
|
570
|
+
|
|
571
|
+
```typescript
|
|
572
|
+
import { removeAlphaFromHex } from 'colorizr';
|
|
573
|
+
|
|
574
|
+
removeAlphaFromHex('#ff0044cc'); // '#ff0044'
|
|
575
|
+
```
|
|
576
|
+
|
|
577
|
+
**textColor(input: string): string**
|
|
578
|
+
Get a contrasting color (black or white) for the input color.
|
|
579
|
+
|
|
580
|
+
```typescript
|
|
581
|
+
import { textColor } from 'colorizr';
|
|
582
|
+
|
|
583
|
+
textColor('#ff0044'); // #ffffff
|
|
584
|
+
textColor('#fff800'); // #000000
|
|
585
|
+
```
|
|
586
|
+
|
|
581
587
|
### Validators
|
|
582
588
|
|
|
583
589
|
**isValidColor(input: any): boolean**
|
|
@@ -703,44 +709,20 @@ Get the luminance (0-1).
|
|
|
703
709
|
Get the opacity (0-1).
|
|
704
710
|
|
|
705
711
|
**colorizr.css**
|
|
706
|
-
Get the css string of the same
|
|
712
|
+
Get the css string of the same type as the input.
|
|
707
713
|
|
|
708
714
|
**colorizr.textColor**
|
|
709
715
|
Get a contrasting color (black or white).
|
|
710
716
|
|
|
711
|
-
####
|
|
712
|
-
|
|
713
|
-
**colorizr.lighten(percentage: number)**
|
|
714
|
-
Get a lighter color.
|
|
715
|
-
|
|
716
|
-
**colorizr.darken(percentage: number)**
|
|
717
|
-
Get a darker color.
|
|
718
|
-
|
|
719
|
-
**colorizr.desaturate(percentage: number)**
|
|
720
|
-
Get a desaturated color.
|
|
721
|
-
|
|
722
|
-
**colorizr.saturate(percentage: number)**
|
|
723
|
-
Get a saturated color.
|
|
724
|
-
|
|
725
|
-
**colorizr.rotate(degrees: number)**
|
|
726
|
-
Get a color with a hue rotated.
|
|
727
|
-
|
|
728
|
-
**colorizr.invert()**
|
|
729
|
-
Get the inverted color.
|
|
730
|
-
|
|
731
|
-
**colorizr.transparentize(percentage: number)**
|
|
732
|
-
Get a faded color.
|
|
733
|
-
|
|
734
|
-
#### Comparison
|
|
735
|
-
|
|
736
|
-
**colorizr.compare(color: string)**
|
|
737
|
-
Returns an object with the analysis (check the compare format above)
|
|
738
|
-
|
|
739
|
-
#### Fortmatting
|
|
717
|
+
#### Methods
|
|
740
718
|
|
|
741
719
|
**colorizr.format(type: ColorType, precision?: number)**
|
|
742
720
|
Returns the formatted color with the type
|
|
743
721
|
|
|
722
|
+
...
|
|
723
|
+
|
|
724
|
+
Also, all the [manipulators](#Manipulators) and [comparison](#Comparison) utilities.
|
|
725
|
+
|
|
744
726
|
## Credits / References
|
|
745
727
|
|
|
746
728
|
[color.js](https://github.com/color-js/color.js)
|