Files
Rosea/tools/art-prep/src/prompt.ts
T
mnovak f24597dfdb [art-prep] ArtBoard: Build prompts, stand-ins and the art board from the manifest, and accept pictures into art/final
tools/art-prep reads art/STYLE.md (draft) and art/manifest.json (25 Phase 1
pictures), refuses forbidden words in any prompt, writes 29 stand-ins and the
board files, and cuts and encodes accepted pictures as WebP within budget.
2026-11-01 20:16:59 +01:00

182 lines
9.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* The prompts on the art board, built from the manifest and STYLE.md §1. Nobody types
* a prompt by hand (STYLE.md §6): the same words describe the rose every time.
*/
import type { Asset, Cell, Manifest } from './manifest.js';
/** Where a cut falls: rounded once and shared by the board's overlay and `art:accept`. */
export function cutLines(size: number, parts: number): number[] {
return Array.from({ length: parts + 1 }, (_, i) => Math.round((i * size) / parts));
}
export interface CellBox {
readonly index: number;
readonly row: number;
readonly col: number;
readonly x0: number;
readonly y0: number;
readonly x1: number;
readonly y1: number;
readonly cx: number;
readonly cy: number;
/** The safe box: the drawing stays inside it (STYLE.md §9). */
readonly safe: { readonly x0: number; readonly y0: number; readonly x1: number; readonly y1: number };
}
/** STYLE.md §9: the clear band inside every cell edge, as a share of the cell's shorter side. */
export const SAFE_MARGIN = 0.12;
export function cellBoxes(width: number, height: number, cols: number, rows: number): CellBox[] {
const xs = cutLines(width, cols);
const ys = cutLines(height, rows);
const boxes: CellBox[] = [];
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const x0 = xs[col] ?? 0, x1 = xs[col + 1] ?? width, y0 = ys[row] ?? 0, y1 = ys[row + 1] ?? height;
const m = Math.round(SAFE_MARGIN * Math.min(x1 - x0, y1 - y0));
boxes.push({
index: boxes.length, row, col, x0, y0, x1, y1,
cx: Math.round((x0 + x1) / 2), cy: Math.round((y0 + y1) / 2),
safe: { x0: x0 + m, y0: y0 + m, x1: x1 - m, y1: y1 - m }
});
}
}
return boxes;
}
/** The size one cell ships at: its share of the sheet. */
export function cellSize(a: Asset): { width: number; height: number } {
const g = a.grid ?? { cols: 1, rows: 1 };
return { width: Math.floor(a.width / g.cols), height: Math.floor(a.height / g.rows) };
}
/** A cell made on its own is made on the generator canvas closest to its shape. */
export function soloCanvas(a: Asset): { width: number; height: number } {
const { width, height } = cellSize(a);
if (height > width * 1.2) return { width: 1024, height: 1536 };
if (width > height * 1.2) return { width: 1536, height: 1024 };
return { width: 1024, height: 1024 };
}
const KIND: Record<Asset['type'], string> = {
sheet: 'character sheet',
spritesheet: 'sprite sheet',
sprite: 'single picture',
layer: 'garden layer',
icon: 'app icon'
};
const bg = (b: Asset['background']): string => (b === 'transparent' ? 'transparent background' : 'full background');
const list = (items: readonly string[]): string => items.join('; ');
/** Extra things a sprite sheet must never have, so it can be cut cleanly. */
const SHEET_AVOID = [
'grid lines, cell borders or frames',
'a background colour or paper behind the drawings',
'ground or cast shadows',
'drawings touching or overlapping',
'cropped petals, leaves or stems'
];
/** The character the picture shows, with the sheet to attach, or the sheet's own description. */
function subjectLine(a: Asset, m: Manifest, subject: string): string {
const c = a.character === undefined ? undefined : m.characters[a.character];
if (!c) return subject;
if (c.sheet === a.id) return `${subject} ${c.description}`;
return `${subject} Attach the character sheet ${c.sheet} as the reference picture and match it exactly. ${c.description}`.trim();
}
/** The reference pictures to attach when making `a`. */
export function attachments(a: Asset, m: Manifest): string[] {
const c = a.character === undefined ? undefined : m.characters[a.character];
return c && c.sheet !== a.id ? [c.sheet] : [];
}
function canvasLine(width: number, height: number): string {
const mx = Math.round(width * 0.15), my = Math.round(height * 0.15);
return `Canvas: ${width} × ${height} px, fully transparent background. One subject, centred on (${width / 2}, ${height / 2}), whole and uncropped, filling about 70 % of the canvas: everything inside x ${mx}–${width - mx}, y ${my}–${height - my}, nothing touching an edge. No background colour or paper behind it, no ground, no cast shadow.`;
}
/** The prompt for one picture, or for a whole sprite sheet. */
export function promptFor(a: Asset, m: Manifest, style: string): string {
if (a.type === 'spritesheet') return sheetPrompt(a, m, style);
const lines = [
style,
'',
`Asset: ${a.id}`,
`Purpose: ${a.purpose}`,
`Subject: ${subjectLine(a, m, a.subject ?? '')}`
];
if (a.setting) lines.push(`Setting: ${a.setting}`);
if (a.type === 'sprite') lines.push(canvasLine(a.width, a.height));
if (a.composition) lines.push(`Composition: ${a.composition}`);
lines.push(
`Size: ${a.width} × ${a.height} px, ${bg(a.background)} (STYLE.md §5, ${KIND[a.type]})`,
`Must include: ${list(a.include)}`,
`Must avoid: ${list([...a.avoid, ...m.avoid])}`
);
return lines.join('\n');
}
function sheetPrompt(a: Asset, m: Manifest, style: string): string {
const g = a.grid ?? { cols: 1, rows: 1 };
const cells = a.cells ?? [];
const { width: cw, height: ch } = cellSize(a);
const boxes = cellBoxes(a.width, a.height, g.cols, g.rows);
const margin = Math.round(SAFE_MARGIN * Math.min(cw, ch));
const xs = cutLines(a.width, g.cols).slice(1, -1);
const ys = cutLines(a.height, g.rows).slice(1, -1);
const cuts = [
xs.length ? `Cut lines down the image at x = ${xs.join(', ')}.` : '',
ys.length ? `Cut lines across the image at y = ${ys.join(', ')}.` : ''
].filter(Boolean).join(' ');
return [
style,
'',
`Asset: ${a.id}`,
`Kind: a sprite sheet. One image holding ${cells.length} separate pictures on a clean grid; it is cut apart along the grid and each cell becomes its own picture in the app. The sheet itself is never shown.`,
`Purpose: ${a.purpose}`,
`Subject: ${subjectLine(a, m, '') || 'Each cell holds its own thing, described in the cell list below.'}`,
`Angle and scale: ${a.angle ?? ''}`,
`Canvas: ${a.width} × ${a.height} px, transparent background: a size image generators offer, and the size the grid below is measured on. The app divides the image into ${g.cols} × ${g.rows} equal cells whatever its size.`,
`Layout: a clean grid of ${g.cols} column${g.cols === 1 ? '' : 's'} × ${g.rows} row${g.rows === 1 ? '' : 's'} of equal, invisible cells, each ${cw} × ${ch} px, read like a book (left to right, top row first). One subject per cell, centred in its cell, whole and uncropped, with generous empty gutters, so nothing touches the edge of its cell or reaches into a neighbour. No grid lines, borders, frames, numbers, labels or captions anywhere. Fully transparent background between and around the subjects: no paper colour, no ground, no cast shadows. The same light from the left, the same outline weight and the same gouache texture in every cell.`,
`Cells (${cells.length}, in reading order):`,
...cells.map((c, i) => {
const b = boxes[i];
return `${i + 1}. ${c.id} (row ${(b?.row ?? 0) + 1}, column ${(b?.col ?? 0) + 1}): ${c.does}`;
}),
`Grid in pixels:`,
`- ${cuts} The lines are never drawn: they are where the scissors go.`,
`- Each drawing stays whole inside its own safe box, at least ${margin} px clear of every edge of its cell, so a band of at least ${margin * 2} px of empty transparency runs along every cut line and round the outside. Nothing crosses a cut line: no petal, leaf, stem, shadow or sparkle.`,
...cells.map((c, i) => {
const b = boxes[i];
if (!b) return '';
return `- Cell ${i + 1}, ${c.id}: starts at (${b.x0}, ${b.y0}), ends at (${b.x1}, ${b.y1}); centre the drawing on (${b.cx}, ${b.cy}); safe box x ${b.safe.x0}–${b.safe.x1}, y ${b.safe.y0}–${b.safe.y1}.`;
}).filter(Boolean),
`- One scale for the whole sheet: the largest drawing fills its safe box and every other cell is drawn at that same scale, centred on its cell's centre.`,
`- If the tool makes the image another size, keep the same ${g.cols} × ${g.rows} division into equal cells and scale every number above by the same factor.`,
`Size: generate at ${a.width} × ${a.height} px (see Canvas); each cell ships at up to ${cw} × ${ch} px, transparent (STYLE.md §5 and §9).`,
`Must include: ${list(a.include)}`,
`Must avoid: ${list([...a.avoid, ...SHEET_AVOID, ...m.avoid])}`
].join('\n');
}
/** The prompt for one cell of a sprite sheet, made on its own (STYLE.md §9, "One at a time"). */
export function cellPromptFor(a: Asset, cell: Cell, m: Manifest, style: string): string {
const { width, height } = soloCanvas(a);
return [
style,
'',
`Asset: ${cell.id} (one picture of ${a.id}, made on its own)`,
'Kind: a single picture on a transparent background; it becomes one picture in the app.',
`Purpose: ${a.purpose}`,
`Subject: ${subjectLine(a, m, '') || 'Each cell holds its own thing, described in the cell list below.'}`,
`Angle and scale: ${a.angle ?? ''}`,
`This picture: ${cell.does}`,
canvasLine(width, height),
`Must include: ${list(a.include)}`,
`Must avoid: ${list([...a.avoid, ...m.avoid])}`
].join('\n');
}