Repository navigation
feat: img element — public surface, validation, and test suite #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4231e64
feat: img element — public surface, validation, and test suite
natemoo-re d5b3ddc
test: large transmissions arrive whole within the output buffer
natemoo-re 2581387
examples: img tier ladder demo
natemoo-re df5139d
test: kitty continuation framing and art under a declared bg
natemoo-re File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| --- | ||
| "@bomb.sh/tty": minor | ||
| --- | ||
|
|
||
| Adds the `img()` directive — declarative images rendered through a fidelity ladder: kitty graphics protocol pixels, shape-aware ASCII character art, or alt text, resolved from capability evidence (`RuntimeCapabilities.kittyGraphics`). | ||
|
|
||
| Surfaces are caller-owned decoded RGBA buffers managed by the Term: | ||
|
|
||
| ```ts | ||
| term.setImage(1, { width: 128, height: 64, pixels: rgba8 }); | ||
| term.render([img("cover", { image: 1, alt: "cover art" })]); | ||
| term.removeImage(1); // the next render deletes it from the terminal | ||
| ``` | ||
|
|
||
| `createTerm` takes an `imagePoolBytes` option (default 4 MiB) sizing the fixed pixel pool. Transmission is lazy; re-transmission only on data change; steady-state renders are byte-silent. Like `update()`, `removeImage()` returns nothing: cleanup after removal, capability denial, or resize is emitted at the start of the next render. `bg` accepts any color value `open({ bg })` does. Line mode caps the ladder at ascii. Errors surface as `IMAGE_NOT_FOUND`, `IMAGE_PLACEMENTS_EXCEEDED`, and `IMAGE_PLACEMENT_COLLISION`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // Generated in-process: the renderer consumes decoded RGBA only | ||
| // (graphics-spec INV-I1), so the demo performs no IO. | ||
| // | ||
| // sphere smooth gradients and a specular highlight | ||
| // checker saturated hues with round alpha holes | ||
| // plasma re-set through setImage() every tick (graphics-spec §4.4) | ||
|
|
||
| export interface Image { | ||
| width: number; | ||
| height: number; | ||
| pixels: Uint8Array; // RGBA8, straight alpha | ||
| } | ||
|
|
||
| export type ImageId = 1 | 2 | 3; | ||
|
|
||
| export function sphere(size = 64): Image { | ||
| let pixels = new Uint8Array(size * size * 4); | ||
| let light = norm3(-0.45, -0.6, 0.66); | ||
| for (let y = 0; y < size; y++) { | ||
| for (let x = 0; x < size; x++) { | ||
| let u = ((x + 0.5) / size) * 2 - 1; | ||
| let v = ((y + 0.5) / size) * 2 - 1; | ||
| let i = (y * size + x) * 4; | ||
| if (u * u + v * v > 1) { | ||
| drawBackdrop(pixels, i, x, y); | ||
| continue; | ||
| } | ||
| let z = Math.sqrt(1 - u * u - v * v); | ||
| let diff = Math.max(0, u * light[0] + v * light[1] + z * light[2]); | ||
| let spec = Math.pow(diff, 48) * 0.9; | ||
| let rim = Math.pow(1 - z, 3) * 0.35; | ||
| let r = 60 * (0.25 + 0.75 * diff) + 255 * spec + 20 * rim; | ||
| let g = 120 * (0.25 + 0.75 * diff) + 255 * spec + 150 * rim; | ||
| let b = 220 * (0.25 + 0.75 * diff) + 255 * spec + 180 * rim; | ||
| pixels[i] = clamp8(r); | ||
| pixels[i + 1] = clamp8(g); | ||
| pixels[i + 2] = clamp8(b); | ||
| pixels[i + 3] = 255; | ||
| } | ||
| } | ||
| return { width: size, height: size, pixels }; | ||
| } | ||
|
|
||
| export function checker(size = 64): Image { | ||
| let pixels = new Uint8Array(size * size * 4); | ||
| let hues = [ | ||
| [226, 60, 60], | ||
| [60, 178, 72], | ||
| [66, 118, 230], | ||
| [240, 200, 48], | ||
| [184, 78, 204], | ||
| [48, 200, 210], | ||
| ]; | ||
| let cell = size / 8; | ||
| for (let y = 0; y < size; y++) { | ||
| for (let x = 0; x < size; x++) { | ||
| let i = (y * size + x) * 4; | ||
| let cx = Math.floor(x / cell); | ||
| let cy = Math.floor(y / cell); | ||
| let [r, g, b] = hues[(cx + cy * 3) % hues.length]; | ||
| // every other diagonal cell carries a round alpha hole (soft edge) | ||
| let hole = (cx + cy) % 2 === 1; | ||
| let a = 255; | ||
| if (hole) { | ||
| let du = (x % cell) / cell - 0.5; | ||
| let dv = (y % cell) / cell - 0.5; | ||
| let d = Math.sqrt(du * du + dv * dv) * 2; | ||
| a = clamp8((0.72 - d) * 8); | ||
| a = Math.round(a * 2.55); | ||
| } | ||
| pixels[i] = r; | ||
| pixels[i + 1] = g; | ||
| pixels[i + 2] = b; | ||
| pixels[i + 3] = a; | ||
| } | ||
| } | ||
| return { width: size, height: size, pixels }; | ||
| } | ||
|
|
||
| export function plasma(size = 64, t = 0): Image { | ||
| let pixels = new Uint8Array(size * size * 4); | ||
| // palette stops: deep blue → violet → orange → pale yellow | ||
| let stops = [ | ||
| [12, 16, 48], | ||
| [90, 40, 160], | ||
| [230, 120, 40], | ||
| [255, 230, 140], | ||
| ]; | ||
| for (let y = 0; y < size; y++) { | ||
| for (let x = 0; x < size; x++) { | ||
| let i = (y * size + x) * 4; | ||
| let v = Math.sin(x * 0.11 + t) + | ||
| Math.sin(y * 0.13 + t * 1.31) + | ||
| Math.sin((x + y) * 0.09 + t * 0.77) + | ||
| Math.sin(Math.sqrt(x * x + y * y) * 0.14 - t * 0.53); | ||
| let f = (v + 4) / 8; // [0, 1) | ||
| let seg = Math.min(2.999, f * 3); | ||
| let k = Math.floor(seg); | ||
| let mix = seg - k; | ||
| let [r0, g0, b0] = stops[k]; | ||
| let [r1, g1, b1] = stops[k + 1]; | ||
| pixels[i] = clamp8(r0 + (r1 - r0) * mix); | ||
| pixels[i + 1] = clamp8(g0 + (g1 - g0) * mix); | ||
| pixels[i + 2] = clamp8(b0 + (b1 - b0) * mix); | ||
| pixels[i + 3] = 255; | ||
| } | ||
| } | ||
| return { width: size, height: size, pixels }; | ||
| } | ||
|
|
||
| function drawBackdrop( | ||
| pixels: Uint8Array, | ||
| i: number, | ||
| x: number, | ||
| y: number, | ||
| ): void { | ||
| // near-black with a faint grid so the sphere's silhouette reads | ||
| let grid = x % 8 === 0 || y % 8 === 0; | ||
| pixels[i] = grid ? 62 : 44; | ||
| pixels[i + 1] = grid ? 62 : 44; | ||
| pixels[i + 2] = grid ? 76 : 52; | ||
| pixels[i + 3] = 255; | ||
| } | ||
|
|
||
| function clamp8(v: number): number { | ||
| return v < 0 ? 0 : v > 255 ? 255 : Math.round(v); | ||
| } | ||
|
|
||
| function norm3(x: number, y: number, z: number): [number, number, number] { | ||
| let n = Math.sqrt(x * x + y * y + z * z); | ||
| return [x / n, y / n, z / n]; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changeset needs revision.
Changeset package scope does not match the affected packages confidently.
View the proposed replacement