Skip to main content

Module luminosity_blend

Module luminosity_blend 

Source
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:

  1. Shift every channel by d = target_lum - lum(backdrop).
  2. 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.
  3. 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.