Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Hue/Saturation: Colorize at 0% saturation gives grey, not a 25% tint (#2343)

LloydNicholsond166450c771e
diff --git a/crates/compose/src/adjust.rs b/crates/compose/src/adjust.rs
index b4d213510..deca6f7d5 100644
--- a/crates/compose/src/adjust.rs
+++ b/crates/compose/src/adjust.rs
@@ -307,12 +307,13 @@ pub fn hue_range_tables(ranges: &[HueRange; 6]) -> [Vec<f32>; 3] {
}
/// One Hue/Saturation evaluation: hue shift in degrees, saturation and lightness in -1..=1.
-/// Colorize sets the hue and saturation instead of shifting them.
+/// 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] {
let (mut hh, mut ss, ll) = rgb_to_hsl(c);
if colorize {
hh = hue.rem_euclid(360.0) / 360.0;
- ss = s.abs().max(0.25);
+ ss = s.abs().min(1.0);
} else {
hh = (hh + hue / 360.0).rem_euclid(1.0);
ss = (ss * (1.0 + s)).clamp(0.0, 1.0);
@@ -970,6 +971,30 @@ mod tone_tests {
assert_eq!(b.px[2], [0.5, 0.5, 0.5, 1.0]);
}
+ #[test]
+ fn colorize_at_zero_saturation_is_grey() {
+ // Photoshop: Colorize at 0% saturation leaves no colour, only the pixel's lightness (#2326).
+ let px = vec![[0.9, 0.2, 0.1, 1.0], [0.1, 0.6, 0.8, 1.0], [0.5, 0.5, 0.5, 1.0]];
+ for depth in [None, Some(SampleType::U8), Some(SampleType::U16), Some(SampleType::F32)] {
+ for lightness in [0.0, 30.0, -30.0] {
+ let a = Adjustment::HueSaturation { hue: 200.0, saturation: 0.0, lightness, colorize: true, ranges: HueRange::defaults() };
+ let mut b = Buffer { rect: Rect::new(0, 0, 3, 1), px: px.clone() };
+ apply_depth(&a, &mut b, Transfer::Srgb, depth);
+ for p in &b.px {
+ assert!((p[0] - p[1]).abs() < 1e-4 && (p[1] - p[2]).abs() < 1e-4, "{depth:?} lightness {lightness}: {p:?}");
+ }
+ }
+ // Low saturations still tint: the old 25% floor no longer hides them.
+ let tint = |saturation: f32| {
+ let a = Adjustment::HueSaturation { hue: 0.0, saturation, lightness: 0.0, colorize: true, ranges: HueRange::defaults() };
+ let mut b = Buffer { rect: Rect::new(0, 0, 1, 1), px: vec![[0.5, 0.5, 0.5, 1.0]] };
+ apply_depth(&a, &mut b, Transfer::Srgb, depth);
+ b.px[0][0] - b.px[0][2]
+ };
+ assert!(tint(10.0) > 0.05 && tint(10.0) < tint(25.0), "{depth:?}: 10% {} vs 25% {}", tint(10.0), tint(25.0));
+ }
+ }
+
#[test]
fn black_white_weights() {
let d = [40.0, 60.0, 40.0, 60.0, 20.0, 80.0];
diff --git a/crates/engine/src/adjust_params.rs b/crates/engine/src/adjust_params.rs
index 7bad00f44..00fb67dd0 100644
--- a/crates/engine/src/adjust_params.rs
+++ b/crates/engine/src/adjust_params.rs
@@ -391,6 +391,9 @@ fn hue_saturation(r: &Reader<'_>, base: Option<&Adjustment>) -> Result<Adjustmen
let colorize = r.boolean("colorize", c)?;
// Colorize hue is 0..360, a shift is -180..180.
let hue = if colorize { r.num("hue", h.rem_euclid(360.0), 0.0, 360.0)? } else { r.num("hue", h, -180.0, 180.0)? };
+ // Turning Colorize on starts at 25% saturation unless one is given, like the dialog's
+ // checkbox and Photoshop; an explicit 0 stays 0, which colorizes to grey (#2326).
+ let s = if colorize && !c { s.max(25.0) } else { s };
let (saturation, lightness) = (r.num("saturation", s, if colorize { 0.0 } else { -100.0 }, 100.0)?, r.num("lightness", l, -100.0, 100.0)?);
for (i, key) in HUE_RANGES.iter().enumerate() {
let Some(o) = r.object(key)? else { continue };
@@ -880,6 +883,13 @@ mod tests {
assert!(matches!(a, Adjustment::BlackWhite { weights, tint: Some(_) } if weights[0] == 120.0 && weights[1] == 60.0));
let a = from_params("hueSaturation", &json!({"blues": {"hue": 40}}), None, ColorMode::Rgb).unwrap();
assert!(matches!(&a, Adjustment::HueSaturation { ranges, .. } if ranges[4].hue == 40.0 && ranges[0].is_neutral()));
+ // Colorize without a saturation starts at 25%; an explicit 0 and an already colorized base are kept.
+ let a = from_params("hueSaturation", &json!({"colorize": true, "hue": 30}), None, ColorMode::Rgb).unwrap();
+ assert!(matches!(a, Adjustment::HueSaturation { colorize: true, saturation: 25.0, .. }), "{a:?}");
+ let a = from_params("hueSaturation", &json!({"colorize": true, "saturation": 0}), None, ColorMode::Rgb).unwrap();
+ assert!(matches!(a, Adjustment::HueSaturation { colorize: true, saturation: 0.0, .. }), "{a:?}");
+ let a = from_params("hueSaturation", &json!({"hue": 120}), Some(&a), ColorMode::Rgb).unwrap();
+ assert!(matches!(a, Adjustment::HueSaturation { colorize: true, saturation: 0.0, hue: 120.0, .. }), "{a:?}");
let a = from_params("gradientMap", &json!({"stops": [[1, "#ffffff"], [0, "#000000"]], "dither": true}), None, ColorMode::Rgb).unwrap();
assert!(matches!(&a, Adjustment::GradientMap { stops, dither: true, .. } if stops[0].0 == 0.0));
// CMYK curves: ink channels; Lab: lightness without a composite.
diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgsl
index 48fb3bae7..b85df6fb9 100644
--- a/crates/gpu/src/compose.wgsl
+++ b/crates/gpu/src/compose.wgsl
@@ -617,7 +617,7 @@ fn adjust(c: vec3<f32>) -> vec3<f32> {
let light = clamp(p0.z + dl, -1.0, 1.0);
if (p0.w > 0.5) {
hh = rem_euclid(p0.x, 360.0) / 360.0;
- ss = max(abs(sat), 0.25);
+ ss = min(abs(sat), 1.0);
} else {
hh = rem_euclid(hsl.x + (p0.x + dh) / 360.0, 1.0);
ss = clamp(hsl.y * (1.0 + sat), 0.0, 1.0);
diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rs
index 530bc8b5b..ee04cd898 100644
--- a/crates/gpu/tests/parity.rs
+++ b/crates/gpu/tests/parity.rs
@@ -251,6 +251,7 @@ fn adjustments() -> Vec<Adjustment> {
},
Adjustment::HueSaturation { hue: 40.0, saturation: 30.0, lightness: -10.0, colorize: false, ranges: HueRange::defaults() },
Adjustment::HueSaturation { hue: 200.0, saturation: 50.0, lightness: 20.0, colorize: true, ranges: HueRange::defaults() },
+ Adjustment::HueSaturation { hue: 30.0, saturation: 0.0, lightness: -15.0, colorize: true, ranges: HueRange::defaults() },
Adjustment::HueSaturation { hue: -10.0, saturation: 10.0, lightness: 5.0, colorize: false, ranges: hue_ranges() },
Adjustment::Vibrance { vibrance: 50.0, saturation: -20.0 },
Adjustment::Vibrance { vibrance: -60.0, saturation: 0.0 },