You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Obtain a random color from a particular color space, with optional constraints. The resulting color will be in the color space from where it has been picked.
461
+
462
+
Basic usage:
463
+
464
+
```js
465
+
culori.random();
466
+
// => { mode: 'rgb', r: 0.75, g: 0.12, b: 0.99 }
467
+
```
468
+
469
+
You can specify constraints for each individual channel in the color space, as either a _fixed number_ or an _interval_:
470
+
471
+
```js
472
+
culori.random('hsv', {
473
+
h:120// number,
474
+
s: [0.25, 0.75] // interval
475
+
});
476
+
```
477
+
478
+
The resulting color will not include an _alpha_ value, unless you include it in the list of constraints.
479
+
480
+
The value for any channel in the color space for which there are no constraints will be picked from the entire range of that channel. However, some color spaces, such as LAB or LCH, don't have explicit ranges for certain channels; for these, some approximate ranges [have been pre-computed](https://github.com/evercoder/culori/blob/master/tools/ranges.js) as the limits of the displayable sRGB gamut. Even with these ranges in place, a combination of channel values may not be displayable. You can use [`culori.displayable()`](#culoriDisplayable) to check this, and [`culori.clamp()`](#culoriClamp) to obtain a displayable version.
@@ -468,6 +495,9 @@ Defines a new color space through a _definition_ object. By way of example, here
468
495
rgb: convertRgbToHsl
469
496
},
470
497
channels: ['h', 's', 'l', 'alpha'],
498
+
ranges: {
499
+
h: [0, 360]
500
+
},
471
501
parsers: [parseHsl],
472
502
interpolate: {
473
503
h:interpolateLinear(interpolateHue),
@@ -484,6 +514,7 @@ The properties a definition needs are the following:
484
514
-`output`: a set of functions to convert from the color space we're defining to other color spaces. At least `rgb` needs to be included; in case a specific conversion pair between two color spaces is missing, RGB is used as the "buffer" for the conversion.
485
515
-`input`: opposite of `output`; a set of function to convert from various color spaces to the color space we're defining. At least `rgb` needs to be included.
486
516
-`channels`: a list of channels for the color space.
517
+
-`ranges`: the ranges for values in specific channels; if left unspecified, defaults to `[0, 1]`.
487
518
-`parsers`: any parsers for the color space that can transform strings into colors
488
519
-`interpolate`: the default interpolations for the color space.
489
520
@@ -564,19 +595,19 @@ The figure below shows a slice of the HSI color space for a particular hue:
> 💡 The range for the `a` and `b` channels in Lab, and the `c` channel in LCh, depend on the specific implementation. I've obtained the ranges from the tables above by converting all sRGB colors defined by `r, g, b ∈ ℕ ⋂ [0, 255]` into Lab and LCh respectively.
582
613
@@ -586,19 +617,19 @@ The [DIN99][din99o] color space "squishes" the CIE Lab color space to obtain an
586
617
587
618
#### `dlab`
588
619
589
-
| Channel | Range | Description |
590
-
| ------- | ----- | ----------- |
591
-
|`l`|?| Lightness |
592
-
|`a`|? |
593
-
|`b`|? |
620
+
| Channel | Range | Description |
621
+
| ------- | -------------------| ----------- |
622
+
|`l`|`[0, 100]`| Lightness |
623
+
|`a`|`[-39.229, 45.166]`|
624
+
|`b`|`[-43.002, 44.424]`|
594
625
595
626
#### `dlch`
596
627
597
-
| Channel | Range | Description |
598
-
| ------- | ----- | ----------- |
599
-
|`l`|? | Lightness |
600
-
|`c`|? | Chroma |
601
-
|`h`|? | Hue |
628
+
| Channel | Range | Description |
629
+
| ------- | -------------| ----------- |
630
+
|`l`|`[0, 100]`| Lightness |
631
+
|`c`|`[0, 50.944]`| Chroma |
632
+
|`h`|`[0, 360)`| Hue |
602
633
603
634
**References:**
604
635
@@ -608,11 +639,11 @@ The [DIN99][din99o] color space "squishes" the CIE Lab color space to obtain an
608
639
609
640
[YIQ](yiq) is the color space used by the NTSC color TV system. It contains the following channels:
0 commit comments