@dicebear/core 10.0.0-rc.2 → 10.0.0-rc.4

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.
Files changed (59) hide show
  1. package/lib/Avatar.d.ts +17 -1
  2. package/lib/Avatar.js +23 -5
  3. package/lib/Error/CircularColorReferenceError.d.ts +4 -0
  4. package/lib/Error/CircularColorReferenceError.js +4 -0
  5. package/lib/Error/OptionsValidationError.d.ts +3 -0
  6. package/lib/Error/OptionsValidationError.js +3 -0
  7. package/lib/Error/StyleValidationError.d.ts +3 -0
  8. package/lib/Error/StyleValidationError.js +3 -0
  9. package/lib/Error/ValidationError.d.ts +4 -0
  10. package/lib/Error/ValidationError.js +4 -0
  11. package/lib/Options.d.ts +39 -18
  12. package/lib/Options.js +64 -164
  13. package/lib/OptionsDescriptor.d.ts +8 -0
  14. package/lib/OptionsDescriptor.js +14 -6
  15. package/lib/Prng/Fnv1a.d.ts +14 -0
  16. package/lib/Prng/Fnv1a.js +14 -3
  17. package/lib/Prng/Mulberry32.d.ts +22 -0
  18. package/lib/Prng/Mulberry32.js +22 -8
  19. package/lib/Prng.d.ts +35 -0
  20. package/lib/Prng.js +35 -6
  21. package/lib/Renderer.d.ts +11 -2
  22. package/lib/Renderer.js +60 -54
  23. package/lib/Resolver.d.ts +62 -0
  24. package/lib/Resolver.js +204 -0
  25. package/lib/Style/Canvas.d.ts +14 -0
  26. package/lib/Style/Canvas.js +14 -0
  27. package/lib/Style/Color.d.ts +14 -0
  28. package/lib/Style/Color.js +14 -0
  29. package/lib/Style/Component.d.ts +50 -1
  30. package/lib/Style/Component.js +88 -9
  31. package/lib/Style/ComponentTranslate.d.ts +10 -0
  32. package/lib/Style/ComponentTranslate.js +10 -0
  33. package/lib/Style/ComponentVariant.d.ts +10 -0
  34. package/lib/Style/ComponentVariant.js +10 -0
  35. package/lib/Style/Element.d.ts +25 -0
  36. package/lib/Style/Element.js +25 -0
  37. package/lib/Style/Meta.d.ts +16 -0
  38. package/lib/Style/Meta.js +16 -0
  39. package/lib/Style/MetaCreator.d.ts +9 -0
  40. package/lib/Style/MetaCreator.js +9 -0
  41. package/lib/Style/MetaLicense.d.ts +12 -0
  42. package/lib/Style/MetaLicense.js +12 -0
  43. package/lib/Style/MetaSource.d.ts +10 -0
  44. package/lib/Style/MetaSource.js +10 -0
  45. package/lib/Style.d.ts +37 -1
  46. package/lib/Style.js +90 -6
  47. package/lib/StyleDefinition.d.ts +8 -3
  48. package/lib/StyleOptions.d.ts +12 -10
  49. package/lib/Utils/Color.d.ts +28 -0
  50. package/lib/Utils/Color.js +28 -8
  51. package/lib/Utils/Initials.d.ts +10 -0
  52. package/lib/Utils/Initials.js +10 -3
  53. package/lib/Utils/License.d.ts +14 -0
  54. package/lib/Utils/License.js +14 -0
  55. package/lib/Utils/Xml.d.ts +6 -0
  56. package/lib/Utils/Xml.js +6 -0
  57. package/lib/Validator/OptionsValidator.js +1549 -1675
  58. package/lib/Validator/StyleValidator.js +3355 -3115
  59. package/package.json +2 -2
@@ -10,24 +10,49 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _Element_data, _Element_children;
13
+ /**
14
+ * Read-only view over a single render-tree element from a style definition.
15
+ *
16
+ * The same node type covers SVG elements, text, and component references —
17
+ * `type()` discriminates between them.
18
+ */
13
19
  export class Element {
14
20
  constructor(data) {
15
21
  _Element_data.set(this, void 0);
16
22
  _Element_children.set(this, void 0);
17
23
  __classPrivateFieldSet(this, _Element_data, data, "f");
18
24
  }
25
+ /**
26
+ * Returns the element type discriminator (`svg`, `text`, `component`, …).
27
+ */
19
28
  type() {
20
29
  return __classPrivateFieldGet(this, _Element_data, "f").type;
21
30
  }
31
+ /**
32
+ * Returns the element's tag/component name, or `undefined` for elements
33
+ * that don't have one.
34
+ */
22
35
  name() {
23
36
  return __classPrivateFieldGet(this, _Element_data, "f").name;
24
37
  }
38
+ /**
39
+ * Returns the element's textual value (for `text` elements) or template
40
+ * fragment, or `undefined` when not applicable.
41
+ */
25
42
  value() {
26
43
  return __classPrivateFieldGet(this, _Element_data, "f").value;
27
44
  }
45
+ /**
46
+ * Returns the element's raw attribute map, or `undefined` when no
47
+ * attributes are defined.
48
+ */
28
49
  attributes() {
29
50
  return __classPrivateFieldGet(this, _Element_data, "f").attributes;
30
51
  }
52
+ /**
53
+ * Returns the element's children, lazily wrapped as {@link Element}
54
+ * instances on first access.
55
+ */
31
56
  children() {
32
57
  __classPrivateFieldSet(this, _Element_children, __classPrivateFieldGet(this, _Element_children, "f") ?? (__classPrivateFieldGet(this, _Element_data, "f").children ?? []).map((child) => new Element(child)), "f");
33
58
  return __classPrivateFieldGet(this, _Element_children, "f");
@@ -2,10 +2,26 @@ import type { StyleDefinitionMeta } from '../StyleDefinition.js';
2
2
  import { MetaLicense } from './MetaLicense.js';
3
3
  import { MetaCreator } from './MetaCreator.js';
4
4
  import { MetaSource } from './MetaSource.js';
5
+ /**
6
+ * Lazily-constructed view over a style definition's `meta` block, exposing
7
+ * the license, creator, and source descriptors.
8
+ */
5
9
  export declare class Meta {
6
10
  #private;
7
11
  constructor(data: StyleDefinitionMeta);
12
+ /**
13
+ * Returns the license descriptor, defaulting to an empty object when the
14
+ * style definition omits the field.
15
+ */
8
16
  license(): MetaLicense;
17
+ /**
18
+ * Returns the creator descriptor, defaulting to an empty object when the
19
+ * style definition omits the field.
20
+ */
9
21
  creator(): MetaCreator;
22
+ /**
23
+ * Returns the source descriptor, defaulting to an empty object when the
24
+ * style definition omits the field.
25
+ */
10
26
  source(): MetaSource;
11
27
  }
package/lib/Style/Meta.js CHANGED
@@ -13,6 +13,10 @@ var _Meta_data, _Meta_license, _Meta_creator, _Meta_source;
13
13
  import { MetaLicense } from './MetaLicense.js';
14
14
  import { MetaCreator } from './MetaCreator.js';
15
15
  import { MetaSource } from './MetaSource.js';
16
+ /**
17
+ * Lazily-constructed view over a style definition's `meta` block, exposing
18
+ * the license, creator, and source descriptors.
19
+ */
16
20
  export class Meta {
17
21
  constructor(data) {
18
22
  _Meta_data.set(this, void 0);
@@ -21,14 +25,26 @@ export class Meta {
21
25
  _Meta_source.set(this, void 0);
22
26
  __classPrivateFieldSet(this, _Meta_data, data, "f");
23
27
  }
28
+ /**
29
+ * Returns the license descriptor, defaulting to an empty object when the
30
+ * style definition omits the field.
31
+ */
24
32
  license() {
25
33
  __classPrivateFieldSet(this, _Meta_license, __classPrivateFieldGet(this, _Meta_license, "f") ?? new MetaLicense(__classPrivateFieldGet(this, _Meta_data, "f").license ?? {}), "f");
26
34
  return __classPrivateFieldGet(this, _Meta_license, "f");
27
35
  }
36
+ /**
37
+ * Returns the creator descriptor, defaulting to an empty object when the
38
+ * style definition omits the field.
39
+ */
28
40
  creator() {
29
41
  __classPrivateFieldSet(this, _Meta_creator, __classPrivateFieldGet(this, _Meta_creator, "f") ?? new MetaCreator(__classPrivateFieldGet(this, _Meta_data, "f").creator ?? {}), "f");
30
42
  return __classPrivateFieldGet(this, _Meta_creator, "f");
31
43
  }
44
+ /**
45
+ * Returns the source descriptor, defaulting to an empty object when the
46
+ * style definition omits the field.
47
+ */
32
48
  source() {
33
49
  __classPrivateFieldSet(this, _Meta_source, __classPrivateFieldGet(this, _Meta_source, "f") ?? new MetaSource(__classPrivateFieldGet(this, _Meta_data, "f").source ?? {}), "f");
34
50
  return __classPrivateFieldGet(this, _Meta_source, "f");
@@ -1,7 +1,16 @@
1
1
  import type { StyleDefinitionMetaCreator } from '../StyleDefinition.js';
2
+ /**
3
+ * Read-only view over the `meta.creator` block of a style definition.
4
+ */
2
5
  export declare class MetaCreator {
3
6
  #private;
4
7
  constructor(data: StyleDefinitionMetaCreator);
8
+ /**
9
+ * Returns the creator's display name, or `undefined` when not set.
10
+ */
5
11
  name(): string | undefined;
12
+ /**
13
+ * Returns the creator's homepage URL, or `undefined` when not set.
14
+ */
6
15
  url(): string | undefined;
7
16
  }
@@ -10,14 +10,23 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _MetaCreator_data;
13
+ /**
14
+ * Read-only view over the `meta.creator` block of a style definition.
15
+ */
13
16
  export class MetaCreator {
14
17
  constructor(data) {
15
18
  _MetaCreator_data.set(this, void 0);
16
19
  __classPrivateFieldSet(this, _MetaCreator_data, data, "f");
17
20
  }
21
+ /**
22
+ * Returns the creator's display name, or `undefined` when not set.
23
+ */
18
24
  name() {
19
25
  return __classPrivateFieldGet(this, _MetaCreator_data, "f").name;
20
26
  }
27
+ /**
28
+ * Returns the creator's homepage URL, or `undefined` when not set.
29
+ */
21
30
  url() {
22
31
  return __classPrivateFieldGet(this, _MetaCreator_data, "f").url;
23
32
  }
@@ -1,8 +1,20 @@
1
1
  import type { StyleDefinitionMetaLicense } from '../StyleDefinition.js';
2
+ /**
3
+ * Read-only view over the `meta.license` block of a style definition.
4
+ */
2
5
  export declare class MetaLicense {
3
6
  #private;
4
7
  constructor(data: StyleDefinitionMetaLicense);
8
+ /**
9
+ * Returns the license name (e.g. `"CC BY 4.0"`), or `undefined` when not set.
10
+ */
5
11
  name(): string | undefined;
12
+ /**
13
+ * Returns the license URL, or `undefined` when not set.
14
+ */
6
15
  url(): string | undefined;
16
+ /**
17
+ * Returns the full license text, or `undefined` when not set.
18
+ */
7
19
  text(): string | undefined;
8
20
  }
@@ -10,17 +10,29 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _MetaLicense_data;
13
+ /**
14
+ * Read-only view over the `meta.license` block of a style definition.
15
+ */
13
16
  export class MetaLicense {
14
17
  constructor(data) {
15
18
  _MetaLicense_data.set(this, void 0);
16
19
  __classPrivateFieldSet(this, _MetaLicense_data, data, "f");
17
20
  }
21
+ /**
22
+ * Returns the license name (e.g. `"CC BY 4.0"`), or `undefined` when not set.
23
+ */
18
24
  name() {
19
25
  return __classPrivateFieldGet(this, _MetaLicense_data, "f").name;
20
26
  }
27
+ /**
28
+ * Returns the license URL, or `undefined` when not set.
29
+ */
21
30
  url() {
22
31
  return __classPrivateFieldGet(this, _MetaLicense_data, "f").url;
23
32
  }
33
+ /**
34
+ * Returns the full license text, or `undefined` when not set.
35
+ */
24
36
  text() {
25
37
  return __classPrivateFieldGet(this, _MetaLicense_data, "f").text;
26
38
  }
@@ -1,7 +1,17 @@
1
1
  import type { StyleDefinitionMetaSource } from '../StyleDefinition.js';
2
+ /**
3
+ * Read-only view over the `meta.source` block of a style definition.
4
+ */
2
5
  export declare class MetaSource {
3
6
  #private;
4
7
  constructor(data: StyleDefinitionMetaSource);
8
+ /**
9
+ * Returns the source name (e.g. the original work title), or `undefined`
10
+ * when not set.
11
+ */
5
12
  name(): string | undefined;
13
+ /**
14
+ * Returns the URL of the source, or `undefined` when not set.
15
+ */
6
16
  url(): string | undefined;
7
17
  }
@@ -10,14 +10,24 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _MetaSource_data;
13
+ /**
14
+ * Read-only view over the `meta.source` block of a style definition.
15
+ */
13
16
  export class MetaSource {
14
17
  constructor(data) {
15
18
  _MetaSource_data.set(this, void 0);
16
19
  __classPrivateFieldSet(this, _MetaSource_data, data, "f");
17
20
  }
21
+ /**
22
+ * Returns the source name (e.g. the original work title), or `undefined`
23
+ * when not set.
24
+ */
18
25
  name() {
19
26
  return __classPrivateFieldGet(this, _MetaSource_data, "f").name;
20
27
  }
28
+ /**
29
+ * Returns the URL of the source, or `undefined` when not set.
30
+ */
21
31
  url() {
22
32
  return __classPrivateFieldGet(this, _MetaSource_data, "f").url;
23
33
  }
package/lib/Style.d.ts CHANGED
@@ -1,18 +1,54 @@
1
- import type { StyleDefinitionAttributes } from './StyleDefinition.js';
1
+ import type { StyleDefinition, StyleDefinitionAttributes } from './StyleDefinition.js';
2
2
  import { Meta } from './Style/Meta.js';
3
3
  import { Canvas } from './Style/Canvas.js';
4
4
  import { Component } from './Style/Component.js';
5
5
  import { Color } from './Style/Color.js';
6
6
  export type { StyleDefinition } from './StyleDefinition.js';
7
+ /**
8
+ * Validated, lazily-decomposed wrapper around a style definition. Construction
9
+ * runs the JSON Schema validator and stores a deep clone of the input so that
10
+ * later mutation of the source object cannot leak into the rendered avatar.
11
+ */
7
12
  export declare class Style<D = unknown> {
8
13
  #private;
9
14
  constructor(data: D);
15
+ /**
16
+ * Returns the definition's `$id`, or `undefined` when not set.
17
+ */
10
18
  id(): string | undefined;
19
+ /**
20
+ * Returns the definition's `$schema` URI, or `undefined` when not set.
21
+ */
11
22
  schema(): string | undefined;
23
+ /**
24
+ * Returns the definition's `$comment`, or `undefined` when not set.
25
+ */
12
26
  comment(): string | undefined;
27
+ /**
28
+ * Returns the {@link Meta} view, lazily constructed on first access.
29
+ */
13
30
  meta(): Meta;
31
+ /**
32
+ * Returns a deep clone of the root SVG attributes from the definition,
33
+ * defaulting to an empty object.
34
+ */
14
35
  attributes(): StyleDefinitionAttributes;
36
+ /**
37
+ * Returns a deep clone of the underlying definition.
38
+ */
39
+ definition(): StyleDefinition;
40
+ /**
41
+ * Returns the {@link Canvas} view, lazily constructed on first access.
42
+ */
15
43
  canvas(): Canvas;
44
+ /**
45
+ * Returns a name → {@link Component} map for all defined components, built
46
+ * lazily on first access.
47
+ */
16
48
  components(): ReadonlyMap<string, Component>;
49
+ /**
50
+ * Returns a name → {@link Color} map for all defined colors, built lazily
51
+ * on first access.
52
+ */
17
53
  colors(): ReadonlyMap<string, Color>;
18
54
  }
package/lib/Style.js CHANGED
@@ -9,14 +9,21 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
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
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _Style_data, _Style_meta, _Style_canvas, _Style_components, _Style_colors;
12
+ var _Style_instances, _a, _Style_data, _Style_meta, _Style_canvas, _Style_components, _Style_colors, _Style_validateAliases, _Style_isAlias;
13
13
  import { StyleValidator } from './Validator/StyleValidator.js';
14
+ import { StyleValidationError } from './Error/StyleValidationError.js';
14
15
  import { Meta } from './Style/Meta.js';
15
16
  import { Canvas } from './Style/Canvas.js';
16
17
  import { Component } from './Style/Component.js';
17
18
  import { Color } from './Style/Color.js';
19
+ /**
20
+ * Validated, lazily-decomposed wrapper around a style definition. Construction
21
+ * runs the JSON Schema validator and stores a deep clone of the input so that
22
+ * later mutation of the source object cannot leak into the rendered avatar.
23
+ */
18
24
  export class Style {
19
25
  constructor(data) {
26
+ _Style_instances.add(this);
20
27
  _Style_data.set(this, void 0);
21
28
  _Style_meta.set(this, void 0);
22
29
  _Style_canvas.set(this, void 0);
@@ -24,34 +31,80 @@ export class Style {
24
31
  _Style_colors.set(this, void 0);
25
32
  StyleValidator.validate(data);
26
33
  __classPrivateFieldSet(this, _Style_data, structuredClone(data), "f");
34
+ __classPrivateFieldGet(this, _Style_instances, "m", _Style_validateAliases).call(this);
27
35
  }
36
+ /**
37
+ * Returns the definition's `$id`, or `undefined` when not set.
38
+ */
28
39
  id() {
29
40
  return __classPrivateFieldGet(this, _Style_data, "f").$id;
30
41
  }
42
+ /**
43
+ * Returns the definition's `$schema` URI, or `undefined` when not set.
44
+ */
31
45
  schema() {
32
46
  return __classPrivateFieldGet(this, _Style_data, "f").$schema;
33
47
  }
48
+ /**
49
+ * Returns the definition's `$comment`, or `undefined` when not set.
50
+ */
34
51
  comment() {
35
52
  return __classPrivateFieldGet(this, _Style_data, "f").$comment;
36
53
  }
54
+ /**
55
+ * Returns the {@link Meta} view, lazily constructed on first access.
56
+ */
37
57
  meta() {
38
58
  __classPrivateFieldSet(this, _Style_meta, __classPrivateFieldGet(this, _Style_meta, "f") ?? new Meta(__classPrivateFieldGet(this, _Style_data, "f").meta ?? {}), "f");
39
59
  return __classPrivateFieldGet(this, _Style_meta, "f");
40
60
  }
61
+ /**
62
+ * Returns a deep clone of the root SVG attributes from the definition,
63
+ * defaulting to an empty object.
64
+ */
41
65
  attributes() {
42
66
  return structuredClone(__classPrivateFieldGet(this, _Style_data, "f").attributes ?? {});
43
67
  }
68
+ /**
69
+ * Returns a deep clone of the underlying definition.
70
+ */
71
+ definition() {
72
+ return structuredClone(__classPrivateFieldGet(this, _Style_data, "f"));
73
+ }
74
+ /**
75
+ * Returns the {@link Canvas} view, lazily constructed on first access.
76
+ */
44
77
  canvas() {
45
78
  __classPrivateFieldSet(this, _Style_canvas, __classPrivateFieldGet(this, _Style_canvas, "f") ?? new Canvas(__classPrivateFieldGet(this, _Style_data, "f").canvas), "f");
46
79
  return __classPrivateFieldGet(this, _Style_canvas, "f");
47
80
  }
81
+ /**
82
+ * Returns a name → {@link Component} map for all defined components, built
83
+ * lazily on first access.
84
+ */
48
85
  components() {
49
- __classPrivateFieldSet(this, _Style_components, __classPrivateFieldGet(this, _Style_components, "f") ?? new Map(Object.entries(__classPrivateFieldGet(this, _Style_data, "f").components ?? {}).map(([name, data]) => [
50
- name,
51
- new Component(data),
52
- ])), "f");
86
+ if (__classPrivateFieldGet(this, _Style_components, "f")) {
87
+ return __classPrivateFieldGet(this, _Style_components, "f");
88
+ }
89
+ const entries = Object.entries(__classPrivateFieldGet(this, _Style_data, "f").components ?? {});
90
+ const map = new Map();
91
+ for (const [name, data] of entries) {
92
+ if (!__classPrivateFieldGet(_a, _a, "m", _Style_isAlias).call(_a, data)) {
93
+ map.set(name, new Component(name, data));
94
+ }
95
+ }
96
+ for (const [name, data] of entries) {
97
+ if (__classPrivateFieldGet(_a, _a, "m", _Style_isAlias).call(_a, data)) {
98
+ map.set(name, new Component(name, data, map.get(data.extends)));
99
+ }
100
+ }
101
+ __classPrivateFieldSet(this, _Style_components, map, "f");
53
102
  return __classPrivateFieldGet(this, _Style_components, "f");
54
103
  }
104
+ /**
105
+ * Returns a name → {@link Color} map for all defined colors, built lazily
106
+ * on first access.
107
+ */
55
108
  colors() {
56
109
  __classPrivateFieldSet(this, _Style_colors, __classPrivateFieldGet(this, _Style_colors, "f") ?? new Map(Object.entries(__classPrivateFieldGet(this, _Style_data, "f").colors ?? {}).map(([name, data]) => [
57
110
  name,
@@ -60,4 +113,35 @@ export class Style {
60
113
  return __classPrivateFieldGet(this, _Style_colors, "f");
61
114
  }
62
115
  }
63
- _Style_data = new WeakMap(), _Style_meta = new WeakMap(), _Style_canvas = new WeakMap(), _Style_components = new WeakMap(), _Style_colors = new WeakMap();
116
+ _a = Style, _Style_data = new WeakMap(), _Style_meta = new WeakMap(), _Style_canvas = new WeakMap(), _Style_components = new WeakMap(), _Style_colors = new WeakMap(), _Style_instances = new WeakSet(), _Style_validateAliases = function _Style_validateAliases() {
117
+ const components = __classPrivateFieldGet(this, _Style_data, "f").components;
118
+ if (!components) {
119
+ return;
120
+ }
121
+ const errors = [];
122
+ for (const [name, data] of Object.entries(components)) {
123
+ if (!__classPrivateFieldGet(_a, _a, "m", _Style_isAlias).call(_a, data)) {
124
+ continue;
125
+ }
126
+ const target = data.extends;
127
+ const targetData = components[target];
128
+ if (!targetData) {
129
+ errors.push({
130
+ instancePath: `/components/${name}/extends`,
131
+ message: `references unknown component "${target}"`,
132
+ });
133
+ continue;
134
+ }
135
+ if (__classPrivateFieldGet(_a, _a, "m", _Style_isAlias).call(_a, targetData)) {
136
+ errors.push({
137
+ instancePath: `/components/${name}/extends`,
138
+ message: `references alias "${target}" — alias chains are not allowed`,
139
+ });
140
+ }
141
+ }
142
+ if (errors.length > 0) {
143
+ throw new StyleValidationError(errors);
144
+ }
145
+ }, _Style_isAlias = function _Style_isAlias(data) {
146
+ return 'extends' in data;
147
+ };
@@ -18,11 +18,11 @@ export interface StyleDefinitionMeta {
18
18
  }
19
19
  export interface StyleDefinitionVariableReference {
20
20
  readonly type: 'variable';
21
- readonly value: 'initial' | 'initials' | 'fontWeight' | 'fontFamily';
21
+ readonly name: 'initial' | 'initials' | 'fontWeight' | 'fontFamily';
22
22
  }
23
23
  export interface StyleDefinitionColorReference {
24
24
  readonly type: 'color';
25
- readonly value: string;
25
+ readonly name: string;
26
26
  }
27
27
  export type StyleDefinitionColorAttributeValue = string | StyleDefinitionColorReference;
28
28
  export type StyleDefinitionElementValue = string | StyleDefinitionVariableReference;
@@ -63,14 +63,19 @@ export interface StyleDefinitionComponentVariant {
63
63
  readonly elements: readonly StyleDefinitionElement[];
64
64
  readonly weight?: number;
65
65
  }
66
- export interface StyleDefinitionComponent {
66
+ export interface StyleDefinitionComponentBase {
67
67
  readonly width: number;
68
68
  readonly height: number;
69
69
  readonly probability?: number;
70
70
  readonly rotate?: readonly number[];
71
+ readonly scale?: readonly number[];
71
72
  readonly translate?: StyleDefinitionComponentTranslate;
72
73
  readonly variants: Readonly<Record<string, StyleDefinitionComponentVariant>>;
73
74
  }
75
+ export interface StyleDefinitionComponentAlias {
76
+ readonly extends: string;
77
+ }
78
+ export type StyleDefinitionComponent = StyleDefinitionComponentBase | StyleDefinitionComponentAlias;
74
79
  export interface StyleDefinition {
75
80
  readonly $id?: string;
76
81
  readonly $schema?: string;
@@ -7,11 +7,14 @@ type ColorNames<D> = D extends {
7
7
  colors: Record<infer K extends string, unknown>;
8
8
  } ? string extends K ? never : K : never;
9
9
  type AllColorNames<D> = ColorNames<D> | 'background';
10
- type VariantNames<D, C extends string> = D extends {
10
+ type ResolveComponent<D, C extends string> = D extends {
11
11
  components: Record<string, unknown>;
12
12
  } ? C extends keyof D['components'] ? D['components'][C] extends {
13
+ extends: infer P extends string;
14
+ } ? P extends keyof D['components'] ? D['components'][P] : never : D['components'][C] : never : never;
15
+ type VariantNames<D, C extends string> = ResolveComponent<D, C> extends {
13
16
  variants: Record<infer V extends string, unknown>;
14
- } ? string extends V ? string : V : string : string : string;
17
+ } ? string extends V ? string : V : string;
15
18
  type HasSpecificKeys<D> = [ComponentNames<D>] extends [never] ? [ColorNames<D>] extends [never] ? false : true : true;
16
19
  export interface StyleOptionsBase {
17
20
  readonly seed?: string;
@@ -28,16 +31,15 @@ export interface StyleOptionsBase {
28
31
  readonly translateY?: number | readonly [number, number];
29
32
  }
30
33
  type ComponentVariantOption<D, K extends string> = VariantNames<D, K> | readonly VariantNames<D, K>[] | Readonly<Partial<Record<VariantNames<D, K>, number>>>;
34
+ type IsAlias<D, C extends string> = D extends {
35
+ components: Record<string, unknown>;
36
+ } ? C extends keyof D['components'] ? D['components'][C] extends {
37
+ extends: string;
38
+ } ? true : false : false : false;
31
39
  type ComponentOptions<D, C extends string> = [C] extends [never] ? unknown : {
32
- readonly [K in C as `${K}Variant`]?: ComponentVariantOption<D, K>;
33
- } & {
34
- readonly [K in C as `${K}Probability`]?: number;
35
- } & {
36
- readonly [K in C as `${K}Rotate`]?: number | readonly [number, number];
37
- } & {
38
- readonly [K in C as `${K}TranslateX`]?: number | readonly [number, number];
40
+ readonly [K in C as IsAlias<D, K> extends true ? never : `${K}Variant`]?: ComponentVariantOption<D, K>;
39
41
  } & {
40
- readonly [K in C as `${K}TranslateY`]?: number | readonly [number, number];
42
+ readonly [K in C as IsAlias<D, K> extends true ? never : `${K}Probability`]?: number;
41
43
  };
42
44
  type ColorOptions<C extends string> = [C] extends [never] ? unknown : {
43
45
  readonly [K in C as `${K}Color`]?: string | readonly string[];
@@ -1,9 +1,37 @@
1
+ /**
2
+ * Color helpers used by the renderer and option resolver.
3
+ */
1
4
  export declare class Color {
2
5
  #private;
6
+ /**
7
+ * Normalizes any hex format to 6- or 8-digit lowercase with `#` prefix.
8
+ */
3
9
  static toHex(hex: string): string;
10
+ /**
11
+ * Like {@link toHex}, but strips the alpha channel and always returns
12
+ * 6-digit hex.
13
+ */
4
14
  static toRgbHex(hex: string): string;
15
+ /**
16
+ * Parses a hex color into an `[r, g, b]` triple of 8-bit channel values.
17
+ */
5
18
  static parseHex(hex: string): [number, number, number];
19
+ /**
20
+ * WCAG 2.1 relative luminance with sRGB linearization.
21
+ *
22
+ * @see https://www.w3.org/WAI/GL/wiki/Relative_luminance
23
+ */
6
24
  static luminance(hex: string): number;
25
+ /**
26
+ * Returns a new array sorted by descending contrast against the reference
27
+ * color.
28
+ *
29
+ * @see https://www.w3.org/WAI/GL/wiki/Contrast_ratio
30
+ */
7
31
  static sortByContrast(candidates: readonly string[], refColor: string): string[];
32
+ /**
33
+ * Returns a new array with excluded colors removed. Falls back to the
34
+ * original candidates when filtering would empty the list.
35
+ */
8
36
  static filterNotEqualTo(candidates: readonly string[], excluded: readonly string[]): string[];
9
37
  }
@@ -4,8 +4,13 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
4
4
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
5
  };
6
6
  var _a, _Color_linearize;
7
+ /**
8
+ * Color helpers used by the renderer and option resolver.
9
+ */
7
10
  export class Color {
8
- // Normalizes any hex format to 6- or 8-digit lowercase with '#' prefix.
11
+ /**
12
+ * Normalizes any hex format to 6- or 8-digit lowercase with `#` prefix.
13
+ */
9
14
  static toHex(hex) {
10
15
  const h = hex.replace(/^#/, '').toLowerCase();
11
16
  if (h.length === 3) {
@@ -16,11 +21,17 @@ export class Color {
16
21
  }
17
22
  return '#' + h;
18
23
  }
19
- // Like toHex, but strips the alpha channel to always return 6-digit hex.
24
+ /**
25
+ * Like {@link toHex}, but strips the alpha channel and always returns
26
+ * 6-digit hex.
27
+ */
20
28
  static toRgbHex(hex) {
21
29
  const h = this.toHex(hex);
22
30
  return h.length > 7 ? h.slice(0, 7) : h;
23
31
  }
32
+ /**
33
+ * Parses a hex color into an `[r, g, b]` triple of 8-bit channel values.
34
+ */
24
35
  static parseHex(hex) {
25
36
  const h = this.toHex(hex).slice(1);
26
37
  return [
@@ -29,8 +40,11 @@ export class Color {
29
40
  parseInt(h.slice(4, 6), 16),
30
41
  ];
31
42
  }
32
- // WCAG 2.1 relative luminance with sRGB linearization.
33
- // https://www.w3.org/WAI/GL/wiki/Relative_luminance
43
+ /**
44
+ * WCAG 2.1 relative luminance with sRGB linearization.
45
+ *
46
+ * @see https://www.w3.org/WAI/GL/wiki/Relative_luminance
47
+ */
34
48
  static luminance(hex) {
35
49
  const rgb = this.parseHex(hex);
36
50
  const linearR = __classPrivateFieldGet(this, _a, "m", _Color_linearize).call(this, rgb[0]);
@@ -38,8 +52,12 @@ export class Color {
38
52
  const linearB = __classPrivateFieldGet(this, _a, "m", _Color_linearize).call(this, rgb[2]);
39
53
  return 0.2126 * linearR + 0.7152 * linearG + 0.0722 * linearB;
40
54
  }
41
- // Returns a new array sorted by descending contrast against the reference color.
42
- // https://www.w3.org/WAI/GL/wiki/Contrast_ratio
55
+ /**
56
+ * Returns a new array sorted by descending contrast against the reference
57
+ * color.
58
+ *
59
+ * @see https://www.w3.org/WAI/GL/wiki/Contrast_ratio
60
+ */
43
61
  static sortByContrast(candidates, refColor) {
44
62
  const refLum = this.luminance(refColor);
45
63
  const withRatio = candidates.map((c) => {
@@ -50,8 +68,10 @@ export class Color {
50
68
  withRatio.sort((a, b) => b.ratio - a.ratio);
51
69
  return withRatio.map((e) => e.color);
52
70
  }
53
- // Returns a new array with excluded colors removed.
54
- // Returns the original candidates if filtering would empty the list.
71
+ /**
72
+ * Returns a new array with excluded colors removed. Falls back to the
73
+ * original candidates when filtering would empty the list.
74
+ */
55
75
  static filterNotEqualTo(candidates, excluded) {
56
76
  const normalized = new Set(excluded.map((c) => this.toRgbHex(c)));
57
77
  const filtered = candidates.filter((c) => !normalized.has(this.toRgbHex(c)));
@@ -1,3 +1,13 @@
1
+ /**
2
+ * Derives display initials from a seed string.
3
+ *
4
+ * @see https://www.regular-expressions.info/unicode.html
5
+ */
1
6
  export declare class Initials {
7
+ /**
8
+ * Returns one or two uppercase initials for the given seed. By default
9
+ * strips `@...` so email addresses yield a single initial instead of being
10
+ * treated as two words.
11
+ */
2
12
  static fromSeed(seed: string, discardAtSymbol?: boolean): string;
3
13
  }