-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_palette.ts
65 lines (61 loc) · 1.56 KB
/
random_palette.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2023 mineejo. All rights reserved. MIT license.
import { Rgb } from "./deps.ts";
import * as palettes from "./palettes/mod.ts";
import { randomColors } from "./random_colors.ts";
const palettesOptions: [number, number][][] = [];
for (const palette in palettes) {
palettesOptions.push(
Object.values(palettes[palette as keyof typeof palettes]),
);
}
/**
* ### Example
*
* ```ts
* const palette = randomPalette();
* console.log(palette);
* // [
* // [114.90..., 255, 255],
* // [126.65..., 255, 255],
* // [255, 122.43..., 240.57...],
* // [142.88..., 128.04..., 255],
* // [255, 127.67..., 177.44...]
* // ]
*
* const pastelPalette = randomPalette({
* palette: "pastel",
* });
*
* console.log(pastelPalette);
* // [
* // [172.83..., 129.42..., 255],
* // [138.22..., 255, 255],
* // [255, 116.43..., 155.02...],
* // [114.46..., 119.88..., 255],
* // [133.54..., 255, 212.66...]
* // ]
* ```
*
* @param colorCount
* Number of colors in the palette.
*
* @param palette
* The name of a palette category, to generate similar palettes.
*/
export function randomPalette({
colorCount = 5,
palette,
}: {
colorCount?: number;
palette?: keyof typeof palettes;
} = {}): Rgb[] {
let index: number;
if (palette) index = Object.keys(palettes).indexOf(palette);
else index = Math.floor(Math.random() * palettesOptions.length);
return randomColors({
hue: palettesOptions[index][0] as [number, number],
saturation: palettesOptions[index][1],
lightness: palettesOptions[index][2],
count: colorCount,
});
}