From 3da8da9955407461be55f031468404231d85d794 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Sat, 21 Nov 2026 16:34:02 +0100 Subject: [PATCH 1/2] Modern Brightness at +150 and -150 matches the reference ramps (#14348) Past +110 the raised Brightness curve now eases into a curve fit to the reference application's 8-bit +150 ramp: a line of slope 2.585, a cubic Hermite bridge, and an exponential approach to white that is still rising at white. The power roll-off it replaces reached white too early (254.5 at input 200 where the reference has 253), so the lowered curve, its inverse, stopped at 254 -> 191 instead of 214. The measured -150 table is the inverse of the +150 one once the rounding plateaus are read at their middle, so lowering stays the inverse of raising. Both ramps now match within a level, and settings up to +/-110 are unchanged. Fixes #14348 --- crates/compose/src/adjust/tone.rs | 58 +++++++++++++++++-- .../brightness_contrast_extreme_brightness.rs | 50 ++++++++++++++++ crates/compose/tests/it/main.rs | 1 + 3 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 crates/compose/tests/it/brightness_contrast_extreme_brightness.rs diff --git a/crates/compose/src/adjust/tone.rs b/crates/compose/src/adjust/tone.rs index 5c57608e02..79c70a9915 100644 --- a/crates/compose/src/adjust/tone.rs +++ b/crates/compose/src/adjust/tone.rs @@ -145,13 +145,15 @@ fn lut_extended(table: &[f32], v: f32) -> f32 { /// and `b = 0` is the identity. Raised, it is a line of slope `s = 1.375^(b/50)` from the origin /// up to a knee at output 126/255, then a cubic Hermite to (1, 1) whose end slope is /// `1 / (1 + 0.084 b)`; past about +110, where no such cubic stays below white, the roll-off is -/// `1 - (1 - y0)(1 - u)^α` instead (continuous slope `α` at the knee, flat at white). Lowered, it -/// is the inverse of the curve raised by as much. +/// `1 - (1 - y0)(1 - u)^α` instead (continuous slope `α` at the knee, flat at white). From +110 +/// to +150 that curve eases into [`brightness_150`]. Lowered, it is the inverse of the curve +/// raised by as much. /// /// Fit to every Photoshop measurement we have, all within about a level and a half: its ±50 /// samples (`modern_brightness_matches_photoshop`), the photoshop corpus's brightness-contrast.psd -/// (+25 with contrast +30) and psd-tools' brightnesscontrast_rgb.psd, whose masked layers give -/// +23, -31 and -95 (+150 within 6 levels there, where every curve family tried bottoms out). +/// (+25 with contrast +30), psd-tools' brightnesscontrast_rgb.psd, whose masked layers give +/// +23, -31 and -95 (and +150, within 2 levels), and full 8-bit ramps at +150 and -150 (within a +/// level once rounded). pub fn modern_brightness(v: f32, brightness: f32) -> f32 { if brightness == 0.0 || !brightness.is_finite() { return v; @@ -169,11 +171,17 @@ struct Brightness { /// replaces the Hermite when α² + β² > 9. alpha: f32, hermite: bool, + /// How far the curve has eased into [`brightness_150`]: 0 up to ±110, 1 at ±150. + toward_150: f32, } /// Output level of the Brightness curve's knee. const BRIGHTNESS_KNEE: f32 = 126.0 / 255.0; +/// The slider value where the curve starts to ease into [`brightness_150`], just past where the +/// power roll-off takes over from the Hermite (about 109.7). +const BRIGHTNESS_EASE_FROM: f32 = 110.0; + impl Brightness { fn new(brightness: f32) -> Self { let b = brightness.abs(); @@ -182,7 +190,8 @@ impl Brightness { let x0 = (BRIGHTNESS_KNEE / s).min(1.0); let secant = (1.0 - BRIGHTNESS_KNEE) / (1.0 - x0).max(1e-6); let (alpha, beta) = (s / secant, e / secant); - Brightness { raise: brightness > 0.0, s, e, x0, alpha, hermite: alpha * alpha + beta * beta <= 9.0 } + let toward_150 = ((b - BRIGHTNESS_EASE_FROM) / (150.0 - BRIGHTNESS_EASE_FROM)).clamp(0.0, 1.0); + Brightness { raise: brightness > 0.0, s, e, x0, alpha, hermite: alpha * alpha + beta * beta <= 9.0, toward_150 } } fn eval(&self, v: f32) -> f32 { @@ -206,8 +215,18 @@ impl Brightness { 0.5 * (lo + hi) } - /// The curve raised: the line, then a monotone roll-off to (1, 1). + /// The curve raised: the line and its roll-off, eased into [`brightness_150`] past +110 (a mix + /// of two monotone curves that pin black and white, so it stays monotone and pinned). fn raised(&self, x: f32) -> f32 { + let y = self.rolled(x); + if self.toward_150 <= 0.0 { + return y; + } + (y + self.toward_150 * (brightness_150(x) - y)).clamp(0.0, 1.0) + } + + /// The line, then a monotone roll-off to (1, 1). + fn rolled(&self, x: f32) -> f32 { let Brightness { s, e, x0, alpha, hermite, .. } = *self; let y0 = BRIGHTNESS_KNEE; if x <= x0 { @@ -225,6 +244,33 @@ impl Brightness { } } +/// The modern Brightness curve at +150, fit to the reference application's 8-bit ramp (and, +/// inverted, to its -150 ramp; both within a level and a quarter before rounding). A line of +/// slope 2.585 up to input 52/255, then a cubic Hermite to input 168/255, then an exponential +/// approach to white, `1 - d(e^((1 - x)/c) - 1)`, that is still rising there (slope `d/c`, about +/// 0.012). The power roll-off, flat at white, comes too close to white past input 200 (254.5 +/// where the reference has 253), so lowered it gave 254 -> 191 instead of 214. +fn brightness_150(x: f32) -> f32 { + const S: f32 = 2.585; + const C: f32 = 0.131; + const D: f32 = 0.4 / 255.0; + const X1: f32 = 52.0 / 255.0; + const X2: f32 = 168.0 / 255.0; + let tail = |x: f32| 1.0 - D * (((1.0 - x) / C).exp() - 1.0); + let x = x.clamp(0.0, 1.0); + if x <= X1 { + return S * x; + } + if x >= X2 { + return tail(x).min(1.0); + } + let (y1, y2, m2) = (S * X1, tail(X2), D / C * ((1.0 - X2) / C).exp()); + let h = X2 - X1; + let t = (x - X1) / h; + let (t2, t3) = (t * t, t * t * t); + (2.0 * t3 - 3.0 * t2 + 1.0) * y1 + (t3 - 2.0 * t2 + t) * h * S + (-2.0 * t3 + 3.0 * t2) * y2 + (t3 - t2) * h * m2 +} + /// Modern Brightness/Contrast as one channel's lookup table (4096 samples over 0..1): /// [`modern_brightness`] then [`modern_contrast`], as Photoshop applies them. pub fn modern_lut(brightness: f32, contrast: f32) -> Vec { diff --git a/crates/compose/tests/it/brightness_contrast_extreme_brightness.rs b/crates/compose/tests/it/brightness_contrast_extreme_brightness.rs new file mode 100644 index 0000000000..e967b3b62e --- /dev/null +++ b/crates/compose/tests/it/brightness_contrast_extreme_brightness.rs @@ -0,0 +1,50 @@ +//! Modern Brightness/Contrast at the ends of the Brightness slider (#14348), against the +//! reference application's 8-bit gray ramps. + +use photocraft_compose::Buffer; +use photocraft_compose::adjust::apply; +use photocraft_doc::Adjustment; +use photocraft_geom::Rect; + +/// Brightness -150, Contrast 0, Use Legacy off: output for gray inputs 0..=255. +const LOWERED: [u8; 256] = [ + 0, 0, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 12, 12, 12, 13, 13, 14, 14, 14, 15, 15, 16, 16, 16, 17, 17, + 17, 18, 18, 19, 19, 19, 20, 20, 21, 21, 21, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, + 33, 33, 33, 34, 34, 35, 35, 35, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 40, 40, 40, 41, 41, 42, 42, 42, 43, 43, 44, 44, 44, 45, 45, 45, 46, 46, 47, 47, 47, + 48, 48, 49, 49, 49, 50, 50, 51, 51, 51, 52, 52, 53, 53, 53, 54, 54, 55, 55, 55, 56, 56, 57, 57, 57, 58, 58, 59, 59, 60, 60, 60, 61, 61, 62, 62, 63, 63, 63, + 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 69, 69, 70, 70, 71, 71, 72, 72, 73, 73, 74, 74, 75, 75, 76, 76, 77, 78, 78, 79, 79, 80, 81, 81, 82, 83, 83, 84, 85, + 85, 86, 87, 87, 88, 89, 90, 90, 91, 92, 93, 94, 95, 96, 96, 97, 98, 99, 100, 101, 102, 103, 105, 106, 107, 108, 109, 111, 112, 113, 115, 116, 118, 120, + 121, 123, 125, 127, 129, 131, 133, 136, 139, 141, 145, 148, 152, 156, 161, 167, 174, 183, 195, 214, 255, +]; + +/// Brightness +150, Contrast 0, Use Legacy off: output for gray inputs 0..=255. +const RAISED: [u8; 256] = [ + 0, 3, 5, 8, 10, 13, 15, 18, 21, 23, 26, 28, 31, 33, 36, 39, 41, 44, 46, 49, 51, 54, 57, 59, 62, 64, 67, 69, 72, 75, 77, 80, 82, 85, 87, 90, 93, 95, 98, + 100, 103, 106, 108, 111, 113, 116, 118, 121, 124, 126, 129, 131, 134, 136, 139, 141, 144, 146, 148, 151, 153, 155, 158, 160, 162, 164, 167, 169, 171, 173, + 175, 177, 179, 181, 183, 184, 186, 188, 190, 191, 193, 195, 196, 198, 199, 201, 202, 203, 205, 206, 207, 209, 210, 211, 212, 213, 214, 216, 217, 218, 219, + 220, 221, 222, 222, 223, 224, 225, 226, 227, 227, 228, 229, 230, 230, 231, 232, 232, 233, 234, 234, 235, 235, 236, 237, 237, 238, 238, 239, 239, 240, 240, + 240, 241, 241, 242, 242, 242, 243, 243, 244, 244, 244, 244, 245, 245, 245, 246, 246, 246, 247, 247, 247, 247, 247, 248, 248, 248, 248, 249, 249, 249, 249, + 249, 250, 250, 250, 250, 250, 250, 250, 251, 251, 251, 251, 251, 251, 251, 251, 252, 252, 252, 252, 252, 252, 252, 252, 252, 252, 253, 253, 253, 253, 253, + 253, 253, 253, 253, 253, 253, 253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, + 254, 254, 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, +]; + +/// The 8-bit gray ramp 0..=255 through modern Brightness/Contrast, read back as levels. +fn ramp_through(brightness: f32, contrast: f32) -> Vec { + let px = (0..=255u8).map(|x| f32::from(x) / 255.0).map(|v| [v, v, v, 1.0]).collect(); + let mut b = Buffer { rect: Rect::new(0, 0, 256, 1), px }; + apply(&Adjustment::BrightnessContrast { brightness, contrast, legacy: false }, &mut b); + b.px.iter().map(|p| (p[0] * 255.0).round() as i32).collect() +} + +/// At -150 and +150 every input lands within a level of the reference, and black and white stay +/// put; near white the lowered curve climbs steeply (254 -> 214) where it used to stop at 191. +#[test] +fn brightness_150_either_way_matches_the_reference_ramp() { + for (brightness, want) in [(-150.0, &LOWERED), (150.0, &RAISED)] { + let got = ramp_through(brightness, 0.0); + let off: Vec<_> = got.iter().zip(want.iter()).enumerate().filter(|(_, (g, w))| (**g - i32::from(**w)).abs() > 1).collect(); + assert!(off.is_empty(), "brightness {brightness}: (input, (got, reference)) off by more than a level: {off:?}"); + assert_eq!((got[0], got[255]), (0, 255), "brightness {brightness} keeps black and white"); + } +} diff --git a/crates/compose/tests/it/main.rs b/crates/compose/tests/it/main.rs index 75dc82f400..37a61989f0 100644 --- a/crates/compose/tests/it/main.rs +++ b/crates/compose/tests/it/main.rs @@ -1,6 +1,7 @@ //! The crate's integration tests, linked into one test binary. mod artboard; +mod brightness_contrast_extreme_brightness; mod buffer_reuse; mod interrupt_surface; mod scratch_retry; From b1100518a166c24b7978af599dad735ec4e02a42 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:06:51 +0100 Subject: [PATCH 2/2] =?UTF-8?q?Modern=20Brightness:=20each=20input=20rises?= =?UTF-8?q?=20with=20the=20slider=20up=20to=20=C2=B1150=20(#14348)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Past +110 the curve mixed the slider's own roll-off with the fixed +150 fit. The power roll-offs come closer to white than that fit past about input 220, so the mix fell there as the slider neared +150: input 200 gave 253.55 at +134 and 253.32 at +150. The ease now starts at +109.5, the last Hermite roll-off, which stays at or below the +150 fit everywhere, and mixes from the roll-off held at or below the fit. Both parts of the mix then rise with the slider, and the curve reaches the +150 fit itself at +150, so the measured ramps are unchanged. The new test steps the slider one unit at a time from ±100 to ±150 and checks every 8-bit input; it fails on the previous curve. --- crates/compose/src/adjust/tone.rs | 59 +++++++++++++------ .../brightness_contrast_extreme_brightness.rs | 28 +++++++++ 2 files changed, 69 insertions(+), 18 deletions(-) diff --git a/crates/compose/src/adjust/tone.rs b/crates/compose/src/adjust/tone.rs index 79c70a9915..8b37222969 100644 --- a/crates/compose/src/adjust/tone.rs +++ b/crates/compose/src/adjust/tone.rs @@ -145,7 +145,7 @@ fn lut_extended(table: &[f32], v: f32) -> f32 { /// and `b = 0` is the identity. Raised, it is a line of slope `s = 1.375^(b/50)` from the origin /// up to a knee at output 126/255, then a cubic Hermite to (1, 1) whose end slope is /// `1 / (1 + 0.084 b)`; past about +110, where no such cubic stays below white, the roll-off is -/// `1 - (1 - y0)(1 - u)^α` instead (continuous slope `α` at the knee, flat at white). From +110 +/// `1 - (1 - y0)(1 - u)^α` instead (continuous slope `α` at the knee, flat at white). From +109.5 /// to +150 that curve eases into [`brightness_150`]. Lowered, it is the inverse of the curve /// raised by as much. /// @@ -164,6 +164,14 @@ pub fn modern_brightness(v: f32, brightness: f32) -> f32 { /// [`modern_brightness`] for one slider value, its curve's constants worked out once. struct Brightness { raise: bool, + roll: Roll, + /// How far the curve has eased into [`brightness_150`]: 0 up to ±109.5, 1 at ±150. + toward_150: f32, +} + +/// The raised curve's line and roll-off for one slider value. +#[derive(Clone, Copy)] +struct Roll { s: f32, e: f32, x0: f32, @@ -171,27 +179,21 @@ struct Brightness { /// replaces the Hermite when α² + β² > 9. alpha: f32, hermite: bool, - /// How far the curve has eased into [`brightness_150`]: 0 up to ±110, 1 at ±150. - toward_150: f32, } /// Output level of the Brightness curve's knee. const BRIGHTNESS_KNEE: f32 = 126.0 / 255.0; -/// The slider value where the curve starts to ease into [`brightness_150`], just past where the -/// power roll-off takes over from the Hermite (about 109.7). -const BRIGHTNESS_EASE_FROM: f32 = 110.0; +/// The slider value where the curve starts to ease into [`brightness_150`]: the last Hermite +/// roll-off before the power roll-off takes over (about 109.7). Every curve up to here stays at +/// or below [`brightness_150`]; the power roll-offs come closer to white past input 220. +const BRIGHTNESS_EASE_FROM: f32 = 109.5; impl Brightness { fn new(brightness: f32) -> Self { let b = brightness.abs(); - let s = 1.375f32.powf(b / 50.0); - let e = 1.0 / (1.0 + 0.084 * b); - let x0 = (BRIGHTNESS_KNEE / s).min(1.0); - let secant = (1.0 - BRIGHTNESS_KNEE) / (1.0 - x0).max(1e-6); - let (alpha, beta) = (s / secant, e / secant); let toward_150 = ((b - BRIGHTNESS_EASE_FROM) / (150.0 - BRIGHTNESS_EASE_FROM)).clamp(0.0, 1.0); - Brightness { raise: brightness > 0.0, s, e, x0, alpha, hermite: alpha * alpha + beta * beta <= 9.0, toward_150 } + Brightness { raise: brightness > 0.0, roll: Roll::new(b), toward_150 } } fn eval(&self, v: f32) -> f32 { @@ -215,19 +217,40 @@ impl Brightness { 0.5 * (lo + hi) } - /// The curve raised: the line and its roll-off, eased into [`brightness_150`] past +110 (a mix - /// of two monotone curves that pin black and white, so it stays monotone and pinned). + /// The curve raised: the line and its roll-off, eased into [`brightness_150`] past +109.5. + /// + /// Each input must rise with the slider, not only the output with the input. The roll-off + /// alone rises with the slider, but past about input 220 the power roll-offs come closer to + /// white than [`brightness_150`], so a plain mix of the two fell there as the slider neared + /// +150 (input 200 gave 253.55 at +134 and 253.32 at +150). So the ease starts from the + /// roll-off held at or below [`brightness_150`]: in `held + t (target - held)` both `held` + /// and `t` rise with the slider, and `target - held` is never negative. It is a mix of two + /// monotone curves that pin black and white, so it stays monotone and pinned, and reaches + /// [`brightness_150`] itself at +150. fn raised(&self, x: f32) -> f32 { - let y = self.rolled(x); + let y = self.roll.eval(x); if self.toward_150 <= 0.0 { return y; } - (y + self.toward_150 * (brightness_150(x) - y)).clamp(0.0, 1.0) + let target = brightness_150(x); + let held = y.min(target); + (held + self.toward_150 * (target - held)).clamp(0.0, 1.0) + } +} + +impl Roll { + fn new(b: f32) -> Self { + let s = 1.375f32.powf(b / 50.0); + let e = 1.0 / (1.0 + 0.084 * b); + let x0 = (BRIGHTNESS_KNEE / s).min(1.0); + let secant = (1.0 - BRIGHTNESS_KNEE) / (1.0 - x0).max(1e-6); + let (alpha, beta) = (s / secant, e / secant); + Roll { s, e, x0, alpha, hermite: alpha * alpha + beta * beta <= 9.0 } } /// The line, then a monotone roll-off to (1, 1). - fn rolled(&self, x: f32) -> f32 { - let Brightness { s, e, x0, alpha, hermite, .. } = *self; + fn eval(&self, x: f32) -> f32 { + let Roll { s, e, x0, alpha, hermite } = *self; let y0 = BRIGHTNESS_KNEE; if x <= x0 { return y0.min(s * x); diff --git a/crates/compose/tests/it/brightness_contrast_extreme_brightness.rs b/crates/compose/tests/it/brightness_contrast_extreme_brightness.rs index e967b3b62e..c1707d6f53 100644 --- a/crates/compose/tests/it/brightness_contrast_extreme_brightness.rs +++ b/crates/compose/tests/it/brightness_contrast_extreme_brightness.rs @@ -48,3 +48,31 @@ fn brightness_150_either_way_matches_the_reference_ramp() { assert_eq!((got[0], got[255]), (0, 255), "brightness {brightness} keeps black and white"); } } + +/// The 8-bit gray ramp 0..=255 through modern Brightness at Contrast 0, as unrounded outputs. +fn ramp_levels(brightness: f32) -> Vec { + let px = (0..=255u8).map(|x| f32::from(x) / 255.0).map(|v| [v, v, v, 1.0]).collect(); + let mut b = Buffer { rect: Rect::new(0, 0, 256, 1), px }; + apply(&Adjustment::BrightnessContrast { brightness, contrast: 0.0, legacy: false }, &mut b); + b.px.iter().map(|p| p[0] * 255.0).collect() +} + +/// Over the top of the slider, where the curve eases into the +150 fit, no input gets darker as +/// Brightness goes up (input 200 used to fall from 253.55 at +134 to 253.32 at +150), and none +/// gets lighter as it goes down. +#[test] +fn each_input_follows_the_slider_up_to_150_either_way() { + for sign in [1.0f32, -1.0] { + let mut prev = ramp_levels(sign * 100.0); + for b in 101..=150u8 { + let brightness = sign * f32::from(b); + let got = ramp_levels(brightness); + let wrong: Vec<_> = (0..=255usize) + .filter_map(|x| Some((x, *prev.get(x)?, *got.get(x)?))) + .filter(|&(_, before, now)| if sign > 0.0 { now < before - 1e-4 } else { now > before + 1e-4 }) + .collect(); + assert!(wrong.is_empty(), "brightness {brightness}: (input, one step before, now) moved against the slider: {wrong:?}"); + prev = got; + } + } +}