@dicebear/core 10.4.0 → 10.6.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/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  <h1><img src="https://www.dicebear.com/logo-readme.svg" width="28" /> DiceBear Core (JavaScript)</h1>
2
2
 
3
+ [![stars](https://www.dicebear.com/badges/stars.svg)](https://github.com/dicebear/dicebear/stargazers)
4
+ [![license](https://www.dicebear.com/badges/license.svg)](https://github.com/dicebear/dicebear/blob/10.x/LICENSE)
5
+
3
6
  JavaScript implementation of the DiceBear avatar library. Generates
4
7
  deterministic SVG avatars from style definitions and a seed string.
5
8
 
@@ -1,6 +1,10 @@
1
1
  export interface ValidationErrorDetail {
2
2
  readonly message?: string;
3
3
  readonly instancePath?: string;
4
+ /** JSON pointer to the schema rule that failed, e.g. `#/properties/size/minimum`. */
5
+ readonly schemaPath?: string;
6
+ /** The failing schema keyword, e.g. `minimum` or `pattern`. */
7
+ readonly keyword?: string;
4
8
  }
5
9
  /**
6
10
  * Base class for schema validation errors. Carries the prefix in `message`
package/lib/Options.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Range } from './StyleDefinition.js';
2
- import type { StyleOptions, StyleOptionsFlipValue, StyleOptionsColorFillValue, TagFilterToken } from './StyleOptions.js';
2
+ import type { StyleOptions, StyleOptionsFlipValue, StyleOptionsColorFillValue, StyleOptionsColorOrderValue, TagFilterToken } from './StyleOptions.js';
3
3
  /**
4
4
  * Validates the raw user-supplied options and exposes them through typed
5
5
  * accessors. Each accessor returns the user's input in a normalized form
@@ -50,4 +50,5 @@ export declare class Options<D = unknown> {
50
50
  colorFill(name: string): readonly StyleOptionsColorFillValue[];
51
51
  colorAngle(name: string): Range | undefined;
52
52
  colorFillStops(name: string): Range | undefined;
53
+ colorOrder(name: string): StyleOptionsColorOrderValue | undefined;
53
54
  }
package/lib/Options.js CHANGED
@@ -124,6 +124,9 @@ export class Options {
124
124
  colorFillStops(name) {
125
125
  return __classPrivateFieldGet(this, _Options_instances, "m", _Options_toRange).call(this, __classPrivateFieldGet(this, _Options_instances, "m", _Options_dynamic).call(this, `${name}ColorFillStops`));
126
126
  }
127
+ colorOrder(name) {
128
+ return __classPrivateFieldGet(this, _Options_instances, "m", _Options_dynamic).call(this, `${name}ColorOrder`);
129
+ }
127
130
  }
128
131
  _Options_data = new WeakMap(), _Options_tags = new WeakMap(), _Options_instances = new WeakSet(), _Options_dynamic = function _Options_dynamic(key) {
129
132
  return __classPrivateFieldGet(this, _Options_data, "f")[key];
@@ -23,6 +23,7 @@ interface ColorField {
23
23
  readonly type: 'color';
24
24
  readonly list?: true;
25
25
  readonly contrastTo?: string;
26
+ readonly notEqualTo?: readonly string[];
26
27
  }
27
28
  interface RangeField {
28
29
  readonly type: 'range';
@@ -10,6 +10,7 @@ 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 _OptionsDescriptor_instances, _a, _OptionsDescriptor_rotateRange, _OptionsDescriptor_translateRange, _OptionsDescriptor_descriptor, _OptionsDescriptor_style, _OptionsDescriptor_build;
13
+ import { COLOR_ORDER_FIXED, COLOR_ORDER_RANDOM } from './StyleOptions.js';
13
14
  /**
14
15
  * Builds a descriptor of every option a given style accepts. Tooling such as
15
16
  * the editor uses the result to render form controls and validation hints
@@ -69,11 +70,16 @@ _a = OptionsDescriptor, _OptionsDescriptor_descriptor = new WeakMap(), _OptionsD
69
70
  }
70
71
  }
71
72
  for (const name of [...__classPrivateFieldGet(this, _OptionsDescriptor_style, "f").colors().keys(), 'background']) {
72
- const contrastTo = __classPrivateFieldGet(this, _OptionsDescriptor_style, "f").colors().get(name)?.contrastTo();
73
+ const color = __classPrivateFieldGet(this, _OptionsDescriptor_style, "f").colors().get(name);
74
+ const contrastTo = color?.contrastTo();
75
+ const notEqualTo = color?.notEqualTo() ?? [];
73
76
  result[`${name}Color`] = {
74
77
  type: 'color',
75
78
  list: true,
76
79
  ...(contrastTo ? { contrastTo } : {}),
80
+ ...(notEqualTo.length > 0
81
+ ? { notEqualTo: Array.from(notEqualTo) }
82
+ : {}),
77
83
  };
78
84
  result[`${name}ColorFill`] = {
79
85
  type: 'enum',
@@ -82,6 +88,10 @@ _a = OptionsDescriptor, _OptionsDescriptor_descriptor = new WeakMap(), _OptionsD
82
88
  };
83
89
  result[`${name}ColorFillStops`] = { type: 'range', min: 2 };
84
90
  result[`${name}ColorAngle`] = __classPrivateFieldGet(_a, _a, "f", _OptionsDescriptor_rotateRange);
91
+ result[`${name}ColorOrder`] = {
92
+ type: 'enum',
93
+ values: [COLOR_ORDER_RANDOM, COLOR_ORDER_FIXED],
94
+ };
85
95
  }
86
96
  // Only advertise the `tags` filter when the style actually carries tags.
87
97
  // The values are the sorted union of every tag across the style's variants,
package/lib/Resolver.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Options } from './Options.js';
2
2
  import type { Style } from './Style.js';
3
- import type { StyleOptionsFlipValue, StyleOptionsColorFillValue, StyleOptions } from './StyleOptions.js';
3
+ import { type StyleOptionsFlipValue, type StyleOptionsColorFillValue, type StyleOptionsColorOrderValue, type StyleOptions } from './StyleOptions.js';
4
4
  /**
5
5
  * Bundles the three inputs needed to derive any deterministic value for an
6
6
  * avatar — the {@link Style}, the validated user {@link Options}, and a
@@ -35,6 +35,7 @@ export declare class Resolver<D = unknown> {
35
35
  color(name: string): readonly string[];
36
36
  colorFill(name: string): StyleOptionsColorFillValue;
37
37
  colorAngle(name: string): number;
38
+ colorOrder(name: string): StyleOptionsColorOrderValue;
38
39
  /**
39
40
  * Picks the rotate/translateX/translateY/scale values for a single
40
41
  * component. Memoized per `name`, so the four values land in
package/lib/Resolver.js CHANGED
@@ -9,10 +9,11 @@ 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 _Resolver_instances, _Resolver_style, _Resolver_options, _Resolver_prng, _Resolver_colorResolving, _Resolver_result, _Resolver_tagFilterCache, _Resolver_probability, _Resolver_isVisible, _Resolver_variantWeights, _Resolver_tagFilter, _Resolver_tagFilteredNames, _Resolver_resolveColor, _Resolver_colorFillStops, _Resolver_memoFloat, _Resolver_memo;
12
+ var _Resolver_instances, _Resolver_style, _Resolver_options, _Resolver_prng, _Resolver_colorResolving, _Resolver_result, _Resolver_tagFilterCache, _Resolver_probability, _Resolver_isVisible, _Resolver_variantWeights, _Resolver_tagFilter, _Resolver_tagFilteredNames, _Resolver_resolveColor, _Resolver_order, _Resolver_colorFillStops, _Resolver_memoFloat, _Resolver_memo;
13
13
  import { Prng } from './Prng.js';
14
14
  import { Color } from './Utils/Color.js';
15
15
  import { CircularColorReferenceError } from './Error/CircularColorReferenceError.js';
16
+ import { COLOR_ORDER_FIXED, COLOR_ORDER_RANDOM, } from './StyleOptions.js';
16
17
  /**
17
18
  * Bundles the three inputs needed to derive any deterministic value for an
18
19
  * avatar — the {@link Style}, the validated user {@link Options}, and a
@@ -99,6 +100,11 @@ export class Resolver {
99
100
  colorAngle(name) {
100
101
  return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memoFloat).call(this, `${name}ColorAngle`, __classPrivateFieldGet(this, _Resolver_options, "f").colorAngle(name), 0);
101
102
  }
103
+ colorOrder(name) {
104
+ // Deliberately not memoized: unlike colorFill this is no PRNG pick, so it
105
+ // stays out of the {@link resolved} snapshot.
106
+ return __classPrivateFieldGet(this, _Resolver_options, "f").colorOrder(name) ?? COLOR_ORDER_RANDOM;
107
+ }
102
108
  /**
103
109
  * Picks the rotate/translateX/translateY/scale values for a single
104
110
  * component. Memoized per `name`, so the four values land in
@@ -216,10 +222,14 @@ _Resolver_style = new WeakMap(), _Resolver_options = new WeakMap(), _Resolver_pr
216
222
  const styleColor = __classPrivateFieldGet(this, _Resolver_style, "f").colors().get(name);
217
223
  const source = userColors ?? styleColor?.values() ?? [];
218
224
  let candidates = source.map((c) => Color.toHex(c));
225
+ const fixed = this.colorOrder(name) === COLOR_ORDER_FIXED;
226
+ const verbatim = userColors !== undefined && fixed;
219
227
  const fill = this.colorFill(name);
220
- const stops = fill === 'solid' ? 1 : __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_colorFillStops).call(this, name);
228
+ const stops = fill === 'solid'
229
+ ? 1
230
+ : __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_colorFillStops).call(this, name, verbatim ? candidates.length : 2);
221
231
  if (!styleColor) {
222
- return __classPrivateFieldGet(this, _Resolver_prng, "f").shuffle(`${name}Color`, candidates).slice(0, stops);
232
+ return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_order).call(this, name, candidates, fixed, verbatim).slice(0, stops);
223
233
  }
224
234
  // Detect circular references (e.g. a.contrastTo = b, b.contrastTo = a)
225
235
  if (__classPrivateFieldGet(this, _Resolver_colorResolving, "f").includes(name)) {
@@ -229,7 +239,7 @@ _Resolver_style = new WeakMap(), _Resolver_options = new WeakMap(), _Resolver_pr
229
239
  const contrastTo = styleColor.contrastTo();
230
240
  const notEqualTo = styleColor.notEqualTo();
231
241
  try {
232
- if (contrastTo) {
242
+ if (contrastTo && !verbatim) {
233
243
  const refColor = this.color(contrastTo)[0];
234
244
  if (refColor) {
235
245
  candidates = Color.sortByContrast(candidates, refColor);
@@ -251,11 +261,24 @@ _Resolver_style = new WeakMap(), _Resolver_options = new WeakMap(), _Resolver_pr
251
261
  // Skip shuffle when sorted by contrast to preserve the ordering
252
262
  const ordered = contrastTo
253
263
  ? candidates
254
- : __classPrivateFieldGet(this, _Resolver_prng, "f").shuffle(`${name}Color`, candidates);
264
+ : __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_order).call(this, name, candidates, fixed, verbatim);
255
265
  return ordered.slice(0, stops);
256
- }, _Resolver_colorFillStops = function _Resolver_colorFillStops(name) {
266
+ }, _Resolver_order = function _Resolver_order(name, candidates, fixed, verbatim) {
267
+ if (!fixed) {
268
+ return __classPrivateFieldGet(this, _Resolver_prng, "f").shuffle(`${name}Color`, candidates);
269
+ }
270
+ if (verbatim) {
271
+ return candidates;
272
+ }
273
+ // Deprecated: DiceBear 11 will take the palette in its definition order
274
+ // here, the same verbatim rule as user-supplied colors, and drop this
275
+ // sort (see CHANGELOG.md, "Deprecated").
276
+ return Array.from(new Set(candidates)).sort();
277
+ }, _Resolver_colorFillStops = function _Resolver_colorFillStops(name, fallback) {
257
278
  const range = __classPrivateFieldGet(this, _Resolver_options, "f").colorFillStops(name);
258
- return range ? __classPrivateFieldGet(this, _Resolver_prng, "f").integer(`${name}ColorFillStops`, range) : 2;
279
+ return range
280
+ ? __classPrivateFieldGet(this, _Resolver_prng, "f").integer(`${name}ColorFillStops`, range)
281
+ : fallback;
259
282
  }, _Resolver_memoFloat = function _Resolver_memoFloat(key, range, fallback) {
260
283
  return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memo).call(this, key, () => range ? __classPrivateFieldGet(this, _Resolver_prng, "f").float(key, range) : fallback);
261
284
  }, _Resolver_memo = function _Resolver_memo(key, compute) {
@@ -1,5 +1,8 @@
1
1
  export type StyleOptionsFlipValue = 'none' | 'horizontal' | 'vertical' | 'both';
2
2
  export type StyleOptionsColorFillValue = 'solid' | 'linear' | 'radial';
3
+ export type StyleOptionsColorOrderValue = 'random' | 'fixed';
4
+ export declare const COLOR_ORDER_RANDOM: StyleOptionsColorOrderValue;
5
+ export declare const COLOR_ORDER_FIXED: StyleOptionsColorOrderValue;
3
6
  /**
4
7
  * A parsed `tags` filter token. {@link Options.tags} decodes each raw
5
8
  * `category` / `category:value` / `!…` string into this shape so the resolver
@@ -60,6 +63,8 @@ type ColorOptions<C extends string> = [C] extends [never] ? unknown : {
60
63
  readonly [K in C as `${K}ColorFillStops`]?: number | readonly [number, number];
61
64
  } & {
62
65
  readonly [K in C as `${K}ColorAngle`]?: number | readonly [number, number];
66
+ } & {
67
+ readonly [K in C as `${K}ColorOrder`]?: StyleOptionsColorOrderValue;
63
68
  };
64
69
  export type StyleOptions<D = unknown> = StyleOptionsBase & ComponentOptions<D, ComponentNames<D>> & ColorOptions<AllColorNames<D>> & (HasSpecificKeys<D> extends true ? unknown : {
65
70
  readonly [key: string]: unknown;
@@ -1 +1,2 @@
1
- export {};
1
+ export const COLOR_ORDER_RANDOM = 'random';
2
+ export const COLOR_ORDER_FIXED = 'fixed';