mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
fix(engine): name a Gradient mode drag into a layer mask (#17519)
A Gradient mode drag with a layer mask targeted recorded its History state as "Gradient"; the reference application names it "Draw gradient on mask". The step name now follows the paint target, and every other target (pixels, an alpha channel, Quick Mask, a filter mask) keeps today's name. Co-Authored-By: Codebuff <[email protected]>
This commit is contained in:
1 parent
e0b1d8ff5f
commit
d445d93026
3 files changed
+93
-1
No files matched your search
@@ -15,6 +15,8 @@ pub mod fill_cmds;
|
||||
pub mod fill_key_cmds;
|
||||
#[cfg(test)]
|
||||
mod gradient_alpha_method_tests;
|
||||
#[cfg(test)]
|
||||
mod gradient_mask_history_name_tests;
|
||||
pub mod paint_cmds;
|
||||
pub mod pick_cmds;
|
||||
pub mod redeye_cmds;
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
//! #17519: a Gradient mode drag with a layer mask targeted records the History state
|
||||
//! "Draw gradient on mask"; the same drag on a layer's pixels, on an alpha channel or on the
|
||||
//! Quick Mask keeps the command's own name, as does the recorded command's label.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::paint_cmds::{GRADIENT_MASK_STEP, GRADIENT_STEP};
|
||||
use crate::Session;
|
||||
|
||||
fn session() -> Session {
|
||||
let mut s = crate::session();
|
||||
s.execute("file.new", json!({"width": 80, "height": 40, "background": "white"})).unwrap();
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn drag() -> Value {
|
||||
json!({"from": [0, 20], "to": [80, 20], "colors": ["#000000", "#ffffff"], "dither": false})
|
||||
}
|
||||
|
||||
fn drawn(extra: Value) -> (Session, String) {
|
||||
let mut s = session();
|
||||
if extra.get("target").and_then(Value::as_str) == Some("mask") {
|
||||
s.execute("layer.layerMask.revealAll", json!({})).unwrap();
|
||||
}
|
||||
let mut p = drag();
|
||||
for (k, v) in extra.as_object().unwrap() {
|
||||
p[k] = v.clone();
|
||||
}
|
||||
s.execute("paint.gradient", p).unwrap();
|
||||
let step = s.active().unwrap().history.entries().last().expect("one History state").to_string();
|
||||
(s, step)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_drag_into_a_layer_mask_is_draw_gradient_on_mask() {
|
||||
let (s, step) = drawn(json!({"target": "mask"}));
|
||||
assert_eq!(step, GRADIENT_MASK_STEP);
|
||||
assert_eq!(GRADIENT_MASK_STEP, "Draw gradient on mask");
|
||||
let st = s.active().unwrap();
|
||||
let layer = st.doc.layer(st.active_layer.unwrap()).unwrap();
|
||||
// The mask took the paint; the layer's own pixels did not.
|
||||
let mask = layer.mask.as_ref().expect("a layer mask").surface.pixel(40, 20);
|
||||
assert!(mask[0] < 0.5, "the mask is black where the gradient ends: {mask:?}");
|
||||
if let photocraft_doc::LayerContent::Raster(surface) = &layer.content {
|
||||
assert_eq!(surface.pixel(40, 20)[3], 0.0, "the pixels stay transparent");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_drag_into_the_pixels_keeps_the_gradient_name() {
|
||||
let (_s, step) = drawn(json!({}));
|
||||
assert_eq!(step, GRADIENT_STEP);
|
||||
assert_eq!(GRADIENT_STEP, "Gradient");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_other_targets_that_were_not_measured_keep_the_gradient_name() {
|
||||
let mut s = session();
|
||||
s.execute("channel.new", json!({"name": "Alpha 1"})).unwrap();
|
||||
let p = {
|
||||
let mut p = drag();
|
||||
p["target"] = json!({"channel": 0});
|
||||
p
|
||||
};
|
||||
s.execute("paint.gradient", p).unwrap();
|
||||
let h = &s.active().unwrap().history;
|
||||
assert_eq!(h.entries().last().unwrap().to_string(), GRADIENT_STEP);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_recorded_command_keeps_its_label() {
|
||||
crate::install().unwrap();
|
||||
let spec = crate::commands::find("paint.gradient").expect("the command");
|
||||
assert_eq!(spec.label, GRADIENT_STEP);
|
||||
}
|
||||
@@ -199,7 +199,7 @@ fn gradient(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let blend = p.get("mode").and_then(Value::as_str).and_then(parse_blend).unwrap_or(photocraft_color::BlendMode::Normal);
|
||||
let dither = b(p, "dither", true); // Photoshop dithers gradients by default (reduces banding).
|
||||
let layer = if p.get("layer").is_some() { Some(crate::commands::layer_param(s, p)?) } else { s.active().and_then(|st| st.active_layer) };
|
||||
s.edit("Gradient", |doc, _| {
|
||||
s.edit(step_name(p), |doc, _| {
|
||||
let sel = doc.selection.clone();
|
||||
let area = sel.as_ref().map(|m| m.content_bounds()).filter(|r| !r.is_empty()).unwrap_or_else(|| doc.bounds()).intersect(&doc.bounds());
|
||||
let (surf, lock) = crate::channel_cmds::target_surface(doc, layer, p)?;
|
||||
@@ -209,6 +209,20 @@ fn gradient(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
/// The History step `p` makes: a drag into a layer mask names the mask (#17519); every other
|
||||
/// target keeps the command's own name.
|
||||
pub(crate) fn step_name(p: &Value) -> &'static str {
|
||||
match crate::channel_cmds::target_of(p) {
|
||||
crate::channel_cmds::Target::Mask => GRADIENT_MASK_STEP,
|
||||
_ => GRADIENT_STEP,
|
||||
}
|
||||
}
|
||||
|
||||
/// The Gradient tool's step on a layer's own pixels.
|
||||
pub(crate) const GRADIENT_STEP: &str = "Gradient";
|
||||
/// The Gradient tool's step on a layer mask (#17519).
|
||||
pub(crate) const GRADIENT_MASK_STEP: &str = "Draw gradient on mask";
|
||||
|
||||
/// Smooth's colour and opacity stops baked apart, then joined. Both bakes sample the same
|
||||
/// positions, so the alpha is multiplied in at each one: [`gradients::apply_opacity`] would cut
|
||||
/// every interval into four (385 stops instead of 97), and `paint_gradient` scans the stops for
|
||||
|
||||
Reference in new issue
Block a user