Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- `@botharness/pixel-avatar` 0.3.0: Avatar Species and split side hair as asset version 2. `withSpecies(recipe, species)` adds a `species` (`human` or `goblin`), a separate `rightSideHair` and optional `leftSideHairColor`/`rightSideHairColor`, moving a suggested skin color to the new species' `AVATAR_SPECIES_SWATCHES`. The goblin has pointed ears drawn over hair and small tusks, and keeps every head turn and speech mouth layer. Version 1 recipes, seeded faces and the existing golden output are unchanged, and a human version 2 recipe renders exactly like its version 1 source. Older consumers reject version 2 recipes through `isPixelAvatarRecipe`, so they can fall back to a saved snapshot.
- `@botharness/pixel-banner` 0.1.0 (new): seeded 3:1 pixel nature banners for profile headers in ten scenes. A `{ scene, seed }` recipe renders 150×50 pixels and scales them ×10 to exactly 1500×500 RGBA with no smoothing; `seededBannerRecipe(name)` gives the same banner for the same name, and a golden test locks every scene's output.
- `@botharness/pixel-avatar` 0.2.0: opt-in `mouthLayers` adds saved/closed/half-open/open speech features to every head turn. Existing recipes, static snapshots and default golden output remain unchanged; text pacing stays with the consumer.
- `@botharness/pixel-morph`: one morph that throws no longer stops the shared animation loop for the others, and a morph started from inside a frame no longer starts a second frame chain.
Expand Down
1 change: 1 addition & 0 deletions packages/avatar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ const svg = pixelAvatarSvg(recipe); // 32×32 viewBox, crisp edges, no ids or sc
- `AVATAR_PARTS`, `AVATAR_SWATCHES`, `AVATAR_PRESETS`, `DEFAULT_RECIPE`: everything an editor needs.
- `isPixelAvatarRecipe`, `canonicalRecipe`: validate and normalise saved recipes.
- `AVATAR_HAIR_PARTS`, `AVATAR_RANGES`, `detailedRecipe(recipe)`: optional finer controls. A recipe may carry `bangs`, `sideHair`, `backHair`, `spacing`, `height` and `hairLength`, all six or none; `detailedRecipe` derives them from the plain `hair` so an editor can start from what is on screen. Recipes without them render exactly as before.
- `AVATAR_SPECIES`, `AVATAR_SPECIES_SWATCHES`, `AVATAR_PIECE_COLORS`, `withSpecies(recipe, species)`: Avatar Species (`human`, `goblin`) on the same rig, as asset version 2. `withSpecies` keeps every choice, splits side hair into `sideHair` (left) and `rightSideHair`, and accepts optional `leftSideHairColor`/`rightSideHairColor`, where left and right are as seen on screen. Consumers built before version 2 reject these recipes with `isPixelAvatarRecipe` and can show a saved snapshot instead.
- `faceCells`, `pixelSymbolCells`, `symbolArtCells`: pixels for [`@botharness/pixel-morph`](../morph).
- `pixelAvatarSvg(recipe, { turns, classPrefix })`: optional pre-rendered head turns, and rig layer classes (`<prefix>-body`, `-head`, `-face`, `-gaze`, `-blink`) for CSS animation.
2 changes: 1 addition & 1 deletion packages/avatar/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@botharness/pixel-avatar",
"version": "0.2.0",
"version": "0.3.0",
"description": "Seeded, editable 32×32 chibi pixel avatars with state symbols that morph in and out",
"keywords": [
"agent-ui",
Expand Down
47 changes: 41 additions & 6 deletions packages/avatar/src/figure.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { escapeAttribute, type PixelCell } from '@botharness/pixel-morph';
import type { PixelAvatarRecipe } from './recipe.js';
import { AVATAR_PIECE_COLORS, type PixelAvatarRecipe } from './recipe.js';

type Cell = string | undefined;
type Grid = Cell[][];
Expand All @@ -15,6 +15,7 @@ const MOUTH = '#7a2a38';
const MOUTH_INSIDE = '#b8415a';
const TONGUE = '#ef6f84';
const GOLD = '#efb93f';
const TUSK = '#f6f0d8';

export type PixelGrid = Grid;
export type PixelMouthState = 'saved' | 'closed' | 'half-open' | 'open';
Expand Down Expand Up @@ -353,24 +354,31 @@ function detailedHair(
recipe: Recipe,
s: number,
b: number,
): { back: Mask; front: Mask; ties?: Point[]; bands?: boolean } {
): { back: Mask; front: Mask; ties?: Point[]; bands?: boolean; sides?: [Mask, Mask] } {
const of = (style: Recipe['hair'] | undefined) =>
hair({ ...recipe, hair: style ?? 'none' }, s, b);
const bangs = of(recipe.bangs);
const side = of(recipe.sideHair);
const right = recipe.rightSideHair === undefined ? side : of(recipe.rightSideHair);
const back = of(recipe.backHair);
const length = (recipe.hairLength ?? 0) * 2;
const lengthen =
(mask: Mask): Mask =>
(x, y) =>
mask(x, y > 18 ? Math.round(y - length) : y);
// Side pieces cover |dx| >= 7 below the crown; with a right piece, each side has its own.
const sideOf =
(sign: -1 | 1): Mask =>
(x, y) =>
y > 7 && sign * (x + 0.5 - C - s) >= 7 && lengthen((sign < 0 ? side : right).front)(x, y);
return {
front: (x, y) =>
y <= 7
? bangs.front(x, y) || back.front(x, y)
: Math.abs(x + 0.5 - C - s) < 7
? bangs.front(x, y)
: lengthen(side.front)(x, y),
: lengthen((x + 0.5 - C - s < 0 ? side : right).front)(x, y),
sides: [sideOf(-1), sideOf(1)],
back: length === 0 ? back.back : lengthen(back.back),
...(back.ties ? { ties: back.ties } : {}),
...(back.bands ? { bands: back.bands } : {}),
Expand Down Expand Up @@ -708,7 +716,8 @@ export function pixelFigure(
return front(x - Math.round(d * (1 + Math.max(0, y - 20) * 0.4) - d * tuck), y);
}
: front;
const masks = recipe.bangs === undefined ? hair(recipe, s, b) : detailedHair(recipe, s, b);
const masks: ReturnType<typeof detailedHair> =
recipe.bangs === undefined ? hair(recipe, s, b) : detailedHair(recipe, s, b);

const body = blank();
outfit(recipe, body, s);
Expand All @@ -729,15 +738,23 @@ export function pixelFigure(
const xs = [...Array(N).keys()].filter((x) => face(x, earY));
return side < 0 ? xs[0]! - 1 : xs.at(-1)! + 1;
};
if (!turned)
const goblin = recipe.species === 'goblin';
if (goblin) {
// Long pointed ears drawn over hair: the outer tip rises above the ear line.
const ear = ['E...', 'EE..', '.EiE', '..Ee'];
const pal = { E: skin, e: shade(skin, 0.86), i: shade(skin, 0.74) };
paint(head, masks.front, hairColor);
for (const ex of turned ? [-d] : [-1, 1])
sprite(head, ear, ex < 0 ? edge(ex) - 3 : edge(ex), earY - 2, pal, ex > 0);
} else if (!turned)
for (const ex of [-1, 1])
sprite(head, ['E', 'E', 'e'], edge(ex), earY, { E: skin, e: shade(skin, 0.86) });
else
sprite(head, ['EE', 'Ee', 'Ee', 'E.'], d > 0 ? edge(-d) - 1 : edge(-d), earY, {
E: skin,
e: shade(skin, 0.8),
});
paint(head, masks.front, hairColor);
if (!goblin) paint(head, masks.front, hairColor);
for (const [x, y] of masks.ties ?? [])
sprite(back, ['T'], Math.round(x), y, { T: shade(recipe.shirtColor, 0.8) });
accessory(recipe, head, s, face);
Expand Down Expand Up @@ -862,6 +879,9 @@ export function pixelFigure(
T: TONGUE,
W: WHITE,
});
if (recipe.species === 'goblin')
for (const tx of [Math.round(mx - 2.5), Math.round(mx + 2.5)])
sprite(layer, ['T'], tx, 21 + h, { T: TUSK });
if (recipe.cheeks === 'blush') {
sprite(layer, ['PP'], left.x - 1, 19 + h, { P: mix(BLUSH, skin, 0.25) });
sprite(layer, ['PP'], right.x + right.w - 1, 19 + h, { P: mix(BLUSH, skin, 0.25) });
Expand Down Expand Up @@ -920,6 +940,21 @@ export function pixelFigure(
}

const full = clip(outline(figure));
AVATAR_PIECE_COLORS.forEach((key, i) => {
const color = recipe[key];
const mask = masks.sides?.[i];
if (color === undefined || !mask || color.toLowerCase() === hairColor.toLowerCase()) return;
const variant = blank();
const plain = { ...recipe, hairColor: color };
for (const k of AVATAR_PIECE_COLORS) delete plain[k];
for (const cell of pixelFigure(plain, yawDeg).cells) variant[cell.y]![cell.x] = cell.c;
const near = (x: number, y: number) =>
mask(x, y) || mask(x - 1, y) || mask(x + 1, y) || mask(x, y - 1) || mask(x, y + 1);
for (let y = 0; y < N; y++)
for (let x = 0; x < N; x++)
if (full[y]![x] && near(x, y) && !eyes[y]![x] && !glasses[y]![x])
full[y]![x] = variant[y]![x];
});
const bodyCells = blank();
const headCells = blank();
for (let y = 0; y < N; y++)
Expand Down
8 changes: 8 additions & 0 deletions packages/avatar/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@ export {
AVATAR_COLORS,
AVATAR_HAIR_PARTS,
AVATAR_PARTS,
AVATAR_PIECE_COLORS,
AVATAR_PRESETS,
AVATAR_RANGES,
AVATAR_SPECIES,
AVATAR_SPECIES_SWATCHES,
AVATAR_SWATCHES,
DEFAULT_RECIPE,
canonicalRecipe,
createSeededRecipe,
detailedRecipe,
isPixelAvatarRecipe,
seededRecipe,
withSpecies,
type AvatarColor,
type AvatarHairPart,
type AvatarPart,
type AvatarPieceColor,
type AvatarRange,
type AvatarSpecies,
type PixelAvatarRecipe,
type PixelAvatarRecipeV1,
type PixelAvatarRecipeV2,
} from './recipe.js';
export { seededRandom } from './random.js';
export { AVATAR_TURNS, faceCells, pixelAvatarSvg, type PixelAvatarSvgOptions } from './svg.js';
Expand Down
95 changes: 85 additions & 10 deletions packages/avatar/src/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,44 @@ export type AvatarPart = keyof typeof AVATAR_PARTS;
export const AVATAR_COLORS = ['skinColor', 'hairColor', 'eyeColor', 'shirtColor'] as const;
export type AvatarColor = (typeof AVATAR_COLORS)[number];

export type PixelAvatarRecipe = {
/**
* Avatar Species: a base on the pixel bust rig. Species share motion and anchors; each sets its
* own ears, face details and suggested body colors. A recipe with a species is asset version 2.
*/
export const AVATAR_SPECIES = ['human', 'goblin'] as const;
export type AvatarSpecies = (typeof AVATAR_SPECIES)[number];
/** Optional per-piece hair colors (asset version 2); an absent piece uses `hairColor`. */
export const AVATAR_PIECE_COLORS = ['leftSideHairColor', 'rightSideHairColor'] as const;
export type AvatarPieceColor = (typeof AVATAR_PIECE_COLORS)[number];

type BaseRecipe = {
schemaVersion: 1;
family: 'illustrated';
assetVersion: 1;
rigVersion: 1;
} & { [P in AvatarPart]: (typeof AVATAR_PARTS)[P][number] } & Record<AvatarColor, string> & {
[P in AvatarHairPart]?: (typeof AVATAR_PARTS)['hair'][number];
} & { [P in AvatarRange]?: number };
} & { [P in AvatarPart]: (typeof AVATAR_PARTS)[P][number] } & Record<AvatarColor, string>;

/** Asset version 1: optional split hair and geometry, all six or none. */
export type PixelAvatarRecipeV1 = BaseRecipe & { assetVersion: 1 } & {
[P in AvatarHairPart]?: (typeof AVATAR_PARTS)['hair'][number];
} & { [P in AvatarRange]?: number } & {
species?: never;
rightSideHair?: never;
} & { [P in AvatarPieceColor]?: never };

/**
* Asset version 2: a species, the full split hair and geometry, a separate right side hair
* (`sideHair` is then the left side) and optional per-side hair colors.
*/
export type PixelAvatarRecipeV2 = BaseRecipe & { assetVersion: 2 } & {
[P in AvatarHairPart]: (typeof AVATAR_PARTS)['hair'][number];
} & { [P in AvatarRange]: number } & {
species: AvatarSpecies;
rightSideHair: (typeof AVATAR_HAIR_PARTS)['sideHair'][number];
} & { [P in AvatarPieceColor]?: string };

export type PixelAvatarRecipe = PixelAvatarRecipeV1 | PixelAvatarRecipeV2;

export const DEFAULT_RECIPE: PixelAvatarRecipe = {
export const DEFAULT_RECIPE: PixelAvatarRecipeV1 = {
schemaVersion: 1,
family: 'illustrated',
assetVersion: 1,
Expand Down Expand Up @@ -189,7 +217,19 @@ export function isPixelAvatarRecipe(value: unknown): value is PixelAvatarRecipe
const r = value as Record<string, unknown>;
const detailed = DETAIL_KEYS.filter((key) => Object.hasOwn(r, key)).length;
if (detailed !== 0 && detailed !== DETAIL_KEYS.length) return false;
if (Object.keys(r).length !== Object.keys(DEFAULT_RECIPE).length + detailed) return false;
const v2 = r['assetVersion'] === 2;
const pieces = AVATAR_PIECE_COLORS.filter((key) => Object.hasOwn(r, key));
const v2Keys = v2 ? 2 + pieces.length : 0;
if (Object.keys(r).length !== Object.keys(DEFAULT_RECIPE).length + detailed + v2Keys)
return false;
if (
v2 &&
(detailed === 0 ||
!(AVATAR_SPECIES as readonly unknown[]).includes(r['species']) ||
!(AVATAR_HAIR_PARTS.sideHair as readonly unknown[]).includes(r['rightSideHair']) ||
!pieces.every((key) => typeof r[key] === 'string' && /^#[\da-f]{6}$/iu.test(r[key])))
)
return false;
return (
(detailed === 0 ||
((Object.keys(AVATAR_HAIR_PARTS) as AvatarHairPart[]).every(
Expand All @@ -204,7 +244,7 @@ export function isPixelAvatarRecipe(value: unknown): value is PixelAvatarRecipe
}))) &&
r['schemaVersion'] === 1 &&
r['family'] === 'illustrated' &&
r['assetVersion'] === 1 &&
(r['assetVersion'] === 1 || v2) &&
r['rigVersion'] === 1 &&
PART_KEYS.every(
(key) =>
Expand All @@ -218,15 +258,44 @@ export function canonicalRecipe(recipe: PixelAvatarRecipe): PixelAvatarRecipe {
const canonical: Record<string, unknown> = {
schemaVersion: 1,
family: 'illustrated',
assetVersion: 1,
assetVersion: recipe.assetVersion,
rigVersion: 1,
};
for (const key of PART_KEYS) canonical[key] = recipe[key];
for (const key of AVATAR_COLORS) canonical[key] = recipe[key].toLowerCase();
if (recipe.bangs !== undefined) for (const key of DETAIL_KEYS) canonical[key] = recipe[key];
if (recipe.assetVersion === 2) {
canonical['species'] = recipe.species;
canonical['rightSideHair'] = recipe.rightSideHair;
for (const key of AVATAR_PIECE_COLORS)
if (recipe[key] !== undefined) canonical[key] = recipe[key].toLowerCase();
}
return canonical as PixelAvatarRecipe;
}

/**
* Returns the recipe as asset version 2 with the given species, keeping every other choice.
* Side hair splits into left (`sideHair`) and right (`rightSideHair`) pieces that start equal.
* When the skin color is one of the previous species' suggested colors, it moves to the new
* species' first suggestion; a custom color is kept.
*/
export function withSpecies(
recipe: PixelAvatarRecipe,
species: AvatarSpecies,
): PixelAvatarRecipeV2 {
const detailed = detailedRecipe(recipe);
const previous = recipe.species ?? 'human';
const suggested = AVATAR_SPECIES_SWATCHES[previous].includes(recipe.skinColor.toLowerCase());
return {
...detailed,
assetVersion: 2,
species,
rightSideHair: detailed.rightSideHair ?? detailed.sideHair!,
skinColor:
suggested && previous !== species ? AVATAR_SPECIES_SWATCHES[species][0]! : recipe.skinColor,
} as PixelAvatarRecipeV2;
}

export function detailedRecipe(recipe: PixelAvatarRecipe): PixelAvatarRecipe {
if (recipe.bangs !== undefined) return recipe;
const pick = <P extends AvatarHairPart>(part: P) =>
Expand Down Expand Up @@ -341,7 +410,13 @@ export function createSeededRecipe(namespace: string): (seed: string) => PixelAv
export const seededRecipe: (seed: string) => PixelAvatarRecipe =
createSeededRecipe('botharness-avatar');

const preset = (parts: Partial<PixelAvatarRecipe>): PixelAvatarRecipe => ({
/** Suggested body colors per species; any color remains allowed. */
export const AVATAR_SPECIES_SWATCHES: Record<AvatarSpecies, readonly string[]> = {
human: AVATAR_SWATCHES.skinColor,
goblin: ['#9cc464', '#7fae4f', '#6a9a45', '#b3cf7a', '#5c8a4a', '#8fa86a'],
};

const preset = (parts: Partial<PixelAvatarRecipeV1>): PixelAvatarRecipeV1 => ({
...DEFAULT_RECIPE,
...parts,
});
Expand Down
Loading
Loading