Expand description
SVG/Photoshop ‘Luminosity’ blend mode, fused into a single per-pixel pass.
Matches blend.py’s luminosity_blend(), i.e.
_clip_color(backdrop_rgb + d), exactly. The numpy version runs the
luminosity shift and _clip_color() as two separate, unrelated
passes with intermediate arrays; fusing them here enables one
algebraic shortcut on top of skipping the intermediates.
§Algorithm
Per pixel, with luminance lum(r, g, b) = 0.3r + 0.59g + 0.11b:
- Shift every channel by
d = target_lum - lum(backdrop). - Clip the shifted colour back into [0, 1] while preserving its luminance: first pull the minimum channel up to 0 if it went negative, then pull the maximum channel down to 1 if it went over.
- Clamp each channel to [0, 1] to absorb float error.
The fusion shortcut: after step 1, lum(shifted) == target_lum
exactly, because the weights sum to 1.0. So step 2 reuses
target_lum instead of recomputing the weighted sum.
§Which function to call
Call luminosity_blend_core. The *_serial/*_parallel variants
are public only so benches/blend_bench.rs can time both paths to
tune PARALLEL_THRESHOLD.
Functions§
- luminosity_
blend_ core - Replace the luminance of an RGB image with a target luminance, keeping its hue and saturation. The entry point for this blend.
- luminosity_
blend_ parallel - Luminosity blend, parallelised across rows with rayon.
- luminosity_
blend_ serial - Luminosity blend, single-threaded.