@dicebear/core 9.4.1 → 10.0.0-rc.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/LICENSE +1 -1
- package/README.md +12 -0
- package/lib/Avatar.d.ts +15 -0
- package/lib/Avatar.js +38 -0
- package/lib/Error/CircularColorReferenceError.d.ts +4 -0
- package/lib/Error/CircularColorReferenceError.js +8 -0
- package/lib/Error/OptionsValidationError.d.ts +5 -0
- package/lib/Error/OptionsValidationError.js +7 -0
- package/lib/Error/StyleValidationError.d.ts +5 -0
- package/lib/Error/StyleValidationError.js +7 -0
- package/lib/Error/ValidationError.d.ts +8 -0
- package/lib/Error/ValidationError.js +18 -0
- package/lib/Options.d.ts +23 -0
- package/lib/Options.js +217 -0
- package/lib/OptionsDescriptor.d.ts +37 -0
- package/lib/OptionsDescriptor.js +70 -0
- package/lib/Prng/Fnv1a.d.ts +4 -0
- package/lib/Prng/Fnv1a.js +16 -0
- package/lib/Prng/Mulberry32.d.ts +7 -0
- package/lib/Prng/Mulberry32.js +40 -0
- package/lib/Prng.d.ts +11 -0
- package/lib/Prng.js +101 -0
- package/lib/Renderer.d.ts +7 -0
- package/lib/Renderer.js +293 -0
- package/lib/Style/Canvas.d.ts +9 -0
- package/lib/Style/Canvas.js +31 -0
- package/lib/Style/Color.d.ts +8 -0
- package/lib/Style/Color.js +28 -0
- package/lib/Style/Component.d.ts +13 -0
- package/lib/Style/Component.js +46 -0
- package/lib/Style/ComponentTranslate.d.ts +7 -0
- package/lib/Style/ComponentTranslate.js +25 -0
- package/lib/Style/ComponentVariant.d.ts +8 -0
- package/lib/Style/ComponentVariant.js +28 -0
- package/lib/Style/Element.d.ts +10 -0
- package/lib/Style/Element.js +36 -0
- package/lib/Style/Meta.d.ts +11 -0
- package/lib/Style/Meta.js +37 -0
- package/lib/Style/MetaCreator.d.ts +7 -0
- package/lib/Style/MetaCreator.js +25 -0
- package/lib/Style/MetaLicense.d.ts +8 -0
- package/lib/Style/MetaLicense.js +28 -0
- package/lib/Style/MetaSource.d.ts +7 -0
- package/lib/Style/MetaSource.js +25 -0
- package/lib/Style.d.ts +18 -0
- package/lib/Style.js +63 -0
- package/lib/StyleDefinition.d.ts +83 -0
- package/lib/StyleOptions.d.ts +54 -0
- package/lib/StyleOptions.js +1 -0
- package/lib/Utils/Color.d.ts +9 -0
- package/lib/Utils/Color.js +67 -0
- package/lib/Utils/Initials.d.ts +3 -0
- package/lib/Utils/Initials.js +26 -0
- package/lib/Utils/License.d.ts +5 -0
- package/lib/Utils/License.js +64 -0
- package/lib/Utils/Xml.d.ts +4 -0
- package/lib/Utils/Xml.js +20 -0
- package/lib/Validator/OptionsValidator.d.ts +3 -0
- package/lib/Validator/OptionsValidator.js +2203 -0
- package/lib/Validator/StyleValidator.d.ts +3 -0
- package/lib/Validator/StyleValidator.js +4986 -0
- package/lib/index.d.ts +4 -12
- package/lib/index.js +3 -12
- package/package.json +13 -8
- package/lib/core.d.ts +0 -2
- package/lib/core.js +0 -67
- package/lib/schema.d.ts +0 -2
- package/lib/schema.js +0 -79
- package/lib/types.d.ts +0 -73
- package/lib/utils/color.d.ts +0 -6
- package/lib/utils/color.js +0 -35
- package/lib/utils/escape.d.ts +0 -1
- package/lib/utils/escape.js +0 -8
- package/lib/utils/license.d.ts +0 -3
- package/lib/utils/license.js +0 -54
- package/lib/utils/options.d.ts +0 -3
- package/lib/utils/options.js +0 -32
- package/lib/utils/prng.d.ts +0 -2
- package/lib/utils/prng.js +0 -67
- package/lib/utils/svg.d.ts +0 -15
- package/lib/utils/svg.js +0 -79
- /package/lib/{types.js → StyleDefinition.js} +0 -0
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -24,3 +24,15 @@ Playground, your next avatar is always just a stone's throw away!
|
|
|
24
24
|
|
|
25
25
|
[Playground](https://www.dicebear.com/playground) |
|
|
26
26
|
[Documentation](https://www.dicebear.com/introduction)
|
|
27
|
+
|
|
28
|
+
## Sponsors
|
|
29
|
+
|
|
30
|
+
Advertisement: Many thanks to our sponsors who provide us with free or discounted products.
|
|
31
|
+
|
|
32
|
+
<a href="https://bunny.net/" target="_blank" rel="noopener noreferrer">
|
|
33
|
+
<picture>
|
|
34
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://www.dicebear.com/sponsors/bunny-light.svg">
|
|
35
|
+
<source media="(prefers-color-scheme: light)" srcset="https://www.dicebear.com/sponsors/bunny-dark.svg">
|
|
36
|
+
<img alt="bunny.net" src="https://www.dicebear.com/sponsors/bunny-dark.svg" height="64">
|
|
37
|
+
</picture>
|
|
38
|
+
</a>
|
package/lib/Avatar.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Style } from './Style.js';
|
|
2
|
+
import type { StyleOptions } from './StyleOptions.js';
|
|
3
|
+
type UnwrapStyle<D> = D extends Style<infer S> ? S : D;
|
|
4
|
+
interface AvatarJson<D = unknown> {
|
|
5
|
+
readonly svg: string;
|
|
6
|
+
readonly options: StyleOptions<UnwrapStyle<D>>;
|
|
7
|
+
}
|
|
8
|
+
export declare class Avatar<D = unknown> {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(style: D, options?: StyleOptions<UnwrapStyle<D>>);
|
|
11
|
+
toString(): string;
|
|
12
|
+
toJSON(): AvatarJson<D>;
|
|
13
|
+
toDataUri(): string;
|
|
14
|
+
}
|
|
15
|
+
export {};
|
package/lib/Avatar.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _Avatar_svg, _Avatar_resolvedOptions;
|
|
13
|
+
import { Style } from './Style.js';
|
|
14
|
+
import { Options } from './Options.js';
|
|
15
|
+
import { Renderer } from './Renderer.js';
|
|
16
|
+
export class Avatar {
|
|
17
|
+
constructor(style, options) {
|
|
18
|
+
_Avatar_svg.set(this, void 0);
|
|
19
|
+
_Avatar_resolvedOptions.set(this, void 0);
|
|
20
|
+
const resolvedStyle = style instanceof Style ? style : new Style(style);
|
|
21
|
+
const resolvedOptions = new Options(resolvedStyle, options ?? {});
|
|
22
|
+
__classPrivateFieldSet(this, _Avatar_svg, new Renderer(resolvedStyle, resolvedOptions).render(), "f");
|
|
23
|
+
__classPrivateFieldSet(this, _Avatar_resolvedOptions, resolvedOptions.resolved(), "f");
|
|
24
|
+
}
|
|
25
|
+
toString() {
|
|
26
|
+
return __classPrivateFieldGet(this, _Avatar_svg, "f");
|
|
27
|
+
}
|
|
28
|
+
toJSON() {
|
|
29
|
+
return {
|
|
30
|
+
svg: __classPrivateFieldGet(this, _Avatar_svg, "f"),
|
|
31
|
+
options: structuredClone(__classPrivateFieldGet(this, _Avatar_resolvedOptions, "f")),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
toDataUri() {
|
|
35
|
+
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(__classPrivateFieldGet(this, _Avatar_svg, "f"))}`;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
_Avatar_svg = new WeakMap(), _Avatar_resolvedOptions = new WeakMap();
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface ValidationErrorDetail {
|
|
2
|
+
readonly message?: string;
|
|
3
|
+
readonly instancePath?: string;
|
|
4
|
+
}
|
|
5
|
+
export declare class ValidationError extends Error {
|
|
6
|
+
readonly details: readonly ValidationErrorDetail[];
|
|
7
|
+
constructor(prefix: string, details: readonly ValidationErrorDetail[]);
|
|
8
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export class ValidationError extends Error {
|
|
2
|
+
constructor(prefix, details) {
|
|
3
|
+
const parts = [];
|
|
4
|
+
for (const detail of details) {
|
|
5
|
+
const segments = [];
|
|
6
|
+
if (detail.instancePath) {
|
|
7
|
+
segments.push(detail.instancePath);
|
|
8
|
+
}
|
|
9
|
+
if (detail.message) {
|
|
10
|
+
segments.push(detail.message);
|
|
11
|
+
}
|
|
12
|
+
parts.push(segments.join(' '));
|
|
13
|
+
}
|
|
14
|
+
super(`${prefix}: ${parts.join(', ')}`);
|
|
15
|
+
this.name = 'ValidationError';
|
|
16
|
+
this.details = details;
|
|
17
|
+
}
|
|
18
|
+
}
|
package/lib/Options.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Style } from './Style.js';
|
|
2
|
+
import type { StyleOptionsFlipValue, StyleOptionsColorFillValue, StyleOptions } from './StyleOptions.js';
|
|
3
|
+
export declare class Options<D = unknown> {
|
|
4
|
+
#private;
|
|
5
|
+
constructor(style: Style<D>, data: StyleOptions<D>);
|
|
6
|
+
seed(): string;
|
|
7
|
+
size(): number | undefined;
|
|
8
|
+
idRandomization(): boolean;
|
|
9
|
+
title(): string | undefined;
|
|
10
|
+
flip(): StyleOptionsFlipValue;
|
|
11
|
+
fontFamily(): string;
|
|
12
|
+
fontWeight(): number;
|
|
13
|
+
scale(): number;
|
|
14
|
+
borderRadius(): number;
|
|
15
|
+
variant(name: string): string | undefined;
|
|
16
|
+
color(name: string): readonly string[];
|
|
17
|
+
colorFill(name: string): StyleOptionsColorFillValue;
|
|
18
|
+
colorAngle(name: string): number;
|
|
19
|
+
rotate(name?: string): number;
|
|
20
|
+
translateX(name?: string): number;
|
|
21
|
+
translateY(name?: string): number;
|
|
22
|
+
resolved(): StyleOptions<D>;
|
|
23
|
+
}
|
package/lib/Options.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _Options_instances, _Options_data, _Options_style, _Options_prng, _Options_colorResolving, _Options_result, _Options_numericComponentOption, _Options_probability, _Options_isVisible, _Options_colorFillStops, _Options_resolveColor, _Options_get, _Options_memo, _Options_toArray;
|
|
13
|
+
import { OptionsValidator } from './Validator/OptionsValidator.js';
|
|
14
|
+
import { Prng } from './Prng.js';
|
|
15
|
+
import { Color } from './Utils/Color.js';
|
|
16
|
+
import { CircularColorReferenceError } from './Error/CircularColorReferenceError.js';
|
|
17
|
+
export class Options {
|
|
18
|
+
constructor(style, data) {
|
|
19
|
+
_Options_instances.add(this);
|
|
20
|
+
_Options_data.set(this, void 0);
|
|
21
|
+
_Options_style.set(this, void 0);
|
|
22
|
+
_Options_prng.set(this, void 0);
|
|
23
|
+
_Options_colorResolving.set(this, []);
|
|
24
|
+
_Options_result.set(this, {});
|
|
25
|
+
OptionsValidator.validate(data);
|
|
26
|
+
__classPrivateFieldSet(this, _Options_data, structuredClone(data), "f");
|
|
27
|
+
__classPrivateFieldSet(this, _Options_style, style, "f");
|
|
28
|
+
__classPrivateFieldSet(this, _Options_prng, new Prng(this.seed()), "f");
|
|
29
|
+
}
|
|
30
|
+
seed() {
|
|
31
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_memo).call(this, 'seed', () => __classPrivateFieldGet(this, _Options_data, "f").seed ?? '');
|
|
32
|
+
}
|
|
33
|
+
size() {
|
|
34
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_memo).call(this, 'size', () => __classPrivateFieldGet(this, _Options_data, "f").size);
|
|
35
|
+
}
|
|
36
|
+
idRandomization() {
|
|
37
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_memo).call(this, 'idRandomization', () => __classPrivateFieldGet(this, _Options_data, "f").idRandomization ?? false);
|
|
38
|
+
}
|
|
39
|
+
title() {
|
|
40
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_memo).call(this, 'title', () => __classPrivateFieldGet(this, _Options_data, "f").title);
|
|
41
|
+
}
|
|
42
|
+
flip() {
|
|
43
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_memo).call(this, 'flip', () => __classPrivateFieldGet(this, _Options_prng, "f").pick('flip', __classPrivateFieldGet(this, _Options_instances, "m", _Options_toArray).call(this, __classPrivateFieldGet(this, _Options_data, "f").flip)) ?? 'none');
|
|
44
|
+
}
|
|
45
|
+
fontFamily() {
|
|
46
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_memo).call(this, 'fontFamily', () => __classPrivateFieldGet(this, _Options_prng, "f").pick('fontFamily', __classPrivateFieldGet(this, _Options_instances, "m", _Options_toArray).call(this, __classPrivateFieldGet(this, _Options_data, "f").fontFamily)) ??
|
|
47
|
+
'system-ui');
|
|
48
|
+
}
|
|
49
|
+
fontWeight() {
|
|
50
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_memo).call(this, 'fontWeight', () => __classPrivateFieldGet(this, _Options_prng, "f").pick('fontWeight', __classPrivateFieldGet(this, _Options_instances, "m", _Options_toArray).call(this, __classPrivateFieldGet(this, _Options_data, "f").fontWeight)) ??
|
|
51
|
+
400);
|
|
52
|
+
}
|
|
53
|
+
scale() {
|
|
54
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_memo).call(this, 'scale', () => __classPrivateFieldGet(this, _Options_prng, "f").float('scale', __classPrivateFieldGet(this, _Options_instances, "m", _Options_toArray).call(this, __classPrivateFieldGet(this, _Options_data, "f").scale)) ?? 1);
|
|
55
|
+
}
|
|
56
|
+
borderRadius() {
|
|
57
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_memo).call(this, 'borderRadius', () => __classPrivateFieldGet(this, _Options_prng, "f").float('borderRadius', __classPrivateFieldGet(this, _Options_instances, "m", _Options_toArray).call(this, __classPrivateFieldGet(this, _Options_data, "f").borderRadius)) ?? 0);
|
|
58
|
+
}
|
|
59
|
+
// Selects a variant for the given component. Depending on what was passed
|
|
60
|
+
// as `${name}Variant` in the input data:
|
|
61
|
+
// - undefined: PRNG picks from all style variants using their weights
|
|
62
|
+
// - string or string[]: PRNG picks from the given subset (weight 1 each)
|
|
63
|
+
// - Record<string, number>: PRNG picks using the provided weights
|
|
64
|
+
// Only variants that exist in the style definition are considered.
|
|
65
|
+
variant(name) {
|
|
66
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_memo).call(this, `${name}Variant`, () => {
|
|
67
|
+
if (!__classPrivateFieldGet(this, _Options_instances, "m", _Options_isVisible).call(this, name)) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
const component = __classPrivateFieldGet(this, _Options_style, "f").components().get(name);
|
|
71
|
+
if (!component) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
const raw = __classPrivateFieldGet(this, _Options_instances, "m", _Options_get).call(this, `${name}Variant`);
|
|
75
|
+
const variants = component.variants();
|
|
76
|
+
let entries;
|
|
77
|
+
if (raw === undefined) {
|
|
78
|
+
entries = Array.from(variants).map(([v, variant]) => [
|
|
79
|
+
v,
|
|
80
|
+
variant.weight(),
|
|
81
|
+
]);
|
|
82
|
+
}
|
|
83
|
+
else if (typeof raw === 'string' || Array.isArray(raw)) {
|
|
84
|
+
entries = __classPrivateFieldGet(this, _Options_instances, "m", _Options_toArray).call(this, raw)
|
|
85
|
+
.filter((v) => variants.has(v))
|
|
86
|
+
.map((v) => [v, 1]);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
entries = Object.entries(raw).filter(([v]) => variants.has(v));
|
|
90
|
+
}
|
|
91
|
+
return __classPrivateFieldGet(this, _Options_prng, "f").weightedPick(`${name}Variant`, entries);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
color(name) {
|
|
95
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_memo).call(this, `${name}Color`, () => __classPrivateFieldGet(this, _Options_instances, "m", _Options_resolveColor).call(this, name));
|
|
96
|
+
}
|
|
97
|
+
colorFill(name) {
|
|
98
|
+
const key = `${name}ColorFill`;
|
|
99
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_memo).call(this, key, () => {
|
|
100
|
+
const raw = __classPrivateFieldGet(this, _Options_instances, "m", _Options_get).call(this, key);
|
|
101
|
+
return __classPrivateFieldGet(this, _Options_prng, "f").pick(key, __classPrivateFieldGet(this, _Options_instances, "m", _Options_toArray).call(this, raw)) ?? 'solid';
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
colorAngle(name) {
|
|
105
|
+
const key = `${name}ColorAngle`;
|
|
106
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_memo).call(this, key, () => {
|
|
107
|
+
const raw = __classPrivateFieldGet(this, _Options_instances, "m", _Options_get).call(this, key);
|
|
108
|
+
return __classPrivateFieldGet(this, _Options_prng, "f").float(key, __classPrivateFieldGet(this, _Options_instances, "m", _Options_toArray).call(this, raw)) ?? 0;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
rotate(name) {
|
|
112
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_numericComponentOption).call(this, 'rotate', name, (c) => c.rotate());
|
|
113
|
+
}
|
|
114
|
+
translateX(name) {
|
|
115
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_numericComponentOption).call(this, 'translateX', name, (c) => c.translate().x());
|
|
116
|
+
}
|
|
117
|
+
translateY(name) {
|
|
118
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_numericComponentOption).call(this, 'translateY', name, (c) => c.translate().y());
|
|
119
|
+
}
|
|
120
|
+
resolved() {
|
|
121
|
+
return structuredClone(__classPrivateFieldGet(this, _Options_result, "f"));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
_Options_data = new WeakMap(), _Options_style = new WeakMap(), _Options_prng = new WeakMap(), _Options_colorResolving = new WeakMap(), _Options_result = new WeakMap(), _Options_instances = new WeakSet(), _Options_numericComponentOption = function _Options_numericComponentOption(option, name, componentDefault) {
|
|
125
|
+
const key = name
|
|
126
|
+
? `${name}${option.charAt(0).toUpperCase()}${option.slice(1)}`
|
|
127
|
+
: option;
|
|
128
|
+
return __classPrivateFieldGet(this, _Options_instances, "m", _Options_memo).call(this, key, () => {
|
|
129
|
+
const raw = __classPrivateFieldGet(this, _Options_instances, "m", _Options_get).call(this, key);
|
|
130
|
+
let values;
|
|
131
|
+
if (raw === undefined && name) {
|
|
132
|
+
const component = __classPrivateFieldGet(this, _Options_style, "f").components().get(name);
|
|
133
|
+
values = component ? componentDefault(component) : [];
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
values = __classPrivateFieldGet(this, _Options_instances, "m", _Options_toArray).call(this, raw);
|
|
137
|
+
}
|
|
138
|
+
return __classPrivateFieldGet(this, _Options_prng, "f").float(key, values) ?? 0;
|
|
139
|
+
});
|
|
140
|
+
}, _Options_probability = function _Options_probability(name) {
|
|
141
|
+
const raw = __classPrivateFieldGet(this, _Options_instances, "m", _Options_get).call(this, `${name}Probability`);
|
|
142
|
+
if (raw !== undefined) {
|
|
143
|
+
return raw;
|
|
144
|
+
}
|
|
145
|
+
return __classPrivateFieldGet(this, _Options_style, "f").components().get(name)?.probability() ?? 100;
|
|
146
|
+
}, _Options_isVisible = function _Options_isVisible(name) {
|
|
147
|
+
return __classPrivateFieldGet(this, _Options_prng, "f").bool(`${name}Probability`, __classPrivateFieldGet(this, _Options_instances, "m", _Options_probability).call(this, name));
|
|
148
|
+
}, _Options_colorFillStops = function _Options_colorFillStops(name) {
|
|
149
|
+
const raw = __classPrivateFieldGet(this, _Options_instances, "m", _Options_get).call(this, `${name}ColorFillStops`);
|
|
150
|
+
const values = __classPrivateFieldGet(this, _Options_instances, "m", _Options_toArray).call(this, raw);
|
|
151
|
+
return __classPrivateFieldGet(this, _Options_prng, "f").integer(`${name}ColorFillStops`, values) ?? 2;
|
|
152
|
+
}, _Options_resolveColor = function _Options_resolveColor(name) {
|
|
153
|
+
const raw = __classPrivateFieldGet(this, _Options_instances, "m", _Options_get).call(this, `${name}Color`);
|
|
154
|
+
const styleColor = __classPrivateFieldGet(this, _Options_style, "f").colors().get(name);
|
|
155
|
+
let source;
|
|
156
|
+
if (raw === undefined) {
|
|
157
|
+
source = styleColor ? styleColor.values() : [];
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
source = __classPrivateFieldGet(this, _Options_instances, "m", _Options_toArray).call(this, raw);
|
|
161
|
+
}
|
|
162
|
+
let candidates = source.map((c) => Color.toHex(c));
|
|
163
|
+
const fill = this.colorFill(name);
|
|
164
|
+
const stops = fill === 'solid' ? 1 : __classPrivateFieldGet(this, _Options_instances, "m", _Options_colorFillStops).call(this, name);
|
|
165
|
+
if (!styleColor) {
|
|
166
|
+
return __classPrivateFieldGet(this, _Options_prng, "f").shuffle(`${name}Color`, candidates).slice(0, stops);
|
|
167
|
+
}
|
|
168
|
+
// Detect circular references (e.g. a.contrastTo = b, b.contrastTo = a)
|
|
169
|
+
if (__classPrivateFieldGet(this, _Options_colorResolving, "f").includes(name)) {
|
|
170
|
+
throw new CircularColorReferenceError(__classPrivateFieldGet(this, _Options_colorResolving, "f").concat(name));
|
|
171
|
+
}
|
|
172
|
+
__classPrivateFieldGet(this, _Options_colorResolving, "f").push(name);
|
|
173
|
+
const contrastTo = styleColor.contrastTo();
|
|
174
|
+
const notEqualTo = styleColor.notEqualTo();
|
|
175
|
+
try {
|
|
176
|
+
if (contrastTo) {
|
|
177
|
+
const refColor = this.color(contrastTo)[0];
|
|
178
|
+
if (refColor) {
|
|
179
|
+
candidates = Color.sortByContrast(candidates, refColor);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (notEqualTo.length > 0) {
|
|
183
|
+
const excluded = [];
|
|
184
|
+
for (const ref of notEqualTo) {
|
|
185
|
+
for (const color of this.color(ref)) {
|
|
186
|
+
excluded.push(color);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
candidates = Color.filterNotEqualTo(candidates, excluded);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
finally {
|
|
193
|
+
__classPrivateFieldGet(this, _Options_colorResolving, "f").pop();
|
|
194
|
+
}
|
|
195
|
+
// Skip shuffle when sorted by contrast to preserve the ordering
|
|
196
|
+
const ordered = contrastTo
|
|
197
|
+
? candidates
|
|
198
|
+
: __classPrivateFieldGet(this, _Options_prng, "f").shuffle(`${name}Color`, candidates);
|
|
199
|
+
return ordered.slice(0, stops);
|
|
200
|
+
}, _Options_get = function _Options_get(key) {
|
|
201
|
+
return __classPrivateFieldGet(this, _Options_data, "f")[key];
|
|
202
|
+
}, _Options_memo = function _Options_memo(key, compute) {
|
|
203
|
+
if (key in __classPrivateFieldGet(this, _Options_result, "f")) {
|
|
204
|
+
return __classPrivateFieldGet(this, _Options_result, "f")[key];
|
|
205
|
+
}
|
|
206
|
+
const value = compute();
|
|
207
|
+
__classPrivateFieldGet(this, _Options_result, "f")[key] = value;
|
|
208
|
+
return value;
|
|
209
|
+
}, _Options_toArray = function _Options_toArray(value) {
|
|
210
|
+
if (value === undefined) {
|
|
211
|
+
return [];
|
|
212
|
+
}
|
|
213
|
+
if (Array.isArray(value)) {
|
|
214
|
+
return value;
|
|
215
|
+
}
|
|
216
|
+
return [value];
|
|
217
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Style } from './Style.js';
|
|
2
|
+
interface StringField {
|
|
3
|
+
readonly type: 'string';
|
|
4
|
+
readonly list?: true;
|
|
5
|
+
}
|
|
6
|
+
interface NumberField {
|
|
7
|
+
readonly type: 'number';
|
|
8
|
+
readonly min?: number;
|
|
9
|
+
readonly max?: number;
|
|
10
|
+
readonly list?: true;
|
|
11
|
+
}
|
|
12
|
+
interface BooleanField {
|
|
13
|
+
readonly type: 'boolean';
|
|
14
|
+
}
|
|
15
|
+
interface EnumField {
|
|
16
|
+
readonly type: 'enum';
|
|
17
|
+
readonly values: readonly string[];
|
|
18
|
+
readonly list?: true;
|
|
19
|
+
readonly weighted?: true;
|
|
20
|
+
}
|
|
21
|
+
interface ColorField {
|
|
22
|
+
readonly type: 'color';
|
|
23
|
+
readonly list?: true;
|
|
24
|
+
}
|
|
25
|
+
interface RangeField {
|
|
26
|
+
readonly type: 'range';
|
|
27
|
+
readonly min?: number;
|
|
28
|
+
readonly max?: number;
|
|
29
|
+
}
|
|
30
|
+
export type FieldDescriptor = StringField | NumberField | BooleanField | EnumField | ColorField | RangeField;
|
|
31
|
+
export type Descriptor = Record<string, FieldDescriptor>;
|
|
32
|
+
export declare class OptionsDescriptor {
|
|
33
|
+
#private;
|
|
34
|
+
constructor(style: Style);
|
|
35
|
+
toJSON(): Descriptor;
|
|
36
|
+
}
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _OptionsDescriptor_instances, _a, _OptionsDescriptor_rotateRange, _OptionsDescriptor_translateRange, _OptionsDescriptor_descriptor, _OptionsDescriptor_style, _OptionsDescriptor_build;
|
|
13
|
+
export class OptionsDescriptor {
|
|
14
|
+
constructor(style) {
|
|
15
|
+
_OptionsDescriptor_instances.add(this);
|
|
16
|
+
_OptionsDescriptor_descriptor.set(this, void 0);
|
|
17
|
+
_OptionsDescriptor_style.set(this, void 0);
|
|
18
|
+
__classPrivateFieldSet(this, _OptionsDescriptor_style, style, "f");
|
|
19
|
+
}
|
|
20
|
+
toJSON() {
|
|
21
|
+
__classPrivateFieldSet(this, _OptionsDescriptor_descriptor, __classPrivateFieldGet(this, _OptionsDescriptor_descriptor, "f") ?? __classPrivateFieldGet(this, _OptionsDescriptor_instances, "m", _OptionsDescriptor_build).call(this), "f");
|
|
22
|
+
return structuredClone(__classPrivateFieldGet(this, _OptionsDescriptor_descriptor, "f"));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
_a = OptionsDescriptor, _OptionsDescriptor_descriptor = new WeakMap(), _OptionsDescriptor_style = new WeakMap(), _OptionsDescriptor_instances = new WeakSet(), _OptionsDescriptor_build = function _OptionsDescriptor_build() {
|
|
26
|
+
const result = {
|
|
27
|
+
seed: { type: 'string' },
|
|
28
|
+
size: { type: 'number', min: 1 },
|
|
29
|
+
idRandomization: { type: 'boolean' },
|
|
30
|
+
title: { type: 'string' },
|
|
31
|
+
flip: {
|
|
32
|
+
type: 'enum',
|
|
33
|
+
values: ['none', 'horizontal', 'vertical', 'both'],
|
|
34
|
+
list: true,
|
|
35
|
+
},
|
|
36
|
+
fontFamily: { type: 'string', list: true },
|
|
37
|
+
fontWeight: { type: 'number', min: 1, max: 1000, list: true },
|
|
38
|
+
scale: { type: 'range', min: 0 },
|
|
39
|
+
borderRadius: { type: 'range', min: 0, max: 50 },
|
|
40
|
+
rotate: __classPrivateFieldGet(_a, _a, "f", _OptionsDescriptor_rotateRange),
|
|
41
|
+
translateX: __classPrivateFieldGet(_a, _a, "f", _OptionsDescriptor_translateRange),
|
|
42
|
+
translateY: __classPrivateFieldGet(_a, _a, "f", _OptionsDescriptor_translateRange),
|
|
43
|
+
};
|
|
44
|
+
for (const [name, component] of __classPrivateFieldGet(this, _OptionsDescriptor_style, "f").components()) {
|
|
45
|
+
const variants = Array.from(component.variants().keys()).sort();
|
|
46
|
+
result[`${name}Variant`] = {
|
|
47
|
+
type: 'enum',
|
|
48
|
+
values: variants,
|
|
49
|
+
list: true,
|
|
50
|
+
weighted: true,
|
|
51
|
+
};
|
|
52
|
+
result[`${name}Probability`] = { type: 'number', min: 0, max: 100 };
|
|
53
|
+
result[`${name}Rotate`] = __classPrivateFieldGet(_a, _a, "f", _OptionsDescriptor_rotateRange);
|
|
54
|
+
result[`${name}TranslateX`] = __classPrivateFieldGet(_a, _a, "f", _OptionsDescriptor_translateRange);
|
|
55
|
+
result[`${name}TranslateY`] = __classPrivateFieldGet(_a, _a, "f", _OptionsDescriptor_translateRange);
|
|
56
|
+
}
|
|
57
|
+
for (const name of [...__classPrivateFieldGet(this, _OptionsDescriptor_style, "f").colors().keys(), 'background']) {
|
|
58
|
+
result[`${name}Color`] = { type: 'color', list: true };
|
|
59
|
+
result[`${name}ColorFill`] = {
|
|
60
|
+
type: 'enum',
|
|
61
|
+
values: ['solid', 'linear', 'radial'],
|
|
62
|
+
list: true,
|
|
63
|
+
};
|
|
64
|
+
result[`${name}ColorFillStops`] = { type: 'range', min: 2 };
|
|
65
|
+
result[`${name}ColorAngle`] = __classPrivateFieldGet(_a, _a, "f", _OptionsDescriptor_rotateRange);
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
};
|
|
69
|
+
_OptionsDescriptor_rotateRange = { value: { type: 'range', min: -360, max: 360 } };
|
|
70
|
+
_OptionsDescriptor_translateRange = { value: { type: 'range', min: -100, max: 100 } };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// FNV-1a 32-bit hash.
|
|
2
|
+
// Offset basis: 0x811c9dc5, prime: 0x01000193.
|
|
3
|
+
// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
|
|
4
|
+
export class Fnv1a {
|
|
5
|
+
static hash(input) {
|
|
6
|
+
let hash = 0x811c9dc5;
|
|
7
|
+
for (let i = 0; i < input.length; i++) {
|
|
8
|
+
hash ^= input.charCodeAt(i);
|
|
9
|
+
hash = Math.imul(hash, 0x01000193);
|
|
10
|
+
}
|
|
11
|
+
return hash >>> 0;
|
|
12
|
+
}
|
|
13
|
+
static hex(input) {
|
|
14
|
+
return Fnv1a.hash(input).toString(16).padStart(8, '0');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _Mulberry32_state;
|
|
13
|
+
// Mulberry32 PRNG — stateful, matching the C reference by Tommy Ettinger.
|
|
14
|
+
// https://gist.github.com/tommyettinger/46a874533244883189143505d203312c
|
|
15
|
+
//
|
|
16
|
+
// C original:
|
|
17
|
+
// uint32_t z = (x += 0x6D2B79F5UL);
|
|
18
|
+
// z = (z ^ (z >> 15)) * (z | 1UL);
|
|
19
|
+
// z ^= z + (z ^ (z >> 7)) * (z | 61UL);
|
|
20
|
+
// return z ^ (z >> 14);
|
|
21
|
+
const UINT32_MAX_PLUS_1 = 2 ** 32;
|
|
22
|
+
export class Mulberry32 {
|
|
23
|
+
constructor(seed) {
|
|
24
|
+
_Mulberry32_state.set(this, void 0);
|
|
25
|
+
__classPrivateFieldSet(this, _Mulberry32_state, seed, "f");
|
|
26
|
+
}
|
|
27
|
+
next() {
|
|
28
|
+
const z = (__classPrivateFieldSet(this, _Mulberry32_state, (__classPrivateFieldGet(this, _Mulberry32_state, "f") + 0x6d2b79f5) | 0, "f"));
|
|
29
|
+
let t = Math.imul(z ^ (z >>> 15), z | 1);
|
|
30
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
31
|
+
return ((t ^ (t >>> 14)) >>> 0);
|
|
32
|
+
}
|
|
33
|
+
nextFloat() {
|
|
34
|
+
return this.next() / UINT32_MAX_PLUS_1;
|
|
35
|
+
}
|
|
36
|
+
state() {
|
|
37
|
+
return __classPrivateFieldGet(this, _Mulberry32_state, "f");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
_Mulberry32_state = new WeakMap();
|
package/lib/Prng.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class Prng {
|
|
2
|
+
#private;
|
|
3
|
+
constructor(seed: string);
|
|
4
|
+
pick<T>(key: string, items: readonly T[]): T | undefined;
|
|
5
|
+
weightedPick<T>(key: string, entries: readonly (readonly [T, number])[]): T | undefined;
|
|
6
|
+
bool(key: string, likelihood?: number): boolean;
|
|
7
|
+
float(key: string, values: readonly number[]): number | undefined;
|
|
8
|
+
integer(key: string, values: readonly number[]): number | undefined;
|
|
9
|
+
shuffle<T>(key: string, items: readonly T[]): T[];
|
|
10
|
+
getValue(key: string): number;
|
|
11
|
+
}
|