Files
agentic-labs-challenge/adaptive-loop/src/ui/svg.ts
T
2026-08-15 19:22:22 +02:00

101 lines
9.3 KiB
TypeScript

// Procedural SVG glyphs for species, fixtures, and pressure icons (SPEC §13).
// Each returns inner SVG markup (shapes) centered at (x,y) at the given scale.
import type { FixtureKind, PressureId } from "../domain/types.ts";
// ---------- Species ----------
/** Producer "Lumiflora": 6-petal diatom/flower around a chloroplast center. Green. */
export function glyphProducer(x: number, y: number, s: number): string {
const petals = [];
for (let i = 0; i < 6; i++) {
const a = (i * 60) * Math.PI / 180;
const px = x + Math.cos(a) * s * 0.5;
const py = y - Math.sin(a) * s * 0.5;
petals.push(
`<ellipse cx="${px.toFixed(1)}" cy="${py.toFixed(1)}" rx="${(s*0.30).toFixed(1)}" ry="${(s*0.16).toFixed(1)}" transform="rotate(${-i*60} ${px.toFixed(1)} ${py.toFixed(1)})" fill="#5fd97a" fill-opacity="0.85"/>`,
);
}
return `<g>${petals.join("")}<circle cx="${x}" cy="${y}" r="${(s*0.22).toFixed(1)}" fill="#bdf56a"/><circle cx="${x}" cy="${y}" r="${(s*0.11).toFixed(1)}" fill="#2e8b3d"/></g>`;
}
/** Grazer "Vellea": segmented crescent with cilia legs + mouth notch. Amber. */
export function glyphGrazer(x: number, y: number, s: number): string {
const cilia: string[] = [];
for (let i = 0; i < 3; i++) {
const t = (i + 1) / 4;
const lx = x - s * 0.45 + t * s * 0.9;
cilia.push(`<line x1="${lx.toFixed(1)}" y1="${y}" x2="${(lx+s*0.06).toFixed(1)}" y2="${(y+s*0.42).toFixed(1)}" stroke="#e6a23c" stroke-width="${(s*0.05).toFixed(1)}"/>`);
cilia.push(`<line x1="${lx.toFixed(1)}" y1="${y}" x2="${(lx-s*0.06).toFixed(1)}" y2="${(y+s*0.42).toFixed(1)}" stroke="#e6a23c" stroke-width="${(s*0.05).toFixed(1)}"/>`);
}
return `<g>
<path d="M ${(x-s*0.5).toFixed(1)} ${y} a ${ (s*0.5).toFixed(1)} ${(s*0.32).toFixed(1)} 0 0 0 ${(s).toFixed(1)} 0 a ${(s*0.34).toFixed(1)} ${(s*0.22).toFixed(1)} 0 0 0 ${(-s).toFixed(1)} 0 z" fill="#f3b34a" stroke="#c97f1e" stroke-width="${(s*0.05).toFixed(1)}"/>
<path d="M ${(x+s*0.42).toFixed(1)} ${(y-s*0.04).toFixed(1)} l ${(s*0.14).toFixed(1)} ${(s*0.10).toFixed(1)} l ${(-s*0.14).toFixed(1)} ${(s*0.10).toFixed(1)} z" fill="#7a4a08"/>
${cilia.join("")}
</g>`;
}
/** Phage: lunar-lander virus (hex capsid head + 6 leg spikes). Pink. */
export function glyphPhage(x: number, y: number, s: number): string {
const legs: string[] = [];
for (let i = 0; i < 6; i++) {
const a = (i * 60 + 30) * Math.PI / 180;
const x1 = x + Math.cos(a) * s * 0.5;
const y1 = y + Math.sin(a) * s * 0.5;
const x2 = x + Math.cos(a) * s * 0.78;
const y2 = y + Math.sin(a) * s * 0.78;
legs.push(`<line x1="${x1.toFixed(1)}" y1="${y1.toFixed(1)}" x2="${x2.toFixed(1)}" y2="${y2.toFixed(1)}" stroke="#ff6fae" stroke-width="${(s*0.07).toFixed(1)}"/><circle cx="${x2.toFixed(1)}" cy="${y2.toFixed(1)}" r="${(s*0.05).toFixed(1)}" fill="#ff6fae"/>`);
}
const hp = [];
for (let i = 0; i < 6; i++) {
const a = (90 - 60 * i) * Math.PI / 180;
hp.push(`${(x + Math.cos(a) * s * 0.45).toFixed(1)},${(y - Math.sin(a) * s * 0.45).toFixed(1)}`);
}
return `<g>${legs.join("")}<polygon points="${hp.join(" ")}" fill="#ff9ec7" stroke="#d63d86" stroke-width="${(s*0.05).toFixed(1)}"/><polygon points="${hp.slice(0,3).join(" ")}" fill="#ffd0e4" fill-opacity="0.7"/></g>`;
}
export function glyphSpecies(id: "producer" | "grazer" | "phage", x: number, y: number, s: number): string {
if (id === "producer") return glyphProducer(x, y, s);
if (id === "grazer") return glyphGrazer(x, y, s);
return glyphPhage(x, y, s);
}
// ---------- Fixtures ----------
export function glyphFixture(kind: FixtureKind, x: number, y: number, s: number): string {
switch (kind) {
case "nutrientInlet": // pipette/valve dropping a bead (blue)
return `<g>
<rect x="${(x-s*0.14).toFixed(1)}" y="${(y-s*0.5).toFixed(1)}" width="${(s*0.28).toFixed(1)}" height="${(s*0.5).toFixed(1)}" rx="${(s*0.05).toFixed(1)}" fill="#3a6ea5" stroke="#8fb8e6"/>
<polygon points="${(x-s*0.14).toFixed(1)},${y} ${(x+s*0.14).toFixed(1)},${y} ${x.toFixed(1)},${(y+s*0.22).toFixed(1)}" fill="#6fa8dc"/>
<circle cx="${x}" cy="${(y+s*0.36).toFixed(1)}" r="${(s*0.12).toFixed(1)}" fill="#9fd0ff"/>
</g>`;
case "heatSource": // coiled filament radiating warm rays (orange)
{ const rays: string[] = []; for (let i=0;i<8;i++){const a=(i*45)*Math.PI/180;rays.push(`<line x1="${(x+Math.cos(a)*s*0.55).toFixed(1)}" y1="${(y-Math.sin(a)*s*0.55).toFixed(1)}" x2="${(x+Math.cos(a)*s*0.8).toFixed(1)}" y2="${(y-Math.sin(a)*s*0.8).toFixed(1)}" stroke="#ff7a3c" stroke-width="${(s*0.05).toFixed(1)}"/>`);}
return `<g>${rays.join("")}<path d="M ${(x-s*0.3).toFixed(1)} ${y} q ${(s*0.3).toFixed(1)} ${(-s*0.3).toFixed(1)} ${(s*0.6).toFixed(1)} 0 q ${(s*0.3).toFixed(1)} ${(s*0.3).toFixed(1)} ${(s*0.6).toFixed(1)} 0" fill="none" stroke="#ff9a5c" stroke-width="${(s*0.09).toFixed(1)}"/><circle cx="${x}" cy="${y}" r="${(s*0.13).toFixed(1)}" fill="#ffb347"/></g>`; }
case "toxinInlet": // dropper of greenish droplet
return `<g><polygon points="${(x-s*0.22).toFixed(1)},${(y-s*0.4).toFixed(1)} ${(x+s*0.22).toFixed(1)},${(y-s*0.4).toFixed(1)} ${x.toFixed(1)},${(y-s*0.1).toFixed(1)}" fill="#6b8f3a" stroke="#a7d977"/><path d="M ${(x-s*0.12).toFixed(1)} ${(y-s*0.42).toFixed(1)} l ${(s*0.24).toFixed(1)} 0 l 0 ${(-s*0.18).toFixed(1)} l ${(-s*0.24).toFixed(1)} 0 z" fill="#8a6fbf"/><circle cx="${x}" cy="${(y+s*0.18).toFixed(1)}" r="${(s*0.15).toFixed(1)}" fill="#9fe06a" stroke="#5e8a2e" stroke-width="${(s*0.04).toFixed(1)}"/></g>`;
case "drain": // funnel siphon
return `<g><polygon points="${(x-s*0.34).toFixed(1)},${(y-s*0.34).toFixed(1)} ${(x+s*0.34).toFixed(1)},${(y-s*0.34).toFixed(1)} ${x.toFixed(1)},${(y+s*0.05).toFixed(1)}" fill="#445" stroke="#7d8aa0"/><rect x="${(x-s*0.07).toFixed(1)}" y="${(y+s*0.05).toFixed(1)}" width="${(s*0.14).toFixed(1)}" height="${(s*0.3).toFixed(1)}" fill="#6a7588"/><path d="M ${(x-s*0.26).toFixed(1)} ${(y-s*0.32).toFixed(1)} q ${(s*0.26).toFixed(1)} ${(s*0.16).toFixed(1)} ${(s*0.52).toFixed(1)} 0" fill="none" stroke="#9fe06a" stroke-width="${(s*0.06).toFixed(1)}"/></g>`;
case "immuneNode": // sentinel eye/lens (cyan)
return `<g><ellipse cx="${x}" cy="${y}" rx="${(s*0.4).toFixed(1)}" ry="${(s*0.24).toFixed(1)}" fill="#10303a" stroke="#5fd0ff" stroke-width="${(s*0.05).toFixed(1)}"/><circle cx="${x}" cy="${y}" r="${(s*0.16).toFixed(1)}" fill="#5fd0ff"/><circle cx="${(x-s*0.05).toFixed(1)}" cy="${(y-s*0.05).toFixed(1)}" r="${(s*0.06).toFixed(1)}" fill="#eafdff"/></g>`;
case "objective": // targeting reticle (gold)
return `<g><circle cx="${x}" cy="${y}" r="${(s*0.42).toFixed(1)}" fill="none" stroke="#ffd24a" stroke-width="${(s*0.05).toFixed(1)}" stroke-dasharray="${(s*0.14).toFixed(1)} ${(s*0.1).toFixed(1)}"/><circle cx="${x}" cy="${y}" r="${(s*0.10).toFixed(1)}" fill="#ffd24a"/><line x1="${(x-s*0.55).toFixed(1)}" y1="${y}" x2="${(x-s*0.2).toFixed(1)}" y2="${y}" stroke="#ffd24a" stroke-width="${(s*0.04).toFixed(1)}"/><line x1="${(x+s*0.2).toFixed(1)}" y1="${y}" x2="${(x+s*0.55).toFixed(1)}" y2="${y}" stroke="#ffd24a" stroke-width="${(s*0.04).toFixed(1)}"/><line x1="${x}" y1="${(y-s*0.55).toFixed(1)}" x2="${x}" y2="${(y-s*0.2).toFixed(1)}" stroke="#ffd24a" stroke-width="${(s*0.04).toFixed(1)}"/><line x1="${x}" y1="${(y+s*0.2).toFixed(1)}" x2="${x}" y2="${(y+s*0.55).toFixed(1)}" stroke="#ffd24a" stroke-width="${(s*0.04).toFixed(1)}"/></g>`;
}
}
// ---------- Pressure icons ----------
export function glyphPressure(p: PressureId, x: number, y: number, s: number): string {
switch (p) {
case "heat": { // radiating sun rays (warm)
const r: string[] = []; for (let i=0;i<8;i++){const a=(i*45)*Math.PI/180;r.push(`<line x1="${x}" y1="${y}" x2="${(x+Math.cos(a)*s*0.5).toFixed(1)}" y2="${(y-Math.sin(a)*s*0.5).toFixed(1)}" stroke="#ff8a4c" stroke-width="${(s*0.06).toFixed(1)}"/>`);}
return `<g>${r.join("")}<circle cx="${x}" cy="${y}" r="${(s*0.2).toFixed(1)}" fill="#ff8a4c"/></g>`; }
case "hunger": // waning crescent (violet)
return `<g><path d="M ${(x+s*0.28).toFixed(1)} ${y} a ${(s*0.4).toFixed(1)} ${(s*0.4).toFixed(1)} 0 1 0 0 ${(s*0.001).toFixed(1)} z" fill="#b388ff"/><path d="M ${(x+s*0.28).toFixed(1)} ${y} a ${(s*0.28).toFixed(1)} ${(s*0.28).toFixed(1)} 0 1 1 0 ${(s*0.001).toFixed(1)} z" fill="#0d1117"/></g>`;
case "toxin": // skull-and-drop (green)
return `<g><path d="M ${(x-s*0.18).toFixed(1)} ${(y+s*0.34).toFixed(1)} q ${(s*0.18).toFixed(1)} ${(-s*0.1).toFixed(1)} ${(s*0.36).toFixed(1)} 0 q ${(-s*0.05).toFixed(1)} ${(s*0.18).toFixed(1)} ${(-s*0.36).toFixed(1)} 0 z" fill="#7fd66b"/><circle cx="${(x-s*0.07).toFixed(1)}" cy="${(y-s*0.08).toFixed(1)}" r="${(s*0.10).toFixed(1)}" fill="#0d1117"/><circle cx="${(x+s*0.07).toFixed(1)}" cy="${(y-s*0.08).toFixed(1)}" r="${(s*0.10).toFixed(1)}" fill="#0d1117"/></g>`;
case "immunity": // shield (cyan)
return `<g><path d="M ${x} ${(y-s*0.4).toFixed(1)} l ${(s*0.34).toFixed(1)} ${(s*0.12).toFixed(1)} l 0 ${(s*0.26).toFixed(1)} q 0 ${(s*0.22).toFixed(1)} ${(-s*0.34).toFixed(1)} ${(s*0.14).toFixed(1)} q ${(-s*0.34).toFixed(1)} ${(-s*0.22).toFixed(1)} 0 ${(-s*0.14).toFixed(1)} z" fill="#5fd0ff" fill-opacity="0.5" stroke="#5fd0ff" stroke-width="${(s*0.05).toFixed(1)}"/><path d="M ${(x-s*0.12).toFixed(1)} ${y} l ${(s*0.10).toFixed(1)} ${(s*0.12).toFixed(1)} l ${(s*0.18).toFixed(1)} ${(-s*0.2).toFixed(1)}" fill="none" stroke="#0d1117" stroke-width="${(s*0.06).toFixed(1)}"/></g>`;
}
}