Engineering · src/recolor.rs · v0.2.1

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.

src/recolor.rs64³ OKLab LUTChroma GatePhoto MaskingView .md

01 · The Problem

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.

02 · Page Classification

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:

PageToneDetection rule (src/recolor.rs)Action
LightPaperHigh background coverage, neutral high-lightness backgroundApply OKLab transfer LUT and per-page auto-crop
Darklab.l < 0.45 on dominant border backgroundSkip recoloring and auto-crop (keeps dark slide decks pristine)
ChromaticSaturated colored slide or cover backgroundKeep original hue and full-bleed geometry
Busycoverage < 0.30 (dense full-bleed art or magazine page)Keep uncropped and unrecolored
MidGrayMid-tone backdropHandled conservatively to preserve contrast

03 · Perceptual Transfer

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:

perceptual lightness transfer + chroma gate (src/recolor.rs & oklab-shader.ts)
// 1. Neutral paper-to-ink transfer coordinatefloat 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) huefloat 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.

04 · Raster Photo Masking

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.