@versatiles/style 5.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 VersaTiles
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/browser.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export type * from './styles/index.js';
2
2
  export * from './styles/index.js';
3
+ export * as styles from './styles/index.js';
3
4
  export type { GuessStyleOptions } from './guess_style/index.js';
4
5
  export { guessStyle } from './guess_style/index.js';
6
+ export { Color } from './color/index.js';
package/dist/browser.js CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './styles/index.js';
2
+ export * as styles from './styles/index.js';
2
3
  export { guessStyle } from './guess_style/index.js';
4
+ export { Color } from './color/index.js';
@@ -0,0 +1,32 @@
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(): InstanceType<typeof Color>;
12
+ asHex(): string;
13
+ abstract asString(): string;
14
+ abstract asArray(): number[];
15
+ abstract asHSL(): HSL;
16
+ abstract asHSV(): HSV;
17
+ abstract asRGB(): RGB;
18
+ toHSL(): HSL;
19
+ toHSV(): HSV;
20
+ toRGB(): RGB;
21
+ invertLuminosity(): HSL;
22
+ rotateHue(offset: number): HSL;
23
+ saturate(ratio: number): HSL;
24
+ gamma(value: number): RGB;
25
+ invert(): RGB;
26
+ contrast(value: number): RGB;
27
+ brightness(value: number): RGB;
28
+ lighten(value: number): RGB;
29
+ darken(value: number): RGB;
30
+ tint(value: number, tintColor: Color): RGB;
31
+ abstract fade(value: number): InstanceType<typeof Color>;
32
+ }
@@ -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,22 @@
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[];
11
+ clone(): HSL;
12
+ asString(): string;
13
+ asHSL(): HSL;
14
+ toHSL(): HSL;
15
+ asHSV(): HSV;
16
+ asRGB(): RGB;
17
+ static parse(str: string): HSL;
18
+ invertLuminosity(): HSL;
19
+ rotateHue(offset: number): HSL;
20
+ saturate(ratio: number): HSL;
21
+ fade(value: number): HSL;
22
+ }
@@ -0,0 +1,101 @@
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
+ clone() {
21
+ return new HSL(this.h, this.s, this.l, this.a);
22
+ }
23
+ asString() {
24
+ if (this.a === 1) {
25
+ return `hsl(${this.h.toFixed(0)},${this.s.toFixed(0)}%,${this.l.toFixed(0)}%)`;
26
+ }
27
+ else {
28
+ return `hsla(${this.h.toFixed(0)},${this.s.toFixed(0)}%,${this.l.toFixed(0)}%,${formatFloat(this.a, 3)})`;
29
+ }
30
+ }
31
+ asHSL() {
32
+ return this.clone();
33
+ }
34
+ toHSL() {
35
+ return this;
36
+ }
37
+ asHSV() {
38
+ const s = this.s / 100, l = this.l / 100;
39
+ const v = l + s * Math.min(l, 1 - l);
40
+ const sv = v === 0 ? 0 : 2 * (1 - l / v);
41
+ return new HSV(this.h, sv * 100, v * 100, this.a);
42
+ }
43
+ asRGB() {
44
+ const h = this.h / 360;
45
+ const s = this.s / 100;
46
+ const l = this.l / 100;
47
+ // Achromatic (grey)
48
+ if (s === 0)
49
+ return new RGB(l * 255, l * 255, l * 255, this.a);
50
+ const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
51
+ const p = 2 * l - q;
52
+ const hueToRgb = (t) => {
53
+ if (t < 0)
54
+ t += 1;
55
+ if (t > 1)
56
+ t -= 1;
57
+ if (t < 1 / 6)
58
+ return p + (q - p) * 6 * t;
59
+ if (t < 1 / 2)
60
+ return q;
61
+ if (t < 2 / 3)
62
+ return p + (q - p) * (2 / 3 - t) * 6;
63
+ return p;
64
+ };
65
+ // Convert to RGB in the 0-255 range and return
66
+ return new RGB(255 * hueToRgb(h + 1 / 3), 255 * hueToRgb(h), 255 * hueToRgb(h - 1 / 3), this.a);
67
+ }
68
+ static parse(str) {
69
+ str = str.replace(/\s+/g, '').toLowerCase();
70
+ let match = str.match(/^hsl\((?<h>[-+0-9.]+)(?:deg)?,(?<s>[-+0-9.]+)%,(?<l>[-+0-9.]+)%\)$/);
71
+ if (match) {
72
+ return new HSL(parseFloat(match.groups.h), parseFloat(match.groups.s), parseFloat(match.groups.l));
73
+ }
74
+ match = str.match(/^hsla\((?<h>[-+0-9.]+)(?:deg)?,(?<s>[-+0-9.]+)%,(?<l>[-+0-9.]+)%,(?<a>[-+0-9.]+)\)$/);
75
+ if (match) {
76
+ return new HSL(parseFloat(match.groups.h), parseFloat(match.groups.s), parseFloat(match.groups.l), parseFloat(match.groups.a));
77
+ }
78
+ throw new Error(`Invalid HSL color string: "${str}"`);
79
+ }
80
+ invertLuminosity() {
81
+ this.l = 100 - this.l;
82
+ return this;
83
+ }
84
+ rotateHue(offset) {
85
+ this.h = mod(this.h + offset, 360);
86
+ return this;
87
+ }
88
+ saturate(ratio) {
89
+ if (ratio < 0) {
90
+ this.s = clamp(this.s * (1 + ratio), 0, 100);
91
+ }
92
+ else {
93
+ this.s = clamp(100 - (100 - this.s) * (1 - ratio), 0, 100);
94
+ }
95
+ return this;
96
+ }
97
+ fade(value) {
98
+ this.a *= 1 - value;
99
+ return this;
100
+ }
101
+ }
@@ -0,0 +1,18 @@
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[];
11
+ asString(): string;
12
+ clone(): HSV;
13
+ asHSL(): HSL;
14
+ asHSV(): HSV;
15
+ toHSV(): HSV;
16
+ asRGB(): RGB;
17
+ fade(value: number): HSV;
18
+ }
@@ -0,0 +1,94 @@
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
+ asString() {
21
+ return this.asHSL().asString();
22
+ }
23
+ clone() {
24
+ return new HSV(this.h, this.s, this.v, this.a);
25
+ }
26
+ asHSL() {
27
+ const s = this.s / 100;
28
+ const v = this.v / 100;
29
+ const k = (2 - s) * v;
30
+ const q = k < 1 ? k : 2 - k;
31
+ return new HSL(this.h, q == 0 ? 0 : 100 * s * v / q, 100 * k / 2, this.a);
32
+ }
33
+ asHSV() {
34
+ return this.clone();
35
+ }
36
+ toHSV() {
37
+ return this;
38
+ }
39
+ asRGB() {
40
+ const h = this.h / 360; // Normalize h to range [0, 1]
41
+ const s = this.s / 100; // Normalize s to range [0, 1]
42
+ const v = this.v / 100; // Normalize v to range [0, 1]
43
+ let r = 0, g = 0, b = 0;
44
+ if (s === 0) {
45
+ // Achromatic (grey)
46
+ r = g = b = v;
47
+ }
48
+ else {
49
+ const i = Math.floor(h * 6); // Determine the sector of the color wheel
50
+ const f = h * 6 - i; // Fractional part of h * 6
51
+ const p = v * (1 - s);
52
+ const q = v * (1 - s * f);
53
+ const t = v * (1 - s * (1 - f));
54
+ switch (i % 6) {
55
+ case 0:
56
+ r = v;
57
+ g = t;
58
+ b = p;
59
+ break;
60
+ case 1:
61
+ r = q;
62
+ g = v;
63
+ b = p;
64
+ break;
65
+ case 2:
66
+ r = p;
67
+ g = v;
68
+ b = t;
69
+ break;
70
+ case 3:
71
+ r = p;
72
+ g = q;
73
+ b = v;
74
+ break;
75
+ case 4:
76
+ r = t;
77
+ g = p;
78
+ b = v;
79
+ break;
80
+ case 5:
81
+ r = v;
82
+ g = p;
83
+ b = q;
84
+ break;
85
+ }
86
+ }
87
+ // Convert to RGB in the 0-255 range and return
88
+ return new RGB(r * 255, g * 255, b * 255, this.a);
89
+ }
90
+ fade(value) {
91
+ this.a *= 1 - value;
92
+ return this;
93
+ }
94
+ }
@@ -0,0 +1,3 @@
1
+ import { Color } from './abstract.js';
2
+ export { Color };
3
+ export default Color;
@@ -0,0 +1,27 @@
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 };
27
+ export default 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 type RandomColorFunction = (options?: RandomColorOptions) => string;
9
- export default function randomColorGenerator(startSeed?: number | string): RandomColorFunction;
9
+ export default function randomColor(options?: RandomColorOptions): HSV;
@@ -0,0 +1,127 @@
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
+ switch (options.luminosity) {
53
+ case 'dark':
54
+ bMax = Math.min(100, bMin + 20);
55
+ break;
56
+ case 'light':
57
+ bMin = (bMax + bMin) / 2;
58
+ break;
59
+ case 'random':
60
+ bMin = 0;
61
+ bMax = 100;
62
+ break;
63
+ default:
64
+ }
65
+ return randomWithin([bMin, bMax]);
66
+ function getMinimumBrightness(h, s) {
67
+ const { lowerBounds } = getColorInfo(h);
68
+ for (let i = 0; i < lowerBounds.length - 1; i++) {
69
+ const [s1, v1] = lowerBounds[i];
70
+ const [s2, v2] = lowerBounds[i + 1];
71
+ if (s >= s1 && s <= s2) {
72
+ const m = (v2 - v1) / (s2 - s1), b = v1 - m * s1;
73
+ return m * s + b;
74
+ }
75
+ }
76
+ return 0;
77
+ }
78
+ }
79
+ function randomWithin(range) {
80
+ //Seeded random algorithm from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
81
+ seed = (seed * 9301 + 49297) % 233280;
82
+ return Math.floor(range[0] + seed / 233280.0 * (range[1] - range[0]));
83
+ }
84
+ }
85
+ function inputToSeed(input) {
86
+ if (input == null)
87
+ return 0;
88
+ if (typeof input === 'number')
89
+ return input;
90
+ let i = 0;
91
+ for (let p = 0; p < input.length; p++)
92
+ i = (i * 0x101 + input.charCodeAt(p)) % 0x100000000;
93
+ return i;
94
+ }
95
+ function initColorDictionary() {
96
+ const dict = new Map();
97
+ const defineColor = (name, hueRange, lowerBounds) => {
98
+ const [greyest] = lowerBounds;
99
+ const colorful = lowerBounds[lowerBounds.length - 1];
100
+ dict.set(name, {
101
+ hueRange,
102
+ lowerBounds,
103
+ saturationRange: [greyest[0], colorful[0]],
104
+ brightnessRange: [colorful[1], greyest[1]],
105
+ });
106
+ };
107
+ defineColor('monochrome', null, [[0, 0], [100, 0]]);
108
+ defineColor('red', [-26, 18], [[20, 100], [30, 92], [40, 89], [50, 85], [60, 78], [70, 70], [80, 60], [90, 55], [100, 50]]);
109
+ defineColor('orange', [18, 46], [[20, 100], [30, 93], [40, 88], [50, 86], [60, 85], [70, 70], [100, 70]]);
110
+ defineColor('yellow', [46, 62], [[25, 100], [40, 94], [50, 89], [60, 86], [70, 84], [80, 82], [90, 80], [100, 75]]);
111
+ defineColor('green', [62, 178], [[30, 100], [40, 90], [50, 85], [60, 81], [70, 74], [80, 64], [90, 50], [100, 40]]);
112
+ defineColor('blue', [178, 257], [[20, 100], [30, 86], [40, 80], [50, 74], [60, 60], [70, 52], [80, 44], [90, 39], [100, 35]]);
113
+ defineColor('purple', [257, 282], [[20, 100], [30, 87], [40, 79], [50, 70], [60, 65], [70, 59], [80, 52], [90, 45], [100, 42]]);
114
+ defineColor('pink', [282, 334], [[20, 100], [30, 90], [40, 86], [60, 84], [80, 80], [90, 75], [100, 73]]);
115
+ return dict;
116
+ }
117
+ function getColorInfo(hue) {
118
+ hue = mod(hue, 360);
119
+ if (hue >= 334)
120
+ hue -= 360;
121
+ for (const color of colorDictionary.values()) {
122
+ if (color.hueRange && hue >= color.hueRange[0] && hue <= color.hueRange[1]) {
123
+ return color;
124
+ }
125
+ }
126
+ throw Error('Color hue value not found');
127
+ }
@@ -0,0 +1,27 @@
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[];
12
+ asString(): string;
13
+ asHex(): string;
14
+ asHSL(): HSL;
15
+ asHSV(): HSV;
16
+ asRGB(): RGB;
17
+ toRGB(): RGB;
18
+ static parse(str: string): RGB;
19
+ gamma(value: number): RGB;
20
+ invert(): RGB;
21
+ contrast(value: number): RGB;
22
+ brightness(value: number): RGB;
23
+ tint(value: number, tintColor: Color): RGB;
24
+ lighten(ratio: number): RGB;
25
+ darken(ratio: number): RGB;
26
+ fade(value: number): RGB;
27
+ }