PR #12644: keep the new transparency curve to the measured case

Smooth's opacity curve was measured only in the Gradient tool at 100 %
Smoothness. The split path also changed Classic, Perceptual and Linear,
Smooth below 100 %, and (through bake_opacity) Gradient Overlay and PSD
Smooth gradients; #12010 says unmeasured cases keep today's behaviour.

- paint.gradient bakes the opacity apart only for Smooth at 100 %
  Smoothness; everything else keeps the merged stops.
- bake_opacity maps Smooth to Classic again; the measured curve is the
  separate bake_smooth_opacity, used only by the Gradient tool.
- A single colour stop with a transparency ramp now follows the curve;
  legacy `colors` with their own alpha keep the merged stops.
- bake_smooth_opacity clamps constant stops.

Tests pin today's ramp for the other methods and Smoothness 50, and
cover the single-colour, RGBA-colour and constant-stop cases.

Co-Authored-By: x-labb <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
committed 2026-11-11 02:16:17 +01:00
1 parent d8c06f4597
commit 0f76df26c4
4 files changed
+99 -49

No files matched your search

@@ -67,11 +67,41 @@ fn opaque_gradients_paint_the_same_with_a_method() {
assert_ne!(smooth, paint(json!({})));
}
// Not measured in the Gradient tool: the other methods, and Smooth below 100 % Smoothness, keep
// today's merged stops (each dense stop takes the nearest stop's alpha).
#[test]
fn a_transparent_ramp_with_other_methods_still_paints() {
for m in ["classic", "perceptual", "linear"] {
let s = drawn(json!({"method": m}));
let (a, b) = (alpha_at(&s, 250), alpha_at(&s, 550));
assert!(a > b && (0.0..=255.0).contains(&a), "{m}: {a} {b}");
fn unmeasured_methods_and_smoothness_keep_todays_ramp() {
for extra in [json!({"method": "classic"}), json!({"method": "perceptual"}), json!({"method": "linear"}), json!({"method": "smooth", "smoothness": 50})] {
let s = drawn(extra.clone());
for (x, want) in [(175, 250.0), (250, 191.0), (400, 128.0), (550, 64.0)] {
let got = alpha_at(&s, x);
assert!((got - want).abs() <= 1.0, "{extra} x={x}: alpha {got:.1}, want {want}");
}
}
}
fn drawn_colors(colors: Value) -> Session {
let mut s = Session::new();
s.execute("file.new", json!({"width": 800, "height": 600, "background": "transparent"})).unwrap();
let p = json!({"colors": colors, "transparency": [[0, 100], [1, 0]], "method": "smooth", "from": [100, 300], "to": [700, 300], "dither": false});
s.execute("paint.gradient", p).unwrap();
s
}
#[test]
fn one_colour_with_a_transparency_ramp_follows_smooth() {
let s = drawn_colors(json!(["#ff0000"]));
for (x, want) in [(50, 255.0), (250, 179.0), (400, 100.0), (550, 35.0), (750, 0.0)] {
let got = alpha_at(&s, x);
assert!((got - want).abs() <= 2.0, "x={x}: alpha {got:.1}, want {want}");
}
}
#[test]
fn legacy_colours_keep_their_own_alpha() {
let s = drawn_colors(json!(["#ff000080", "#ff000080"]));
for (x, want) in [(50, 128.0), (400, 64.0), (750, 0.0)] {
let got = alpha_at(&s, x);
assert!((got - want).abs() <= 1.0, "x={x}: alpha {got:.1}, want {want}");
}
}
+43 -24
View File
@@ -145,36 +145,25 @@ fn gradient(s: &mut Session, p: &Value) -> Result<Value> {
let stops = match p.get("method").and_then(Value::as_str).and_then(photocraft_io::GradientMethod::from_name) {
Some(m) if stops.len() >= 2 => {
let smoothness = (f(p, "smoothness", 100.0) / 100.0).clamp(0.0, 1.0);
// With transparency, the colour and opacity stops are baked apart: the colours by the
// method, the opacity by the method's own transparency curve (#12010). Merged stops
// would ease each of their sub-segments again and take the nearest stop's alpha.
let split = match gradients::tool_fill_stops(s, p)? {
Some((cs, os)) => Some((cs, os)),
None => {
// The legacy `colors` list, evenly spaced.
let os = gradients::transparency_param(p, "paint.gradient")?.unwrap_or_default();
let colors: Vec<[f32; 4]> =
p.get("colors").and_then(Value::as_array).map(|a| a.iter().map(|v| color(Some(v), fg)).collect()).unwrap_or_default();
let n = colors.len();
let cs = colors
.iter()
.enumerate()
.map(|(i, c)| (if n > 1 { i as f32 / (n - 1) as f32 } else { 0.0 }, photocraft_color::Color::rgb(c[0], c[1], c[2])))
.collect();
Some((cs, os))
}
};
// Smooth at 100 % Smoothness bakes the colour and opacity stops apart: the colours by
// the method, the opacity by Smooth's measured transparency curve (#12010). Merged
// stops would take the nearest stop's alpha. The other methods and Smoothness values
// were not measured in the Gradient tool and keep the merged stops.
let split = if m == photocraft_io::GradientMethod::Smooth && smoothness >= 1.0 { split_stops(s, p, fg)? } else { None };
match split {
Some((cs, os)) if os.iter().any(|o| (o.1 - 1.0).abs() > 1e-6) && cs.len() >= 2 => {
let mids = vec![0.5; cs.len() - 1];
Some((cs, os)) => {
let mids = vec![0.5; cs.len().saturating_sub(1)];
let colours: Vec<(f32, [f32; 4])> = photocraft_io::bake_gradient(cs, &mids, smoothness, m)
.into_iter()
.map(|(t, c)| (t, [c.to_rgb()[0], c.to_rgb()[1], c.to_rgb()[2], 1.0]))
.map(|(t, c)| {
let rgb = c.to_rgb();
(t, [rgb[0], rgb[1], rgb[2], 1.0])
})
.collect();
let omids = vec![0.5; os.len().saturating_sub(1)];
gradients::apply_opacity(colours, &photocraft_io::bake_gradient_opacity(os, &omids, smoothness, m))
gradients::apply_opacity(colours, &photocraft_io::bake_gradient_smooth_opacity(os, &omids))
}
_ => {
None => {
let colors = stops.iter().map(|(t, c)| (*t, photocraft_color::Color::rgba(c[0], c[1], c[2], c[3]))).collect();
let mids = vec![0.5; stops.len() - 1];
photocraft_io::bake_gradient(colors, &mids, smoothness, m)
@@ -204,6 +193,36 @@ fn gradient(s: &mut Session, p: &Value) -> Result<Value> {
Ok(Value::Null)
}
/// The Gradient tool's colour stops (opaque) and opacity stops kept apart, for Smooth's own
/// transparency curve. `None` when the opacity is constant, or when legacy `colors` carry their
/// own alpha (that case keeps the merged stops). A single colour stop spans the whole gradient.
fn split_stops(s: &Session, p: &Value, fg: [f32; 4]) -> Result<Option<gradients::FillStops>> {
let (mut cs, os) = match gradients::tool_fill_stops(s, p)? {
Some(split) => split,
None => {
// The legacy `colors` list, evenly spaced.
let colors: Vec<[f32; 4]> = p.get("colors").and_then(Value::as_array).map(|a| a.iter().map(|v| color(Some(v), fg)).collect()).unwrap_or_default();
if colors.iter().any(|c| c[3] < 1.0) {
return Ok(None);
}
let n = colors.len();
let cs = colors
.iter()
.enumerate()
.map(|(i, c)| (if n > 1 { i as f32 / (n - 1) as f32 } else { 0.0 }, photocraft_color::Color::rgb(c[0], c[1], c[2])))
.collect();
(cs, gradients::transparency_param(p, "paint.gradient")?.unwrap_or_default())
}
};
if os.iter().all(|o| (o.1 - 1.0).abs() < 1e-6) {
return Ok(None);
}
if let [(_, c)] = cs.as_slice() {
cs = vec![(0.0, *c), (1.0, *c)];
}
Ok(if cs.is_empty() { None } else { Some((cs, os)) })
}
/// Paint helper command specs.
pub fn specs() -> Vec<CommandSpec> {
vec![
+20 -19
View File
@@ -195,11 +195,10 @@ const OPACITY_RESPONSE: f32 = 0.65;
/// Dense linear opacity stops reproducing Photoshop's interpolation of a gradient's transparency
/// (Smoothness, opacity midpoints and the method, see [`OPACITY_RESPONSE`]). Returns the input
/// unchanged when it already interpolates linearly. Smooth follows [`bake_smooth_opacity`].
/// unchanged when it already interpolates linearly. Smooth keeps Classic's opacity curve here: it
/// was measured only in the Gradient tool at 100 % Smoothness ([`bake_smooth_opacity`]).
pub fn bake_opacity(stops: Vec<(f32, f32)>, midpoints: &[f32], smoothness: f32, method: Method) -> Vec<(f32, f32)> {
if method == Method::Smooth {
return bake_smooth_opacity(stops, midpoints);
}
let method = if method == Method::Smooth { Method::Classic } else { method };
let plain_mids = midpoints.iter().all(|m| (m - 0.5).abs() < 1e-3);
let smoothness = if smoothness.is_finite() { smoothness.clamp(0.0, 1.0) } else { 0.0 };
if stops.len() < 2 || (smoothness <= 0.0 && method == Method::Classic && plain_mids) {
@@ -246,22 +245,17 @@ const SMOOTH_OPACITY_GAMMA: f32 = 2.2;
/// Smooth's transparency ramp: each opacity stop is a grey whose Oklab lightness is
/// `(opacity^2.2)^(1/3)`, which the colours' measured Smooth easing ([`SMOOTH_EASE`]) crosses
/// segment by segment, and the linear value `y = L^3` comes out as opacity `y^(1/2.2)`. Smoothness
/// doesn't change Smooth; opacity midpoints still move each segment's centre. Constant opacity
/// comes back unchanged.
fn bake_smooth_opacity(stops: Vec<(f32, f32)>, midpoints: &[f32]) -> Vec<(f32, f32)> {
/// segment by segment, and the linear value `y = L^3` comes out as opacity `y^(1/2.2)`. Measured
/// in the Gradient tool at 100 % Smoothness with 50 % midpoints, the only case that uses it.
/// Constant opacity comes back as it is, clamped to 0..=1.
pub fn bake_smooth_opacity(stops: Vec<(f32, f32)>, midpoints: &[f32]) -> Vec<(f32, f32)> {
let clamp = |o: f32| if o.is_finite() { o.clamp(0.0, 1.0) } else { 1.0 };
if stops.len() < 2 || stops.windows(2).all(|w| w[0].1 == w[1].1) {
return stops;
return stops.into_iter().map(|(t, o)| (t, clamp(o))).collect();
}
let mut s = stops;
s.sort_by(|a, b| a.0.total_cmp(&b.0));
let pts: Vec<(f32, [f32; 3])> = s
.iter()
.map(|(t, o)| {
let o = if o.is_finite() { o.clamp(0.0, 1.0) } else { 1.0 };
(*t, [o.powf(SMOOTH_OPACITY_GAMMA).cbrt(); 3])
})
.collect();
let pts: Vec<(f32, [f32; 3])> = s.iter().map(|(t, o)| (*t, [clamp(*o).powf(SMOOTH_OPACITY_GAMMA).cbrt(); 3])).collect();
const N: usize = 96;
let mut baked: Vec<(f32, f32)> = (0..=N)
.map(|k| {
@@ -318,11 +312,10 @@ mod tests {
}
#[test]
fn plain_classic_and_constant_smooth_opacity_stay_as_stored() {
fn plain_classic_and_smooth_opacity_stay_as_stored() {
let stops = vec![(1.0, 0.0), (0.0, 1.0)];
assert_eq!(bake_opacity(stops.clone(), &[0.5], 0.0, Method::Classic), stops);
let flat = vec![(0.0, 0.4), (1.0, 0.4)];
assert_eq!(bake_opacity(flat.clone(), &[0.5], 0.0, Method::Smooth), flat);
assert_eq!(bake_opacity(stops.clone(), &[0.5], 0.0, Method::Smooth), stops);
assert_eq!(bake_opacity(vec![(0.5, 0.3)], &[], 1.0, Method::Linear), vec![(0.5, 0.3)]);
}
@@ -431,6 +424,14 @@ mod tests {
}
/// `eval` guards its own stops instead of relying on `bake`'s two-stop check.
#[test]
fn smooth_opacity_clamps_constant_stops() {
let flat = vec![(0.0, 0.4), (1.0, 0.4)];
assert_eq!(bake_smooth_opacity(flat.clone(), &[0.5]), flat);
assert_eq!(bake_smooth_opacity(vec![(0.0, 2.0), (1.0, 2.0)], &[0.5]), vec![(0.0, 1.0), (1.0, 1.0)]);
assert_eq!(bake_smooth_opacity(vec![(0.5, f32::NAN)], &[]), vec![(0.5, 1.0)]);
}
#[test]
fn eval_with_fewer_than_two_stops_does_not_index_out_of_range() {
assert_eq!(eval(&[], &[], 1.0, 0.5, Method::Smooth), [0.0; 3]);
+1 -1
View File
@@ -53,7 +53,7 @@ mod flat;
mod gradient_bake;
pub mod guides_map;
mod png_steps;
pub use gradient_bake::{Method as GradientMethod, bake as bake_gradient, bake_opacity as bake_gradient_opacity};
pub use gradient_bake::{Method as GradientMethod, bake as bake_gradient, bake_smooth_opacity as bake_gradient_smooth_opacity};
mod duotone_map;
mod kra;
pub mod linked;