Merge pull request #15411

This commit is contained in:
autocraft-bot[bot] committed 2026-12-01 15:26:27 +00:00
commit 91bf9f75d2
6 files changed
+151 -29

No files matched your search

+5 -2
View File
@@ -191,8 +191,8 @@ fn apply_depth_mode(adj: &Adjustment, buf: &mut Buffer, transfer: Transfer, dept
}
None => (0.0, 0.0, 0.0),
};
let sat = colorize_saturation((*saturation / 100.0 + ds).clamp(-1.0, 1.0), *lightness / 100.0, *colorize, depth);
let out = hue_saturation_in(c, *hue + dh, sat, (*lightness / 100.0).clamp(-1.0, 1.0), *colorize, depth);
let sat = colorize_saturation((*saturation / 100.0).clamp(-1.0, 1.0), *lightness / 100.0, *colorize, depth);
let out = hue_saturation_in(c, *hue + dh, sat, ds.clamp(-1.0, 1.0), (*lightness / 100.0).clamp(-1.0, 1.0), *colorize, depth);
range_lightness(out, dl)
})
}
@@ -434,6 +434,9 @@ mod gradient_map_grayscale_tests;
#[cfg(test)]
mod tone_tests;
#[cfg(test)]
mod hue_saturation_range_multiply_tests;
/// Legacy Brightness/Contrast scales contrast around 128 of 255 (contrast 80 keeps 128 at 128,
/// measured on Photoshop 27.11; a 0.5 pivot moved it to 130).
pub const LEGACY_PIVOT: f32 = 128.0 / 255.0;
+10 -6
View File
@@ -30,15 +30,18 @@ pub fn colorize_saturation(s: f32, lightness: f32, colorize: bool, depth: Option
/// Colorize sets the hue and saturation instead of shifting them; at 0% it leaves only the
/// lightness, as in Photoshop (#2326). Ticking Colorize in the dialog starts saturation at 25%.
pub fn hue_saturation(c: [f32; 3], hue: f32, s: f32, l: f32, colorize: bool) -> [f32; 3] {
hue_saturation_in(c, hue, s, l, colorize, None)
hue_saturation_in(c, hue, s, 0.0, l, colorize, None)
}
/// [`hue_saturation`] in a document of `depth`. At 32 bits Photoshop doesn't clip the raised
/// saturation, so saturated colours leave 0..1 (red with Hue +40, Saturation +25, Lightness -10
/// becomes (1.0477, 0.649, -0.1477)), and it scales each colour's chroma around its lightness by
/// [`hdr_saturation_factor`], so colours at or above lightness 1 keep their hue too: the photoshop
/// corpus's rgb32 hue-saturation.psd is exact so.
pub fn hue_saturation_in(c: [f32; 3], hue: f32, s: f32, l: f32, colorize: bool, depth: Option<SampleType>) -> [f32; 3] {
/// corpus's rgb32 hue-saturation.psd is exact so. A colour range's saturation `range_s` scales
/// the pixel's saturation again after the master's, so their factors multiply: Master -50 with
/// Reds -50 leaves a quarter of red's saturation, (255, 0, 0) becomes (159, 95, 95) in the
/// reference application (#14741). Colorize ignores it, as ranges don't apply there.
pub fn hue_saturation_in(c: [f32; 3], hue: f32, s: f32, range_s: f32, l: f32, colorize: bool, depth: Option<SampleType>) -> [f32; 3] {
let hdr = depth == Some(SampleType::F32);
let lighten = |v: f32| if l > 0.0 { v + (1.0 - v) * l } else { v * (1.0 + l) };
if colorize {
@@ -56,12 +59,13 @@ pub fn hue_saturation_in(c: [f32; 3], hue: f32, s: f32, l: f32, colorize: bool,
if hdr {
// The chroma, not the HSL saturation, whose `2 - max - min` divisor is 0 at lightness 1
// and negative above it: (2, 0, 0) with Hue +40 becomes (2, 1.333, 0) in Photoshop.
let d = (lc[0].max(lc[1]).max(lc[2]) - lc[0].min(lc[1]).min(lc[2])) * hdr_saturation_factor(s);
let d = (lc[0].max(lc[1]).max(lc[2]) - lc[0].min(lc[1]).min(lc[2])) * (hdr_saturation_factor(s) * hdr_saturation_factor(range_s));
return hue_rgb(hh, ll - d / 2.0, ll + d / 2.0);
}
// Raising saturation divides by 1 - s, lowering it multiplies by 1 + s: +50 % doubles it
// (measured in Photoshop 27.11); +100 % saturates any colour that has some.
let ss = if s > 0.0 { ss / (1.0 - s).max(1.0 / 512.0) } else { ss * (1.0 + s) }.clamp(0.0, 1.0);
let scale = |v: f32, by: f32| if by > 0.0 { v / (1.0 - by).max(1.0 / 512.0) } else { v * (1.0 + by) };
let ss = scale(scale(ss, s), range_s).clamp(0.0, 1.0);
hsl_to_rgb(hh, ss, ll)
}
@@ -71,7 +75,7 @@ pub fn hue_saturation_in(c: [f32; 3], hue: f32, s: f32, l: f32, colorize: bool,
/// 1/256 and stops at 32767/256, so +25 % gives 340/256, +99 % 63.75 and +100 % 127.996.
/// compose.wgsl's Hue/Saturation does the same in whole numbers.
pub fn hdr_saturation_factor(s: f32) -> f32 {
// The 1e-4 keeps a master plus range sum on its step: -0.46 + 0.96 is 0.49999997 in f32.
// The 1e-4 keeps a value a rounding error under a step on that step: 0.49999997 is +50 %.
let s = s.clamp(-1.0, 1.0);
if s <= 0.0 {
return 1.0 - (-s * 256.0 + 1e-4).floor() / 256.0;
@@ -0,0 +1,110 @@
//! #14741: a colour range's Saturation multiplies the Master Saturation's factor instead of
//! adding to it.
use super::*;
fn hue_sat(master: f32, reds: f32) -> Adjustment {
let mut ranges = HueRange::defaults();
ranges[0].saturation = reds;
Adjustment::HueSaturation { hue: 0.0, saturation: master, lightness: 0.0, colorize: false, ranges }
}
fn run(adj: &Adjustment, c: [f32; 3], depth: SampleType) -> [f32; 3] {
let mut b = Buffer::filled(crate::Rect::new(0, 0, 1, 1), [c[0], c[1], c[2], 1.0]);
apply_depth(adj, &mut b, Transfer::Gamma(1.0), Some(depth));
[b.px[0][0], b.px[0][1], b.px[0][2]]
}
fn levels(c: [f32; 3]) -> [f32; 3] {
c.map(|v| (v * 255.0).round())
}
fn within(got: [f32; 3], want: [f32; 3], tolerance: f32) -> bool {
levels(got).iter().zip(levels(want)).all(|(g, w)| (g - w).abs() <= tolerance)
}
/// The reference application, measured: Master -50 with Reds -50 turns (255, 0, 0) into
/// (159, 95, 95), a quarter of red's saturation. Adding the two made -100 and grey.
#[test]
fn master_and_reds_minus_fifty_keep_a_quarter_of_red() {
let got = run(&hue_sat(-50.0, -50.0), [1.0, 0.0, 0.0], SampleType::U8);
let want = [159.0, 95.0, 95.0].map(|v| v / 255.0);
assert!(within(got, want, 1.0), "{:?}", levels(got));
}
/// The 72-hue ramp of the measurement at HSL saturation 100 % and 50 %: no Reds cell turns grey,
/// and away from the range's fades every cell has master × range of its saturation.
#[test]
fn ramp_cells_take_the_product_of_both_factors() {
let adj = hue_sat(-50.0, -50.0);
for s in [1.0, 0.5] {
for step in 0..72 {
let deg = step as f32 * 5.0;
let c = hsl_to_rgb(deg / 360.0, s, 0.5);
let got = run(&adj, c, SampleType::U8);
let from_red = deg.min(360.0 - deg);
if from_red < 45.0 {
let chroma = got[0].max(got[1]).max(got[2]) - got[0].min(got[1]).min(got[2]);
assert!(chroma * 255.0 >= 8.0, "hue {deg} at {s}: {:?} is grey", levels(got));
}
let factor = match from_red {
d if d <= 15.0 => 0.25,
d if d >= 45.0 => 0.5,
_ => continue,
};
let want = hsl_to_rgb(deg / 360.0, s * factor, 0.5);
assert!(within(got, want, 2.0), "hue {deg} at {s}: {:?}, want {:?}", levels(got), levels(want));
}
}
}
/// Raised and mixed settings combine as the same factors applied in turn: +50 doubles the
/// saturation, -50 halves it, +25 multiplies it by 4/3.
#[test]
fn raised_and_mixed_settings_multiply_their_factors() {
let red = |s: f32| hsl_to_rgb(0.0, s, 0.5);
for (master, reds, s, want) in
[(50.0, -50.0, 0.4, 0.4), (-50.0, 50.0, 0.4, 0.4), (25.0, 25.0, 0.3, 0.3 * 16.0 / 9.0), (50.0, 50.0, 0.4, 1.0), (-100.0, 50.0, 0.4, 0.0)]
{
let got = run(&hue_sat(master, reds), red(s), SampleType::U8);
assert!(within(got, red(want), 1.0), "master {master}, reds {reds}: {:?}, want {:?}", levels(got), levels(red(want)));
}
}
/// 32 bits scale the chroma by the product of their own factors: a quarter of red's chroma for
/// -50 twice, and Reds -50 halves what Master +50 (510/256) or +100 (32767/256) alone gives.
#[test]
fn thirty_two_bits_multiply_too() {
let chroma = |c: [f32; 3]| c[0].max(c[1]).max(c[2]) - c[0].min(c[1]).min(c[2]);
for (master, reds, want) in [(-50.0, -50.0, 0.25), (50.0, -50.0, 255.0 / 256.0), (100.0, -50.0, 32767.0 / 512.0)] {
let got = run(&hue_sat(master, reds), [1.0, 0.0, 0.0], SampleType::F32);
assert!((chroma(got) - want).abs() < 1e-3 * want, "master {master}, reds {reds}: {got:?}, chroma {want}");
assert!(got[1..].iter().all(|v| (v - (1.0 - want) / 2.0).abs() < 1e-3 * want.max(1.0)), "{got:?}");
}
}
/// A range alone reaches the saturation step as a master of the same value does, bit for bit, on
/// a colour inside the range: one setting alone is untouched by the change.
#[test]
fn one_setting_alone_passes_through_unchanged() {
let c = hsl_to_rgb(2.0 / 360.0, 0.6, 0.45);
for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {
for k in -20..=20 {
let v = k as f32 * 5.0;
let range = run(&hue_sat(0.0, v), c, depth).map(f32::to_bits);
let master = run(&hue_sat(v, 0.0), c, depth).map(f32::to_bits);
assert_eq!(range, master, "{depth:?}, {v}");
}
}
}
#[test]
fn hostile_values_stay_finite() {
for depth in [SampleType::U8, SampleType::F32] {
for (m, r) in [(f32::INFINITY, -50.0), (f32::NEG_INFINITY, 50.0), (100.0, 100.0), (-100.0, -100.0), (100.0, -100.0), (500.0, 500.0), (-500.0, 1e9)] {
let got = run(&hue_sat(m, r), [0.8, 0.1, 0.2], depth);
assert!(got.iter().all(|v| v.is_finite()), "{depth:?}, {m}, {r}: {got:?}");
}
let _ = run(&hue_sat(f32::NAN, f32::NAN), [0.8, 0.1, 0.2], depth);
}
}
+2 -7
View File
@@ -87,13 +87,8 @@ fn thirty_two_bit_hue_saturation_keeps_bright_colours() {
let got = hdr(&adj, c);
assert!(near(got, want), "hue {hue}, saturation {sat}, {c:?}: {got:?}, Photoshop {want:?}");
}
// A master plus a range edit that add up to a whole step stay on it: Reds +96 over a master
// -46 scales red as much as a master +50 alone (in f32 the sum is 0.49999997).
let mut ranges = HueRange::defaults();
ranges[0].saturation = 96.0;
let summed = Adjustment::HueSaturation { hue: 0.0, saturation: -46.0, lightness: 0.0, colorize: false, ranges };
let single = Adjustment::HueSaturation { hue: 0.0, saturation: 50.0, lightness: 0.0, colorize: false, ranges: HueRange::defaults() };
assert_eq!(hdr(&summed, [1.0, 0.0, 0.0]), hdr(&single, [1.0, 0.0, 0.0]));
// A value a rounding error under a step stays on it: 0.49999997 is +50 %.
assert_eq!(hdr_saturation_factor(0.49999997), hdr_saturation_factor(0.5));
// Lightness 0 with a negative channel divides by zero the same way.
let adj = Adjustment::HueSaturation { hue: 40.0, saturation: 25.0, lightness: 0.0, colorize: false, ranges: HueRange::defaults() };
assert!(hdr(&adj, [0.25, -0.25, 0.0]).iter().all(|v| v.is_finite()));
+18 -14
View File
@@ -479,6 +479,20 @@ fn rgb_to_hsl(c: vec3<f32>) -> vec3<f32> {
}
return vec3(h / 6.0, s, l);
}
// compose::adjust::hue_saturation_in at 8/16 bits.
fn scale_saturation(v: f32, s: f32) -> f32 {
if (s > 0.0) { return v / max(1.0 - s, 1.0 / 512.0); }
return v * (1.0 + s);
}
// compose::adjust::color::hdr_saturation_factor, in whole numbers.
fn hdr_saturation_factor(s: f32) -> f32 {
if (s <= 0.0) { return 1.0 - floor(-s * 256.0 + 1e-4) / 256.0; }
let steps = min(u32(floor(s * 254.0 + 1e-4)), 254u);
return f32(min(65280u / (255u - steps), 32767u)) / 256.0;
}
fn hue_ch(p: f32, q: f32, t0: f32) -> f32 {
let t = rem_euclid(t0, 1.0);
if (t < 1.0 / 6.0) { return p + (q - p) * 6.0 * t; }
@@ -677,7 +691,8 @@ fn adjust(c: vec3<f32>) -> vec3<f32> {
ds = lut(1, hsl.x);
dl = lut(2, hsl.x);
}
let sat = clamp(p0.y + ds, -1.0, 1.0);
let sat = clamp(p0.y, -1.0, 1.0);
let rs = clamp(ds, -1.0, 1.0);
let light = clamp(p0.z, -1.0, 1.0);
if (p0.w > 0.5) {
// compose::adjust::hue_saturation: Colorize at the lightened / darkened lightness.
@@ -694,24 +709,13 @@ fn adjust(c: vec3<f32>) -> vec3<f32> {
if (hdr) {
// 32-bit: the chroma scaled around the lightness, unclipped, by Photoshop's factor
// in whole numbers (compose::adjust::hue_saturation_in, hdr_saturation_factor).
var k: f32;
if (sat <= 0.0) {
k = 1.0 - floor(-sat * 256.0 + 1e-4) / 256.0;
} else {
let steps = min(u32(floor(sat * 254.0 + 1e-4)), 254u);
k = f32(min(65280u / (255u - steps), 32767u)) / 256.0;
}
let k = hdr_saturation_factor(sat) * hdr_saturation_factor(rs);
let d = (max(max(lc.r, lc.g), lc.b) - min(min(lc.r, lc.g), lc.b)) * k;
let lo = lh.z - d / 2.0;
let hi = lh.z + d / 2.0;
rgb = vec3(hue_ch(lo, hi, hh + 1.0 / 3.0), hue_ch(lo, hi, hh), hue_ch(lo, hi, hh - 1.0 / 3.0));
} else {
// Raising saturation divides by 1 - s; lowering it multiplies by 1 + s.
if (sat > 0.0) {
ss = clamp(lh.y / max(1.0 - sat, 1.0 / 512.0), 0.0, 1.0);
} else {
ss = clamp(lh.y * (1.0 + sat), 0.0, 1.0);
}
ss = clamp(scale_saturation(scale_saturation(lh.y, sat), rs), 0.0, 1.0);
rgb = hsl_to_rgb(hh, ss, lh.z);
}
// compose::adjust::range_lightness: towards the brightest channel when raised, the darkest when lowered.
+6
View File
@@ -858,6 +858,12 @@ fn adjustment_layers() {
let hs = Adjustment::HueSaturation { hue: 40.0, saturation: 25.0, lightness: -10.0, colorize: false, ranges: HueRange::defaults() };
d.layers.push(Layer::new("hs2", LayerContent::Adjustment(hs)));
check(&mut g, &d, &format!("hue/saturation {depth:?}"));
// A range's saturation multiplies the master's factor (#14741).
let hs = Adjustment::HueSaturation { hue: 0.0, saturation: 60.0, lightness: 0.0, colorize: false, ranges: hue_ranges() };
d.layers.push(Layer::new("hs3", LayerContent::Adjustment(hs)));
check(&mut g, &d, &format!("hue/saturation ranges {depth:?}"));
// Off again: stacked under Threshold, it puts a 16-bit sample on the level's edge.
d.layers.pop();
// Threshold: whole 16-bit samples against the level in 16 bits, or the float luminosity.
d.layers.push(Layer::new("th", LayerContent::Adjustment(Adjustment::Threshold { level: 0.5 })));
check(&mut g, &d, &format!("threshold {depth:?}"));