Skip to content
API reference

avatar

Import
import * as api from "@fungi.computer/sprite/avatar";
Kind Export Description
type alias AvatarCharacter One character in the closed v1 avatar catalog.
type alias AgentColors Whether an Agent created from a member’s avatar wears a shifted identity.
type alias Avatar A parsed, envelope-valid Avatar v1 descriptor.
type alias AvatarCatalogEntry Pure per-character facts a catalog owner (e.g. sprite-zdog) supplies.
type alias AvatarCatalog Pure character facts for every avatar character, owned by its renderer.
type alias Hsl A hexadecimal colour decomposed into hue (degrees), saturation, lightness.
type alias AgentAppearance The mushroom’s two identity controls for one seed: its cap and cap spots take a hue from the seed, so an Agent wears the same cap in every list it appears in. Kept byte-identical to the Sprite seen before avatars existed.
constant avatarCharacters The closed v1 catalog of avatar characters. Adding a character is a new major version; this list is not meant to grow silently.
constant agentColorsSchema A saved agentColors preference: shift an Agent’s identity, or copy it.
constant avatarEnvelopeSchema The Avatar v1 envelope: a closed character, up to 16 appearance values, a resting expression, and a render style. It is a complete snapshot, not a reference into character data, so it survives a later change to the catalog. This checks shape only; it does not know a character’s real controls or expressions, so a bound catalog cannot validate against it by name alone. createAvatarSchema/parseAvatar build the catalog-refined schema a caller should actually parse untrusted data with; sprite-zdog’s avatarSchema is that schema, bound to the built-in catalog.
constant AGENT_HUE_MIN_SHIFT Lower bound (degrees) every colour-shift step must clear, each direction.
function createAvatarSchema() Build the full avatar schema for a catalog: the envelope above, refined so every appearance key is one of the character’s controls, every value is typed by that control, and the expression is one the character declares.
function parseAvatar() Parse an unknown value into a catalog-valid Avatar v1 descriptor.
function hexFromHsl() Encode HSL (hue in degrees, saturation/lightness in percent) as #rrggbb.
function hslFromHex() Decode a #rrggbb colour into HSL (hue in degrees, saturation/lightness in percent).
function hueDistance() Shortest angular distance between two hues in degrees, 0 to 180.
function seedHash() A deterministic FNV-1a hash: the same seed always lands on the same value. A copy of @fungi.computer/caps’s seedHash so this package stays free of a React-package dependency; both must keep computing the same value.
function agentHueStep() The hue-rotation step for seed, chosen against hex’s own realised result, not the idealised hue + step arithmetic: shiftHue rounds to a #rrggbb colour, and re-decoding that colour can land a few tenths of a degree short of an exact-degree step, which the 40-degree guarantee must still hold against. A step is only a candidate when rotating hex by it and reading the result’s hue back out clears 40 degrees from hex‘s own hue. When avoid carries other agents’ identity hues, the candidate farthest from all of them (by minimum distance, scored on the same realised hue) is preferred; ties keep whichever candidate is nearest the seed’s own deterministic default step, so an unconflicted seed keeps its usual look.
function shiftHue() Rotate one hex colour’s hue by step degrees, keeping its saturation and lightness.
function agentAppearance() The seeded mushroom identity colours for seed.
function seededAvatar() The avatar an Agent wears until its owner chooses one: the mushroom, in the cap colour seeded from the Agent’s id, at rest, undecorated. It is a pure function of identity, so hosts compute it instead of storing it.
function deriveAgentAvatar() Derive an Agent’s avatar from its creator’s, for a catalog’s character. "keep" hands back the member’s look unchanged (their Agent is a visible copy, told apart only by name). "shift" resolves the member’s full appearance against the catalog and rotates only the character’s identity slots by one shared hue step, so accessories, face and drawing style carry over untouched. avoid lists hues already worn by the creator’s other Agents so a new one does not land beside them. An achromatic primary identity colour has no hue to rotate, so its resolved appearance is kept.

AvatarCharactertype alias

Section titled “AvatarCharacter”
Signature
type AvatarCharacter = (typeof avatarCharacters)[number];

One character in the closed v1 avatar catalog.


AgentColorstype alias

Section titled “AgentColors”
Signature
type AgentColors = z.infer<typeof agentColorsSchema>;

Whether an Agent created from a member’s avatar wears a shifted identity.


Signature
type Avatar = z.infer<typeof avatarEnvelopeSchema>;

A parsed, envelope-valid Avatar v1 descriptor.


AvatarCatalogEntrytype alias

Section titled “AvatarCatalogEntry”
Signature
type AvatarCatalogEntry = object;

Pure per-character facts a catalog owner (e.g. sprite-zdog) supplies.

Signature
readonly controls: Readonly<Record<string, Control>>;

Every control the character’s document declares.

Signature
readonly expressions: readonly string[];

Every expression name the character’s document declares.

Signature
readonly identitySlots: readonly string[];

Controls that carry the character’s identity colour, in priority order.


AvatarCatalogtype alias

Section titled “AvatarCatalog”
Signature
type AvatarCatalog = Readonly<Record<AvatarCharacter, AvatarCatalogEntry>>;

Pure character facts for every avatar character, owned by its renderer.


Signature
type Hsl = object;

A hexadecimal colour decomposed into hue (degrees), saturation, lightness.

Signature
readonly h: number;
Signature
readonly s: number;
Signature
readonly l: number;

AgentAppearancetype alias

Section titled “AgentAppearance”
Signature
type AgentAppearance = Readonly<{
cap: string;
capSpot: string;
}>;

The mushroom’s two identity controls for one seed: its cap and cap spots take a hue from the seed, so an Agent wears the same cap in every list it appears in. Kept byte-identical to the Sprite seen before avatars existed.

avatarCharactersconstant

Section titled “avatarCharacters”
Signature
const avatarCharacters: readonly ["mushroom", "cat", "burger"];

The closed v1 catalog of avatar characters. Adding a character is a new major version; this list is not meant to grow silently.


agentColorsSchemaconstant

Section titled “agentColorsSchema”
Signature
const agentColorsSchema: ZodEnum<{
shift: "shift";
keep: "keep";
}>;

A saved agentColors preference: shift an Agent’s identity, or copy it.


avatarEnvelopeSchemaconstant

Section titled “avatarEnvelopeSchema”
Signature
const avatarEnvelopeSchema: ZodObject<
{
v: ZodLiteral<1>;
character: ZodEnum<{
mushroom: "mushroom";
cat: "cat";
burger: "burger";
}>;
appearance: ZodRecord<
ZodString,
ZodUnion<readonly [ZodString, ZodNumber, ZodBoolean]>
>;
expression: ZodString;
renderStyle: ZodEnum<{
plain: "plain";
pencil: "pencil";
manga: "manga";
riso: "riso";
halftone: "halftone";
blueprint: "blueprint";
crt: "crt";
watercolor: "watercolor";
}>;
},
$strict
>;

The Avatar v1 envelope: a closed character, up to 16 appearance values, a resting expression, and a render style. It is a complete snapshot, not a reference into character data, so it survives a later change to the catalog. This checks shape only; it does not know a character’s real controls or expressions, so a bound catalog cannot validate against it by name alone. createAvatarSchema/parseAvatar build the catalog-refined schema a caller should actually parse untrusted data with; sprite-zdog’s avatarSchema is that schema, bound to the built-in catalog.


AGENT_HUE_MIN_SHIFTconstant

Section titled “AGENT_HUE_MIN_SHIFT”
Signature
const AGENT_HUE_MIN_SHIFT: 40 = 40;

Lower bound (degrees) every colour-shift step must clear, each direction.

createAvatarSchema()function

Section titled “createAvatarSchema()”
Signature
function createAvatarSchema(catalog): ZodObject<
{
v: ZodLiteral<1>;
character: ZodEnum<{
mushroom: "mushroom";
cat: "cat";
burger: "burger";
}>;
appearance: ZodRecord<
ZodString,
ZodUnion<readonly [ZodString, ZodNumber, ZodBoolean]>
>;
expression: ZodString;
renderStyle: ZodEnum<{
plain: "plain";
pencil: "pencil";
manga: "manga";
riso: "riso";
halftone: "halftone";
blueprint: "blueprint";
crt: "crt";
watercolor: "watercolor";
}>;
},
$strict
>;

Build the full avatar schema for a catalog: the envelope above, refined so every appearance key is one of the character’s controls, every value is typed by that control, and the expression is one the character declares.

Parameter Type
catalog AvatarCatalog

ZodObject<{ v: ZodLiteral<1>; character: ZodEnum<{ mushroom: "mushroom"; cat: "cat"; burger: "burger"; }>; appearance: ZodRecord<ZodString, ZodUnion<readonly [ZodString, ZodNumber, ZodBoolean]>>; expression: ZodString; renderStyle: ZodEnum<{ plain: "plain"; pencil: "pencil"; manga: "manga"; riso: "riso"; halftone: "halftone"; blueprint: "blueprint"; crt: "crt"; watercolor: "watercolor"; }>; }, $strict>


parseAvatar()function

Section titled “parseAvatar()”
Signature
function parseAvatar(data, catalog): object;

Parse an unknown value into a catalog-valid Avatar v1 descriptor.

Parameter Type
data unknown
catalog AvatarCatalog

object

Name Type Default value
v 1 -
character "mushroom" | "cat" | "burger" -
appearance Record<string, string | number | boolean> -
expression string -
renderStyle | "plain" | "pencil" | "manga" | "riso" | "halftone" | "blueprint" | "crt" | "watercolor" renderStyleSchema

hexFromHsl()function

Section titled “hexFromHsl()”
Signature
function hexFromHsl(hue, saturation, lightness): string;

Encode HSL (hue in degrees, saturation/lightness in percent) as #rrggbb.

Parameter Type
hue number
saturation number
lightness number

string


hslFromHex()function

Section titled “hslFromHex()”
Signature
function hslFromHex(hex): Hsl;

Decode a #rrggbb colour into HSL (hue in degrees, saturation/lightness in percent).

Parameter Type
hex unknown

Hsl


hueDistance()function

Section titled “hueDistance()”
Signature
function hueDistance(a, b): number;

Shortest angular distance between two hues in degrees, 0 to 180.

Parameter Type
a number
b number

number


Signature
function seedHash(seed): number;

A deterministic FNV-1a hash: the same seed always lands on the same value. A copy of @fungi.computer/caps’s seedHash so this package stays free of a React-package dependency; both must keep computing the same value.

Parameter Type
seed string

number


agentHueStep()function

Section titled “agentHueStep()”
Signature
function agentHueStep(hex, seed, avoid?): number;

The hue-rotation step for seed, chosen against hex’s own realised result, not the idealised hue + step arithmetic: shiftHue rounds to a #rrggbb colour, and re-decoding that colour can land a few tenths of a degree short of an exact-degree step, which the 40-degree guarantee must still hold against. A step is only a candidate when rotating hex by it and reading the result’s hue back out clears 40 degrees from hex‘s own hue. When avoid carries other agents’ identity hues, the candidate farthest from all of them (by minimum distance, scored on the same realised hue) is preferred; ties keep whichever candidate is nearest the seed’s own deterministic default step, so an unconflicted seed keeps its usual look.

Parameter Type Default value
hex string undefined
seed string undefined
avoid readonly number[] []

number


Signature
function shiftHue(hex, step): string;

Rotate one hex colour’s hue by step degrees, keeping its saturation and lightness.

Parameter Type
hex string
step number

string


agentAppearance()function

Section titled “agentAppearance()”
Signature
function agentAppearance(seed): AgentAppearance;

The seeded mushroom identity colours for seed.

Parameter Type
seed string

AgentAppearance


seededAvatar()function

Section titled “seededAvatar()”
Signature
function seededAvatar(seed): object;

The avatar an Agent wears until its owner chooses one: the mushroom, in the cap colour seeded from the Agent’s id, at rest, undecorated. It is a pure function of identity, so hosts compute it instead of storing it.

Parameter Type
seed string

object

Name Type Default value
v 1 -
character "mushroom" | "cat" | "burger" -
appearance Record<string, string | number | boolean> -
expression string -
renderStyle | "plain" | "pencil" | "manga" | "riso" | "halftone" | "blueprint" | "crt" | "watercolor" renderStyleSchema

deriveAgentAvatar()function

Section titled “deriveAgentAvatar()”
Signature
function deriveAgentAvatar(member, agentColors, seed, catalog, avoid?): object;

Derive an Agent’s avatar from its creator’s, for a catalog’s character. "keep" hands back the member’s look unchanged (their Agent is a visible copy, told apart only by name). "shift" resolves the member’s full appearance against the catalog and rotates only the character’s identity slots by one shared hue step, so accessories, face and drawing style carry over untouched. avoid lists hues already worn by the creator’s other Agents so a new one does not land beside them. An achromatic primary identity colour has no hue to rotate, so its resolved appearance is kept.

Parameter Type Default value
member { v: 1; character: "mushroom" | "cat" | "burger"; appearance: Record<string, string | number | boolean>; expression: string; renderStyle: | "plain" | "pencil" | "manga" | "riso" | "halftone" | "blueprint" | "crt" | "watercolor"; } undefined
member.v 1 ...
member.character "mushroom" | "cat" | "burger" ...
member.appearance Record<string, string | number | boolean> ...
member.expression string ...
member.renderStyle | "plain" | "pencil" | "manga" | "riso" | "halftone" | "blueprint" | "crt" | "watercolor" renderStyleSchema
agentColors "shift" | "keep" undefined
seed string undefined
catalog AvatarCatalog undefined
avoid readonly number[] []

object

Name Type Default value
v 1 -
character "mushroom" | "cat" | "burger" -
appearance Record<string, string | number | boolean> -
expression string -
renderStyle | "plain" | "pencil" | "manga" | "riso" | "halftone" | "blueprint" | "crt" | "watercolor" renderStyleSchema