From 964da5968f4b679679a4d5bb08c12985625e8945 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Fri, 27 Nov 2026 05:00:46 -0500 Subject: [PATCH] Preserve hue wrap and saturation rounding in color adjustments --- crates/nodes/src/atomic/hsl.rs | 25 ++++++++++++++----------- crates/nodes/tests/brick_primitives.rs | 26 ++++++++++++++++++++++++++ docs/parity-fixes.md | 19 +++++++++++++++++++ docs/sbs-root-nodes.md | 4 ++-- 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/crates/nodes/src/atomic/hsl.rs b/crates/nodes/src/atomic/hsl.rs index 14437ba0..336d1446 100644 --- a/crates/nodes/src/atomic/hsl.rs +++ b/crates/nodes/src/atomic/hsl.rs @@ -17,7 +17,8 @@ pub(super) fn definition() -> NodeDefinition { fn hsl(context: &CookContext) -> CookResult { let image = context.any_or(0, Kind::Color, [0.0; 4]).to_color(); - let [hue_shift, saturation_shift, lightness_shift] = ["hue", "saturation", "lightness"].map(|name| (context.f(name) - 0.5) * 2.0); + let hue_shift = context.f("hue") * 12.0 - 6.0; + let [saturation_shift, lightness_shift] = ["saturation", "lightness"].map(|name| (context.f(name) - 0.5) * 2.0); Ok(vec![image.map(Kind::Color, |[red, green, blue, alpha]| { let gain = red.max(green).max(blue).max(1.0); @@ -31,8 +32,8 @@ fn hsl(context: &CookContext) -> CookResult { // The nonzero divisors also affect ordinary colors at the slider midpoints. const EPSILON: f32 = 1.0 / 4096.0; let chroma_range = |lightness: f32| 1.0 - (2.0 * lightness - 1.0).abs(); - let saturation = if chroma == 0.0 { 0.0 } else { pixel_divide(chroma, chroma_range(lightness) + EPSILON) }; - let s = (saturation + saturation_shift).clamp(0.0, 1.0); + let s = if chroma == 0.0 { saturation_shift } else { chroma.mul_add(pixel_divide(1.0, chroma_range(lightness) + EPSILON), saturation_shift) }; + let s = s.clamp(0.0, 1.0); let l = (lightness + lightness_shift).clamp(0.0, 1.0); let (left, right, sector) = if low == r { @@ -44,15 +45,17 @@ fn hsl(context: &CookContext) -> CookResult { }; // Round the bias with the subtracted channel. Raising saturation makes // this rounding visible even when all three input channels are equal. - let slope = pixel_divide(left - (right + 3.0 * EPSILON), chroma + EPSILON); - let hue = (slope + sector) * (1.0 / 6.0) + hue_shift; + let difference = left - (right + 3.0 * EPSILON); + let sextants = difference.mul_add(pixel_divide(1.0, chroma + EPSILON), sector) + hue_shift; + // Remove whole turns in the multiply-add. Rounding the product first loses the residual at the wrap. + const SIXTH: f32 = 1.0 / 6.0; + let hue = sextants.mul_add(SIXTH, -(sextants * SIXTH).floor()); let upper = s.mul_add(0.5 * chroma_range(l), l); let lower = 2.0 * l - upper; - let channel = |phase: f32| { - let wrapped = phase - phase.floor(); - let weight = (wrapped.mul_add(6.0, -3.0).abs() - 1.0).clamp(0.0, 1.0); - (upper - lower).mul_add(weight, lower) * gain - }; - [channel(hue), channel(hue - 1.0 / 3.0), channel(hue + 1.0 / 3.0), alpha] + let channel = |weight: f32| (upper - lower).mul_add(weight.clamp(0.0, 1.0), lower) * gain; + let red = (hue - 0.5).abs().mul_add(6.0, -1.0); + let green = (hue - 1.0 / 3.0).abs().mul_add(-6.0, 2.0); + let blue = (hue - 2.0 / 3.0).abs().mul_add(-6.0, 2.0); + [channel(red), channel(green), channel(blue), alpha] })]) } diff --git a/crates/nodes/tests/brick_primitives.rs b/crates/nodes/tests/brick_primitives.rs index 03aa197e..d5f0a41b 100644 --- a/crates/nodes/tests/brick_primitives.rs +++ b/crates/nodes/tests/brick_primitives.rs @@ -63,6 +63,32 @@ fn hsl_offsets_clamp_in_normalized_lightness_and_restore_hdr_gain() { } } +#[test] +fn hsl_wraps_the_hue_before_reconstructing_rgb() { + let low = 1.0 / 8192.0; + let high = 1.0 - low; + let bias = 3.0 / 4096.0; + for (color, hue, rgb) in [ + ([0.5; 3], 1.0, [1.0, f32::EPSILON, 0.0]), + ([low, high - 0.5 - bias, high], 1.0 / 3.0, [0.5, 1.0, 0.0]), + ([low, high - bias, high], 0.375_f32.next_up(), [0.49999946, 1.0, 0.0]), + ] { + let [r, g, b] = color; + let source = Image::filled(64, 64, Kind::Color, [r, g, b, 0.75]); + let values = BTreeMap::from([("hue".into(), Value::Float(hue)), ("saturation".into(), Value::Float(1.0))]); + let pixel = cook("hsl", values, &[Some(source)]).get(0, 0, false); + assert_eq!(pixel, [rgb[0], rgb[1], rgb[2], 0.75], "hue {hue}"); + } +} + +#[test] +fn hsl_rounds_saturation_after_adding_the_slider_shift() { + let source = Image::filled(16, 16, Kind::Color, [0.6109035, 0.6431967, 0.35680333, 0.75]); + let values = BTreeMap::from([("hue".into(), Value::Float(0.359375)), ("saturation".into(), Value::Float(0.8125))]); + let pixel = cook("hsl", values, &[Some(source)]).get(0, 0, false); + assert_eq!(pixel.map(f32::to_bits), [0x3f74_a640, 0x3d35_9c00, 0x3f12_0e45, 0x3f40_0000]); +} + #[test] fn hsl_rounds_the_channel_bias_before_computing_hue() { for (source, rgb) in [ diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 8b75e038..c46265df 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -59,6 +59,25 @@ The first pass classifies all 546 roots: 166 Parity and 380 Too hard. These meas ## 2026-11-27 root comparisons +### Hue rotation and saturation in Replace Color + +Replace Color matches **170,240/170,240 pixels in 39 settings** on one reference processor, eight processors and after reopening a normal MaterialCraft save. All channels match exactly on each route. The earlier build matched 18 whole images and 158,780 pixels. The same 35,695-byte save reopened for the reference comparison was reproduced byte for byte with the corrected build. + +| Output | Settings | Exact pixels before | Maximum error before | Exact pixels after | Maximum error after | +|---|---:|---:|---:|---:|---:| +| U8 | 10 | 125,439/125,440 | 1 | 125,440/125,440 | 0 | +| U16 | 9 | 13,734/13,824 | 1 | 13,824/13,824 | 0 | +| F16 | 7 | 12,794/12,800 | .00048828125 | 12,800/12,800 | 0 | +| F32 | 13 | 6,813/18,176 | .0000009983778 | 18,176/18,176 | 0 | + +Repeat the [31 established settings](#hue-and-chroma-rounding-near-equal-rgb-channels). Add two linear F32 inputs and render each in all four output formats. With zero-based coordinates and color channel c from 0 to 2, set RGB to ((7x + 11y + 3xy + 41c + 5cx + 13cy + 19j) modulo 259)/256 and alpha to ((31x + 13y + 5) modulo 129)/128. For j = 0 use 32 × 64, seed 5309719, source color (.25,.5,.75) and target (.6875,.125,.5625). For j = 1 use 64 × 16, seed 9072209, source (.75,.25,.375) and target (.375,.71875,.09375). + +The shared HSL adjustment adds the minimum-channel sector in the same multiply-add as the biased channel difference times the reciprocal chroma. It adds the slider displacement in sixths of a turn. Removing whole turns in a multiply-add preserves the small residuals seen at wrap boundaries. A second multiply-add applies the saturation shift before rounding. At saturation 1 and lightness .5, a .5 gray rotated with hue 1 gives RGB (1, 2⁻²³, 0). At hue .359375, saturation .8125 and lightness .5, the F32 input (.6109035,.6431967,.35680333) has output words 3f74a640, 3d359c00, 3f120e45. + +For a separate 32 × 8 linear field, use two grays, .5 and .25, followed by seven colors for each minimum channel in R, G, B order. The minimum is 1/8192 and the maximum is 1 − 1/8192. Set the channel pairs (B,G), (R,B) and (G,R), respectively, to give first − (second + 3/4096) equal to −.75, −.5, −.25, 0, .25, .5 and .75 while retaining that maximum. Repeat the 23 colors across x. Set alpha to ((23x + 29y) modulo 129)/128. At saturation 1 and lightness .5, render hue 0, .03125, .125, .21875, 1/3, .421875, .5, .5625, .625, .765625, .9375, 1 and the two adjacent F32 values on either side of .375. Use seed 9314041 + 13j, starting at j = 0. The 14 settings improved from 1,136/3,584 to **3,584/3,584** on each route, maximum error zero. + +The [five gray ramps, six-sector RGB fields, near-grays and shifted lightness controls](#hue-and-chroma-rounding-near-equal-rgb-channels) match 327,680/327,680, 24,576/24,576, 16,384/16,384 and 14,336/14,336 pixels respectively. A standalone HDR channel-offset comparison still matches 20,462/20,480, maximum .000001013279. On the 32 × 8 field, hold the seed at 9314041 and set hue to .5 + t/2 + q for t = −2048, −128, −16, −4, −2, −1, 0, 1, 2, 4, 16, 128 and 2048 and q = 0 or .125. This out-of-range sweep matches 4,672/6,656, maximum 1. The eight differing settings exceed the scalar parameter's current hard range of −16 to 17. + ### Gradient warp on stretched pixels Liquid matches **820,480/820,480 pixels in 91 distinct settings** on one reference processor, eight processors and after reopening an ordinary MaterialCraft SBS save. The maximum stored error is zero in every format on each route. diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 67f3a428..ba07932d 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -35,7 +35,7 @@ A graph may accept input pins and still be a root under the dependency criterion Statuses: **Not checked**, **In progress**, **Parity** (exact pixels or typed values for the linked measured cases), or **Too hard** (reason recorded). Function calls do not count as regular graph dependencies. -The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard**. The current inventory has **491 Parity (89.9%)**, **55 In progress** and **0 Too hard**. The first eleven open roots match every pixel across 1,059 recorded single-processor settings. Each still differs in at least one eight-processor capture. The November 17–27 comparisons give raw counts, output formats and remaining differences. [November 17 measurements](parity-fixes.md#2026-11-17-root-comparisons) · [November 18 measurements](parity-fixes.md#2026-11-18-root-comparisons) · [November 19 measurements](parity-fixes.md#2026-11-19-root-comparisons) · [November 20 measurements](parity-fixes.md#2026-11-20-root-comparisons) · [November 21 measurements](parity-fixes.md#2026-11-21-root-comparisons) · [November 22 measurements](parity-fixes.md#2026-11-22-root-comparisons) · [November 23 measurements](parity-fixes.md#2026-11-23-root-comparisons) · [November 24 measurements](parity-fixes.md#2026-11-24-root-comparisons) · [November 25 measurements](parity-fixes.md#2026-11-25-root-comparisons) · [November 26 measurements](parity-fixes.md#2026-11-26-root-comparisons) · [November 27 measurements](parity-fixes.md#2026-11-27-root-comparisons) +The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard**. The current inventory has **492 Parity (90.1%)**, **54 In progress** and **0 Too hard**. The first eleven open roots match every pixel across 1,059 recorded single-processor settings. Each still differs in at least one eight-processor capture. The November 17–27 comparisons give raw counts, output formats and remaining differences. [November 17 measurements](parity-fixes.md#2026-11-17-root-comparisons) · [November 18 measurements](parity-fixes.md#2026-11-18-root-comparisons) · [November 19 measurements](parity-fixes.md#2026-11-19-root-comparisons) · [November 20 measurements](parity-fixes.md#2026-11-20-root-comparisons) · [November 21 measurements](parity-fixes.md#2026-11-21-root-comparisons) · [November 22 measurements](parity-fixes.md#2026-11-22-root-comparisons) · [November 23 measurements](parity-fixes.md#2026-11-23-root-comparisons) · [November 24 measurements](parity-fixes.md#2026-11-24-root-comparisons) · [November 25 measurements](parity-fixes.md#2026-11-25-root-comparisons) · [November 26 measurements](parity-fixes.md#2026-11-26-root-comparisons) · [November 27 measurements](parity-fixes.md#2026-11-27-root-comparisons) Writable quadrant tints now match 16,896/16,896 pixels in 44 settings across four output formats, up from 4,608. Fixed and published inputs through a graph instance match 3,840/3,840 original and 3,840/3,840 reopened pixels. Connected image properties match all 18,432 original and all 18,432 reopened pixels across 32 settings, up from 15,168 on each side. Live float4 and integer initial values remain open. The earlier explicit undefined-tint controls covered 70,656 pixels across original squares, ordinary saves, Gaussian and ramp shapes and connected images. [Connected images](parity-fixes.md#connected-image-properties-and-the-first-quadrant-draw) · [Writable floats](parity-fixes.md#first-reads-of-writable-quadrant-floats) · [Explicit undefined tints](parity-fixes.md#undefined-components-in-a-computed-quadrant-tint) @@ -671,7 +671,7 @@ Creased matches 10,947,840/10,947,840 pixels across 100 distinct settings throug | [quantize_color.sbs/modify_color_palette_sub](../assets/sbs/quantize_color.sbs) | 2 | Yes | [Parity · 15,104/15,104 palette entries across 59 distinct settings](parity-fixes.md#modifying-a-256-entry-color-palette) · [Earlier palette measurement](parity-fixes.md#height-distance-texture-mapping-skew-and-weave) | | [quantize_color.sbs/view_color_palette_sub](../assets/sbs/quantize_color.sbs) | 2 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) | | [radial_blur.sbs/radial_blur_grayscale](../assets/sbs/radial_blur.sbs) | 1 | No | [Parity · 591,616/591,616 pixels across 80 distinct settings](parity-fixes.md#diagonal-mip-selection-on-rotated-rectangular-bitmaps) · [Earlier radial measurements](parity-fixes.md#tile-weave-splatter-and-fiber-stage-comparisons) · [both isolated branches match in all six cases per shape](parity-fixes.md#tile-weave-splatter-and-fiber-stage-comparisons) | -| [replace_color.sbs/replace_color](../assets/sbs/replace_color.sbs) | 1 | No | [In progress · 148,752/157,952 pixels on each processor count across 31 settings, nine verified saves](parity-fixes.md#hue-and-chroma-rounding-near-equal-rgb-channels) · [Earlier whole-root and direct color comparisons](parity-fixes.md#hue-saturation-lightness-and-replace-color) | +| [replace_color.sbs/replace_color](../assets/sbs/replace_color.sbs) | 1 | No | [Parity · 39/39 settings, 170,240/170,240 pixels on one processor, eight and ordinary reopen](parity-fixes.md#hue-rotation-and-saturation-in-replace-color) · [Previous comparison · 148,752/157,952 pixels across 31 settings](parity-fixes.md#hue-and-chroma-rounding-near-equal-rgb-channels) · [Earlier whole-root and direct color comparisons](parity-fixes.md#hue-saturation-lightness-and-replace-color) | | [rgb-a_merge.sbs/rgb-a_merge](../assets/sbs/rgb-a_merge.sbs) | 2 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) | | [rgb-a_split.sbs/rgb-a_split](../assets/sbs/rgb-a_split.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) | | [rgba_merge.sbs/rgba_merge](../assets/sbs/rgba_merge.sbs) | 4 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |