Recoloring SVGs & Generating Exact Dark-Mode CSS Filters
Batch-recolor multi-path SVG icons without broken strokes, or compute exact brightness/sepia/hue-rotate CSS filters for img tags in dark mode.
Summary
| Technique | Works on HTML img tags | Preserves multi-color fills | Dark-mode friendliness |
|---|---|---|---|
| Inline currentColor / fill edit | No (inline <svg> only) | Yes — selective hue replacement | Instant CSS variable swaps |
| CSS filter: invert() sepia() ... | Yes (HTML img & masks) | Monochrome tinting only | Single CSS class per theme token |
| CSS mask-image + background | No (uses background-color) | Solid silhouette only | Exact token color without filter error |
Why rasterizing SVGs for dark mode creates maintenance bloat
Shipping duplicate black and white PNGs doubles asset requests and blurs icons on 3x Retina displays.
Editing vector fill attributes directly or using Colr's CSS filter solver eliminates redundant dark-mode icon exports.
- DOMParser SVG rewriting keeps crisp bezier geometry intact.
- CSS filter chains start from brightness(0) saturate(100%) to guarantee deterministic tinting regardless of the source icon color.
Exact CSS filter chain for tinting #000 icons to Brand Blue
Colr's CSS filter solver uses SPSA optimization in CIE perceptual space to minimize Delta-E between rendered pixels and your target Hex.
CSS.icon-brand {
/* Turns black #000000 SVG into #2563EB (Delta-E < 0.8) */
filter: brightness(0) saturate(100%) invert(27%) sepia(91%) saturate(2258%) hue-rotate(214deg) brightness(96%) contrast(94%);
}Handling multi-color illustrations without breaking gradients
For multi-color brand illustrations, flat monochrome filters wash everything into one shade. Instead, detect every unique stop-color, fill, and stroke hex inside the SVG AST and remap each slot individually.
- Inspect fill, stroke, and linearGradient stop-color entries automatically.
- Swap brand accents while leaving neutral shadows untouched.
Tools featured in this guide
Related formats
Frequently asked questions
Why prepend brightness(0) saturate(100%) before CSS hue-rotate filters?
Starting with brightness(0) turns any arbitrary source SVG into pure black #000000 first so the downstream invert/sepia/hue-rotate math stays deterministic.
Can SVGs drop their file data on a server during Colr recoloring?
Never — DOMParser rewriting runs entirely inside your local browser tab.
Authoritative Specifications & References
More color guides
- CMYK Ink Profiles, RAL Classic & Spot Approximations in PrintWhy naive 1-max(R,G,B) CMYK fails commercial printing, Coated FOGRA39 vs US SWOP v2 profiles, RAL Classic architectural codes, and Delta-E QA.
- Zero-Server Browser Color Processing: Privacy & ArchitectureHow Colr executes Canvas quantization, CSS rewriting, and binary .ase/.aco/.cube parsing 100% inside local browser RAM with zero file transfers.
- Hex to OKLCH: Migrating Design Tokens to Tailwind CSS v4Step-by-step guide to upgrading Hex and HSL Tailwind v3 themes into CSS Color 4 @theme oklch() scales with perceptual brightness and Display P3 support.
- OKLCH vs HSL vs sRGB: Understanding Wide-Gamut Display P3Compare HSL, sRGB Hex, Display P3, and OKLCH across perceptual lightness, gradient dead zones, gamut volume, and CSS progressive enhancement.