@thi.ng/color 5.6.23 → 5.6.24
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/CHANGELOG.md +8 -1
- package/README.md +121 -57
- package/color-range.d.ts +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2024-02-
|
|
3
|
+
- **Last updated**: 2024-02-23T15:42:39Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,13 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
### [5.6.24](https://github.com/thi-ng/umbrella/tree/@thi.ng/color@5.6.24) (2024-02-23)
|
|
13
|
+
|
|
14
|
+
#### 🩹 Bug fixes
|
|
15
|
+
|
|
16
|
+
- allow base color opt in colorFromRange() ([4e7e7fe](https://github.com/thi-ng/umbrella/commit/4e7e7fe))
|
|
17
|
+
- fix/update readme code examples, tangle all ([6eb48ac](https://github.com/thi-ng/umbrella/commit/6eb48ac))
|
|
18
|
+
|
|
12
19
|
## [5.6.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/color@5.6.0) (2023-11-12)
|
|
13
20
|
|
|
14
21
|
#### 🚀 Features
|
package/README.md
CHANGED
|
@@ -52,14 +52,6 @@ For the Clojure version, please visit: [thi.ng/color-clj](https://thi.ng/color-c
|
|
|
52
52
|
|
|
53
53
|
Array-based color types, CSS parsing, conversions, transformations, declarative theme generation, gradients, presets.
|
|
54
54
|
|
|
55
|
-
---
|
|
56
|
-
|
|
57
|
-
**Note: Version 3.0.0 constitutes an almost complete overhaul of the entire
|
|
58
|
-
package, providing a more simple and flexible API, as well as various new
|
|
59
|
-
additional features (compared to previous versions).**
|
|
60
|
-
|
|
61
|
-
---
|
|
62
|
-
|
|
63
55
|
### Supported color spaces / modes
|
|
64
56
|
|
|
65
57
|
Fast color model/space conversions (any direction) between (in alphabetical
|
|
@@ -126,7 +118,9 @@ given. See [next section](#storage--memory-mapping).
|
|
|
126
118
|
|
|
127
119
|
Some examples:
|
|
128
120
|
|
|
129
|
-
```ts
|
|
121
|
+
```ts tangle:export/readme1.ts
|
|
122
|
+
import { css, labD50, rgb, srgb } from "@thi.ng/color";
|
|
123
|
+
|
|
130
124
|
srgb("#ff0")
|
|
131
125
|
// $Color { offset: 0, stride: 1, buf: [ 1, 1, 0, 1 ] }
|
|
132
126
|
|
|
@@ -187,7 +181,9 @@ all vector functions.
|
|
|
187
181
|
|
|
188
182
|

|
|
189
183
|
|
|
190
|
-
```ts
|
|
184
|
+
```ts tangle:export/readme2.ts
|
|
185
|
+
import { Hue, css, namedHueRgb, rgb, srgb } from "@thi.ng/color";
|
|
186
|
+
|
|
191
187
|
const memory = new Float32Array(16);
|
|
192
188
|
|
|
193
189
|
// create RGBA color views of buffer: num, start index, strides
|
|
@@ -220,7 +216,7 @@ colors[0].deref()
|
|
|
220
216
|
|
|
221
217
|

|
|
222
218
|
|
|
223
|
-
```ts
|
|
219
|
+
```ts tangle:export/readme2.ts
|
|
224
220
|
// here we create a *strided* WebGL attrib buffer for 3 points
|
|
225
221
|
// each point defines a: 3D position, UV coords and RGB(A) color
|
|
226
222
|
const attribs = new Float32Array([
|
|
@@ -232,13 +228,13 @@ const attribs = new Float32Array([
|
|
|
232
228
|
|
|
233
229
|
// create strided view of colors
|
|
234
230
|
// 3 items, start index 5, component stride 1, element stride 9
|
|
235
|
-
|
|
231
|
+
const colors2 = srgb.mapBuffer(attribs, 3, 5, 1, 9);
|
|
236
232
|
|
|
237
|
-
css(
|
|
233
|
+
css(colors2[0])
|
|
238
234
|
// '#408000'
|
|
239
|
-
css(
|
|
235
|
+
css(colors2[1])
|
|
240
236
|
// '#808040'
|
|
241
|
-
css(
|
|
237
|
+
css(colors2[2])
|
|
242
238
|
// '#00ff80'
|
|
243
239
|
```
|
|
244
240
|
|
|
@@ -250,13 +246,22 @@ sampled directly and/or mixed with a base color (of any type) to produce
|
|
|
250
246
|
randomized variations. Furthermore, multiple such ranges can be combined into a
|
|
251
247
|
weighted set to define probabilistic color themes.
|
|
252
248
|
|
|
253
|
-
```ts
|
|
249
|
+
```ts tangle:export/readme-range.ts
|
|
250
|
+
import {
|
|
251
|
+
COLOR_RANGES,
|
|
252
|
+
colorFromRange,
|
|
253
|
+
colorsFromRange,
|
|
254
|
+
colorsFromTheme,
|
|
255
|
+
hsv,
|
|
256
|
+
lch,
|
|
257
|
+
} from "@thi.ng/color";
|
|
258
|
+
|
|
254
259
|
// single random color drawn from the "bright" color range preset
|
|
255
260
|
colorFromRange("bright");
|
|
256
261
|
// [ 0.7302125322518669, 0.8519945301256682, 0.8134374983367859, 1 ]
|
|
257
262
|
|
|
258
263
|
// single random color based on given raw HSV base color and preset
|
|
259
|
-
colorFromRange("warm", { base: hsv(0.33, 1, 1) })
|
|
264
|
+
colorFromRange("warm", { base: hsv(0.33, 1, 1) });
|
|
260
265
|
// $Color {
|
|
261
266
|
// offset: 0,
|
|
262
267
|
// stride: 1,
|
|
@@ -273,25 +278,32 @@ colors.next();
|
|
|
273
278
|
// }
|
|
274
279
|
|
|
275
280
|
// 10 cool reds, w/ ±10% hue variance
|
|
276
|
-
[...colorsFromRange("cool", { num: 10, base: lch(1, 0.8, 0), variance: 0.1 })]
|
|
281
|
+
[...colorsFromRange("cool", { num: 10, base: lch(1, 0.8, 0), variance: 0.1 })];
|
|
277
282
|
|
|
278
283
|
// generate colors based on given (weighted) textual description(s)
|
|
279
284
|
// here using named CSS colors, but could also be or typed colors or raw LCH tuples
|
|
280
|
-
[
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
285
|
+
[
|
|
286
|
+
...colorsFromTheme(
|
|
287
|
+
[
|
|
288
|
+
["warm", "goldenrod"],
|
|
289
|
+
["cool", "springgreen", 0.1],
|
|
290
|
+
],
|
|
291
|
+
{ num: 100, variance: 0.05 }
|
|
292
|
+
),
|
|
293
|
+
];
|
|
284
294
|
|
|
285
295
|
// theme parts can also be given in the format used internally
|
|
286
296
|
// all keys are optional (range, base, weight),
|
|
287
297
|
// but at least `range` or `base` must be given...
|
|
288
|
-
[
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
298
|
+
[
|
|
299
|
+
...colorsFromTheme(
|
|
300
|
+
[
|
|
301
|
+
{ range: "warm", base: "goldenrod" },
|
|
302
|
+
{ range: COLOR_RANGES.cool, base: hsv(0, 1, 0.5), weight: 0.1 },
|
|
303
|
+
],
|
|
304
|
+
{ num: 100, variance: 0.05 }
|
|
305
|
+
),
|
|
306
|
+
];
|
|
295
307
|
```
|
|
296
308
|
|
|
297
309
|
This table below shows three sets of sample swatches for each color range preset
|
|
@@ -342,8 +354,8 @@ and the following color theme (raw samples and chunked & sorted):
|
|
|
342
354
|
|
|
343
355
|
Full example:
|
|
344
356
|
|
|
345
|
-
```ts
|
|
346
|
-
import { colorsFromTheme, swatchesH } from "@thi.ng/color";
|
|
357
|
+
```ts tangle:export/readme-theme.ts
|
|
358
|
+
import { colorsFromTheme, swatchesH, type ColorThemePartTuple } from "@thi.ng/color";
|
|
347
359
|
import { serialize } from "@thi.ng/hiccup";
|
|
348
360
|
import { svg } from "@thi.ng/hiccup-svg";
|
|
349
361
|
import { writeFileSync } from "fs";
|
|
@@ -361,12 +373,12 @@ const colors = [...colorsFromTheme(theme, { num: 200, variance: 0.05 })];
|
|
|
361
373
|
|
|
362
374
|
// create SVG doc of color swatches (hiccup format)
|
|
363
375
|
const doc = svg(
|
|
364
|
-
{ width: 1000, height: 50,
|
|
376
|
+
{ width: 1000, height: 50, __convert: true },
|
|
365
377
|
swatchesH(colors, 5, 50)
|
|
366
378
|
);
|
|
367
379
|
|
|
368
380
|
// serialize to SVG file
|
|
369
|
-
writeFileSync("
|
|
381
|
+
writeFileSync("swatches-ex01.svg", serialize(doc));
|
|
370
382
|
```
|
|
371
383
|
|
|
372
384
|

|
|
@@ -391,11 +403,26 @@ number). The following comparators are bundled:
|
|
|
391
403
|
- `proximity(target, distFn)` - sort by distance to target color using given distance function
|
|
392
404
|
- `luminance` / `luminanceRgb` / `luminanceSrgb` etc.
|
|
393
405
|
|
|
394
|
-
```ts
|
|
395
|
-
|
|
406
|
+
```ts tangle:export/readme-sort-theme.ts
|
|
407
|
+
import {
|
|
408
|
+
colorsFromTheme,
|
|
409
|
+
distCIEDE2000,
|
|
410
|
+
lch,
|
|
411
|
+
proximity,
|
|
412
|
+
sort,
|
|
413
|
+
type ColorThemePartTuple,
|
|
414
|
+
} from "@thi.ng/color";
|
|
415
|
+
|
|
416
|
+
// (theme from above example)
|
|
417
|
+
const theme: ColorThemePartTuple[] = [
|
|
418
|
+
["cool", "goldenrod"],
|
|
419
|
+
["hard", "hotpink", 0.1],
|
|
420
|
+
["fresh", "springgreen", 0.1],
|
|
421
|
+
];
|
|
422
|
+
|
|
396
423
|
const colors = [...colorsFromTheme(theme, { num: 200, variance: 0.05 })];
|
|
397
424
|
|
|
398
|
-
|
|
425
|
+
sort(colors, proximity(lch("#fff"), distCIEDE2000()));
|
|
399
426
|
```
|
|
400
427
|
|
|
401
428
|

|
|
@@ -411,31 +438,35 @@ See the [pixel sorting
|
|
|
411
438
|
example](https://github.com/thi-ng/umbrella/tree/develop/examples/pixel-sorting)
|
|
412
439
|
for a concrete use case...
|
|
413
440
|
|
|
414
|
-
```ts
|
|
441
|
+
```ts tangle:export/readme-sort-buffer.ts
|
|
442
|
+
import { css, luminanceSrgb, sortMapped, srgb } from "@thi.ng/color";
|
|
443
|
+
|
|
415
444
|
// memory buffer of 4 sRGB colors
|
|
416
|
-
const buf = new Float32Array([
|
|
445
|
+
const buf = new Float32Array([
|
|
446
|
+
0, 1, 0, 1, 0, 0.5, 0, 1, 0, 0.25, 0, 1, 0, 0.75, 0, 1,
|
|
447
|
+
]);
|
|
417
448
|
|
|
418
449
|
// map buffer (creates 4 SRGB instances linked to the buffer)
|
|
419
450
|
const pix = srgb.mapBuffer(buf);
|
|
420
451
|
|
|
421
452
|
// display original order
|
|
422
|
-
pix.map(css);
|
|
453
|
+
console.log(pix.map((x) => css(x)));
|
|
423
454
|
// [ '#00ff00', '#008000', '#004000', '#00bf00' ]
|
|
424
455
|
|
|
425
456
|
// sort colors (buffer!) by luminance
|
|
426
457
|
sortMapped(pix, luminanceSrgb);
|
|
427
458
|
|
|
428
459
|
// new order
|
|
429
|
-
pix.map(css);
|
|
460
|
+
console.log(pix.map((x) => css(x)));
|
|
430
461
|
// [ '#004000', '#008000', '#00bf00', '#00ff00' ]
|
|
431
462
|
|
|
432
463
|
// buffer contents have been re-ordered
|
|
433
|
-
buf
|
|
464
|
+
console.log(buf);
|
|
434
465
|
// Float32Array(16) [
|
|
435
|
-
// 0, 0.25, 0, 1,
|
|
436
|
-
//
|
|
437
|
-
// 0,
|
|
438
|
-
// 1
|
|
466
|
+
// 0, 0.25, 0, 1,
|
|
467
|
+
// 0, 0.5, 0, 1,
|
|
468
|
+
// 0, 0.75, 0, 1,
|
|
469
|
+
// 0, 1, 0, 1
|
|
439
470
|
// ]
|
|
440
471
|
```
|
|
441
472
|
|
|
@@ -450,7 +481,12 @@ delegates to type specific strategies. See `GradientOpts` for details.
|
|
|
450
481
|
|
|
451
482
|

|
|
452
483
|
|
|
453
|
-
```ts
|
|
484
|
+
```ts tangle:export/readme-multi-color-gradient.ts
|
|
485
|
+
import { lch, multiColorGradient, swatchesH } from "@thi.ng/color";
|
|
486
|
+
import { serialize } from "@thi.ng/hiccup";
|
|
487
|
+
import { svg } from "@thi.ng/hiccup-svg";
|
|
488
|
+
import { writeFileSync } from "fs";
|
|
489
|
+
|
|
454
490
|
const L = 0.8;
|
|
455
491
|
const C = 0.8;
|
|
456
492
|
|
|
@@ -468,10 +504,10 @@ const gradient = multiColorGradient({
|
|
|
468
504
|
});
|
|
469
505
|
|
|
470
506
|
writeFileSync(
|
|
471
|
-
|
|
507
|
+
"lch-gradient.svg",
|
|
472
508
|
serialize(
|
|
473
509
|
svg(
|
|
474
|
-
{ width: 500, height: 50,
|
|
510
|
+
{ width: 500, height: 50, __convert: true },
|
|
475
511
|
swatchesH(gradient, 5, 50)
|
|
476
512
|
)
|
|
477
513
|
)
|
|
@@ -515,9 +551,13 @@ The following presets are bundled (in [`cosine-gradients.ts`](https://github.com
|
|
|
515
551
|
The `cosineCoeffs()` function can be used to compute the cosine gradient
|
|
516
552
|
coefficients between 2 start/end colors:
|
|
517
553
|
|
|
518
|
-
```ts
|
|
554
|
+
```ts tangle:export/readme-cosine-gradient.ts
|
|
555
|
+
import { css, cosineCoeffs, cosineGradient } from "@thi.ng/color";
|
|
556
|
+
|
|
519
557
|
// compute gradient coeffs between red / green
|
|
520
|
-
|
|
558
|
+
console.log(
|
|
559
|
+
cosineGradient(10, cosineCoeffs([1, 0, 1, 1], [0, 1, 0, 1])).map((x) => css(x))
|
|
560
|
+
);
|
|
521
561
|
// '#ff00ff'
|
|
522
562
|
// '#f708f7'
|
|
523
563
|
// '#e11ee1'
|
|
@@ -536,16 +576,38 @@ The `multiCosineGradient()` function returns an iterator of raw RGB
|
|
|
536
576
|
colors based on given gradient stops. This iterator computes a cosine
|
|
537
577
|
gradient between each color stop and yields a sequence of RGB values.
|
|
538
578
|
|
|
539
|
-
```ts
|
|
540
|
-
multiCosineGradient
|
|
579
|
+
```ts tangle:export/readme-multi-cosine-gradient.ts
|
|
580
|
+
import { css, multiCosineGradient, srgb } from "@thi.ng/color";
|
|
581
|
+
|
|
582
|
+
const gradient = multiCosineGradient({
|
|
541
583
|
num: 10,
|
|
542
584
|
// gradient stops (normalized positions)
|
|
543
|
-
stops: [
|
|
585
|
+
stops: [
|
|
586
|
+
[0.1, [1, 0, 0, 1]],
|
|
587
|
+
[0.5, [0, 1, 0, 1]],
|
|
588
|
+
[0.9, [0, 0, 1, 1]],
|
|
589
|
+
],
|
|
544
590
|
// optional color transform/coercion
|
|
545
|
-
tx:
|
|
546
|
-
})
|
|
591
|
+
tx: srgb
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
console.log(gradient);
|
|
595
|
+
// [
|
|
596
|
+
// [1, 0, 0, 1],
|
|
597
|
+
// [1, 0, 0, 1],
|
|
598
|
+
// [0.854, 0.146, 0, 1],
|
|
599
|
+
// [0.5, 0.5, 0, 1],
|
|
600
|
+
// [0.146, 0.854, 0, 1],
|
|
601
|
+
// [0, 1, 0, 1],
|
|
602
|
+
// [0, 0.854, 0.146, 1],
|
|
603
|
+
// [0, 0.5, 0.5, 1],
|
|
604
|
+
// [0, 0.146, 0.853, 1],
|
|
605
|
+
// [0, 0, 1, 1],
|
|
606
|
+
// [0, 0, 1, 1]
|
|
607
|
+
// ]
|
|
608
|
+
|
|
547
609
|
// convert to CSS
|
|
548
|
-
.map(css)
|
|
610
|
+
console.log(gradient.map((x) => css(x)));
|
|
549
611
|
// [
|
|
550
612
|
// "#ff0000",
|
|
551
613
|
// "#ff0000",
|
|
@@ -580,7 +642,9 @@ including parametric preset transforms:
|
|
|
580
642
|
- luminance to alpha
|
|
581
643
|
|
|
582
644
|
Transformation matrices can be combined using matrix multiplication /
|
|
583
|
-
concatenation (see
|
|
645
|
+
concatenation (see
|
|
646
|
+
[`concat()`](https://docs.thi.ng/umbrella/color/functions/concat.html)) for more
|
|
647
|
+
efficient application.
|
|
584
648
|
|
|
585
649
|
## Status
|
|
586
650
|
|
package/color-range.d.ts
CHANGED
|
@@ -26,7 +26,7 @@ export declare const COLOR_RANGES: Record<ColorRangePreset, ColorRange>;
|
|
|
26
26
|
* @param range -
|
|
27
27
|
* @param opts -
|
|
28
28
|
*/
|
|
29
|
-
export declare const colorFromRange: (range: ColorRange | keyof typeof COLOR_RANGES, opts?: Partial<Pick<ColorRangeOpts, "variance" | "eps" | "rnd">>) => LCH;
|
|
29
|
+
export declare const colorFromRange: (range: ColorRange | keyof typeof COLOR_RANGES, opts?: Partial<Pick<ColorRangeOpts, "base" | "variance" | "eps" | "rnd">>) => LCH;
|
|
30
30
|
/**
|
|
31
31
|
* Generator version of {@link colorFromRange}, by default yielding an infinite
|
|
32
32
|
* sequence of random colors based on given range, base color (optional) and
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/color",
|
|
3
|
-
"version": "5.6.
|
|
3
|
+
"version": "5.6.24",
|
|
4
4
|
"description": "Array-based color types, CSS parsing, conversions, transformations, declarative theme generation, gradients, presets",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -445,5 +445,5 @@
|
|
|
445
445
|
"vectors"
|
|
446
446
|
]
|
|
447
447
|
},
|
|
448
|
-
"gitHead": "
|
|
448
|
+
"gitHead": "b97641d1d722f79a70ea68243fa95e845dca7a43\n"
|
|
449
449
|
}
|