# Pixel

A ` ```pixel ` fence draws a sprite from **data**: a legend, then a grid of characters. There is no
image file to version, no model and no third-party asset: the same grid gives the same SVG, byte for
byte. In v1 it plays in a film (`motion`) and is drawn still (first frame) on a web story card
(`webstory`); a deck or a page comes later.

## Live demo

<StoryEmbed slug="pixel" format="motion" />

## Syntax

````md title="Markdown"
--- id=hand-drawn type=pixel

```pixel id=sprout alt="Lorem sprout, waving" fps=4 reveal=scan
. = transparent
k = text
g = accent
m = data.mint

...kk...
..kggk..
.kggggk.
..kmmk..
...kk...

...kk...
..kggk.k
.kggggk.
..kmmk..
...kk...
```

Dolor sit amet.
````

The legend comes first, one `char = colour` per line, then a blank line and the grid. More grids,
each after a blank line, are the **frames** of an animation. The paragraph after the fence is the
caption of the scene. `--- type=pixel` is optional: a scene that starts with a ` ```pixel ` fence is
a pixel scene.

## Colours

| Value                                                         | Meaning                                            |
| ------------------------------------------------------------- | -------------------------------------------------- |
| `bg`, `surface`, `text`, `muted`, `accent`                    | colours of the theme: the sprite follows dark mode |
| `data.peach`, `data.mint`, `data.beforeBar`, `data.afterBar`… | the data colours of the motion themes              |
| `#rgb`, `#rrggbb`, `#rrggbbaa`                                | a fixed colour                                     |
| `transparent`                                                 | an empty cell                                      |
| `0`, `1`, `2`…                                                | a colour of the palette named by `palette=`        |

Any other value is an error, with its line.

### Named palettes

`palette=<name>` on the fence lets the legend write a colour by its index in that palette, from `0`.
The index becomes its hex colour when the fence is read, so the typed JSON and the rendered SVG hold
the colour itself. An index without `palette=`, an index past the end of the palette and an unknown
palette are errors, with their line. Indices mix freely with theme tokens and hex colours. A fence
the generator writes holds hex colours already. The `palette=` of a `::pixel` recipe is another key:
it picks one of the generator's presets (`ink`, `moss`, `dusk`, `ember`, `tide`), which colour the
body, shade and outline of a generated sprite or texture, and stay the same in dark mode.

````md title="Markdown"
```pixel id=chip alt="Lorem chip" palette=green-4
. = transparent
k = 0
g = 2
a = accent

.kkkk.
kggggk
kgaagk
.kkkk.
```
````

| Palette      | Colours | What it is                                                   |
| ------------ | ------- | ------------------------------------------------------------ |
| `grey-4`     | 4       | black, two greys, white, evenly spaced                       |
| `green-4`    | 4       | the four greens of an early handheld screen, darkest first   |
| `teletext-8` | 8       | black, red, green, yellow, blue, magenta, cyan, white        |
| `cga-16`     | 16      | the sixteen colours of the IBM CGA adapter, in its own order |

These are colour sets fixed by a hardware or a broadcast standard. Palettes drawn by an artist come
later, once their licence is checked. In code, `PIXEL_PALETTES` and `pixelPaletteColor` of
`@recit/components/pixel` hold them.

### Pixel text

`pixelText(text, options)` of `@recit/components/pixel` draws a text in Récit's own pixel font, as a
grid and its legend ready for `pixelToFence`. Each glyph takes 3 × 5 cells: the capitals, the digits
and `. , ! ? - : ' /`. A lowercase letter is drawn as its capital and an accented letter as its base
letter (`é` as `E`). Lines split on a line break and are padded to the widest. The font was drawn
for Récit, so it needs no licence line in `NOTICE.md`.

| Option        | Default | What it does                                  |
| ------------- | ------- | --------------------------------------------- |
| `ink`         | `k`     | the legend key of an inked cell, never `.`    |
| `color`       | `text`  | the colour of the ink: a theme token or a hex |
| `spacing`     | `1`     | empty columns between two glyphs              |
| `lineSpacing` | `1`     | empty rows between two lines                  |

A character the font cannot draw is an error that names it (`pixelTextUnsupported` lists them
first), and so are an empty text, a spacing that is not a whole number of cells 0 or more, and a
grid wider or taller than 128 cells.

```ts title="TypeScript"
import { pixelText, pixelToFence } from "@recit/components/pixel";

const fence = pixelToFence({ ...pixelText("Lorem", { color: "accent" }), alt: "Lorem" });
```

## Attributes

| Attribute | Type                       | Default | Description                                                                                              |
| --------- | -------------------------- | ------- | -------------------------------------------------------------------------------------------------------- |
| `id`      | `string`                   | —       | Stable id of the image, the same in every language                                                       |
| `alt`     | `string`                   | —       | Text alternative: a warning when missing, an error in strict mode                                        |
| `scale`   | `auto \| number`           | `auto`  | Pixels per cell; `auto` fills the space of the scene                                                     |
| `fps`     | `number`                   | `4`     | Frames per second of the frames                                                                          |
| `reveal`  | `scan \| dissolve \| none` | `none`  | Entrance in a film: `scan` draws the rows top to bottom, `dissolve` shows the cells in a scattered order |
| `cycle`   | `string`                   | —       | Legend keys whose colours turn one step per frame at `fps` in a film (`cycle=abc`)                       |
| `palette` | `string`                   | —       | A named palette: the legend may write a colour by its index                                              |
| `caption` | `string`                   | —       | One line under the image (or the paragraph after the fence)                                              |

A film keeps the scene on screen for its reveal and two loops of its frames. Under
`prefers-reduced-motion`, the first frame is shown, fully drawn.

On a web story card (`format: webstory`), a ` ```pixel ` fence or a `::pixel{gen=sprite …}`
directive is drawn still as an inline SVG (the first frame, no animation), on an ordinary `---` card
separator with no `type=pixel`. A missing or blank `alt` raises the `pixel-alt` warning (an error
under `recit validate --strict`) and renders the image decorative:

````md title="card.story.md"
--- id=sprout

```pixel id=sprout alt="Lorem sprout"
. = transparent
k = text
g = accent

...kk...
..kggk..
.kggggk.
...kk...
```

Dolor sit amet.
````

## Errors

Every problem points to its line in the story, and nothing is corrected silently:

- `story.md:24 (pixel): row 3 has 7 cells, expected 8`
- a character missing from the legend, an unknown token, an invalid hex colour;
- frames of different sizes;
- a grid larger than 128 × 128 cells, or more than 32 frames (provisional caps);
- two keys of the same colour: a warning.

## JSON twin

In the typed JSON of a story, the fence stays in the scene `body`, like any Markdown. The props are
validated by `PixelPropsSchema` and `PixelBlockSchema` of `@recit/core/schema`.

## In code

`@recit/components/pixel` holds the pure module: `parsePixelFence`, `pixelPicture` (merged
rectangles), `pixelToSvg`, `pixelToHast`, `pixelToFence`, the remark plugins `remarkPixel` and
`remarkPixelFences`, `createPixelDirective`, and `pixelText` with `PIXEL_FONT` (the pixel font).
`PixelImage` (React, server-rendered, `role="img"`) is exported by `@recit/components`.

See the films on the [images page of motion.recit.dev](https://recit.dev/motion/images).
