# Dark mode PDFs without inverting colors.

> Naive RGB inversion turns crimson attention plots into electric cyan and turns embedded photographs into negatives. Here is the exact perceptual pipeline hoja runs in ~2.1 ms per page.

- Canonical URL: https://hoja.crafter.run/engineering/oklab
- Version: 0.2.1

## Why RGB inversion breaks technical papers.

A typical ML paper mixes four visual layers on the same page: neutral white paper, black New Computer Modern text, colored vector plots (Seaborn, matplotlib, TikZ), and embedded photographic rasters. Flipping (1 - R, 1 - G, 1 - B) fixes the paper background at the cost of rotating every plot hue by 180 degrees and ruining dark slide decks.

## Step 1: Leave dark and chromatic slides untouched.

Before recoloring a single pixel, analyze_page in src/recolor.rs samples the outer border ring into a 12-bit RGB histogram (4096 buckets) to find the dominant page background and measures coverage across the page. It classifies every page into one of five PageTone variants:

| PageTone | Detection rule (src/recolor.rs) | Action |
| --- | --- | --- |
| LightPaper | High background coverage, neutral high-lightness background | Apply OKLab transfer LUT and per-page auto-crop |
| Dark | lab.l < 0.45 on dominant border background | Skip recoloring and auto-crop (keeps dark slide decks pristine) |
| Chromatic | Saturated colored slide or cover background | Keep original hue and full-bleed geometry |
| Busy | coverage < 0.30 (dense full-bleed art or magazine page) | Keep uncropped and unrecolored |
| MidGray | Mid-tone backdrop | Handled conservatively to preserve contrast |

## Step 2: 64³ OKLab LUT with chroma gate.

hoja queries your terminal's real background (bg) and foreground (fg) via OSC 10/11 and maps neutral luminance along the perceptual OKLab L axis using Björn Ottosson's sRGB <-> linear <-> OKLab matrices:

```glsl
// 1. Neutral paper-to-ink transfer coordinate
float t = clamp((paper_l - lab.x) / max(paper_l - ink_l, 0.05), 0.0, 1.0);

// 2. Smooth chroma gate: neutral text follows fg/bg; colored plots keep (a, b) hue
float chroma = length(lab.yz);
float w = smoothstep(0.03, 0.08, chroma);

// 3. Target OKLab lightness blends into terminal glass, clamped via 8-step binary sRGB gamut search
```

- **Hue preservation (ΔH = 0°)**: Chromatic vector lines retain their OKLab (a, b) direction through smoothstep(0.03, 0.08, hypot(a, b)), so red curves stay red and blue curves stay blue on dark terminal glass.
- **8-step binary gamut clamp**: When shifting lightness pushes a vivid color outside the sRGB cube, an 8-step binary chroma search scales chroma inward along constant hue instead of clipping channels unevenly.
- **Cached 64³ LUT (~2.1 ms warm)**: Instead of running cube-roots per pixel across millions of pixels, hoja precomputes a 64x64x64 trilinear lookup table per terminal theme so recoloring a full page takes roughly 2.1 ms.

## Step 3: Keep photographs natural.

hoja inspects PDF image objects (respecting XObjectForm transforms and clip paths) and masks embedded raster photos occupying under 75% of the page area so portraits, microscopy figures, and renders stay in original color. Full-page scanned book pages (>= 75% area) are NOT masked, so scanned textbooks still recolor cleanly into dark mode.

**Live proof:** The interactive wipe on the hoja home page runs this exact GLSL port of src/recolor.rs live in WebGL2 over a Typst-compiled specimen with colored plots and a masked optical raster.
