# Heraldry item: procedural painted coat of arms ## What changed All changes are inside the three allowed paths; nothing else was touched. ### `layers/heraldry/**` (10 files, ordinary 2048×2560 RGBA8 PNGs, ~596 KB total) Every file is a full master-canvas PNG, transparent outside its paint, registered to the banner-arms reserve from `style-guide.md` §2.3: the 240×288 box at `(1500,340)`, i.e. the heater-shield outline (straight sides for the top 5/9 of the height, curving to a point at bottom centre). Nothing is painted outside that box. - `fields/plain.png` — the base field, one coat filling the whole shield. - `divisions/pale-dexter.png` + `divisions/pale-sinister.png` — a per-pale (vertical) division, left/right halves of the shield, antialiased at the seam. - `divisions/fess-chief.png` + `divisions/fess-base.png` — a per-fess (horizontal) division, top/bottom halves. - `ordinaries/cross.png`, `ordinaries/bend.png` — two ordinaries. - `charges/estoile.png`, `charges/trefoil.png`, `charges/tower.png` — three original charges (a six-point estoile, a three-lobed trefoil, a crenellated tower), each an original procedural shape, not copied from any specific real-world or trademarked arms. Every part is painted as a **value map**: R=G=B brush-stroke lightness centred on 176 (highlights above, shadow planes below), with alpha carrying the antialiased shape. The light term is brighter upper-left and darker lower-right everywhere, matching the shared key light. Cloth parts (field/division) use broad (~22 px period) diagonal strokes running down-right, per style-guide §5 "cloth folds diagonally down-right". Metal parts (ordinaries/charges) use a tighter (~10 px) stroke angled toward the upper-left light plus a soft specular highlight near their upper-left extent and a darker reflected patch lower-right, per §5 "metal ... sharp specular dabs facing the upper left". This keeps cloth, metal and charge strokes visibly distinct in direction and contrast, and lets the compositor recolour each part (multiply the chosen tincture by `value/176`) while the brush texture survives. ### `src/heraldry/index.cjs` Exports `buildArms(seedBytes, { root })`, synchronous, no randomness beyond the seed: - Validates a 32-byte `seedBytes` and a `root` directory string. - Makes six **stable labelled draws** from the seed — `sha256(seed || label)`, first 4 bytes mod N — one each for `tincture/field`, `tincture/secondary`, `tincture/metal`, `division`, `ordinary`, `charge`. Each draw is independent of the others and of any future additional draws elsewhere, since each uses its own label rather than a shared sequential PRNG. - Field and secondary are colours (vert/gules/azure/sable, guaranteed distinct); metal is or/argent — observing the heraldic rule of tincture (colour never touches colour, metal never touches metal) by construction: field/division parts are always tinted with a colour and ordinaries/charges always with a metal. - Loads the selected PNG(s) under `layers/heraldry/` relative to `root`, hashes the raw file bytes (sha256) for `usedLayers`, decodes with `src/raster`, tints in place (the value-map recolour above) and composites each onto a fresh 240×288 canvas with `src/raster`'s `composite`, using the same-size fast path (`rect = { x: -1500, y: -340, width: 2048, height: 2560 }`) so the crop from the master canvas to the shield box is itself a `src/raster` composite, not bespoke pixel copying. - Returns `{ image, choices, usedLayers }`: `image` is a 240×288 straight RGBA raster (the field/division layer(s), then the ordinary, then the charge, stacked); `choices.tincture.{field,secondary,metal}` are `{name, hex}`; `choices.{division,ordinary,charge}` are the selected names; `usedLayers` is `[{ path, sha256 }]` with root-relative paths. - The same 240×288 image is meant for both placements: the banner-arms box as-is, and the rail-arms box scaled ×5/12 to 100×120 by the caller (`src/raster`'s `composite` already supports that resize, exercised directly in the tests). - `src/raster` itself was only read, never modified. ### `test/heraldry/arms.test.cjs` Nine `node:test` cases, run with `node --test test/heraldry/arms.test.cjs` (all passing, ~45 s total — each `buildArms` call decodes 2–4 full 2048×2560 PNGs, which dominates the runtime): 1. Output shape is exactly 240×288 RGBA. 2. **Repeated seed** → byte-identical `image`, `choices` and `usedLayers` across two independent calls. 3. **Changed seed** → different `choices` and different `image` bytes. 4. **Valid combinations**: over 12 seeds, field/secondary are distinct published colours, metal is a published metal, division/ordinary/charge are all from the documented sets. 5. Over 30 seeds, every division, ordinary and charge option is drawn at least once (confirms the draw isn't degenerate/biased to one value). 6. **Source part hashes**: every `usedLayers` entry's path is under `layers/heraldry/`, and its recorded sha256 matches an independent hash of the file read straight off disk. 7. Malformed seed length/type and a missing `root` option throw `TypeError`. 8. **Clear exterior**: the four corners of the 240×288 box — outside the heater-shield outline — are fully transparent. 9. **Legible reduced charges**: scales the shield down to the 100×120 rail size with `src/raster`'s `composite`, then checks the charge/ordinary centre is still opaque and its colour differs from an open-field sample point by a wide margin, for three different seeds. ## How I checked it - `node --test test/heraldry/arms.test.cjs` — 9/9 pass. - `node --test test/raster/core.test.cjs` — unaffected, still 8/8 pass (confirms `src/raster` wasn't broken, since it wasn't touched). - Decoded every file in `layers/heraldry/` with `src/raster`'s `decodePNG` and asserted each is exactly 2048×2560. - Rendered preview contact sheets (in a scratch script, not delivered) of the raw shapes and of several tinted combinations, and viewed them: the shield/pale/fess/cross/bend/estoile/trefoil/tower shapes are all legible and correctly clipped to the heater-shield outline, and tinted samples (e.g. vert field with or cross, gules/sable pale with argent bend, azure field with or estoile) show visible brush strokes, upper-left-lit highlights, and clear metal-on-colour contrast consistent with `style-guide.md`. - Confirmed via `git status` that only `layers/heraldry/**`, `src/heraldry/**` and `test/heraldry/**` are new/changed; no other tracked or configuration file was touched. ## Remaining defects / notes - Only 2 divisions (pale, fess) and 2 ordinaries (cross, bend) are supplied, meeting the stated minimums but not covering the full traditional heraldic vocabulary; more can be added later as more `layers/heraldry/divisions|ordinaries/*.png` files plus matching entries in `src/heraldry/index.cjs`'s `DIVISIONS`/`ORDINARIES`/`DIVISION_PARTS` tables. - `buildArms` is comparatively slow (~0.9 s/call) because it re-decodes full 2048×2560 source PNGs on every call rather than caching decoded layers; fine for sample/contact-sheet generation at the 12-portrait scale this batch needs, but a cache keyed by file path would help if arms are built at higher volume later. - No automated check compares the painted hue against a human eye for "readable at 100×120"; the legibility test is a numeric proxy (opacity + colour distance at the rail scale), not a rendered-and-inspected check at that exact size — I did visually inspect the shapes at larger preview size instead.