# Colors, rects and the stage


Three small types run through the whole library: a color is a number, a point is two floats, and a rect is a corner and a size. This page covers them, the helpers that go with them, and the stage they are all measured against.

## Colors

```odin
Color :: u32 // 0xAARRGGBB
```

A color is a 32-bit number written as eight hex digits: alpha first, then red, green and blue. `0xffdd5b3e` is an opaque orange, `0x80ffffff` white at 50%, `0x24f2f0e6` a hairline at 14%. Colors are not premultiplied: the red, green and blue of a translucent color are the same as those of the opaque one.

> **Note:** The alpha comes first and is part of the number. A color copied from CSS as `0xdd5b3e` has an alpha of 0 and draws nothing: write `0xffdd5b3e` or `fx.rgb(0xdd5b3e)`.

```odin
rgb        :: proc "contextless" (hex: u32) -> Color
alpha      :: proc(c: Color, a: f32) -> Color
with_alpha :: proc(c: Color, a: f32) -> Color
mix        :: proc(a, b: Color, p: f32) -> Color
```

| Procedure | Returns |
| --- | --- |
| `rgb(hex)` | The opaque color for a six-digit `0xRRGGBB`. |
| `alpha(c, a)` | `c` with its alpha multiplied by `a` (0..1). A color that is already translucent gets more so. This is the one for fading. |
| `with_alpha(c, a)` | `c` with its alpha replaced by `a` (0..1), whatever it was. |
| `mix(a, b, p)` | The color `p` of the way from `a` to `b`, `p` clamped to 0..1. Alpha, red, green and blue are each interpolated on their own. |

The difference between `alpha` and `with_alpha` matters with the theme's colors, several of which are translucent to begin with: the theme's `nm` is the text color at 72%, so `fx.alpha(nm, 0.5)` is 36% and `fx.with_alpha(nm, 0.5)` is 50%.

```odin
color_mix :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	a, b := fx.rgb(0x3d7bd9), fx.theme.na
	p := 0.5 - 0.5 * math.cos(s.p * math.TAU) // 0 → 1 → 0
	for i in 0 ..< 9 do fx.rrect({168 + f32(i) * 180, 130, 144, 144}, 28, fx.mix(a, b, f32(i) / 8))
	c := fx.mix(a, b, p)
	fx.circle({240 + 1440 * p, 340}, 28, c)
	fx.label(fmt.tprintf("fx.mix(a, b, %.2f) = 0x%08x", p, c), {168, 410}, fx.mono(40), fx.theme.nt)

	// alpha scales the alpha a color already has, with_alpha replaces it
	nm := fx.theme.nm // 0xb8f2f0e6: the text color at 72%
	rows := [?]struct {code: string, c: fx.Color} {
		{"nm", nm},
		{fmt.tprintf("fx.alpha(nm, %.2f)", p), fx.alpha(nm, p)},
		{fmt.tprintf("fx.with_alpha(nm, %.2f)", p), fx.with_alpha(nm, p)},
	}
	for row, i in rows {
		x := 168 + f32(i) * 540
		fx.rrect({x, 600, 504, 150}, 28, row.c)
		fx.label(row.code, {x, 780}, fx.mono(32), fx.theme.nm)
		fx.label(fmt.tprintf("0x%08x", row.c), {x, 830}, fx.mono(32), fx.theme.nt)
	}
}
```

`unpack(c)` goes the other way, to four floats in 0..1 (`RGBA{r, g, b, a}`), which is the form a [shader's uniforms](/docs/post/) take.

A few arguments in the library use the color 0 (fully transparent black) to mean "none" or "the theme's default", for example `Xf.tint`. To multiply by black there, write `0xff000000`.

## Points and rects

```odin
Vec2 :: [2]f32

Rect :: struct {
	x, y, w, h: f32,
}
```

A `Vec2` is a point or an offset in px. Being an Odin array it does arithmetic by component and has `.x` and `.y`: `c + {40, 0}` is 40 px to the right of `c`, `(a + b) / 2` is halfway between two points, and `fx.lerp2(a, b, p)` any fraction of the way (see [Animation](/docs/anim/#progress)).

A `Rect` is its top-left corner and its size, in px. Almost everything that takes up room takes a `Rect`, and the helpers derive one rect from another so that a scene needs few hard numbers:

```odin
inset     :: proc(r: Rect, d: f32) -> Rect
inset4    :: proc(r: Rect, top, right, bottom, left: f32) -> Rect
center    :: proc(r: Rect) -> Vec2
contains  :: proc(r: Rect, p: Vec2) -> bool
lerp_rect :: proc(a, b: Rect, p: f32) -> Rect
contain   :: proc(r: Rect, aw, ah: f32) -> Rect
```

| Procedure | Returns |
| --- | --- |
| `inset(r, d)` | `r` made smaller by `d` px on every side. A negative `d` grows it: `fx.inset(box, -24)` is a frame with 24 px of padding around `box`. |
| `inset4(r, top, right, bottom, left)` | The same with a distance per side, in CSS order. |
| `center(r)` | The middle of `r`. |
| `contains(r, p)` | Whether point `p` is inside `r`. The left and top edges count as inside, the right and bottom ones do not. |
| `lerp_rect(a, b, p)` | The rect `p` of the way from `a` to `b`: position and size are each interpolated. `p` is not clamped, so an ease that overshoots carries through. |
| `contain(r, aw, ah)` | The largest rect with the proportions `aw`:`ah` that fits into `r`, centered, like CSS `object-fit: contain`. |

`lerp_rect` with an eased progress is how one thing becomes another: a thumbnail growing into a full-screen picture, a highlight moving from one row to the next.

```odin
color_rects :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	a, b := fx.Rect{200, 200, 520, 680}, fx.Rect{860, 300, 860, 400}
	p := fx.ep(s.lt, 0.4, 2.2, fx.ease_in_out) - fx.ep(s.lt, 2.9, 4.7, fx.ease_in_out)
	fx.border(a, fx.theme.nb, 2)
	fx.border(b, fx.theme.nb, 2)
	r := fx.lerp_rect(a, b, p) // every field of a on its way to b
	fx.rect(r, fx.theme.night2)
	fx.border(r, fx.theme.nt, 3)
	pad := fx.inset(r, 40) // 40 px in from every side
	fx.border(pad, fx.theme.ns, 2)
	fit := fx.contain(pad, 16, 9) // the largest 16:9 box in pad, centered
	fx.rect(fit, fx.theme.na)
	fx.circle(fx.center(r), 12, fx.theme.nt)
	fx.label("contain(pad, 16, 9)", {fit.x + 16, fit.y + 12}, fx.mono(30), fx.theme.night)
	fx.label("lerp_rect(a, b, p)", {r.x, r.y - 50}, fx.mono(32), fx.theme.nm)
}
```

## The stage

A video is designed on a stage of fixed size, 1920×1080 unless the project says otherwise, and every number you pass to `fx` is in stage px: `{0, 0}` is the top-left corner, x grows to the right, y grows down.

```odin
stage  :: proc() -> Rect
now    :: proc() -> f32
canvas :: proc() -> ^sk.Canvas
```

`stage()` returns the stage as a rect, `{0, 0, 1920, 1080}` by default. Deriving positions from it (`fx.center(fx.stage())`, `fx.inset(fx.stage(), 96)`) keeps a scene correct if the size changes.

`now()` is the video time of the frame being drawn, in seconds: the same value a scene receives as `s.t`. It is for helpers that are called from scenes and should not need the time handed down to them.

```odin
color_stage :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	st := fx.stage() // {0, 0, 1920, 1080}, whatever size the frame is rendered at
	f := fx.mono(34)
	fx.border(st, fx.theme.ns, 4) // the edge of the picture
	fx.label("{0, 0}", {st.x + 36, st.y + 28}, f, fx.theme.nm)
	fx.label(fmt.tprintf("{{%.0f, %.0f}}", st.w, st.h), {st.w - 36, st.h - 28}, f, fx.theme.nm, {1, 1})
	fx.label(fmt.tprintf("fx.now() = %.2f s", fx.now()), {st.w - 36, st.y + 28}, f, fx.theme.nm, {1, 0})

	zone := fx.Rect{1140, 560, 480, 300}
	p := fx.center(st) + {660 * math.sin(s.p * math.TAU), 300 * math.sin(2 * s.p * math.TAU)}
	inside := fx.contains(zone, p)
	fx.rect(zone, inside ? fx.alpha(fx.theme.na, 0.25) : fx.theme.night2)
	fx.border(zone, inside ? fx.theme.na : fx.theme.nb, 2)
	fx.label("zone", {zone.x + 20, zone.y + 14}, f, fx.theme.nm)
	fx.circle(p, 20, fx.theme.nt)
	fx.label(fmt.tprintf("{{%.0f, %.0f}}", p.x, p.y), p + {34, 0}, f, fx.theme.nt, {0, 0.5})
}
```

### Rendering at other sizes

The stage is a design size, not an output size. A renderer that wants more or fewer pixels scales the canvas, and the scene draws exactly as before:

```bash
build/effects render projects/spot              # 1920×1080
build/effects render projects/spot --scale 2    # 3840×2160
build/effects still projects/spot 2.5 --scale 0.5
```

The output is the stage times `--scale`, rounded to even pixel counts. Shapes and text are rasterized at the output size, so a render at twice the scale is twice as sharp, not an enlargement. What is made of pixels (an [image](/docs/draw/#images), [footage](/docs/footage/), a [baked layer](/docs/draw/#baked-layers)) is as sharp as its source. The editor's preview and the players on these pages are the same thing: the 1920×1080 stage drawn smaller.

A project with another format sets its own stage in `video`, and `stage()` returns that:

```odin
fx.video({name = "Story", width = 1080, height = 1920, scenes = SCENES[:]})
```

### The canvas

`canvas()` is the Skia canvas of the frame, for the calls the `sk` package has and `fx` does not wrap, such as an oval or a clip to a path:

```odin
cv := fx.canvas()
p := fx.fill(fx.theme.na)
sk.canvas_draw_oval(cv, 100, 100, 400, 200, &p) // x, y, w, h of the oval's box

sk.canvas_save(cv)
sk.canvas_clip_path(cv, star) // a ^sk.Path
fx.image(img, {0, 0, 24, 24})
sk.canvas_restore(cv)         // every save needs its restore
```

Ask for it where you use it instead of keeping it: inside a [baked layer](/docs/draw/#baked-layers) it is the layer's canvas, not the frame's.

The state behind these accessors is the variable `S`, one per render thread. Two of its fields are worth knowing: `fx.S.scale` is the number of output pixels per stage pixel for the frame being drawn (1 for a normal render, 2 with `--scale 2`), and `fx.S.preview` is true when the frame is drawn for the editor's preview instead of a final render.

## Tilt matrix

```odin
tilt_matrix :: proc(origin: Vec2, rx, ry, rz, persp: f32) -> [16]f32
```

The 4×4 matrix, row-major, of a CSS-like perspective tilt: a rotation by `rz`, then `ry`, then `rx` (degrees) around `origin`, seen from `persp` px away (0 or less means 1600). It is what a [group](/docs/group/#3d-tilt) uses for its `rx` and `ry`, and you only need it yourself when you apply a tilt to the canvas directly:

```odin
m := fx.tilt_matrix({1500, 540}, 0, 50, 0, 1600)
cv := fx.canvas()
sk.canvas_save(cv)
sk.canvas_concat44(cv, &m)
fx.border({1300, 340, 400, 400}, fx.theme.na, 6)
sk.canvas_restore(cv)
```

## Reference

| Name | What it is |
| --- | --- |
| `Color` | A `u32`, `0xAARRGGBB`. |
| `rgb(hex)` | Opaque color from `0xRRGGBB`. |
| `alpha(c, a)` | `c` with its alpha multiplied by `a`. |
| `with_alpha(c, a)` | `c` with its alpha set to `a`. |
| `mix(a, b, p)` | The color between `a` and `b`. |
| `Vec2` | `[2]f32`, a point or an offset in px. |
| `Rect` | `{x, y, w, h}` in px. |
| `inset(r, d)`, `inset4(r, top, right, bottom, left)` | `r` shrunk (or, negative, grown). |
| `center(r)` | The middle of `r`. |
| `contains(r, p)` | Whether `p` is in `r`. |
| `lerp_rect(a, b, p)` | The rect between `a` and `b`. |
| `contain(r, aw, ah)` | An `aw`:`ah` box fitted into `r`. |
| `stage()` | The design stage as a rect. |
| `now()` | The frame's video time in seconds. |
| `canvas()` | The Skia canvas being drawn to. |
| `S` | The per-thread frame state (`Frame_State`) the three above read. |
| `tilt_matrix(origin, rx, ry, rz, persp)` | The matrix of a perspective tilt. |
