Skip to content

Commit 71c87cd

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 8c41a11 + 47b651a commit 71c87cd

File tree

17 files changed

+208
-25
lines changed

17 files changed

+208
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Culori Changelog
22

3+
### 0.8.2
4+
5+
Adds `culori.random()` for generating random colors.
6+
37
### 0.8.0
48

59
Adds `culori.blend()` ([#59](https://github.com/Evercoder/culori/issues/59)).

README.md

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ To import culori as a `<script>` tag to use in a web page, you can load it from
8686
- [Interpolation](#interpolation)
8787
- [Difference](#difference)
8888
- [Blending](#blending)
89+
- [Random colors](#random-colors)
8990
- [Extending culori](#extending-culori)
9091

9192
### Color representation
@@ -452,6 +453,32 @@ culori.blend(['red', 'green'], function average(b, s) {
452453
});
453454
```
454455

456+
### Random colors
457+
458+
<a name="culoriRandom" href="#culoriRandom">#</a> culori.**random**(_mode = 'rgb'_, _constraints = {}_) [<>](https://github.com/evercoder/culori/blob/master/src/random.js 'Source')
459+
460+
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.
481+
455482
### Extending culori
456483

457484
<a name="culoriDefineMode" href="#culoriDefineMode">#</a> culori.**defineMode**(_definition_) [<>](https://github.com/evercoder/culori/blob/master/src/modes.js 'Source')
@@ -468,6 +495,9 @@ Defines a new color space through a _definition_ object. By way of example, here
468495
rgb: convertRgbToHsl
469496
},
470497
channels: ['h', 's', 'l', 'alpha'],
498+
ranges: {
499+
h: [0, 360]
500+
},
471501
parsers: [parseHsl],
472502
interpolate: {
473503
h: interpolateLinear(interpolateHue),
@@ -484,6 +514,7 @@ The properties a definition needs are the following:
484514
- `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.
485515
- `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.
486516
- `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]`.
487518
- `parsers`: any parsers for the color space that can transform strings into colors
488519
- `interpolate`: the default interpolations for the color space.
489520

@@ -564,19 +595,19 @@ The figure below shows a slice of the HSI color space for a particular hue:
564595
565596
#### `lab`
566597

567-
| Channel | Range | Description |
568-
| ------- | ---------------------- | --------------------- |
569-
| `l` | `[0100]` | Lightness |
570-
| `a` | `[-79.2872, 93.55]` | Green–red component |
571-
| `b` | `[-112.0294, 93.3884]` | Blue–yellow component |
598+
| Channel | Range | Description |
599+
| ------- | -------------------- | --------------------- |
600+
| `l` | `[0, 100]` | Lightness |
601+
| `a` | `[-79.167, 93.408]` | Green–red component |
602+
| `b` | `[-111.859, 93.246]` | Blue–yellow component |
572603

573604
#### `lch`
574605

575-
| Channel | Range | Description |
576-
| ------- | --------------- | ----------- |
577-
| `l` | `[0 - 100]` | Lightness |
578-
| `c` | `[0 - 131.207]` | Chroma |
579-
| `h` | `[0 - 360]` | Hue |
606+
| Channel | Range | Description |
607+
| ------- | -------------- | ----------- |
608+
| `l` | `[0, 100]` | Lightness |
609+
| `c` | `[0, 131.008]` | Chroma |
610+
| `h` | `[0, 360)` | Hue |
580611

581612
> 💡 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.
582613
@@ -586,19 +617,19 @@ The [DIN99][din99o] color space "squishes" the CIE Lab color space to obtain an
586617

587618
#### `dlab`
588619

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]` |
594625

595626
#### `dlch`
596627

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 |
602633

603634
**References:**
604635

@@ -608,11 +639,11 @@ The [DIN99][din99o] color space "squishes" the CIE Lab color space to obtain an
608639

609640
[YIQ](yiq) is the color space used by the NTSC color TV system. It contains the following channels:
610641

611-
| Channel | Range | Description |
612-
| ------- | ------------------- | ------------------------------ |
613-
| Y | `[0,1]` | Luma |
614-
| I | `[-0.5957, 0.5957]` | In-phase (orange-blue axis) |
615-
| Q | `[-0.5226, 0.5226]` | Quadrature (green-purple axis) |
642+
| Channel | Range | Description |
643+
| ------- | ----------------- | ------------------------------ |
644+
| Y | `[0, 1]` | Luma |
645+
| I | `[-0.593, 0.593]` | In-phase (orange-blue axis) |
646+
| Q | `[-0.520, 0.520]` | Quadrature (green-purple axis) |
616647

617648
### Cubehelix
618649

src/cubehelix/definition.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ import convertCubehelixToRgb from './convertCubehelixToRgb';
4040
export default {
4141
mode: 'cubehelix',
4242
channels: ['h', 's', 'l', 'alpha'],
43+
ranges: {
44+
h: [0, 360],
45+
s: [0, 4.6143]
46+
},
4347
input: {
4448
rgb: convertRgbToCubehelix
4549
},

src/dlab/definition.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export default {
1616
rgb: convertRgbToDlab
1717
},
1818
channels: ['l', 'a', 'b', 'alpha'],
19+
ranges: {
20+
l: [0, 100],
21+
a: [-39.229, 45.166],
22+
b: [-43.002, 44.424]
23+
},
1924
interpolate: {
2025
l: interpolateLinear(),
2126
a: interpolateLinear(),

src/dlch/definition.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ export default {
2323
rgb: convertRgbToDlch
2424
},
2525
channels: ['l', 'c', 'h', 'alpha'],
26+
ranges: {
27+
l: [0, 100],
28+
c: [0, 50.944],
29+
h: [0, 360]
30+
},
2631
interpolate: {
2732
l: interpolateLinear(),
2833
c: interpolateLinear(),

src/hsi/definition.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export default {
1313
rgb: convertRgbToHsi
1414
},
1515
channels: ['h', 's', 'i', 'alpha'],
16+
ranges: {
17+
h: [0, 360]
18+
},
1619
interpolate: {
1720
h: interpolateLinear(interpolateHue),
1821
s: interpolateLinear(),

src/hsl/definition.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export default {
1414
rgb: convertRgbToHsl
1515
},
1616
channels: ['h', 's', 'l', 'alpha'],
17+
ranges: {
18+
h: [0, 360]
19+
},
1720
parsers: [parseHsl],
1821
interpolate: {
1922
h: interpolateLinear(interpolateHue),

src/hsv/definition.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export default {
1313
rgb: convertRgbToHsv
1414
},
1515
channels: ['h', 's', 'v', 'alpha'],
16+
ranges: {
17+
h: [0, 360]
18+
},
1619
interpolate: {
1720
h: interpolateLinear(interpolateHue),
1821
s: interpolateLinear(),

src/hwb/definition.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export default {
1414
rgb: convertRgbToHwb
1515
},
1616
channels: ['h', 'w', 'b', 'alpha'],
17+
ranges: {
18+
h: [0, 360]
19+
},
1720
parsers: [parseHwb],
1821
interpolate: {
1922
h: interpolateLinear(interpolateHue),

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,4 @@ export {
9090
} from './difference';
9191
export { default as colorsNamed } from './colors/named';
9292
export { default as blend } from './blend';
93+
export { default as random } from './random';

0 commit comments

Comments
 (0)