@versatiles/style 5.1.0 → 5.2.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.
@@ -1,148 +0,0 @@
1
- export default function randomColorGenerator(startSeed) {
2
- let seed = inputToSeed(startSeed);
3
- const colorDictionary = initColorDictionary();
4
- return randomColor;
5
- function randomColor(options) {
6
- options ??= {};
7
- if (options.seed != null) {
8
- seed = inputToSeed(options.seed);
9
- }
10
- options.opacity ??= 1;
11
- const H = pickHue(options);
12
- const S = pickSaturation(H, options);
13
- const V = pickBrightness(H, S, options);
14
- const hsl = HSVtoHSL([H, S, V]).map(v => v.toFixed(0));
15
- if (options.opacity === 1) {
16
- return `hsl(${hsl[0]},${hsl[1]}%,${hsl[2]}%)`;
17
- }
18
- else {
19
- return `hsla(${hsl[0]},${hsl[1]}%,${hsl[2]}%,${options.opacity})`;
20
- }
21
- }
22
- function pickHue(options) {
23
- let hue = randomWithin(getHueRange(options.hue));
24
- if (hue < 0)
25
- hue = 360 + hue;
26
- return hue;
27
- }
28
- function pickSaturation(hue, options) {
29
- if (options.hue === 'monochrome')
30
- return 0;
31
- if (options.luminosity === 'random')
32
- return randomWithin([0, 100]);
33
- const { saturationRange } = getColorInfo(hue);
34
- let [sMin, sMax] = saturationRange;
35
- if (options.saturation === 'strong')
36
- return sMax;
37
- switch (options.luminosity) {
38
- case 'bright':
39
- sMin = 55;
40
- break;
41
- case 'dark':
42
- sMin = sMax - 10;
43
- break;
44
- case 'light':
45
- sMax = 55;
46
- break;
47
- default:
48
- }
49
- return randomWithin([sMin, sMax]);
50
- }
51
- function pickBrightness(h, s, options) {
52
- let bMin = getMinimumBrightness(h, s), bMax = 100;
53
- switch (options.luminosity) {
54
- case 'dark':
55
- bMax = Math.min(100, bMin + 20);
56
- break;
57
- case 'light':
58
- bMin = (bMax + bMin) / 2;
59
- break;
60
- case 'random':
61
- bMin = 0;
62
- bMax = 100;
63
- break;
64
- default:
65
- }
66
- return randomWithin([bMin, bMax]);
67
- }
68
- function getMinimumBrightness(h, s) {
69
- const { lowerBounds } = getColorInfo(h);
70
- for (let i = 0; i < lowerBounds.length - 1; i++) {
71
- const [s1, v1] = lowerBounds[i];
72
- const [s2, v2] = lowerBounds[i + 1];
73
- if (s >= s1 && s <= s2) {
74
- const m = (v2 - v1) / (s2 - s1), b = v1 - m * s1;
75
- return m * s + b;
76
- }
77
- }
78
- return 0;
79
- }
80
- function getHueRange(hue) {
81
- if (typeof hue === 'number') {
82
- if (hue < 360 && hue > 0)
83
- return [hue, hue];
84
- }
85
- if (typeof hue === 'string') {
86
- const color = colorDictionary[hue];
87
- if (color?.hueRange)
88
- return color.hueRange;
89
- }
90
- return [0, 360];
91
- }
92
- function getColorInfo(hue) {
93
- // Maps red colors to make picking hue easier
94
- if (hue >= 334 && hue <= 360)
95
- hue -= 360;
96
- for (const colorName in colorDictionary) {
97
- const color = colorDictionary[colorName];
98
- if (color.hueRange && hue >= color.hueRange[0] && hue <= color.hueRange[1]) {
99
- return colorDictionary[colorName];
100
- }
101
- }
102
- throw Error('Color not found');
103
- }
104
- function randomWithin(range) {
105
- //Seeded random algorithm from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
106
- const max = range[1] || 1;
107
- const min = range[0] || 0;
108
- seed = (seed * 9301 + 49297) % 233280;
109
- const rnd = seed / 233280.0;
110
- return Math.floor(min + rnd * (max - min));
111
- }
112
- function initColorDictionary() {
113
- const dic = {};
114
- const defineColor = (name, hueRange, lowerBounds) => {
115
- const [greyest] = lowerBounds;
116
- const colorful = lowerBounds[lowerBounds.length - 1];
117
- dic[name] = {
118
- hueRange,
119
- lowerBounds,
120
- saturationRange: [greyest[0], colorful[0]],
121
- brightnessRange: [colorful[1], greyest[1]],
122
- };
123
- };
124
- defineColor('monochrome', null, [[0, 0], [100, 0]]);
125
- defineColor('red', [-26, 18], [[20, 100], [30, 92], [40, 89], [50, 85], [60, 78], [70, 70], [80, 60], [90, 55], [100, 50]]);
126
- defineColor('orange', [18, 46], [[20, 100], [30, 93], [40, 88], [50, 86], [60, 85], [70, 70], [100, 70]]);
127
- defineColor('yellow', [46, 62], [[25, 100], [40, 94], [50, 89], [60, 86], [70, 84], [80, 82], [90, 80], [100, 75]]);
128
- defineColor('green', [62, 178], [[30, 100], [40, 90], [50, 85], [60, 81], [70, 74], [80, 64], [90, 50], [100, 40]]);
129
- defineColor('blue', [178, 257], [[20, 100], [30, 86], [40, 80], [50, 74], [60, 60], [70, 52], [80, 44], [90, 39], [100, 35]]);
130
- defineColor('purple', [257, 282], [[20, 100], [30, 87], [40, 79], [50, 70], [60, 65], [70, 59], [80, 52], [90, 45], [100, 42]]);
131
- defineColor('pink', [282, 334], [[20, 100], [30, 90], [40, 86], [60, 84], [80, 80], [90, 75], [100, 73]]);
132
- return dic;
133
- }
134
- function HSVtoHSL(hsv) {
135
- const s = hsv[1] / 100, v = hsv[2] / 100, k = (2 - s) * v;
136
- return [hsv[0], 100 * s * v / (k < 1 ? k : 2 - k), 100 * k / 2];
137
- }
138
- function inputToSeed(input) {
139
- if (input == null)
140
- return 0;
141
- if (typeof input === 'number')
142
- return input;
143
- let i = 0;
144
- for (let p = 0; p < input.length; p++)
145
- i = (i * 0x101 + input.charCodeAt(p)) % 0x100000000;
146
- return i;
147
- }
148
- }