Building a Zero-Upload Dark Mode PDF Inverter & Print Ink-Saver in Pure JavaScript (Hue-Preserving HSL Math)
Students and engineers constantly run into two opposite PDF headaches:
- Printing dark-theme lecture slides (deep navy
#0F172Aor black backgrounds with white text) drains an entire ink cartridge in ten pages. - Reading blinding
#FFFFFFtextbooks and research papers at 2 AM causes instant eye strain.
Most quick online PDF inverters solve this with the simplest one-liner in computer graphics:
r = 255 - r; g = 255 - g; b = 255 - b; That classic RGB negative flip works on plain black-and-white text — and completely ruins everything else. A dark slide with blue hyperlinks, green code snippets, and red diagram callouts turns into harsh neon orange, magenta, and muddy gray paper backgrounds (#E8EDF5) that still waste printer toner across every square inch of the page.
While building the free client-side Invert PDF Tool on EasyDailyTools, we engineered a zero-dependency pixel-processing module (InvertPdfEngine) that runs 100% inside browser memory — zero server uploads — and handles Smart Hue-Preserving Inversion, Pure #FFFFFF Paper Ink-Saving, and Soft Charcoal Night Mode. Here is how the math works.
Why Naive 255 - RGB Fails on Modern Technical PDFs
When you invert RGB channels directly, you shift every color's hue by 180 degrees on the color wheel:
- Syntax-highlighted blue (
hsl(215°, 90%, 60%)) becomes ugly amber/orange (hsl(35°)). - Error/warning red becomes jarring cyan.
- A charcoal
#1E293Bslide background flips to#E1D6C4(tinted beige/gray) instead of clean white paper, forcing your printer to spray toner across the entire background.
1. Smart Hue-Preserving Lightness Inversion (RGB → HSL → Flip L → RGB)
To turn a dark slide into a crisp light page without corrupting diagrams, charts, or code highlights, convert each pixel to HSL, keep Hue (H) intact, invert only Lightness (L = 1 - L), and snap low-saturation near-white backgrounds cleanly to pure #FFFFFF:
function rgbToHsl(r, g, b) { const rn = r / 255, gn = g / 255, bn = b / 255; const max = Math.max(rn, gn, bn), min = Math.min(rn, gn, bn); let h = 0, s = 0; const l = (max + min) / 2; if (max !== min) { const d = max - min; s = l > 0.5 ? d / (2 - max - min) : d / (max + min); switch (max) { case rn: h = (gn - bn) / d + (gn < bn ? 6 : 0); break; case gn: h = (bn - rn) / d + 2; break; case bn: h = (rn - gn) / d + 4; break; } h /= 6; } return [h, s, l]; } Then inside the Uint8ClampedArray pixel loop:
if (mode === 'smart_hue') { const [h, s, l] = rgbToHsl(r, g, b); const invL = 1 - l; // Clip desaturated dark-theme backgrounds cleanly to pure #FFFFFF paper if (s < 0.18 && invL * 255 >= whiteClip) { nr = 255; ng = 255; nb = 255; } else { const boostedL = clamp(((invL - 0.5) * (1 + contrast / 140)) + 0.5, 0, 1); [nr, ng, nb] = hslToRgb(h, Math.min(1, s * 1.06), boostedL); } } Notice the s < 0.18 && invL * 255 >= whiteClip guard (default whiteClip = 212):
- Dark slate/navy slide backgrounds (
slow, inverted lightness high) snap cleanly to#FFFFFF(0% printer ink). - Saturated blue charts, green syntax tokens, and red callouts (
s >= 0.18) keep their exact original hue while adjusting lightness so they remain readable on white paper.
2. Print Ink-Saver B&W Mode + Live Toner Savings Estimator
When users just want maximum legibility on a monochrome laser printer, luminance-based inversion with an explicit white paper threshold (whiteClip) and crisp ink floor (<= 38 → #0C1017) eliminates muddy gray halos:
if (mode === 'ink_saver_white') { const lum = 0.299 * r + 0.587 * g + 0.114 * b; const invLum = 255 - lum; const adj = contrastFactor * (invLum - 128) + 128; if (adj >= whiteClip) { nr = 255; ng = 255; nb = 255; // 0% toner on paper background } else if (adj <= 38) { nr = 12; ng = 16; nb = 23; // Sharp dark ink for text } else { const v = clampByte(adj); nr = v; ng = v; nb = v; } } To give users real quantitative feedback before printing, we sample ~16,000 pixels across the original and inverted buffers to estimate ink coverage (1 - normalizedLuminance):
function estimateTonerMetrics(origRgba, invRgba) { const step = Math.max(4, Math.floor(origRgba.length / 16000) * 4); let sumOrig = 0, sumNew = 0, samples = 0; for (let i = 0; i < origRgba.length; i += step) { const oLum = (0.299 * origRgba[i] + 0.587 * origRgba[i + 1] + 0.114 * origRgba[i + 2]) / 255; const nLum = (0.299 * invRgba[i] + 0.587 * invRgba[i + 1] + 0.114 * invRgba[i + 2]) / 255; sumOrig += (1 - oLum); sumNew += (1 - nLum); samples++; } const origPct = (sumOrig / samples) * 100; const newPct = (sumNew / samples) * 100; const savedRelativePct = origPct > 1 ? Math.round(((origPct - newPct) / origPct) * 100) : 0; return { origInkPct: +origPct.toFixed(1), newInkPct: +newPct.toFixed(1), savedRelativePct }; } On typical dark-mode university slides, origInkPct drops from ~86% down to ~8% — cutting real toner usage by over 90%.
3. Soft Charcoal Night Mode (#12161F) for Late-Night Reading
Pure #000000 backgrounds with #FFFFFF text trigger halation (text "glowing" blur) on OLED screens. Instead, we map neutral white paper linearly into a calm slate ramp from #12161F (18, 22, 31) to soft off-white #E8EEF6 (232, 238, 246), while keeping colored diagrams in their original hue within a gentle 0.08..0.90 lightness window.
Architecture: 100% Browser-Local Privacy
Confidential exam sheets, legal drafts, and research manuscripts should never have to be uploaded to a remote server just to flip their colors. In our browser suite:
- Decode pages locally to an offscreen
<canvas>at print-grade resolution. - Process RGBA buffers in-memory with
InvertPdfEngine.processPdfPageBuffer(). - Package 1-Up, 2-Up, or 4-Up handout sheets directly into a clean downloadable PDF right in browser RAM — zero network calls, zero file size traps, zero watermarks.
We applied the same pure-JS, zero-upload pattern across our entire PDF Tools collection — including Compress PDF to 500KB — as well as companion utilities like Invert Image Colors (which adds 35mm film negative neutralization) and Split Image Online.
Live Demo, Open-Source Code & Walkthrough
- Try the Tool Locally in Your Browser: Invert PDF Colors & Ink-Saver
- MIT Open-Source JS Engines (7 Pure Modules): github.com/ansar6666/easydailytools-browser-engines
- Interactive Engine Docs: EasyDailyTools Browser Engines Docs
- 73-Second Video Demo: How to Invert PDF Colors to Dark Mode or Save Printer Ink
What edge cases have you seen when inverting technical PDFs or scanned diagrams? Let me know in the comments!