---
name: qbeka-smooth-gradients
title: smooth gradients
kind: skill
description: >
  Make gradients that render without banding, steps or blocks, on screen and in
  exported video. Use whenever you build or touch a gradient, glow, mesh/aurora
  background, blurred blob, vignette, fade or dark hero section, or when you
  render, record or encode a demo, product video, GIF or animation that contains
  one. Also use when someone says a gradient looks choppy, banded, stepped,
  striped, blocky, posterized or 'has rings'. Covers Oklab mixing, kink-free
  multi-stop curves, dithering (canvas, CSS grain, shaders),
  ffmpeg/Remotion/screen-recording settings that keep gradients smooth, and a
  checke
updated: 2026-12-05
authored_by: qbeka
author_url: https://github.com/qbeka
source_url: https://github.com/qbeka/skills/blob/main/smooth-gradients/SKILL.md
brought_by: kt
license: MIT
---

# smooth-gradients

Paths below are relative to this skill's folder.

A gradient is only smooth if the pixels people end up seeing are smooth. Every
step from the color math to the last video encode can add bands, so the job
ends with measuring the output, not with writing the CSS.

## Why AI gradients look choppy

| Cause | What you see |
|---|---|
| 8 bits per channel, no dither | Flat stripes. Dark, low-contrast, full-screen gradients are worst: `#0a0a14 → #1a1a2e` over a 1440px retina hero has 59 colors, so each band is ~115 device px wide. |
| Mixing in sRGB | A gray or muddy middle between saturated colors. |
| Linear segments between stops | A visible line at each stop where the slope changes (Mach bands). |
| `filter: blur()` blobs | Rings. No browser dithers blur output. |
| Firefox CSS gradients | Bands. Chrome and WebKit dither CSS gradients, Firefox did not in testing. |
| Video encoders | They treat fine dither as noise, delete it, and the bands come back as flat blocks. Default x264 turns a clean dithered gradient into 50% banded area. |
| Wrong YUV matrix | ffmpeg converts RGB with BT.601 by default, players decode as BT.709: shifted, washed-out darks. |
| JPEG frames, GIF palettes, `mediump` shaders, canvases not scaled by DPR | Blocks and steps. |

## Workflow

### 1. Plan the gradient

```bash
node scripts/css_gradient.mjs --stops "#0a0a14 0, #2a1f5c 0.6, #5b3fd1 1" --span 1080 --dpr 2 --angle 180
```

It prints the widest flat band in device pixels, a verdict, and CSS with
dense stops sampled along an Oklab monotone-cubic curve. The dense stops work
in every browser, no `in oklab` support needed, and have no kinks at stops.

Design choices that prevent banding before any fix:
- Mix in Oklab (the planner and painter do).
- Let hue shift a little along the gradient, not only lightness. When R, G
  and B change at different rates, there are more distinct colors and the
  bands get narrower.
- Avoid very dark, very low-contrast gradients over large areas, above all in
  video. Raise the darkest stop or widen the range.

### 2. Render it smooth

Pick by where it will be seen:

| Target | Use |
|---|---|
| Static web background | `scripts/smooth-gradient.js`. Paints a dithered canvas: exact in every browser. Linear, radial, conic, and `mesh` (soft color points, the right replacement for blurred blobs). |
| Must stay pure CSS | Planner CSS plus `assets/grain.css` on the background layer. Verified band-free in Chromium, Firefox and WebKit, including blurred blobs. |
| Animated (WebGL/WebGPU/three.js) | Dither in the shader: `references/shaders.md`. |
| Video or demo render | Bake in grain of +-2 levels (painter `dither: 2`, or `encode.sh --grain 2`), then encode as in step 3. |

```js
import { mountGradient } from './smooth-gradient.js';
mountGradient(document.querySelector('.hero-bg'), {
  type: 'mesh', base: '#0a0a14',
  points: [
    { at: [0.25, 0.3], color: '#3b2a9c', radius: 0.6 },
    { at: [0.8, 0.7], color: '#0f4c75', radius: 0.55 },
  ],
});
```

Options are documented at the top of the file. It repaints on resize; a
linear, radial or conic paint took 10-65 ms at 1440x900 in testing.

`grain.css` goes on a decorative layer, not on an element that holds text:

```html
<section class="hero">
  <div class="hero-bg grain" aria-hidden="true"><!-- gradient or blobs --></div>
  <h1>...</h1>
</section>
```

### 3. Export and encode without losing it

```bash
scripts/encode.sh INPUT OUTPUT --target web-h264 --grain 2
```

| `--target` | Codec | Use for |
|---|---|---|
| `web-av1` | AV1 10-bit + film-grain synthesis | Smallest by far. Chrome, Firefox, Edge; Safari only with AV1 hardware. |
| `web-hevc` | HEVC 10-bit, tune grain | Safari. |
| `web-h264` | H.264, tuned AQ and deblock | Plays everywhere. Default. |
| `upload` | H.264 CRF 12 | YouTube, X, LinkedIn. Add `--grain 4 --grain-size 2`: lighter grain gets wiped out by their re-encode. |
| `master` | ProRes 422 HQ | Editing. |
| `gif` | Full-stats palette, error diffusion | Only when GIF is required. Prefer a muted, looping MP4. |

The script always tags BT.709 and converts with the right matrix. INPUT can be
a video or a sequence like `frames/%05d.png` (`--fps`).

Rules behind the presets, all measured (see `references/evidence.md`):
- Grain must be at least +-2 levels to survive H.264, and +-4 at 2px cells to
  survive a platform re-encode. +-1 is enough only for screens.
- Keep the grain pattern still across frames. Moving grain cost 1.7x the
  bitrate for the same result.
- Use mono grain. 4:2:0 video throws away chroma noise.
- AV1 film-grain synthesis only works if the source has grain. On an
  already-banded source it fails like everything else.
- 10-bit alone does not fix banding when the source is an 8-bit render.

Tool-specific settings:
- **Remotion:** render with `--image-format=png` (the default JPEG frames add
  blocks), ideally `--codec=prores --prores-profile=hq`, then run `encode.sh`
  on that master.
- **Screen recordings (OBS, QuickTime, Screen Studio):** record at the highest
  quality available (OBS: CRF/CQP 16 or lower). The recording is already 8-bit,
  so encode it with `--grain 2`.
- **ffmpeg by hand:** never leave the RGB-to-YUV conversion to defaults; copy
  the `TO709` line from `encode.sh`.

### 4. Measure the output (required)

```bash
python3 scripts/band_check.py shot.png            # screenshot or exported image
python3 scripts/band_check.py out.mp4 --frames 8  # decodes frames from the final file
python3 scripts/band_check.py shot.png --heatmap bands.png   # bands in red
```

Exit code 0 is PASS. Check the file people will actually get: a
device-resolution screenshot of the page, the encoded video, the GIF.
Not the source. If it fails, open the heatmap, fix the stage that adds the
bands, and check again. Do not report a gradient as done until this passes.

It looks for staircases: three or more flat runs in a row, a few levels apart,
stepping the same way, and consistent across neighboring lines. Random dither
and flat UI do not trigger it. It also reports `blockiness` (codec block
edges; FAIL above 1.4). Use `--region x,y,w,h` to check only the gradient.

Screenshots: Playwright or headless Chrome at the real DPR:

```bash
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --headless=new \
  --hide-scrollbars --force-device-scale-factor=2 --window-size=1440,900 \
  --virtual-time-budget=3000 --screenshot=shot.png http://localhost:3000
```

Check Firefox too when the page uses plain CSS gradients without the grain.

## Blocky rather than banded?

- Canvas sized in CSS px on a retina screen: multiply width and height by
  `devicePixelRatio` (the painter does).
- Shader without `precision highp float`, or an 8-bit render target in the
  middle of a post chain.
- JPEG frames somewhere in the pipeline (Remotion default, screenshots).
- Bitrate too low, or `blockiness` > 1.4: lower the CRF or use `web-av1`.
- Scaling a dithered 8-bit frame afterward averages the dither away. Render
  at the final size, or add grain after scaling (`encode.sh` does).
