Merge PR #8728 into integration batch

This commit is contained in:
Federico Olivares committed 2026-10-24 00:51:20 -03:00
commit 73f4129c0a
5 files changed
+156 -21

No files matched your search

+25 -13
View File
@@ -267,6 +267,19 @@ pub(crate) fn check_f32_values(cmd: &str, value: &Value) -> Result<()> {
check(cmd, value, 0)
}
/// `p[key]` as CMYK inks 0..1 (`[c, m, y, k]`), the values a colour was picked as, or `None`
/// when absent or null. A CMYK document stores exactly these instead of what the colour's sRGB
/// stand-in converts back to (#4196, #8568).
pub(crate) fn cmyk_param(cmd: &str, p: &Value, key: &str) -> Result<Option<[f32; 4]>> {
let Some(v) = p.get(key).filter(|v| !v.is_null()) else { return Ok(None) };
let inks: Option<Vec<f32>> =
v.as_array().filter(|a| a.len() == 4).and_then(|a| a.iter().map(|x| x.as_f64().filter(|x| x.is_finite()).map(|x| x.clamp(0.0, 1.0) as f32)).collect());
match inks.as_deref() {
Some(&[c, m, y, k]) => Ok(Some([c, m, y, k])),
_ => Err(bad(cmd, format!("`{key}` must be [c, m, y, k] numbers 0..1"))),
}
}
pub(crate) fn color_param(cmd: &str, p: &Value, key: &str, default: [f32; 4]) -> Result<[f32; 4]> {
Ok(match p.get(key) {
Some(Value::Array(a)) if a.len() >= 3 => {
@@ -1056,13 +1069,22 @@ fn build() -> Vec<CommandSpec> {
"Solid Color…",
["Layer", "New Fill Layer"],
None,
r##"{"color":"#rrggbb"=foreground,"path":"work"|saved path name|{…path}? (the active path: becomes the vector mask, making a shape; else the selection is the layer mask)}"##,
r##"{"color":"#rrggbb"=foreground,"cmyk":[c,m,y,k]? (inks 0..1 the colour was picked as: a CMYK document's fill holds exactly these; default the foreground's CMYK pick),"path":"work"|saved path name|{…path}? (the active path: becomes the vector mask, making a shape; else the selection is the layer mask)}"##,
has_doc,
|s, p| {
let c = color_param("layer.newFillLayer.solidColor", p, "color", s.tools.foreground)?;
// The inks the colour was picked as, so CMYK 50/50/50/100 stays 50/50/50/100 instead
// of what its sRGB stand-in converts back to through the document profile (#8568).
let inks = match cmyk_param("layer.newFillLayer.solidColor", p, "cmyk")? {
Some(k) => Some(k),
None => s.tools.picked_cmyk([c[0], c[1], c[2]]),
};
let default_mask = layer_panel_option(s, "useDefaultMasksOnFillLayers", true);
let id = s.edit("New Color Fill Layer", |doc, active| {
let fill = Fill::Solid(Color::rgba(c[0], c[1], c[2], c[3])).in_mode(doc.mode);
let fill = match inks {
Some(k) if doc.mode == ColorMode::Cmyk => Fill::Solid(Color { mode: ColorMode::Cmyk, c: k, alpha: c[3] }),
_ => Fill::Solid(Color::rgba(c[0], c[1], c[2], c[3])).in_mode(doc.mode),
};
let mut l = Layer::new(doc.next_layer_name("Color Fill"), LayerContent::Fill(fill));
mask_new_layer(doc, &mut l, p, "layer.newFillLayer.solidColor", default_mask)?;
let id = doc.insert_above(*active, l);
@@ -1201,17 +1223,7 @@ fn build() -> Vec<CommandSpec> {
r##"{"foreground":"#rrggbb"|[r,g,b,a?]?,"background":same?,"foregroundCmyk":[c,m,y,k]? (inks 0..1 the foreground was picked as: a CMYK document is filled with exactly these),"backgroundCmyk":same?}"##,
always,
|s, p| {
let cmyk = |key: &str| -> Result<Option<[f32; 4]>> {
let Some(v) = p.get(key).filter(|v| !v.is_null()) else { return Ok(None) };
let inks: Option<Vec<f32>> = v
.as_array()
.filter(|a| a.len() == 4)
.and_then(|a| a.iter().map(|x| x.as_f64().filter(|x| x.is_finite()).map(|x| x.clamp(0.0, 1.0) as f32)).collect());
match inks.as_deref() {
Some(&[c, m, y, k]) => Ok(Some([c, m, y, k])),
_ => Err(bad("tools.setColors", format!("`{key}` must be [c, m, y, k] numbers 0..1"))),
}
};
let cmyk = |key: &str| cmyk_param("tools.setColors", p, key);
let (fg_cmyk, bg_cmyk) = (cmyk("foregroundCmyk")?, cmyk("backgroundCmyk")?);
let foreground = color_param("tools.setColors", p, "foreground", s.tools.foreground)?;
let background = color_param("tools.setColors", p, "background", s.tools.background)?;
+50 -3
View File
@@ -4,7 +4,7 @@ use photocraft_color::{Color, ColorMode};
use photocraft_doc::{Fill, LayerContent};
use serde_json::{Value, json};
use crate::commands::{CommandSpec, check_pixels_unlocked, layer_param};
use crate::commands::{CommandSpec, check_pixels_unlocked, cmyk_param, layer_param};
use crate::{EngineError, Result, Session};
pub const SET: &str = "layer.fill.solidColor.set";
@@ -57,7 +57,13 @@ fn set(s: &mut Session, p: &Value) -> Result<Value> {
let id = layer_param(s, p)?;
let layer = st.doc.layer(id).ok_or(EngineError::NoLayer(id))?;
let LayerContent::Fill(Fill::Solid(original)) = layer.content else { return Err(bad("the target must be a Solid Color Fill layer")) };
let color = color_param(p.get("color").ok_or_else(|| bad("color is required"))?, original, st.doc.mode)?;
let mut color = color_param(p.get("color").ok_or_else(|| bad("color is required"))?, original, st.doc.mode)?;
// The inks the colour was picked as: a CMYK document keeps them exactly (#8568).
if let Some(k) = cmyk_param(SET, p, "cmyk")?
&& st.doc.mode == ColorMode::Cmyk
{
color = Color { mode: ColorMode::Cmyk, c: k, alpha: color.alpha };
}
if color != original {
check_pixels_unlocked(&st.doc, id)?;
if st.doc.effective_locks(id).transparency && color.alpha != original.alpha {
@@ -78,7 +84,7 @@ pub fn specs() -> Vec<CommandSpec> {
label: "Solid Color…",
menu: &[],
shortcut: None,
params: r##"{"layer":id? (default active),"document":id? (reject a stale dialog),"color":"#rrggbb"|"#rrggbbaa"|[r,g,b,a?]|{mode,c:[f32;4],alpha:f32}}; RGB values without alpha preserve it; stored Color preserves its model and precision"##,
params: r##"{"layer":id? (default active),"document":id? (reject a stale dialog),"color":"#rrggbb"|"#rrggbbaa"|[r,g,b,a?]|{mode,c:[f32;4],alpha:f32},"cmyk":[c,m,y,k]? (inks 0..1 the colour was picked as: a CMYK document keeps exactly these)}; RGB values without alpha preserve it; stored Color preserves its model and precision"##,
enabled: |s| s.active().map(|_| ()).ok_or_else(|| "no document open".into()),
run: set,
journal: true,
@@ -89,6 +95,47 @@ pub fn specs() -> Vec<CommandSpec> {
mod tests {
use super::*;
/// #8568: CMYK 50/50/50/100 entered for a Solid Color fill in a CMYK document stays exactly
/// those inks, on creation and on edit, instead of what its sRGB stand-in converts back to
/// through the document profile. RGB documents ignore the inks.
#[test]
fn cmyk_inks_survive_solid_color_fills_in_a_cmyk_document() {
let inks = [0.5, 0.5, 0.5, 1.0];
let hex = |k: [f32; 4]| {
let [r, g, b] = photocraft_color::convert::cmyk_to_rgb(k).map(|c| (c.clamp(0.0, 1.0) * 255.0).round() as u8);
format!("#{r:02x}{g:02x}{b:02x}")
};
let stored = |s: &Session, id: u64| match s.active().unwrap().doc.layer(photocraft_doc::LayerId(id)).unwrap().content {
LayerContent::Fill(Fill::Solid(c)) => c,
_ => panic!("not a solid fill"),
};
let mut s = Session::new();
s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
s.execute("image.mode.cmyk", json!({})).unwrap();
let id = s.execute("layer.newFillLayer.solidColor", json!({"color": hex(inks), "cmyk": inks})).unwrap()["layer"].as_u64().unwrap();
assert_eq!(stored(&s, id), Color { mode: ColorMode::Cmyk, c: inks, alpha: 1.0 });
// The sRGB stand-in alone does drift: that is what the inks prevent.
let drifted = s.execute("layer.newFillLayer.solidColor", json!({"color": hex(inks)})).unwrap()["layer"].as_u64().unwrap();
assert_ne!(stored(&s, drifted).c, inks);
// Editing keeps the new inks too.
let edit = [0.2, 0.3, 0.4, 0.5];
s.execute(SET, json!({"layer": id, "color": hex(edit), "cmyk": edit})).unwrap();
assert_eq!(stored(&s, id), Color { mode: ColorMode::Cmyk, c: edit, alpha: 1.0 });
// A foreground picked as CMYK seeds a new fill with its inks, as fills do (#4196).
s.execute("tools.setColors", json!({"foreground": hex(edit), "foregroundCmyk": edit})).unwrap();
let seeded = s.execute("layer.newFillLayer.solidColor", json!({})).unwrap()["layer"].as_u64().unwrap();
assert_eq!(stored(&s, seeded).c, edit);
for bad in [json!([0.1, 0.2, 0.3]), json!("50,50,50,100"), json!([0.1, 0.2, 0.3, f64::NAN])] {
assert!(s.execute("layer.newFillLayer.solidColor", json!({"cmyk": bad})).is_err(), "{bad}");
assert!(s.execute(SET, json!({"layer": id, "color": "#000000", "cmyk": bad})).is_err(), "{bad}");
}
// An RGB document takes the colour, not the inks.
let mut rgb = Session::new();
rgb.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
let id = rgb.execute("layer.newFillLayer.solidColor", json!({"color": "#336699", "cmyk": inks})).unwrap()["layer"].as_u64().unwrap();
assert_eq!(stored(&rgb, id).mode, ColorMode::Rgb);
}
#[test]
fn edit_preserves_layer_and_masks_at_all_depths_and_undo_redo() {
for depth in [8, 16, 32] {
+7
View File
@@ -321,6 +321,13 @@ where
(hex(to_rgb(v)) == hex(rgb)).then_some(v)
}
/// The CMYK values typed into the picker, while they still match its colour: the inks a command
/// should keep in a CMYK document (#4196, #8568).
pub(crate) fn picked_cmyk(f: &Map<String, Value>) -> Option<[f32; 4]> {
let rgb = f.get("color").and_then(Value::as_str).and_then(parse_hex)?;
kept::<4>(f, "__cmyk", rgb, photocraft_color::convert::cmyk_to_rgb)
}
/// Current colour, keeping the stored HSB, Lab or CMYK values while they still match `color`.
pub fn current(f: &Map<String, Value>) -> Components {
let rgb = f.get("color").and_then(Value::as_str).and_then(parse_hex).unwrap_or([0.0; 3]);
+20 -5
View File
@@ -27,7 +27,14 @@ pub(crate) fn open(app: &mut PhotocraftApp) -> Option<u64> {
let layer = st.active_layer?;
let LayerContent::Fill(Fill::Solid(color)) = st.doc.layer(layer)?.content else { return None };
let params = json!({"layer": layer.0, "document": st.doc.id.0});
Some(color_picker_ui::open_for_command(app, "Color Picker (Solid Color)", color.to_rgb(), cmds::SET, params))
let id = color_picker_ui::open_for_command(app, "Color Picker (Solid Color)", color.to_rgb(), cmds::SET, params);
// A CMYK fill reopens showing its own inks, not ones recomputed from its sRGB stand-in (#8568).
if color.mode == photocraft_color::ColorMode::Cmyk
&& let Some(d) = app.ui.dialog_mut(id)
{
d.fields.insert("__cmyk".into(), json!(color.c));
}
Some(id)
}
/// Creating a Solid Color fill is transactional: the document is unchanged until OK.
@@ -57,7 +64,8 @@ pub(crate) fn confirm_new(app: &mut PhotocraftApp, f: &Map<String, Value>) -> Re
return Err("the target document is no longer active".into());
}
let color = f.get("color").and_then(Value::as_str).ok_or("missing Solid Color colour")?;
app.run("layer.newFillLayer.solidColor", json!({"color": color}))
// CMYK typed into the picker stays those inks in a CMYK document (#8568).
app.run("layer.newFillLayer.solidColor", json!({"color": color, "cmyk": color_picker_ui::picked_cmyk(f)}))
}
pub(crate) fn owns(f: &Map<String, Value>) -> bool {
@@ -89,15 +97,22 @@ pub(crate) fn confirm(app: &mut PhotocraftApp, f: &Map<String, Value>) -> Result
return Err("the target document is no longer active".into());
}
let layer = LayerId(params.get("layer").and_then(Value::as_u64).ok_or("missing Solid Color layer")?);
if !st.doc.layer(layer).is_some_and(|l| matches!(l.content, LayerContent::Fill(Fill::Solid(_)))) {
let Some(LayerContent::Fill(Fill::Solid(original))) = st.doc.layer(layer).map(|l| &l.content) else {
return Err("the target Solid Color Fill layer no longer exists".into());
}
if f.get("color") == f.get("__orig") {
};
// Different inks can share a display colour (CMYK 0/0/0/100 and 50/50/50/100 are both black):
// only a colour whose inks are unchanged too is no edit (#8568).
let inks = color_picker_ui::picked_cmyk(f);
let same_inks = inks.is_none_or(|k| original.mode != photocraft_color::ColorMode::Cmyk || original.c == k);
if f.get("color") == f.get("__orig") && same_inks {
return Ok(json!({"layer": layer.0, "unchanged": true}));
}
let color = f.get("color").cloned().ok_or("missing color")?;
let object = params.as_object_mut().ok_or("invalid Solid Color target")?;
object.insert("color".into(), color);
if let Some(inks) = inks {
object.insert("cmyk".into(), json!(inks));
}
app.run(cmds::SET, params)
}
+54
View File
@@ -269,3 +269,57 @@ fn cmyk_lab_preview_thumbnail_cancel_and_ok_are_consistent() {
assert_eq!(app.session.active().unwrap().doc.layer(id).unwrap().content, LayerContent::Fill(Fill::Solid(preview)));
}
}
/// #8568: CMYK 50/50/50/100 typed into the Solid Color picker of a CMYK document is the fill's
/// colour after OK, for a new fill and for an edited one, not what its sRGB stand-in converts back
/// to through the document profile.
#[test]
fn cmyk_typed_into_the_picker_is_the_fills_colour_in_a_cmyk_document() {
let mut s = photocraft_engine::Session::new();
s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
s.execute("image.mode.cmyk", json!({})).unwrap();
let mut app = PhotocraftApp::new(s, crate::Services::default());
let typed = |f: &mut serde_json::Map<String, serde_json::Value>, inks: [f32; 4]| {
let [r, g, b] = photocraft_color::convert::cmyk_to_rgb(inks).map(|c| (c.clamp(0.0, 1.0) * 255.0).round() as u8);
f.insert("color".into(), json!(format!("#{r:02x}{g:02x}{b:02x}")));
f.insert("__cmyk".into(), json!(inks));
};
let fill = |app: &PhotocraftApp| {
let st = app.session.active().unwrap();
match st.doc.layer(st.active_layer.unwrap()).unwrap().content {
LayerContent::Fill(Fill::Solid(c)) => c,
_ => panic!("not a solid fill"),
}
};
let inks = [0.5, 0.5, 0.5, 1.0];
let id = open_new(&mut app).unwrap();
let mut f = app.ui.dialogs.iter().find(|d| d.id == id).unwrap().fields.clone();
typed(&mut f, inks);
confirm_new(&mut app, &f).unwrap();
assert_eq!(fill(&app), Color { mode: ColorMode::Cmyk, c: inks, alpha: 1.0 });
let edit = [0.25, 0.75, 0.0, 0.1];
let id = open(&mut app).unwrap();
let mut f = app.ui.dialogs.iter().find(|d| d.id == id).unwrap().fields.clone();
typed(&mut f, edit);
confirm(&mut app, &f).unwrap();
assert_eq!(fill(&app).c, edit);
// Reopening the picker shows the stored inks.
let id = open(&mut app).unwrap();
let shown = color_picker_ui::current(&app.ui.dialogs.iter().find(|d| d.id == id).unwrap().fields);
assert_eq!(shown.cmyk, edit);
// Other inks with the same display colour are still an edit.
let near = [0.5, 0.5, 0.499, 1.0];
let id = open(&mut app).unwrap();
let mut f = app.ui.dialogs.iter().find(|d| d.id == id).unwrap().fields.clone();
typed(&mut f, near);
confirm(&mut app, &f).unwrap();
let rich = [0.5, 0.5, 0.5, 1.0];
let id = open(&mut app).unwrap();
let mut f = app.ui.dialogs.iter().find(|d| d.id == id).unwrap().fields.clone();
typed(&mut f, rich);
assert_eq!(f.get("color"), f.get("__orig"), "both inks show as the same colour");
confirm(&mut app, &f).unwrap();
assert_eq!(fill(&app).c, rich);
}