@versatiles/style 5.1.0 → 5.2.1
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/dist/color/abstract.d.ts +33 -0
- package/dist/color/abstract.js +49 -0
- package/dist/color/hsl.d.ts +23 -0
- package/dist/color/hsl.js +108 -0
- package/dist/color/hsv.d.ts +19 -0
- package/dist/color/hsv.js +101 -0
- package/dist/color/index.d.ts +6 -0
- package/dist/color/index.js +26 -0
- package/dist/{guess_style/random_color.d.ts → color/random.d.ts} +2 -2
- package/dist/color/random.js +133 -0
- package/dist/color/rgb.d.ts +28 -0
- package/dist/color/rgb.js +220 -0
- package/dist/color/utils.d.ts +3 -0
- package/dist/color/utils.js +9 -0
- package/dist/guess_style/guess_style.d.ts +8 -5
- package/dist/guess_style/guess_style.js +42 -108
- package/dist/guess_style/index.d.ts +2 -2
- package/dist/guess_style/index.js +1 -1
- package/dist/index.d.ts +9 -3
- package/dist/index.js +3 -2
- package/dist/lib/utils.js +3 -4
- package/dist/shortbread/template.d.ts +4 -2
- package/dist/shortbread/template.js +308 -309
- package/dist/style_builder/decorator.d.ts +1 -1
- package/dist/style_builder/decorator.js +3 -4
- package/dist/style_builder/recolor.d.ts +2 -1
- package/dist/style_builder/recolor.js +15 -49
- package/dist/style_builder/style_builder.d.ts +4 -4
- package/dist/style_builder/style_builder.js +9 -6
- package/dist/style_builder/types.d.ts +4 -6
- package/dist/styles/colorful.d.ts +1 -1
- package/dist/styles/colorful.js +9 -9
- package/dist/styles/eclipse.js +1 -4
- package/dist/styles/empty.d.ts +1 -1
- package/dist/styles/empty.js +1 -1
- package/dist/styles/graybeard.js +1 -1
- package/dist/styles/index.d.ts +3 -5
- package/dist/styles/neutrino.d.ts +1 -1
- package/dist/styles/neutrino.js +7 -7
- package/dist/types/index.d.ts +2 -2
- package/dist/types/maplibre.d.ts +2 -16
- package/dist/types/tilejson.d.ts +5 -10
- package/dist/types/tilejson.js +21 -38
- package/package.json +5 -8
- package/LICENSE +0 -21
- package/dist/browser.d.ts +0 -4
- package/dist/browser.js +0 -2
- package/dist/guess_style/random_color.js +0 -148
- package/dist/guess_style/types.d.ts +0 -23
- package/dist/guess_style/types.js +0 -1
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { HSL } from './hsl.js';
|
|
2
|
+
import type { HSV } from './hsv.js';
|
|
3
|
+
import { RandomColorOptions } from './random.js';
|
|
4
|
+
import type { RGB } from './rgb.js';
|
|
5
|
+
export declare abstract class Color {
|
|
6
|
+
static parse: (str: string) => Color;
|
|
7
|
+
static HSL: typeof HSL;
|
|
8
|
+
static HSV: typeof HSV;
|
|
9
|
+
static RGB: typeof RGB;
|
|
10
|
+
static random: (options?: RandomColorOptions) => HSV;
|
|
11
|
+
abstract clone(): Color;
|
|
12
|
+
asHex(): string;
|
|
13
|
+
abstract asString(): string;
|
|
14
|
+
abstract round(): Color;
|
|
15
|
+
abstract asArray(): number[];
|
|
16
|
+
abstract asHSL(): HSL;
|
|
17
|
+
abstract asHSV(): HSV;
|
|
18
|
+
abstract asRGB(): RGB;
|
|
19
|
+
toHSL(): HSL;
|
|
20
|
+
toHSV(): HSV;
|
|
21
|
+
toRGB(): RGB;
|
|
22
|
+
invertLuminosity(): HSL;
|
|
23
|
+
rotateHue(offset: number): HSL;
|
|
24
|
+
saturate(ratio: number): HSL;
|
|
25
|
+
gamma(value: number): RGB;
|
|
26
|
+
invert(): RGB;
|
|
27
|
+
contrast(value: number): RGB;
|
|
28
|
+
brightness(value: number): RGB;
|
|
29
|
+
lighten(value: number): RGB;
|
|
30
|
+
darken(value: number): RGB;
|
|
31
|
+
tint(value: number, tintColor: Color): RGB;
|
|
32
|
+
abstract fade(value: number): Color;
|
|
33
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export class Color {
|
|
2
|
+
static parse;
|
|
3
|
+
static HSL;
|
|
4
|
+
static HSV;
|
|
5
|
+
static RGB;
|
|
6
|
+
static random;
|
|
7
|
+
asHex() {
|
|
8
|
+
return this.toRGB().asHex();
|
|
9
|
+
}
|
|
10
|
+
toHSL() {
|
|
11
|
+
return this.asHSL();
|
|
12
|
+
}
|
|
13
|
+
toHSV() {
|
|
14
|
+
return this.asHSV();
|
|
15
|
+
}
|
|
16
|
+
toRGB() {
|
|
17
|
+
return this.asRGB();
|
|
18
|
+
}
|
|
19
|
+
invertLuminosity() {
|
|
20
|
+
return this.toHSL().invertLuminosity();
|
|
21
|
+
}
|
|
22
|
+
rotateHue(offset) {
|
|
23
|
+
return this.toHSL().rotateHue(offset);
|
|
24
|
+
}
|
|
25
|
+
saturate(ratio) {
|
|
26
|
+
return this.toHSL().saturate(ratio);
|
|
27
|
+
}
|
|
28
|
+
gamma(value) {
|
|
29
|
+
return this.toRGB().gamma(value);
|
|
30
|
+
}
|
|
31
|
+
invert() {
|
|
32
|
+
return this.toRGB().invert();
|
|
33
|
+
}
|
|
34
|
+
contrast(value) {
|
|
35
|
+
return this.toRGB().contrast(value);
|
|
36
|
+
}
|
|
37
|
+
brightness(value) {
|
|
38
|
+
return this.toRGB().brightness(value);
|
|
39
|
+
}
|
|
40
|
+
lighten(value) {
|
|
41
|
+
return this.toRGB().lighten(value);
|
|
42
|
+
}
|
|
43
|
+
darken(value) {
|
|
44
|
+
return this.toRGB().darken(value);
|
|
45
|
+
}
|
|
46
|
+
tint(value, tintColor) {
|
|
47
|
+
return this.toRGB().tint(value, tintColor);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Color } from './abstract.js';
|
|
2
|
+
import { HSV } from './hsv.js';
|
|
3
|
+
import { RGB } from './rgb.js';
|
|
4
|
+
export declare class HSL extends Color {
|
|
5
|
+
h: number;
|
|
6
|
+
s: number;
|
|
7
|
+
l: number;
|
|
8
|
+
a: number;
|
|
9
|
+
constructor(h: number, s: number, l: number, a?: number);
|
|
10
|
+
asArray(): [number, number, number, number];
|
|
11
|
+
round(): HSL;
|
|
12
|
+
clone(): HSL;
|
|
13
|
+
asString(): string;
|
|
14
|
+
asHSL(): HSL;
|
|
15
|
+
toHSL(): HSL;
|
|
16
|
+
asHSV(): HSV;
|
|
17
|
+
asRGB(): RGB;
|
|
18
|
+
static parse(str: string): HSL;
|
|
19
|
+
invertLuminosity(): HSL;
|
|
20
|
+
rotateHue(offset: number): HSL;
|
|
21
|
+
saturate(ratio: number): HSL;
|
|
22
|
+
fade(value: number): HSL;
|
|
23
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Color } from './abstract.js';
|
|
2
|
+
import { HSV } from './hsv.js';
|
|
3
|
+
import { RGB } from './rgb.js';
|
|
4
|
+
import { clamp, formatFloat, mod } from './utils.js';
|
|
5
|
+
export class HSL extends Color {
|
|
6
|
+
h = 0; // between 0 and 360
|
|
7
|
+
s = 0; // between 0 and 100
|
|
8
|
+
l = 0; // between 0 and 100
|
|
9
|
+
a = 1; // between 0 and 1
|
|
10
|
+
constructor(h, s, l, a = 1) {
|
|
11
|
+
super();
|
|
12
|
+
this.h = mod(h, 360);
|
|
13
|
+
this.s = clamp(s, 0, 100);
|
|
14
|
+
this.l = clamp(l, 0, 100);
|
|
15
|
+
this.a = clamp(a, 0, 1);
|
|
16
|
+
}
|
|
17
|
+
asArray() {
|
|
18
|
+
return [this.h, this.s, this.l, this.a];
|
|
19
|
+
}
|
|
20
|
+
round() {
|
|
21
|
+
this.h = Math.round(this.h);
|
|
22
|
+
this.s = Math.round(this.s);
|
|
23
|
+
this.l = Math.round(this.l);
|
|
24
|
+
this.a = Math.round(this.a * 1000) / 1000;
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
clone() {
|
|
28
|
+
return new HSL(this.h, this.s, this.l, this.a);
|
|
29
|
+
}
|
|
30
|
+
asString() {
|
|
31
|
+
if (this.a === 1) {
|
|
32
|
+
return `hsl(${this.h.toFixed(0)},${this.s.toFixed(0)}%,${this.l.toFixed(0)}%)`;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
return `hsla(${this.h.toFixed(0)},${this.s.toFixed(0)}%,${this.l.toFixed(0)}%,${formatFloat(this.a, 3)})`;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
asHSL() {
|
|
39
|
+
return this.clone();
|
|
40
|
+
}
|
|
41
|
+
toHSL() {
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
asHSV() {
|
|
45
|
+
const s = this.s / 100, l = this.l / 100;
|
|
46
|
+
const v = l + s * Math.min(l, 1 - l);
|
|
47
|
+
const sv = v === 0 ? 0 : 2 * (1 - l / v);
|
|
48
|
+
return new HSV(this.h, sv * 100, v * 100, this.a);
|
|
49
|
+
}
|
|
50
|
+
asRGB() {
|
|
51
|
+
const h = this.h / 360;
|
|
52
|
+
const s = this.s / 100;
|
|
53
|
+
const l = this.l / 100;
|
|
54
|
+
// Achromatic (grey)
|
|
55
|
+
if (s === 0)
|
|
56
|
+
return new RGB(l * 255, l * 255, l * 255, this.a);
|
|
57
|
+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
58
|
+
const p = 2 * l - q;
|
|
59
|
+
const hueToRgb = (t) => {
|
|
60
|
+
if (t < 0)
|
|
61
|
+
t += 1;
|
|
62
|
+
if (t > 1)
|
|
63
|
+
t -= 1;
|
|
64
|
+
if (t < 1 / 6)
|
|
65
|
+
return p + (q - p) * 6 * t;
|
|
66
|
+
if (t < 1 / 2)
|
|
67
|
+
return q;
|
|
68
|
+
if (t < 2 / 3)
|
|
69
|
+
return p + (q - p) * (2 / 3 - t) * 6;
|
|
70
|
+
return p;
|
|
71
|
+
};
|
|
72
|
+
// Convert to RGB in the 0-255 range and return
|
|
73
|
+
return new RGB(255 * hueToRgb(h + 1 / 3), 255 * hueToRgb(h), 255 * hueToRgb(h - 1 / 3), this.a);
|
|
74
|
+
}
|
|
75
|
+
static parse(str) {
|
|
76
|
+
str = str.replace(/\s+/g, '').toLowerCase();
|
|
77
|
+
let match = str.match(/^hsl\((?<h>[-+0-9.]+)(?:deg)?,(?<s>[-+0-9.]+)%,(?<l>[-+0-9.]+)%\)$/);
|
|
78
|
+
if (match) {
|
|
79
|
+
return new HSL(parseFloat(match.groups.h), parseFloat(match.groups.s), parseFloat(match.groups.l));
|
|
80
|
+
}
|
|
81
|
+
match = str.match(/^hsla\((?<h>[-+0-9.]+)(?:deg)?,(?<s>[-+0-9.]+)%,(?<l>[-+0-9.]+)%,(?<a>[-+0-9.]+)\)$/);
|
|
82
|
+
if (match) {
|
|
83
|
+
return new HSL(parseFloat(match.groups.h), parseFloat(match.groups.s), parseFloat(match.groups.l), parseFloat(match.groups.a));
|
|
84
|
+
}
|
|
85
|
+
throw new Error(`Invalid HSL color string: "${str}"`);
|
|
86
|
+
}
|
|
87
|
+
invertLuminosity() {
|
|
88
|
+
this.l = 100 - this.l;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
rotateHue(offset) {
|
|
92
|
+
this.h = mod(this.h + offset, 360);
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
saturate(ratio) {
|
|
96
|
+
if (ratio < 0) {
|
|
97
|
+
this.s = clamp(this.s * (1 + ratio), 0, 100);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
this.s = clamp(100 - (100 - this.s) * (1 - ratio), 0, 100);
|
|
101
|
+
}
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
fade(value) {
|
|
105
|
+
this.a *= 1 - value;
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Color } from './abstract.js';
|
|
2
|
+
import { HSL } from './hsl.js';
|
|
3
|
+
import { RGB } from './rgb.js';
|
|
4
|
+
export declare class HSV extends Color {
|
|
5
|
+
h: number;
|
|
6
|
+
s: number;
|
|
7
|
+
v: number;
|
|
8
|
+
a: number;
|
|
9
|
+
constructor(h: number, s: number, v: number, a?: number);
|
|
10
|
+
asArray(): [number, number, number, number];
|
|
11
|
+
round(): HSV;
|
|
12
|
+
asString(): string;
|
|
13
|
+
clone(): HSV;
|
|
14
|
+
asHSL(): HSL;
|
|
15
|
+
asHSV(): HSV;
|
|
16
|
+
toHSV(): HSV;
|
|
17
|
+
asRGB(): RGB;
|
|
18
|
+
fade(value: number): HSV;
|
|
19
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Color } from './abstract.js';
|
|
2
|
+
import { HSL } from './hsl.js';
|
|
3
|
+
import { RGB } from './rgb.js';
|
|
4
|
+
import { clamp, mod } from './utils.js';
|
|
5
|
+
export class HSV extends Color {
|
|
6
|
+
h = 0; // between 0 and 360
|
|
7
|
+
s = 0; // between 0 and 100
|
|
8
|
+
v = 0; // between 0 and 100
|
|
9
|
+
a = 1; // between 0 and 1
|
|
10
|
+
constructor(h, s, v, a = 1) {
|
|
11
|
+
super();
|
|
12
|
+
this.h = mod(h, 360);
|
|
13
|
+
this.s = clamp(s, 0, 100);
|
|
14
|
+
this.v = clamp(v, 0, 100);
|
|
15
|
+
this.a = clamp(a, 0, 1);
|
|
16
|
+
}
|
|
17
|
+
asArray() {
|
|
18
|
+
return [this.h, this.s, this.v, this.a];
|
|
19
|
+
}
|
|
20
|
+
round() {
|
|
21
|
+
this.h = Math.round(this.h);
|
|
22
|
+
this.s = Math.round(this.s);
|
|
23
|
+
this.v = Math.round(this.v);
|
|
24
|
+
this.a = Math.round(this.a * 1000) / 1000;
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
asString() {
|
|
28
|
+
return this.asHSL().asString();
|
|
29
|
+
}
|
|
30
|
+
clone() {
|
|
31
|
+
return new HSV(this.h, this.s, this.v, this.a);
|
|
32
|
+
}
|
|
33
|
+
asHSL() {
|
|
34
|
+
const s = this.s / 100;
|
|
35
|
+
const v = this.v / 100;
|
|
36
|
+
const k = (2 - s) * v;
|
|
37
|
+
const q = k < 1 ? k : 2 - k;
|
|
38
|
+
return new HSL(this.h, q == 0 ? 0 : 100 * s * v / q, 100 * k / 2, this.a);
|
|
39
|
+
}
|
|
40
|
+
asHSV() {
|
|
41
|
+
return this.clone();
|
|
42
|
+
}
|
|
43
|
+
toHSV() {
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
asRGB() {
|
|
47
|
+
const h = this.h / 360; // Normalize h to range [0, 1]
|
|
48
|
+
const s = this.s / 100; // Normalize s to range [0, 1]
|
|
49
|
+
const v = this.v / 100; // Normalize v to range [0, 1]
|
|
50
|
+
let r = 0, g = 0, b = 0;
|
|
51
|
+
if (s === 0) {
|
|
52
|
+
// Achromatic (grey)
|
|
53
|
+
r = g = b = v;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const i = Math.floor(h * 6); // Determine the sector of the color wheel
|
|
57
|
+
const f = h * 6 - i; // Fractional part of h * 6
|
|
58
|
+
const p = v * (1 - s);
|
|
59
|
+
const q = v * (1 - s * f);
|
|
60
|
+
const t = v * (1 - s * (1 - f));
|
|
61
|
+
switch (i % 6) {
|
|
62
|
+
case 0:
|
|
63
|
+
r = v;
|
|
64
|
+
g = t;
|
|
65
|
+
b = p;
|
|
66
|
+
break;
|
|
67
|
+
case 1:
|
|
68
|
+
r = q;
|
|
69
|
+
g = v;
|
|
70
|
+
b = p;
|
|
71
|
+
break;
|
|
72
|
+
case 2:
|
|
73
|
+
r = p;
|
|
74
|
+
g = v;
|
|
75
|
+
b = t;
|
|
76
|
+
break;
|
|
77
|
+
case 3:
|
|
78
|
+
r = p;
|
|
79
|
+
g = q;
|
|
80
|
+
b = v;
|
|
81
|
+
break;
|
|
82
|
+
case 4:
|
|
83
|
+
r = t;
|
|
84
|
+
g = p;
|
|
85
|
+
b = v;
|
|
86
|
+
break;
|
|
87
|
+
case 5:
|
|
88
|
+
r = v;
|
|
89
|
+
g = p;
|
|
90
|
+
b = q;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// Convert to RGB in the 0-255 range and return
|
|
95
|
+
return new RGB(r * 255, g * 255, b * 255, this.a);
|
|
96
|
+
}
|
|
97
|
+
fade(value) {
|
|
98
|
+
this.a *= 1 - value;
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Color } from './abstract.js';
|
|
2
|
+
import { HSL } from './hsl.js';
|
|
3
|
+
import { HSV } from './hsv.js';
|
|
4
|
+
import randomColor from './random.js';
|
|
5
|
+
import { RGB } from './rgb.js';
|
|
6
|
+
Color.parse = function (str) {
|
|
7
|
+
str = str.trim().toLowerCase();
|
|
8
|
+
if (str.startsWith('#'))
|
|
9
|
+
return RGB.parse(str);
|
|
10
|
+
const prefix = str.replace(/\d.*/, '').trim().toLowerCase();
|
|
11
|
+
switch (prefix) {
|
|
12
|
+
case 'rgb(':
|
|
13
|
+
case 'rgba(':
|
|
14
|
+
return RGB.parse(str);
|
|
15
|
+
case 'hsl(':
|
|
16
|
+
case 'hsla(':
|
|
17
|
+
return HSL.parse(str);
|
|
18
|
+
default:
|
|
19
|
+
throw Error('Unknown color format: ' + str);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
Color.HSL = HSL;
|
|
23
|
+
Color.HSV = HSV;
|
|
24
|
+
Color.RGB = RGB;
|
|
25
|
+
Color.random = randomColor;
|
|
26
|
+
export { Color };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { HSV } from './hsv.js';
|
|
1
2
|
export interface RandomColorOptions {
|
|
2
3
|
seed?: string;
|
|
3
4
|
hue?: number | string;
|
|
@@ -5,5 +6,4 @@ export interface RandomColorOptions {
|
|
|
5
6
|
luminosity?: number | string;
|
|
6
7
|
saturation?: number | string;
|
|
7
8
|
}
|
|
8
|
-
export
|
|
9
|
-
export default function randomColorGenerator(startSeed?: number | string): RandomColorFunction;
|
|
9
|
+
export default function randomColor(options?: RandomColorOptions): HSV;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { HSV } from './hsv.js';
|
|
2
|
+
import { mod } from './utils.js';
|
|
3
|
+
let colorDictionary = new Map();
|
|
4
|
+
export default function randomColor(options) {
|
|
5
|
+
if (colorDictionary.size === 0)
|
|
6
|
+
colorDictionary = initColorDictionary();
|
|
7
|
+
options ??= {};
|
|
8
|
+
let seed = inputToSeed(options.seed);
|
|
9
|
+
const H = pickHue(options);
|
|
10
|
+
const S = pickSaturation(H, options);
|
|
11
|
+
const V = pickBrightness(H, S, options);
|
|
12
|
+
return new HSV(H, S, V, options.opacity ?? 1);
|
|
13
|
+
function pickHue(options) {
|
|
14
|
+
return mod(randomWithin(getHueRange(options.hue)), 360);
|
|
15
|
+
function getHueRange(hue) {
|
|
16
|
+
if (typeof hue === 'number') {
|
|
17
|
+
hue = mod(hue, 360);
|
|
18
|
+
return [hue, hue];
|
|
19
|
+
}
|
|
20
|
+
if (typeof hue === 'string') {
|
|
21
|
+
const color = colorDictionary.get(hue);
|
|
22
|
+
if (color?.hueRange)
|
|
23
|
+
return color.hueRange;
|
|
24
|
+
}
|
|
25
|
+
return [0, 360];
|
|
26
|
+
}
|
|
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
|
+
let [sMin, sMax] = getColorInfo(hue).saturationRange;
|
|
34
|
+
if (options.saturation === 'strong')
|
|
35
|
+
return sMax;
|
|
36
|
+
switch (options.luminosity) {
|
|
37
|
+
case 'bright':
|
|
38
|
+
sMin = 55;
|
|
39
|
+
break;
|
|
40
|
+
case 'dark':
|
|
41
|
+
sMin = sMax - 10;
|
|
42
|
+
break;
|
|
43
|
+
case 'light':
|
|
44
|
+
sMax = 55;
|
|
45
|
+
break;
|
|
46
|
+
default:
|
|
47
|
+
}
|
|
48
|
+
return randomWithin([sMin, sMax]);
|
|
49
|
+
}
|
|
50
|
+
function pickBrightness(h, s, options) {
|
|
51
|
+
let bMin = getMinimumBrightness(h, s), bMax = 100;
|
|
52
|
+
if (typeof options.luminosity === 'number') {
|
|
53
|
+
bMin = options.luminosity;
|
|
54
|
+
bMax = options.luminosity;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
switch (options.luminosity) {
|
|
58
|
+
case 'dark':
|
|
59
|
+
bMax = Math.min(100, bMin + 20);
|
|
60
|
+
break;
|
|
61
|
+
case 'light':
|
|
62
|
+
bMin = (bMax + bMin) / 2;
|
|
63
|
+
break;
|
|
64
|
+
case 'random':
|
|
65
|
+
bMin = 0;
|
|
66
|
+
bMax = 100;
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return randomWithin([bMin, bMax]);
|
|
72
|
+
function getMinimumBrightness(h, s) {
|
|
73
|
+
const { lowerBounds } = getColorInfo(h);
|
|
74
|
+
for (let i = 0; i < lowerBounds.length - 1; i++) {
|
|
75
|
+
const [s1, v1] = lowerBounds[i];
|
|
76
|
+
const [s2, v2] = lowerBounds[i + 1];
|
|
77
|
+
if (s >= s1 && s <= s2) {
|
|
78
|
+
const m = (v2 - v1) / (s2 - s1), b = v1 - m * s1;
|
|
79
|
+
return m * s + b;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return 0;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function randomWithin(range) {
|
|
86
|
+
//Seeded random algorithm from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
|
|
87
|
+
seed = (seed * 9301 + 49297) % 233280;
|
|
88
|
+
return Math.floor(range[0] + seed / 233280.0 * (range[1] - range[0]));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function inputToSeed(input) {
|
|
92
|
+
if (input == null)
|
|
93
|
+
return 0;
|
|
94
|
+
if (typeof input === 'number')
|
|
95
|
+
return input;
|
|
96
|
+
let i = 0;
|
|
97
|
+
for (let p = 0; p < input.length; p++)
|
|
98
|
+
i = (i * 0x101 + input.charCodeAt(p)) % 0x100000000;
|
|
99
|
+
return i;
|
|
100
|
+
}
|
|
101
|
+
function initColorDictionary() {
|
|
102
|
+
const dict = new Map();
|
|
103
|
+
const defineColor = (name, hueRange, lowerBounds) => {
|
|
104
|
+
const [greyest] = lowerBounds;
|
|
105
|
+
const colorful = lowerBounds[lowerBounds.length - 1];
|
|
106
|
+
dict.set(name, {
|
|
107
|
+
hueRange,
|
|
108
|
+
lowerBounds,
|
|
109
|
+
saturationRange: [greyest[0], colorful[0]],
|
|
110
|
+
brightnessRange: [colorful[1], greyest[1]],
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
defineColor('monochrome', null, [[0, 0], [100, 0]]);
|
|
114
|
+
defineColor('red', [-26, 18], [[20, 100], [30, 92], [40, 89], [50, 85], [60, 78], [70, 70], [80, 60], [90, 55], [100, 50]]);
|
|
115
|
+
defineColor('orange', [18, 46], [[20, 100], [30, 93], [40, 88], [50, 86], [60, 85], [70, 70], [100, 70]]);
|
|
116
|
+
defineColor('yellow', [46, 62], [[25, 100], [40, 94], [50, 89], [60, 86], [70, 84], [80, 82], [90, 80], [100, 75]]);
|
|
117
|
+
defineColor('green', [62, 178], [[30, 100], [40, 90], [50, 85], [60, 81], [70, 74], [80, 64], [90, 50], [100, 40]]);
|
|
118
|
+
defineColor('blue', [178, 257], [[20, 100], [30, 86], [40, 80], [50, 74], [60, 60], [70, 52], [80, 44], [90, 39], [100, 35]]);
|
|
119
|
+
defineColor('purple', [257, 282], [[20, 100], [30, 87], [40, 79], [50, 70], [60, 65], [70, 59], [80, 52], [90, 45], [100, 42]]);
|
|
120
|
+
defineColor('pink', [282, 334], [[20, 100], [30, 90], [40, 86], [60, 84], [80, 80], [90, 75], [100, 73]]);
|
|
121
|
+
return dict;
|
|
122
|
+
}
|
|
123
|
+
function getColorInfo(hue) {
|
|
124
|
+
hue = mod(hue, 360);
|
|
125
|
+
if (hue >= 334)
|
|
126
|
+
hue -= 360;
|
|
127
|
+
for (const color of colorDictionary.values()) {
|
|
128
|
+
if (color.hueRange && hue >= color.hueRange[0] && hue <= color.hueRange[1]) {
|
|
129
|
+
return color;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
throw Error('Color hue value not found');
|
|
133
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { HSL } from './hsl.js';
|
|
2
|
+
import { HSV } from './hsv.js';
|
|
3
|
+
import { Color } from './abstract.js';
|
|
4
|
+
export declare class RGB extends Color {
|
|
5
|
+
r: number;
|
|
6
|
+
g: number;
|
|
7
|
+
b: number;
|
|
8
|
+
a: number;
|
|
9
|
+
constructor(r: number, g: number, b: number, a?: number);
|
|
10
|
+
clone(): RGB;
|
|
11
|
+
asArray(): [number, number, number, number];
|
|
12
|
+
round(): RGB;
|
|
13
|
+
asString(): string;
|
|
14
|
+
asHex(): string;
|
|
15
|
+
asHSL(): HSL;
|
|
16
|
+
asHSV(): HSV;
|
|
17
|
+
asRGB(): RGB;
|
|
18
|
+
toRGB(): RGB;
|
|
19
|
+
static parse(str: string): RGB;
|
|
20
|
+
gamma(value: number): RGB;
|
|
21
|
+
invert(): RGB;
|
|
22
|
+
contrast(value: number): RGB;
|
|
23
|
+
brightness(value: number): RGB;
|
|
24
|
+
tint(value: number, tintColor: Color): RGB;
|
|
25
|
+
lighten(ratio: number): RGB;
|
|
26
|
+
darken(ratio: number): RGB;
|
|
27
|
+
fade(value: number): RGB;
|
|
28
|
+
}
|