mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Merge pull request #12951 from rmsff/fix/art-history-mode-mismatch-12826
Art History Brush: refuse a source of another colour mode
This commit is contained in:
4 files changed
+80
-6
No files matched your search
@@ -406,7 +406,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
menu: &[],
|
||||
shortcut: None,
|
||||
params: brush_params!(
|
||||
r#","state":row (a History panel row, as history.setSource),"snapshot":id (a history.newSnapshot id; default: the History panel's source, as paint.historyBrush; a source of another canvas size is an error),"style":"tightShort|tightMedium|tightLong|looseMedium|looseLong|dab|tightCurl|looseCurl"="tightMedium","area":0..100=50,"tolerance":0..100=0,"opacity":0..100=100 (overrides the common 1..100: how strongly the history state is painted; 0 changes nothing; no flow, mode is Normal)"#
|
||||
r#","state":row (a History panel row, as history.setSource),"snapshot":id (a history.newSnapshot id; default: the History panel's source, as paint.historyBrush; a source of another canvas size or colour mode is an error),"style":"tightShort|tightMedium|tightLong|looseMedium|looseLong|dab|tightCurl|looseCurl"="tightMedium","area":0..100=50,"tolerance":0..100=0,"opacity":0..100=100 (overrides the common 1..100: how strongly the history state is painted; 0 changes nothing; no flow, mode is Normal)"#
|
||||
),
|
||||
enabled: has_pixel_layer,
|
||||
run: art_history::run,
|
||||
|
||||
@@ -195,7 +195,15 @@ pub fn run(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (stroke, id) = parse_brush(s, p, CMD)?;
|
||||
let _ = stroke;
|
||||
// The same source as the History Brush: the `state` or `snapshot` passed, else the History
|
||||
// panel's source, refused after a canvas size change.
|
||||
// panel's source, refused after a canvas size change, then after a colour mode change, as
|
||||
// Photoshop does (#12826), before a layer missing from it. Compare document modes: the pixel
|
||||
// format reads Indexed and Multichannel as RGB.
|
||||
let st = s.active().ok_or(EngineError::NoDocument)?;
|
||||
if crate::history_source_cmds::paint_source(st, p, CMD)?.mode != st.doc.mode {
|
||||
return Err(EngineError::Other(
|
||||
"the current color mode does not match that of the History Brush source: set a source of this color mode in the History panel".into(),
|
||||
));
|
||||
}
|
||||
let src_surface = super::history_source(s, id, p, CMD)?;
|
||||
// Truncating a huge row index only changes the seed.
|
||||
let seed = RNG_SEED ^ p.get("state").and_then(Value::as_u64).map_or(0, |i| i as u32);
|
||||
@@ -535,3 +543,7 @@ mod tests {
|
||||
#[cfg(test)]
|
||||
#[path = "art_history_noop_state_tests.rs"]
|
||||
mod art_history_noop_state_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "art_history_mode_mismatch_tests.rs"]
|
||||
mod art_history_mode_mismatch_tests;
|
||||
@@ -0,0 +1,62 @@
|
||||
//! The Art History Brush refuses a source state from before Image › Mode changed the document's
|
||||
//! colour mode, as Photoshop does, for a normal stroke and for a click the tolerance holds back:
|
||||
//! no state, no pixel change (#12826).
|
||||
|
||||
use super::art_history_noop_state_tests::{held_back_session, pixels, steps, stroke};
|
||||
use super::*;
|
||||
|
||||
const MODES: [&str; 3] = ["image.mode.grayscale", "image.mode.cmyk", "image.mode.lab"];
|
||||
|
||||
/// Indexed Color flattens the document, so the source no longer holds the active layer: the
|
||||
/// refusal still names the colour mode, which its RGB pixel format would hide. (Multichannel leaves
|
||||
/// no layer, so the command is unavailable there before any source is read.)
|
||||
#[test]
|
||||
fn a_source_of_another_color_mode_is_refused() {
|
||||
let cases = [8u64, 16].into_iter().flat_map(|depth| MODES.map(move |mode| (depth, mode)));
|
||||
for (depth, mode) in cases.chain([(8, "image.mode.indexedColor")]) {
|
||||
// Tolerance 0 paints; 90 holds this small change back, which #12636 records as a state.
|
||||
for tolerance in [0, 90] {
|
||||
// Row 1 (RGB) passed, and the History panel's default source, the RGB Open state.
|
||||
for state in [json!(1), Value::Null] {
|
||||
let mut s = held_back_session(depth);
|
||||
s.execute(mode, json!({})).unwrap();
|
||||
let (before, n) = (s.active().unwrap().doc.clone(), steps(&s));
|
||||
let case = format!("depth {depth} {mode} tolerance {tolerance} state {state}");
|
||||
let e = stroke(&mut s, json!({"tolerance": tolerance, "state": state})).unwrap_err().to_string();
|
||||
assert!(e.contains("color mode does not match"), "{case}: {e}");
|
||||
assert_eq!(steps(&s), n, "{case}: no state");
|
||||
assert!(s.active().unwrap().doc == before, "{case}: no pixel changed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_source_in_the_current_color_mode_still_paints() {
|
||||
for depth in [8u64, 16] {
|
||||
for mode in MODES {
|
||||
let mut s = held_back_session(depth);
|
||||
s.execute(mode, json!({})).unwrap();
|
||||
let row = s.active().unwrap().history.entries().len() - 1;
|
||||
s.execute("edit.fill", json!({"color": "#ffffff"})).unwrap();
|
||||
let (before, n) = (pixels(&s), steps(&s));
|
||||
let r = stroke(&mut s, json!({"tolerance": 0, "state": row})).unwrap();
|
||||
assert!(!r["damage"].is_null(), "depth {depth} {mode}: {r}");
|
||||
assert_eq!(steps(&s), n + 1, "depth {depth} {mode}");
|
||||
assert_ne!(pixels(&s), before, "depth {depth} {mode}: the source state painted");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Only the colour mode is refused: a source of another bit depth converts as before (not measured
|
||||
/// for this brush; today's behaviour).
|
||||
#[test]
|
||||
fn a_source_of_another_bit_depth_still_paints() {
|
||||
let mut s = held_back_session(8);
|
||||
s.execute("image.mode.bits16", json!({})).unwrap();
|
||||
let (before, n) = (pixels(&s), steps(&s));
|
||||
let r = stroke(&mut s, json!({"tolerance": 0})).unwrap();
|
||||
assert!(!r["damage"].is_null(), "{r}");
|
||||
assert_eq!(steps(&s), n + 1);
|
||||
assert_ne!(pixels(&s), before);
|
||||
}
|
||||
@@ -23,23 +23,23 @@ fn fill_gray(s: &mut Session, v: f32) {
|
||||
}
|
||||
|
||||
/// History row 1 is grey 0.5, the layer now grey 0.6: a small change every high tolerance holds back.
|
||||
fn held_back_session(depth: u64) -> Session {
|
||||
pub(super) fn held_back_session(depth: u64) -> Session {
|
||||
let mut s = session(depth);
|
||||
fill_gray(&mut s, 0.5);
|
||||
fill_gray(&mut s, 0.6);
|
||||
s
|
||||
}
|
||||
|
||||
fn pixels(s: &Session) -> Vec<f32> {
|
||||
pub(super) fn pixels(s: &Session) -> Vec<f32> {
|
||||
let d = s.active().unwrap();
|
||||
d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().read_region(d.doc.bounds())
|
||||
}
|
||||
|
||||
fn steps(s: &Session) -> usize {
|
||||
pub(super) fn steps(s: &Session) -> usize {
|
||||
s.active().unwrap().history.past_len()
|
||||
}
|
||||
|
||||
fn stroke(s: &mut Session, extra: Value) -> Result<Value> {
|
||||
pub(super) fn stroke(s: &mut Session, extra: Value) -> Result<Value> {
|
||||
let mut p = json!({"points": [[8, 16], [40, 16]], "size": 12, "hardness": 100, "area": 50, "style": "tightMedium", "state": 1, "tolerance": 90});
|
||||
if let Some(map) = extra.as_object() {
|
||||
for (k, v) in map {
|
||||
|
||||
Reference in new issue
Block a user