diff --git a/AGENTS.md b/AGENTS.md index 8b21570..a120dbd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,6 +1,6 @@ # AGENTS.md — BotPixel -Two packages: `packages/morph` (`@botharness/pixel-morph`, generic pixel morph) and `packages/avatar` (`@botharness/pixel-avatar`, the avatar generator and symbols). Both are pure TypeScript with no third-party runtime dependencies; `pixel-avatar` depends only on `pixel-morph` (for `PixelCell` and attribute escaping). +Three packages: `packages/morph` (`@botharness/pixel-morph`, generic pixel morph), `packages/avatar` (`@botharness/pixel-avatar`, the avatar generator and symbols) and `packages/banner` (`@botharness/pixel-banner`, seeded 3:1 nature banners). All are pure TypeScript with no third-party runtime dependencies; `pixel-avatar` depends only on `pixel-morph` (for `PixelCell` and attribute escaping), and `pixel-banner` depends on nothing. ## Commands @@ -12,7 +12,7 @@ pnpm build ## Rules -- Output compatibility is the contract. `packages/avatar/test/fixtures/botharness-golden.json` holds hashes of BotHarness's output for every option, preset, seed, symbol and morph. Do not regenerate it to make a change pass; a visual change to existing options needs a new `rigVersion`/`assetVersion`. +- Output compatibility is the contract. `packages/avatar/test/fixtures/botharness-golden.json` holds hashes of BotHarness's output for every option, preset, seed, symbol and morph. Do not regenerate it to make a change pass; a visual change to existing options needs a new `rigVersion`/`assetVersion`. `packages/banner/test/fixtures/banner-golden.json` does the same for banners: a saved `{ scene, seed }` must keep its pixels, so a visual change needs a new scene name. - Keep `pixel-morph` free of avatar concepts. - Tool-to-symbol mapping belongs to the host app, not to this repo. @@ -21,7 +21,7 @@ pnpm build ```bash bash scripts/npm-token.sh # store the npm publish token (gitignored, mode 600) pnpm release # build + verify, then print what it would publish -pnpm release -- --yes # publish pixel-morph, then pixel-avatar +pnpm release -- --yes # publish pixel-morph, pixel-avatar, then pixel-banner ``` Same flow as BotUI: a granular npm token with _Read and write_ + _Bypass 2FA_, scoped to `@botharness` (a token limited to package names cannot create new packages). To reuse BotUI's saved token, point at it: `NPM_TOKEN_FILE=../BotUI/npm_release.token pnpm release -- --yes`. diff --git a/CHANGELOG.md b/CHANGELOG.md index fc95838..12be83e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- `@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. diff --git a/README.md b/README.md index 68e7dce..4d59dd2 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,13 @@ # BotPixel -Pixel art that changes shape. Two small, dependency-free packages from [BotHarness](https://github.com/BotHarness/BotHarness): +Pixel art that changes shape. Three small, dependency-free packages from [BotHarness](https://github.com/BotHarness/BotHarness): | Package | What it does | | --------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | [`@botharness/pixel-morph`](packages/morph) | Morphs any set of pixels into any other: every pixel hops along a short arc to a partner and snaps to the grid. Works on plain `{ x, y, c }` cells, so it is not tied to avatars. | | [`@botharness/pixel-avatar`](packages/avatar) | Seeded, editable 32×32 chibi pixel avatars, plus 16 state symbols (thinking, reading, editing, searching…) the avatar can morph into while an agent works. | +| [`@botharness/pixel-banner`](packages/banner) | Seeded 3:1 pixel nature banners for profile headers in ten scenes (spring, summer, autumn, winter, sea, mountain, desert, forest, night sky, space), 1500×500 RGBA. | ```bash npm install @botharness/pixel-avatar @@ -57,17 +58,24 @@ A recipe is plain JSON (`head`, `hair`, `eyes`, `outfit`, `accessory`, colours Hosts add their own state symbols with `symbolArtCells(rows, color)`, which draws 24×24 character art (`A` body, `H` highlight) in the built-in style. +## Profile banners + +`seededBannerRecipe(name)` picks a scene and a seed; `pixelBannerImage(recipe)` returns 1500×500 RGBA pixels, scaled ×10 from a 150×50 drawing with no smoothing. Every scene, on a light and a dark page: + +

All ten banner scenes on a light page All ten banner scenes on a dark page

+ ## Contributing ```bash pnpm install pnpm verify # format, lint, typecheck, test pnpm build -pnpm assets # re-render the README banner and GIFs (needs ffmpeg) +pnpm assets # re-render the README banner, GIFs and banner scenes (needs ffmpeg) ``` - New eyes, brows, mouths and symbols are character art: add rows, and the diff shows the picture. - `packages/avatar/test/fixtures/botharness-golden.json` locks the output of every existing option and seed. Saved avatars must not change, so a PR that breaks it needs a new recipe version, not a new fixture. +- `packages/banner/test/fixtures/banner-golden.json` does the same for every banner scene. ## License diff --git a/README.zh.md b/README.zh.md index 605bbdb..11806b9 100644 --- a/README.zh.md +++ b/README.zh.md @@ -2,16 +2,23 @@ # BotPixel -会变形的像素画。来自 [BotHarness](https://github.com/BotHarness/BotHarness) 的两个零依赖小包: +会变形的像素画。来自 [BotHarness](https://github.com/BotHarness/BotHarness) 的三个零依赖小包: - **`@botharness/pixel-morph`**:任意一组像素变成另一组像素。每个像素按极角配对,沿小弧线跳到目标,并始终对齐网格。只认 `{ x, y, c }`,不限于头像。 - **`@botharness/pixel-avatar`**:按名字生成、可编辑的 32×32 Q 版像素头像,加上 16 个状态符号(思考、读文件、编辑、搜索……),Agent 工作时头像可以变形成这些符号。 +- **`@botharness/pixel-banner`**:按名字生成的 3:1 像素自然风景横幅,用作资料页头图。共十个场景(春、夏、秋、冬、海、山、沙漠、森林、星空、太空),输出 1500×500 RGBA。

头像变形成各个工具的符号再变回来

多个头像各自变形成不同工具的符号

用法、原理和贡献方式见 [README.md](README.md)。 +## 资料页横幅 + +`seededBannerRecipe(name)` 按名字选出场景和种子;`pixelBannerImage(recipe)` 返回 1500×500 的 RGBA 像素,由 150×50 的原图整数放大 10 倍,不做平滑。全部场景在浅色和深色页面上的效果: + +

浅色页面上的十个横幅场景 深色页面上的十个横幅场景

+ ## 说话嘴型 给 `pixelAvatarSvg` 或 `pixelFigure` 传入 `{ mouthLayers: true }`,即可按需生成嘴型图层。每个转头角度都包含四组 `[data-avatar-mouth]`:`saved`(原本的表情)、`closed`(闭合)、`half-open`(半开)、`open`(张开)。每个角度同时只显示一个状态;说话结束或取消时恢复 `saved`。图层保留共享的眉毛、鼻子和脸颊,以维持原有像素绘制顺序;眼神、眨眼、眼镜和转头保持独立。文本节奏与减少动效策略由使用方负责。不启用该选项时,现有输出逐字节保持不变,配方与已保存的快照不变。 diff --git a/assets/banner-scenes-dark.png b/assets/banner-scenes-dark.png new file mode 100644 index 0000000..eb212ad Binary files /dev/null and b/assets/banner-scenes-dark.png differ diff --git a/assets/banner-scenes-light.png b/assets/banner-scenes-light.png new file mode 100644 index 0000000..cf61963 Binary files /dev/null and b/assets/banner-scenes-light.png differ diff --git a/package.json b/package.json index cf2f1b3..35593f2 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,11 @@ "name": "botpixel", "version": "0.0.0", "private": true, - "description": "BotPixel — a pixel morph engine and a pixel avatar kit for agent products", + "description": "BotPixel — a pixel morph engine, a pixel avatar kit and pixel banners for agent products", "type": "module", "scripts": { "build": "pnpm -r --filter \"./packages/*\" build", - "typecheck": "tsc --noEmit -p packages/morph && tsc --noEmit -p packages/avatar", + "typecheck": "tsc --noEmit -p packages/morph && tsc --noEmit -p packages/avatar && tsc --noEmit -p packages/banner", "lint": "oxlint", "lint:fix": "oxlint --fix", "format": "oxfmt --write .", diff --git a/packages/banner/LICENSE b/packages/banner/LICENSE new file mode 100644 index 0000000..34b99b5 --- /dev/null +++ b/packages/banner/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 BotHarness + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/banner/README.md b/packages/banner/README.md new file mode 100644 index 0000000..b8087c2 --- /dev/null +++ b/packages/banner/README.md @@ -0,0 +1,26 @@ +# @botharness/pixel-banner + +Seeded pixel-art nature banners in ten scenes, for profile headers. Each banner is drawn at 150×50 and scaled by a whole number to exactly 1500×500 (3:1), so every pixel stays a crisp square. + +

All ten banner scenes on a light page

+ +```ts +import { pixelBannerImage, seededBannerRecipe } from '@botharness/pixel-banner'; + +const recipe = seededBannerRecipe('DeepSeekBot'); // { scene: 'sea', seed: 2786543041 }, same name, same banner +const { width, height, data } = pixelBannerImage(recipe); // 1500×500 RGBA + +const canvas = document.createElement('canvas'); +canvas.width = width; +canvas.height = height; +canvas.getContext('2d')!.putImageData(new ImageData(data, width, height), 0, 0); +canvas.toBlob((png) => upload(png!), 'image/png'); +``` + +- `BANNER_SCENES`: `spring`, `summer`, `autumn`, `winter`, `sea`, `mountain`, `desert`, `forest`, `night-sky`, `space`. +- A recipe is plain JSON: `{ scene, seed }`, where `seed` is a 32-bit unsigned integer. `isPixelBannerRecipe` validates a saved one. +- `seededBannerRecipe(name)`: the same name always gives the same recipe. +- `pixelBannerPixels(recipe)`: the 150×50 source pixels. Draw it on a 150×50 canvas and scale it with CSS `image-rendering: pixelated` if you do not need the full-size file. +- `pixelBannerImage(recipe, { scale })`: the pixels repeated `scale` times (default `BANNER_SCALE`, 10), with no smoothing. `scalePixels(image, scale)` does the same for any image. + +Output is RGBA, row-major, 4 bytes per pixel, fully opaque. It uses only integer and basic float arithmetic, so the same recipe gives the same bytes in every JavaScript engine. `test/fixtures/banner-golden.json` locks the output of every scene; a change that alters existing banners needs a new scene name, not a new fixture. diff --git a/packages/banner/package.json b/packages/banner/package.json new file mode 100644 index 0000000..204bd3a --- /dev/null +++ b/packages/banner/package.json @@ -0,0 +1,46 @@ +{ + "name": "@botharness/pixel-banner", + "version": "0.1.0", + "description": "Seeded pixel-art nature banners (3:1, 1500×500) in ten scenes, as crisp RGBA pixels", + "keywords": [ + "banner", + "generator", + "header", + "pixel-art", + "procedural", + "profile" + ], + "homepage": "https://github.com/BotHarness/BotPixel", + "bugs": { + "url": "https://github.com/BotHarness/BotPixel/issues" + }, + "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/BotHarness/BotPixel.git", + "directory": "packages/banner" + }, + "files": [ + "dist" + ], + "type": "module", + "sideEffects": false, + "main": "./dist/index.js", + "module": "./dist/index.js", + "types": "./dist/index.d.ts", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + }, + "./package.json": "./package.json" + }, + "scripts": { + "build": "tsdown", + "typecheck": "tsc --noEmit" + }, + "devDependencies": { + "tsdown": "^0.20.0", + "typescript": "^5.9.3" + } +} diff --git a/packages/banner/src/grid.ts b/packages/banner/src/grid.ts new file mode 100644 index 0000000..569627d --- /dev/null +++ b/packages/banner/src/grid.ts @@ -0,0 +1,139 @@ +import type { Random } from './random.js'; + +export type Rgb = number; + +export const hex = (value: string): Rgb => Number.parseInt(value.slice(1), 16); + +const BAYER = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5]; + +// ordered-dither threshold in (0, 1) for a pixel, the classic 4×4 Bayer matrix +export const bayer = (x: number, y: number): number => (BAYER[(y & 3) * 4 + (x & 3)]! + 0.5) / 16; + +const smooth = (t: number): number => t * t * (3 - 2 * t); + +export class Grid { + readonly data: Uint8ClampedArray; + + constructor( + readonly width: number, + readonly height: number, + ) { + this.data = new Uint8ClampedArray(width * height * 4); + } + + set(x: number, y: number, color: Rgb): void { + x = Math.round(x); + y = Math.round(y); + if (x < 0 || y < 0 || x >= this.width || y >= this.height) return; + const o = (y * this.width + x) * 4; + this.data[o] = (color >> 16) & 255; + this.data[o + 1] = (color >> 8) & 255; + this.data[o + 2] = color & 255; + this.data[o + 3] = 255; + } + + get(x: number, y: number): Rgb { + const o = (y * this.width + x) * 4; + return (this.data[o]! << 16) | (this.data[o + 1]! << 8) | this.data[o + 2]!; + } + + rect(x: number, y: number, w: number, h: number, color: Rgb): void { + for (let j = y; j < y + h; j++) for (let i = x; i < x + w; i++) this.set(i, j, color); + } + + disc(cx: number, cy: number, r: number, color: Rgb): void { + for (let y = Math.floor(cy - r); y <= cy + r; y++) + for (let x = Math.floor(cx - r); x <= cx + r; x++) + if ((x - cx) ** 2 + (y - cy) ** 2 <= r * r + r * 0.8) this.set(x, y, color); + } + + // vertical bands between colour stops, ordered-dithered where two stops meet + gradient(stops: readonly Rgb[], top = 0, bottom = this.height): void { + const span = Math.max(1, bottom - top - 1); + for (let y = top; y < bottom; y++) { + const t = ((y - top) / span) * (stops.length - 1); + const i = Math.min(stops.length - 2, Math.floor(t)); + const f = t - i; + for (let x = 0; x < this.width; x++) + this.set(x, y, f > bayer(x, y) ? stops[i + 1]! : stops[i]!); + } + } + + fillBelow(tops: readonly number[], color: Rgb, bottom = this.height): void { + tops.forEach((top, x) => { + for (let y = Math.round(top); y < bottom; y++) this.set(x, y, color); + }); + } + + edge(tops: readonly number[], color: Rgb): void { + tops.forEach((top, x) => this.set(x, Math.round(top), color)); + } +} + +// a smooth 1-D skyline: value noise in a few octaves around `base` +export function ridge( + rnd: Random, + width: number, + base: number, + amplitude: number, + step: number, + octaves = 2, +): number[] { + const tops = Array.from({ length: width }, () => base); + let amp = amplitude; + let span = step; + for (let o = 0; o < octaves; o++) { + const points = Array.from({ length: Math.ceil(width / span) + 2 }, () => rnd() * 2 - 1); + for (let x = 0; x < width; x++) { + const i = Math.floor(x / span); + const t = smooth((x % span) / span); + tops[x]! += (points[i]! * (1 - t) + points[i + 1]! * t) * amp; + } + amp /= 2; + span = Math.max(2, Math.floor(span / 2)); + } + return tops.map(Math.round); +} + +// a mountain range: the highest of several jagged triangular peaks at each column +export function peaks( + rnd: Random, + width: number, + base: number, + count: number, + minHeight: number, + maxHeight: number, +): number[] { + const list = Array.from({ length: count }, (_, i) => ({ + x: ((i + 0.2 + rnd() * 0.6) / count) * width, + h: minHeight + rnd() * (maxHeight - minHeight), + left: 0.45 + rnd() * 0.5, + right: 0.45 + rnd() * 0.5, + })); + const jitter = ridge(rnd, width, 0, 1.2, 3, 1); + return Array.from({ length: width }, (_, x) => { + let height = 0; + for (const p of list) { + const d = x - p.x; + height = Math.max(height, p.h - Math.abs(d) * (d < 0 ? p.left : p.right)); + } + return Math.round(base - height + jitter[x]!); + }); +} + +// smooth 2-D value noise in [0, 1], for clouds of gas and mist +export function field(rnd: Random, width: number, height: number, cell: number) { + const cols = Math.ceil(width / cell) + 2; + const rows = Math.ceil(height / cell) + 2; + const values = Array.from({ length: cols * rows }, () => rnd()); + return (x: number, y: number): number => { + const i = Math.floor(x / cell); + const j = Math.floor(y / cell); + const tx = smooth((x % cell) / cell); + const ty = smooth((y % cell) / cell); + const v = (a: number, b: number) => values[(j + b) * cols + i + a]!; + const top = v(0, 0) * (1 - tx) + v(1, 0) * tx; + const bottom = v(0, 1) * (1 - tx) + v(1, 1) * tx; + return top * (1 - ty) + bottom * ty; + }; +} diff --git a/packages/banner/src/index.ts b/packages/banner/src/index.ts new file mode 100644 index 0000000..c91d467 --- /dev/null +++ b/packages/banner/src/index.ts @@ -0,0 +1,84 @@ +import { Grid } from './grid.js'; +import { seededRandom } from './random.js'; +import { PAINTERS } from './scenes.js'; + +export const BANNER_SCENES = [ + 'spring', + 'summer', + 'autumn', + 'winter', + 'sea', + 'mountain', + 'desert', + 'forest', + 'night-sky', + 'space', +] as const satisfies readonly (keyof typeof PAINTERS)[]; + +export type PixelBannerScene = (typeof BANNER_SCENES)[number]; + +export interface PixelBannerRecipe { + scene: PixelBannerScene; + seed: number; +} + +export interface PixelBannerImage { + width: number; + height: number; + // row-major RGBA, 4 bytes per pixel, fully opaque; ready for ImageData or a PNG encoder + data: Uint8ClampedArray; +} + +// 150×50 scaled ×10 is exactly 1500×500, the 3:1 banner size, with no fractional pixels +export const BANNER_WIDTH = 150; +export const BANNER_HEIGHT = 50; +export const BANNER_SCALE = 10; + +export function isPixelBannerRecipe(value: unknown): value is PixelBannerRecipe { + if (typeof value !== 'object' || value === null) return false; + const { scene, seed } = value as Record; + return ( + BANNER_SCENES.includes(scene as PixelBannerScene) && + Number.isInteger(seed) && + (seed as number) >= 0 && + (seed as number) < 2 ** 32 + ); +} + +export function seededBannerRecipe(name: string): PixelBannerRecipe { + const rnd = seededRandom(`pixel-banner:${name}`); + return { + scene: BANNER_SCENES[Math.floor(rnd() * BANNER_SCENES.length)]!, + seed: Math.floor(rnd() * 2 ** 32), + }; +} + +export function pixelBannerPixels(recipe: PixelBannerRecipe): PixelBannerImage { + if (!isPixelBannerRecipe(recipe)) throw new TypeError('invalid pixel banner recipe'); + const grid = new Grid(BANNER_WIDTH, BANNER_HEIGHT); + PAINTERS[recipe.scene](grid, seededRandom(`${recipe.scene}:${recipe.seed}`)); + return { width: grid.width, height: grid.height, data: grid.data }; +} + +export function scalePixels(image: PixelBannerImage, scale: number): PixelBannerImage { + if (!Number.isInteger(scale) || scale < 1) + throw new RangeError('scale must be a positive integer'); + const width = image.width * scale; + const height = image.height * scale; + const data = new Uint8ClampedArray(width * height * 4); + const bytes = image.data.byteOffset % 4 === 0 ? image.data : new Uint8ClampedArray(image.data); + const source = new Uint32Array(bytes.buffer, bytes.byteOffset, image.width * image.height); + const target = new Uint32Array(data.buffer); + for (let y = 0; y < height; y++) { + const row = Math.floor(y / scale) * image.width; + for (let x = 0; x < width; x++) target[y * width + x] = source[row + Math.floor(x / scale)]!; + } + return { width, height, data }; +} + +export function pixelBannerImage( + recipe: PixelBannerRecipe, + options: { scale?: number } = {}, +): PixelBannerImage { + return scalePixels(pixelBannerPixels(recipe), options.scale ?? BANNER_SCALE); +} diff --git a/packages/banner/src/random.ts b/packages/banner/src/random.ts new file mode 100644 index 0000000..0f86885 --- /dev/null +++ b/packages/banner/src/random.ts @@ -0,0 +1,21 @@ +export type Random = () => number; + +export function seededRandom(seed: string): Random { + let h = 2166136261; + for (const char of seed) { + h ^= char.codePointAt(0)!; + h = Math.imul(h, 16777619); + } + return () => { + h = Math.imul(h ^ (h >>> 15), 2246822507); + h = Math.imul(h ^ (h >>> 13), 3266489909); + h ^= h >>> 16; + return (h >>> 0) / 4294967296; + }; +} + +export const int = (rnd: Random, min: number, max: number): number => + min + Math.floor(rnd() * (max - min + 1)); + +export const pick = (rnd: Random, items: readonly T[]): T => + items[Math.floor(rnd() * items.length)]!; diff --git a/packages/banner/src/scenes.ts b/packages/banner/src/scenes.ts new file mode 100644 index 0000000..d385cf8 --- /dev/null +++ b/packages/banner/src/scenes.ts @@ -0,0 +1,294 @@ +import { bayer, field, type Grid, hex, peaks, ridge, type Rgb } from './grid.js'; +import { int, pick, type Random } from './random.js'; +import { bird, clouds, crescent, pine, speckle, stars, sun, tree } from './sprites.js'; + +export type Painter = (g: Grid, rnd: Random) => void; + +const c = (...values: string[]): Rgb[] => values.map(hex); + +const spring: Painter = (g, rnd) => { + g.gradient(c('#8ec9f0', '#a9d6f3', '#c9e6f7', '#e6f3f1'), 0, 36); + clouds(g, rnd, 4, 6, 18, hex('#ffffff'), hex('#d3e5f2')); + const far = ridge(rnd, g.width, 29, 4, 30); + g.fillBelow(far, hex('#a3cfae')); + const mid = ridge(rnd, g.width, 35, 3, 24); + g.fillBelow(mid, hex('#82c070')); + const blossoms = c('#f4b3c6', '#f7c4d3'); + for (let i = 0, n = int(rnd, 3, 5); i < n; i++) { + const x = Math.round(((i + 0.2 + rnd() * 0.6) / n) * g.width); + tree(g, x, mid[x]! + 1, int(rnd, 3, 5), hex('#7a5242'), pick(rnd, blossoms), hex('#fde3ea')); + } + const near = ridge(rnd, g.width, 42, 2, 40); + g.fillBelow(near, hex('#62ab52')); + g.edge(near, hex('#79bd64')); + speckle(g, rnd, 26, near, 6, c('#4f9543')); + speckle(g, rnd, 44, near, 7, c('#ffffff', '#ffd84d', '#f48fb1', '#c9a6f2')); +}; + +const summer: Painter = (g, rnd) => { + g.gradient(c('#2e8ee0', '#4aa5ee', '#76c0f4', '#ace0f8'), 0, 36); + sun(g, int(rnd, 16, g.width - 16), int(rnd, 9, 13), 5, hex('#fff6b8'), hex('#ffe27a')); + clouds(g, rnd, 3, 8, 20, hex('#ffffff'), hex('#cde2f3')); + const far = ridge(rnd, g.width, 31, 3, 34); + g.fillBelow(far, hex('#5aa98a')); + const mid = ridge(rnd, g.width, 36, 2, 26); + g.fillBelow(mid, hex('#3f9a45')); + for (let i = 0, n = int(rnd, 4, 7); i < n; i++) { + const x = Math.round(((i + 0.2 + rnd() * 0.6) / n) * g.width); + tree(g, x, mid[x]! + 1, int(rnd, 3, 5), hex('#5c3b24'), hex('#2e7d32'), hex('#49a84e')); + } + const near = ridge(rnd, g.width, 41, 1, 40); + g.fillBelow(near, hex('#5db74a')); + g.edge(near, hex('#7ccc5f')); + for (let i = 0, n = int(rnd, 6, 10); i < n; i++) { + const x = int(rnd, 2, g.width - 3); + const top = near[x]! + int(rnd, 1, 4); + const stem = int(rnd, 3, 5); + g.rect(x, top, 1, stem + 6, hex('#2f7a2a')); + g.disc(x, top, 1.5, hex('#ffc61a')); + g.set(x, top, hex('#7a4a1c')); + } + speckle(g, rnd, 30, near, 7, c('#4aa33a', '#80cf60')); +}; + +const autumn: Painter = (g, rnd) => { + g.gradient(c('#ef9d58', '#f4b876', '#f8d29c', '#fbe6c2'), 0, 40); + sun(g, int(rnd, 20, g.width - 20), int(rnd, 22, 26), 6, hex('#fff2c6'), hex('#fbd894')); + clouds(g, rnd, 3, 6, 14, hex('#fbe2bf'), hex('#efb886')); + const far = ridge(rnd, g.width, 31, 4, 32); + g.fillBelow(far, hex('#bf8159')); + const mid = ridge(rnd, g.width, 37, 3, 24); + g.fillBelow(mid, hex('#9c5d3a')); + const leaves: [string, string][] = [ + ['#e0632e', '#f28a50'], + ['#efa12e', '#f8c75c'], + ['#c63d2b', '#e0644c'], + ]; + for (let i = 0, n = int(rnd, 5, 7); i < n; i++) { + const x = Math.round(((i + 0.2 + rnd() * 0.6) / n) * g.width); + const [leaf, light] = pick(rnd, leaves); + tree(g, x, mid[x]! + int(rnd, 1, 4), int(rnd, 3, 6), hex('#5a3828'), hex(leaf), hex(light)); + } + const near = ridge(rnd, g.width, 44, 1, 40); + g.fillBelow(near, hex('#86532f')); + speckle(g, rnd, 50, near, 5, c('#e0632e', '#efa12e', '#c63d2b', '#a56a3c')); + for (let i = 0; i < 18; i++) + g.set(int(rnd, 0, g.width - 1), int(rnd, 4, 40), hex(pick(rnd, leaves)[0])); +}; + +const winter: Painter = (g, rnd) => { + g.gradient(c('#9db3cc', '#b9c9db', '#d4dfea', '#e7edf3'), 0, 36); + const range = peaks(rnd, g.width, 32, int(rnd, 4, 6), 10, 20); + range.forEach((top, x) => { + for (let y = top; y < g.height; y++) + g.set(x, y, y - top < 3 && top < 26 ? hex('#f3f7fb') : hex('#8ea3ba')); + }); + const mid = ridge(rnd, g.width, 36, 3, 26); + g.fillBelow(mid, hex('#dde6ef')); + g.edge(mid, hex('#f6f9fc')); + for (let i = 0, n = int(rnd, 6, 9); i < n; i++) { + const x = int(rnd, 3, g.width - 4); + pine(g, x, mid[x]! + 2, int(rnd, 8, 13), hex('#3c5a58'), hex('#eef4f8')); + } + const near = ridge(rnd, g.width, 44, 2, 36); + g.fillBelow(near, hex('#f1f5f9')); + g.edge(near, hex('#c8d5e2')); + for (let i = 0; i < 46; i++) g.set(int(rnd, 0, g.width - 1), int(rnd, 0, 46), hex('#ffffff')); +}; + +const sea: Painter = (g, rnd) => { + const horizon = 28; + g.gradient(c('#5ca6d8', '#88c2e6', '#bcdcef', '#f2e2c4'), 0, horizon); + const sx = int(rnd, 24, g.width - 24); + sun(g, sx, horizon - 6, 5, hex('#fff3c8'), hex('#fbe2a6')); + clouds(g, rnd, 3, 5, 14, hex('#ffffff'), hex('#cfe2ef')); + const ix = int(rnd, 10, g.width - 40); + const island = ridge(rnd, 26, horizon - 3, 2, 8, 1); + island.forEach((top, i) => { + const fade = Math.min(i, 25 - i); + if (fade > 1) g.rect(ix + i, top + Math.max(0, 4 - fade), 1, horizon, hex('#6d9caf')); + }); + g.gradient(c('#5aaccf', '#3a8fbc', '#246f9e', '#195b86'), horizon, g.height); + for (let y = horizon + 1; y < g.height; y += 2) { + const half = Math.max(1, 6 - (y - horizon) / 4); + g.rect(Math.round(sx - half + (y % 4) - 1), y, Math.round(half * 2), 1, hex('#ffe7a8')); + } + for (let i = 0; i < 40; i++) { + const y = int(rnd, horizon + 2, g.height - 1); + g.rect( + int(rnd, 0, g.width - 1), + y, + int(rnd, 2, 2 + Math.floor((y - horizon) / 5)), + 1, + hex('#a6d8ec'), + ); + } + const bx = int(rnd, 10, g.width - 20); + const by = horizon + int(rnd, 4, 8); + g.rect(bx, by, 9, 1, hex('#7b4a32')); + g.rect(bx + 1, by + 1, 7, 1, hex('#5e3726')); + for (let j = 0; j < 8; j++) g.rect(bx + 4, by - 8 + j, 1 + Math.floor(j / 2), 1, hex('#ffffff')); + g.rect(bx + 3, by - 8, 1, 8, hex('#5e3726')); + for (let i = 0; i < 3; i++) bird(g, int(rnd, 8, g.width - 8), int(rnd, 6, 18), hex('#3d5a72')); +}; + +const mountain: Painter = (g, rnd) => { + g.gradient(c('#6aa3d8', '#90bce2', '#b9d4eb', '#dbe8f2'), 0, 40); + clouds(g, rnd, 2, 6, 12, hex('#ffffff'), hex('#d0e1ee')); + const layers: [number, number, number, string, string][] = [ + [26, 12, 22, '#9fb5cd', '#eef3f8'], + [34, 10, 20, '#6e88a4', '#e2eaf3'], + [42, 6, 14, '#4c6279', '#d4dee9'], + ]; + for (const [base, min, max, rock, snow] of layers) { + const tops = peaks(rnd, g.width, base, int(rnd, 4, 6), min, max); + tops.forEach((top, x) => { + for (let y = top; y < g.height; y++) + g.set(x, y, y - top < 2 && base - top > min + 2 ? hex(snow) : hex(rock)); + }); + } + const meadow = ridge(rnd, g.width, 46, 1, 30); + g.fillBelow(meadow, hex('#5d8c53')); + for (let i = 0, n = int(rnd, 4, 7); i < n; i++) { + const x = int(rnd, 2, g.width - 3); + pine(g, x, meadow[x]! + 1, int(rnd, 6, 10), hex('#2f5737')); + } + for (let i = 0; i < 4; i++) bird(g, int(rnd, 10, g.width - 10), int(rnd, 4, 14), hex('#36506a')); +}; + +const desert: Painter = (g, rnd) => { + g.gradient(c('#5aa2d6', '#84bbe2', '#b4d4eb', '#f0dcb4'), 0, 40); + sun(g, int(rnd, 16, g.width - 16), int(rnd, 8, 12), 5, hex('#fff5d2'), hex('#fde3a2')); + const mx = int(rnd, 10, g.width - 50); + const mw = int(rnd, 22, 34); + const top = int(rnd, 16, 20); + for (let y = top; y < 38; y++) { + const spread = Math.floor((y - top) * 0.6); + g.rect(mx - spread, y, mw + spread * 2, 1, hex('#c88a62')); + g.rect(mx + mw + spread - 3, y, 3, 1, hex('#ad714d')); + } + const dunes: [number, number, string, string][] = [ + [32, 3, '#e6bd85', '#f1d3a3'], + [38, 3, '#dba96b', '#ebc58f'], + [44, 2, '#cf9657', '#e0b072'], + ]; + dunes.forEach(([base, amp, sand, crest], i) => { + const tops = ridge(rnd, g.width, base, amp, 28 - i * 4); + g.fillBelow(tops, hex(sand)); + g.edge(tops, hex(crest)); + if (i === 1) + for (let k = 0, n = int(rnd, 2, 4); k < n; k++) { + const x = int(rnd, 4, g.width - 5); + const h = int(rnd, 6, 10); + const y = tops[x]! + 1; + g.rect(x, y - h, 2, h, hex('#4f8a4b')); + const arm = int(rnd, 2, h - 3); + g.rect(x - 2, y - arm - 1, 2, 1, hex('#4f8a4b')); + g.rect(x - 2, y - arm - 4, 1, 3, hex('#4f8a4b')); + g.rect(x + 2, y - arm + 1, 2, 1, hex('#4f8a4b')); + g.rect(x + 3, y - arm - 2, 1, 3, hex('#4f8a4b')); + } + }); +}; + +const forest: Painter = (g, rnd) => { + g.gradient(c('#a7d2c1', '#c2e1d1', '#d9eee1', '#e7f4ea'), 0, 40); + sun(g, int(rnd, 20, g.width - 20), int(rnd, 8, 12), 4, hex('#fbf6dc'), hex('#eef3d6')); + const layers: [number, number, number, string][] = [ + [30, 6, 10, '#8fbba6'], + [36, 9, 14, '#5e997b'], + [43, 12, 18, '#2f6a4f'], + ]; + for (const [base, min, max, color] of layers) { + const ground = ridge(rnd, g.width, base, 2, 30); + g.fillBelow(ground, hex(color)); + for (let x = int(rnd, 0, 4); x < g.width; x += int(rnd, 4, 9)) + pine(g, x, ground[x]!, int(rnd, min, max), hex(color)); + } + const floor = ridge(rnd, g.width, 47, 1, 30); + g.fillBelow(floor, hex('#24533d')); + for (let i = 0, n = int(rnd, 3, 6); i < n; i++) { + const x = int(rnd, 3, g.width - 4); + const y = floor[x]!; + g.rect(x, y - 1, 1, 2, hex('#efe6d2')); + g.rect(x - 1, y - 2, 3, 1, hex('#d84a3a')); + g.set(x, y - 3, hex('#d84a3a')); + } +}; + +const nightSky: Painter = (g, rnd) => { + g.gradient(c('#0e1634', '#172250', '#243265', '#33487a'), 0, g.height); + stars(g, rnd, 80, 38, c('#ffffff', '#ffeeb5', '#b9d0ff')); + crescent(g, int(rnd, 14, g.width - 14), int(rnd, 9, 13), 6, hex('#fff3c6')); + const sx = int(rnd, 20, g.width - 40); + const sy = int(rnd, 4, 10); + for (let i = 0; i < 12; i++) + g.set(sx + i, sy + Math.floor(i / 2), i < 4 ? hex('#ffffff') : hex('#9fb4e6')); + const far = ridge(rnd, g.width, 38, 4, 30); + g.fillBelow(far, hex('#1b2547')); + const near = ridge(rnd, g.width, 44, 2, 26); + g.fillBelow(near, hex('#0d1430')); + for (let i = 0, n = int(rnd, 5, 8); i < n; i++) { + const x = int(rnd, 2, g.width - 3); + pine(g, x, near[x]! + 1, int(rnd, 6, 11), hex('#0d1430')); + } + const hx = int(rnd, 10, g.width - 20); + const hy = near[hx]!; + g.rect(hx, hy - 5, 7, 6, hex('#0d1430')); + for (let j = 0; j < 4; j++) g.rect(hx - 1 + j, hy - 6 - j, 9 - j * 2, 1, hex('#0d1430')); + g.rect(hx + 2, hy - 3, 2, 2, hex('#ffd36b')); +}; + +const space: Painter = (g, rnd) => { + g.gradient(c('#0c0922', '#151035', '#1d1648', '#140f33'), 0, g.height); + const gas = field(rnd, g.width, g.height, 14); + const detail = field(rnd, g.width, g.height, 5); + const nebula = c('#2c2264', '#4a2f86', '#7a3d98', '#b0579e'); + for (let y = 0; y < g.height; y++) + for (let x = 0; x < g.width; x++) { + const v = gas(x, y) * 0.8 + detail(x, y) * 0.2; + const level = Math.floor((v - 0.45) * 9 + bayer(x, y) - 0.5); + if (level >= 0) g.set(x, y, nebula[Math.min(level, nebula.length - 1)]!); + } + stars(g, rnd, 110, g.height - 1, c('#ffffff', '#cfd8ff', '#ffe3b0', '#9fb0ff')); + const px = int(rnd, 24, g.width - 24); + const py = int(rnd, 18, 32); + const r = int(rnd, 8, 10); + const bands = c('#e3a65c', '#cc8649', '#e9b97a'); + for (let y = py - r; y <= py + r; y++) + for (let x = px - r; x <= px + r; x++) { + const dx = x - px; + const dy = y - py; + if (dx * dx + dy * dy > r * r + r * 0.8) continue; + const shade = (dx + 1.5) ** 2 + (dy + 2) ** 2 > (r + 0.5) ** 2; + g.set(x, y, shade ? hex('#9a5e35') : bands[Math.floor((y - py + r) / 3) % bands.length]!); + } + const rx = r * 1.9; + const ry = r * 0.45; + for (let y = py - r; y <= py + r; y++) + for (let x = Math.floor(px - rx - 1); x <= px + rx + 1; x++) { + const dx = x - px; + const dy = y - py + dx * 0.18; + const e = (dx / rx) ** 2 + (dy / ry) ** 2; + const behind = dy < 0 && dx * dx + (y - py) ** 2 <= r * r + r * 0.8; + if (e > 0.72 && e < 1.15 && !behind) g.set(x, y, e < 0.95 ? hex('#f2d8a2') : hex('#c9a874')); + } + const mx = px + (px < g.width / 2 ? int(rnd, 30, 50) : -int(rnd, 30, 50)); + const my = int(rnd, 8, 14); + g.disc(mx, my, 3, hex('#b9c3d8')); + g.set(mx + 1, my - 1, hex('#8e98b0')); + g.set(mx - 1, my + 1, hex('#8e98b0')); +}; + +export const PAINTERS = { + spring, + summer, + autumn, + winter, + sea, + mountain, + desert, + forest, + 'night-sky': nightSky, + space, +} as const satisfies Record; diff --git a/packages/banner/src/sprites.ts b/packages/banner/src/sprites.ts new file mode 100644 index 0000000..b6ec135 --- /dev/null +++ b/packages/banner/src/sprites.ts @@ -0,0 +1,126 @@ +import { type Grid, type Rgb } from './grid.js'; +import { int, type Random } from './random.js'; + +export function cloud( + g: Grid, + rnd: Random, + x: number, + y: number, + width: number, + light: Rgb, + shade: Rgb, +): void { + const puffs = Math.max(2, Math.round(width / 5)); + const radii = Array.from({ length: puffs }, (_, i) => { + const middle = 1 - Math.abs(i / (puffs - 1) - 0.5) * 2; + return 1.5 + middle * 2 + rnd() * 1.2; + }); + const cx = (i: number) => x + (i / (puffs - 1)) * width; + radii.forEach((r, i) => g.disc(cx(i), y - r + 2, r, shade)); + radii.forEach((r, i) => g.disc(cx(i), y - r + 1, r, light)); + g.rect(Math.round(x), y + 1, Math.round(width) + 1, 1, shade); +} + +export function clouds( + g: Grid, + rnd: Random, + count: number, + top: number, + bottom: number, + light: Rgb, + shade: Rgb, +): void { + for (let i = 0; i < count; i++) { + const x = ((i + rnd() * 0.7) / count) * g.width - 6; + cloud(g, rnd, x, int(rnd, top, bottom), int(rnd, 14, 26), light, shade); + } +} + +export function sun(g: Grid, cx: number, cy: number, r: number, core: Rgb, halo: Rgb): void { + g.disc(cx, cy, r + 2, halo); + g.disc(cx, cy, r, core); +} + +export function crescent(g: Grid, cx: number, cy: number, r: number, color: Rgb): void { + const inside = (x: number, y: number, ox: number, oy: number) => + (x - ox) ** 2 + (y - oy) ** 2 <= r * r + r * 0.8; + for (let y = cy - r; y <= cy + r; y++) + for (let x = cx - r; x <= cx + r; x++) + if (inside(x, y, cx, cy) && !inside(x, y, cx + r * 0.55, cy - r * 0.35)) g.set(x, y, color); +} + +export function stars( + g: Grid, + rnd: Random, + count: number, + bottom: number, + colors: readonly Rgb[], +): void { + for (let i = 0; i < count; i++) { + const x = int(rnd, 0, g.width - 1); + const y = int(rnd, 0, bottom); + const color = colors[i % colors.length]!; + g.set(x, y, color); + if (rnd() < 0.08) { + g.set(x - 1, y, color); + g.set(x + 1, y, color); + g.set(x, y - 1, color); + g.set(x, y + 1, color); + } + } +} + +export function pine( + g: Grid, + x: number, + base: number, + height: number, + color: Rgb, + snow?: Rgb, +): void { + g.set(x, base, color); + const tier = Math.max(3, Math.round(height / 3)); + for (let j = 0; j < height; j++) { + const y = base - height + j; + const half = Math.floor((j % tier) * 0.6 + j * 0.22); + for (let i = -half; i <= half; i++) g.set(x + i, y, color); + if (snow !== undefined && j % tier === 0) + for (let i = -half; i <= half; i++) g.set(x + i, y, snow); + } +} + +export function tree( + g: Grid, + x: number, + base: number, + r: number, + trunk: Rgb, + leaf: Rgb, + light: Rgb, +): void { + const trunkHeight = Math.max(2, Math.round(r * 0.8)); + g.rect(x, base - trunkHeight, 1, trunkHeight + 1, trunk); + const cy = base - trunkHeight - r + 1; + g.disc(x, cy, r, leaf); + g.disc(x - r * 0.3, cy - r * 0.3, Math.max(1, r - 2), light); +} + +export function speckle( + g: Grid, + rnd: Random, + count: number, + tops: readonly number[], + depth: number, + colors: readonly Rgb[], +): void { + for (let i = 0; i < count; i++) { + const x = int(rnd, 0, g.width - 1); + g.set(x, tops[x]! + 1 + int(rnd, 0, depth), colors[i % colors.length]!); + } +} + +export function bird(g: Grid, x: number, y: number, color: Rgb): void { + g.set(x - 1, y - 1, color); + g.set(x, y, color); + g.set(x + 1, y - 1, color); +} diff --git a/packages/banner/test/banner.test.ts b/packages/banner/test/banner.test.ts new file mode 100644 index 0000000..e73fd9c --- /dev/null +++ b/packages/banner/test/banner.test.ts @@ -0,0 +1,113 @@ +import { createHash } from 'node:crypto'; +import { readFileSync } from 'node:fs'; +import { describe, expect, it } from 'vitest'; +import { + BANNER_HEIGHT, + BANNER_SCALE, + BANNER_SCENES, + BANNER_WIDTH, + isPixelBannerRecipe, + pixelBannerImage, + pixelBannerPixels, + scalePixels, + seededBannerRecipe, + type PixelBannerRecipe, +} from '../src/index.js'; + +const golden = JSON.parse( + readFileSync(new URL('./fixtures/banner-golden.json', import.meta.url), 'utf8'), +) as { + seeds: number[]; + names: string[]; + pixels: Record; + image: Record; + seeded: Record; +}; +const hash = (data: Uint8ClampedArray) => + createHash('sha256').update(data).digest('hex').slice(0, 16); + +describe('golden output', () => { + it('renders every scene and seed to the locked pixels', () => { + const actual: Record = {}; + for (const scene of BANNER_SCENES) + for (const seed of golden.seeds) + actual[`${scene}:${seed}`] = hash(pixelBannerPixels({ scene, seed }).data); + expect(actual).toEqual(golden.pixels); + }); + + it('scales to the locked 1500×500 image', () => { + const actual: Record = {}; + for (const scene of BANNER_SCENES) + actual[scene] = hash(pixelBannerImage({ scene, seed: golden.seeds[0]! }).data); + expect(actual).toEqual(golden.image); + }); + + it('seeds the same recipe from the same name', () => { + for (const name of golden.names) + expect(seededBannerRecipe(name), name).toEqual(golden.seeded[name]); + }); +}); + +describe('pixels', () => { + it('fills every pixel opaquely at 150×50 and scales to exactly 1500×500', () => { + for (const scene of BANNER_SCENES) { + const base = pixelBannerPixels({ scene, seed: 7 }); + expect([base.width, base.height]).toEqual([BANNER_WIDTH, BANNER_HEIGHT]); + for (let i = 3; i < base.data.length; i += 4) expect(base.data[i], scene).toBe(255); + const image = pixelBannerImage({ scene, seed: 7 }); + expect([image.width, image.height]).toEqual([1500, 500]); + expect(BANNER_SCALE).toBe(10); + } + }); + + it('repeats each source pixel as a crisp block with no smoothing', () => { + const base = pixelBannerPixels({ scene: 'sea', seed: 3 }); + const image = scalePixels(base, 4); + const at = (img: typeof base, x: number, y: number) => + Array.from(img.data.subarray((y * img.width + x) * 4, (y * img.width + x) * 4 + 4)); + for (const [x, y] of [ + [0, 0], + [75, 25], + [149, 49], + ] as const) + for (let dy = 0; dy < 4; dy++) + for (let dx = 0; dx < 4; dx++) + expect(at(image, x * 4 + dx, y * 4 + dy)).toEqual(at(base, x, y)); + }); + + it('scales RGBA views that start at an unaligned byte offset', () => { + const base = pixelBannerPixels({ scene: 'desert', seed: 5 }); + const shifted = new Uint8ClampedArray(base.data.length + 1); + shifted.set(base.data, 1); + const view = { ...base, data: shifted.subarray(1) }; + expect(scalePixels(view, 2).data).toEqual(scalePixels(base, 2).data); + }); + + it('gives different seeds different pictures and the same seed the same one', () => { + const a = pixelBannerPixels({ scene: 'mountain', seed: 1 }).data; + expect(pixelBannerPixels({ scene: 'mountain', seed: 1 }).data).toEqual(a); + expect(pixelBannerPixels({ scene: 'mountain', seed: 2 }).data).not.toEqual(a); + }); +}); + +describe('recipes', () => { + it('accepts only known scenes and 32-bit unsigned seeds', () => { + expect(isPixelBannerRecipe({ scene: 'space', seed: 0 })).toBe(true); + expect(isPixelBannerRecipe({ scene: 'space', seed: 2 ** 32 - 1 })).toBe(true); + expect(isPixelBannerRecipe({ scene: 'beach', seed: 0 })).toBe(false); + expect(isPixelBannerRecipe({ scene: 'space', seed: -1 })).toBe(false); + expect(isPixelBannerRecipe({ scene: 'space', seed: 1.5 })).toBe(false); + expect(isPixelBannerRecipe(null)).toBe(false); + expect(() => pixelBannerPixels({ scene: 'beach' } as never)).toThrow(TypeError); + expect(() => scalePixels(pixelBannerPixels({ scene: 'space', seed: 0 }), 1.5)).toThrow( + RangeError, + ); + }); + + it('spreads seeded names across every scene', () => { + const scenes = new Set( + Array.from({ length: 200 }, (_, i) => seededBannerRecipe(`bot-${i}`).scene), + ); + expect([...scenes].sort()).toEqual([...BANNER_SCENES].sort()); + }); +}); diff --git a/packages/banner/test/fixtures/banner-golden.json b/packages/banner/test/fixtures/banner-golden.json new file mode 100644 index 0000000..61e3e45 --- /dev/null +++ b/packages/banner/test/fixtures/banner-golden.json @@ -0,0 +1,84 @@ +{ + "seeds": [0, 1, 42, 4294967295], + "names": ["DeepSeekBot", "Ada", "Bob", "Cleo", "研究员", "qa-bot-01"], + "pixels": { + "spring:0": "a6715d803a872993", + "spring:1": "4f29e021de1083d9", + "spring:42": "cdd6aa7b5b17ba41", + "spring:4294967295": "bdc8ce0e9b98ac28", + "summer:0": "6b49a2240eba7a6d", + "summer:1": "6e04f5eec722ec80", + "summer:42": "3249395133e604bd", + "summer:4294967295": "fd46aa6d796ce768", + "autumn:0": "7f155d765ee75099", + "autumn:1": "afb813c411b03a7d", + "autumn:42": "d8a93f0d2c1a8537", + "autumn:4294967295": "b66937680f77e129", + "winter:0": "a9f041eaaf647a68", + "winter:1": "1c106eca92dcd9f1", + "winter:42": "4bf1941f7fa55cc2", + "winter:4294967295": "7b5a573a79eceecd", + "sea:0": "4ac1a1c636f819f0", + "sea:1": "b787c09e6899e5f0", + "sea:42": "dbdbfa18a5f53435", + "sea:4294967295": "69ae8d213582111a", + "mountain:0": "b05461a3562c8a81", + "mountain:1": "42bf37b0ea0d0602", + "mountain:42": "f7fd2893b2757bc7", + "mountain:4294967295": "ebdf94ee3b56f8da", + "desert:0": "e203ca548cc6e432", + "desert:1": "5b92f1c070ac56a8", + "desert:42": "0d94fbfa668ad18e", + "desert:4294967295": "18b8d4260f0fbd11", + "forest:0": "e39d22b6f29a17ed", + "forest:1": "92c2dbb8efbe30fc", + "forest:42": "9f13457d3dddda48", + "forest:4294967295": "82790f018bdc98cf", + "night-sky:0": "e65ca7047a49eb47", + "night-sky:1": "c6a93f4ff874a04f", + "night-sky:42": "bc2e8c91c0fd4c04", + "night-sky:4294967295": "109c1efc211fc7c1", + "space:0": "700f0e6bc73d8ecc", + "space:1": "d34cb1e8b69d439b", + "space:42": "2f8d229a2f62fa93", + "space:4294967295": "45771a31b1caf3c3" + }, + "image": { + "spring": "a703784e1a3b1846", + "summer": "6e3e7da844b50898", + "autumn": "14ebeec9cf038904", + "winter": "7c601bc9c17ad1e8", + "sea": "061ab86a31ac26b1", + "mountain": "8905c2e3d9ea1dd8", + "desert": "eb246dcbf5a0a6e6", + "forest": "7e28ad74b48e3cb9", + "night-sky": "e06d2a4c4ce44f2f", + "space": "521ea9b7cde6fce1" + }, + "seeded": { + "DeepSeekBot": { + "scene": "forest", + "seed": 374352197 + }, + "Ada": { + "scene": "autumn", + "seed": 901016741 + }, + "Bob": { + "scene": "autumn", + "seed": 2555008588 + }, + "Cleo": { + "scene": "desert", + "seed": 257281357 + }, + "研究员": { + "scene": "space", + "seed": 1338610429 + }, + "qa-bot-01": { + "scene": "desert", + "seed": 1931069019 + } + } +} diff --git a/packages/banner/tsconfig.json b/packages/banner/tsconfig.json new file mode 100644 index 0000000..7680f99 --- /dev/null +++ b/packages/banner/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../../tsconfig.base.json", + "include": ["src", "test"] +} diff --git a/packages/banner/tsdown.config.ts b/packages/banner/tsdown.config.ts new file mode 100644 index 0000000..a77b5a2 --- /dev/null +++ b/packages/banner/tsdown.config.ts @@ -0,0 +1,11 @@ +import { defineConfig } from 'tsdown'; + +export default defineConfig({ + entry: ['src/index.ts'], + format: ['esm'], + dts: true, + clean: true, + sourcemap: true, + // the package is type: module, so the ESM output is plain .js + outExtensions: () => ({ js: '.js', dts: '.d.ts' }), +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bd0fa6a..b8604cf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -198,6 +198,15 @@ importers: specifier: ^5.9.3 version: 5.9.3 + packages/banner: + devDependencies: + tsdown: + specifier: ^0.20.0 + version: 0.20.3(@emnapi/core@1.10.0)(@emnapi/runtime@1.10.0)(typescript@5.9.3) + typescript: + specifier: ^5.9.3 + version: 5.9.3 + packages/morph: devDependencies: tsdown: diff --git a/scripts/readme-assets.mjs b/scripts/readme-assets.mjs index 1340538..234b78d 100644 --- a/scripts/readme-assets.mjs +++ b/scripts/readme-assets.mjs @@ -11,6 +11,7 @@ import { pixelTileColor, seededRecipe, } from '../packages/avatar/dist/index.js'; +import { BANNER_SCENES, pixelBannerPixels } from '../packages/banner/dist/index.js'; import { pixelFrame, planPixels } from '../packages/morph/dist/index.js'; const OUT = new URL('../assets/', import.meta.url); @@ -265,6 +266,31 @@ function timeline(recipe, keys, hold) { ); } +// every banner scene, drawn on a light and a dark page so both app themes can be checked +function bannerSheet(background, name) { + const s = 2; + const gap = 12; + const cols = 2; + const w = 150 * s; + const h = 50 * s; + const rows = Math.ceil(BANNER_SCENES.length / cols); + const sheet = new Canvas(cols * (w + gap) + gap, rows * (h + gap) + gap, background); + BANNER_SCENES.forEach((scene, i) => { + const { data, width, height } = pixelBannerPixels({ scene, seed: 1 }); + const ox = gap + (i % cols) * (w + gap); + const oy = gap + Math.floor(i / cols) * (h + gap); + for (let y = 0; y < height; y++) + for (let x = 0; x < width; x++) { + const o = (y * width + x) * 4; + const hexColor = `#${[data[o], data[o + 1], data[o + 2]].map((v) => v.toString(16).padStart(2, '0')).join('')}`; + sheet.rect(ox + x * s, oy + y * s, s, s, hexColor); + } + }); + savePng(sheet, name); +} +bannerSheet('#f4f4f6', 'banner-scenes-light.png'); +bannerSheet(BG, 'banner-scenes-dark.png'); + console.log( - `rendered banner.png, morph-turn.gif, morph-crew.gif, morph-faces.gif (${AVATAR_PARTS.hair.length} hairstyles in the catalog)`, + `rendered banner.png, morph-turn.gif, morph-crew.gif, morph-faces.gif, banner-scenes-light.png, banner-scenes-dark.png (${AVATAR_PARTS.hair.length} hairstyles in the catalog)`, ); diff --git a/scripts/release.mjs b/scripts/release.mjs index de150e2..6420867 100644 --- a/scripts/release.mjs +++ b/scripts/release.mjs @@ -43,6 +43,7 @@ if (token && yes) { const ORDER = [ { dir: 'packages/morph', name: '@botharness/pixel-morph', why: 'depends on nothing' }, { dir: 'packages/avatar', name: '@botharness/pixel-avatar', why: 'depends on pixel-morph' }, + { dir: 'packages/banner', name: '@botharness/pixel-banner', why: 'depends on nothing' }, ]; /** diff --git a/tsconfig.base.json b/tsconfig.base.json index 274598c..4c9aaaa 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -19,7 +19,8 @@ // against its siblings' source without a build first "paths": { "@botharness/pixel-morph": ["./packages/morph/src/index.ts"], - "@botharness/pixel-avatar": ["./packages/avatar/src/index.ts"] + "@botharness/pixel-avatar": ["./packages/avatar/src/index.ts"], + "@botharness/pixel-banner": ["./packages/banner/src/index.ts"] } } } diff --git a/vitest.config.ts b/vitest.config.ts index 558273c..f61fa01 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -10,6 +10,9 @@ export default defineConfig({ '@botharness/pixel-avatar': fileURLToPath( new URL('./packages/avatar/src/index.ts', import.meta.url), ), + '@botharness/pixel-banner': fileURLToPath( + new URL('./packages/banner/src/index.ts', import.meta.url), + ), }, }, test: {