mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Replace Color: Add to Sample and Subtract from Sample (#6280)
Replace Color had one eyedropper and matched one colour. As in Photoshop, its dialog now has the Eyedropper, Add to Sample and Subtract from Sample tools (Shift-click adds, Alt-click subtracts), and image.adjustments.replaceColor takes several points, subtractPoints and their click order. Several samples match through Select › Color Range's own matcher, so the two agree for the same clicks; a single colour keeps the existing path. Fixes #6280 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
3d60ad01f9
commit
0e421d6a33
6 files changed
+117
-16
No files matched your search
@@ -434,6 +434,22 @@ fn replace_color(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// Localized Color Clusters, as in Select › Color Range: the colour only matches within Range
|
||||
// (a percentage of the canvas's longer side) of where it was picked.
|
||||
let points = sample_points(p).map_err(|m| bad(CMD, m))?;
|
||||
// Several samples (Add to Sample / Subtract from Sample, #6280): the colours match as
|
||||
// Select › Color Range's do for the same clicks, through its own matcher.
|
||||
let subtracted = p.get("subtractPoints").and_then(Value::as_array).is_some_and(|a| !a.is_empty());
|
||||
if subtracted || points.len() > 1 {
|
||||
let mut q = json!({"points": p.get("points").cloned().unwrap_or(Value::Array(Vec::new())), "fuzziness": fuzz});
|
||||
for key in ["subtractPoints", "order", "localized", "range"] {
|
||||
if let Some(v) = p.get(key) {
|
||||
q[key] = v.clone();
|
||||
}
|
||||
}
|
||||
let samples = crate::selection_cmds::ColorRangeSamples::new(s, &q).map_err(|e| bad(CMD, e.to_string()))?;
|
||||
return rgba_edit(s, "Replace Color", p, |px, r| {
|
||||
let mask = samples.coverage(px, r.width() as usize, 1.0, [r.x0 as f32, r.y0 as f32]);
|
||||
tone::replace_color_masked(px, &mask, h, sat, l);
|
||||
});
|
||||
}
|
||||
let localized = if p.get("localized").and_then(Value::as_bool).unwrap_or(false) {
|
||||
if points.is_empty() {
|
||||
return Err(bad(CMD, "localized clusters need eyedropper `points` (where the colour was picked)"));
|
||||
@@ -587,7 +603,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"image.adjustments.replaceColor",
|
||||
"Replace Color…",
|
||||
["Image", "Adjustments"],
|
||||
r##"{"localized":bool=false,"color":color,"fuzziness":0..200=40,"range":0..100=100,"hue":-180..180=0,"saturation":-100..100=0,"lightness":-100..100=0,"points":[[x,y],…]?,"labMinimum":[L,a,b]?,"labMaximum":[L,a,b]?} (Lab bounds: a finite ordered D50 box, mutually exclusive with color/points/localized/range; composite pixel-layer target only. Otherwise color defaults to the foreground; localized points use range % of the longer side.)"##,
|
||||
r##"{"localized":bool=false,"color":color,"fuzziness":0..200=40,"range":0..100=100,"hue":-180..180=0,"saturation":-100..100=0,"lightness":-100..100=0,"points":[[x,y],…]?,"subtractPoints":[[x,y],…]?,"order":["+"|"-",…]?,"labMinimum":[L,a,b]?,"labMaximum":[L,a,b]?} (Lab bounds: a finite ordered D50 box, mutually exclusive with color/points/subtractPoints/order/localized/range; composite pixel-layer target only. Otherwise color ("#rrggbb") defaults to the foreground; localized points use range % of the longer side; several "points" (Add to Sample) or any "subtractPoints" (Subtract from Sample) match as select.colorRange does for the same samples, "order" the click order.)"##,
|
||||
has_pixels,
|
||||
replace_color
|
||||
),
|
||||
|
||||
@@ -754,6 +754,45 @@ fn replace_color_localized_clusters_only_change_the_colour_near_the_sample() {
|
||||
assert!(err.to_string().contains("bad point"), "{err}");
|
||||
}
|
||||
|
||||
/// #6280: Add to Sample and Subtract from Sample, as in Photoshop: several picked colours are
|
||||
/// all replaced, and a subtracted one is left alone, matching as Select › Color Range does for
|
||||
/// the same samples.
|
||||
#[test]
|
||||
fn replace_color_adds_and_subtracts_samples_as_color_range_does() {
|
||||
// Red on the left (x < 16), blue in the middle, green on the right (x ≥ 32).
|
||||
let (red, blue, green) = ([0.85, 0.1, 0.1, 1.0], [0.1, 0.15, 0.85, 1.0], [0.1, 0.8, 0.15, 1.0]);
|
||||
let mut s = session(8, "rgb");
|
||||
paint(&mut s, |x, _| if x < 16 { red } else if x < 32 { blue } else { green });
|
||||
let before = (rgba(&s, 4, 16), rgba(&s, 24, 16), rgba(&s, 40, 16));
|
||||
let shift = |extra: Value| {
|
||||
let mut p = json!({"fuzziness": 40, "hue": 120, "saturation": 20});
|
||||
p.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
|
||||
p
|
||||
};
|
||||
// Red and blue sampled (one added): both change, green doesn't.
|
||||
s.execute("image.adjustments.replaceColor", shift(json!({"color": "#d91a1a", "points": [[4, 16], [24, 16]]}))).unwrap();
|
||||
assert!(changed(rgba(&s, 4, 16), before.0) > 0.1, "red is replaced");
|
||||
assert!(changed(rgba(&s, 24, 16), before.1) > 0.1, "the added blue is replaced");
|
||||
assert_eq!(rgba(&s, 40, 16), before.2, "green wasn't sampled");
|
||||
// The same coverage as Select › Color Range for the same clicks.
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
s.execute("select.colorRange", json!({"points": [[4, 16], [24, 16]], "fuzziness": 40})).unwrap();
|
||||
let sel = doc(&s).selection.clone().unwrap();
|
||||
for x in [4, 24, 40] {
|
||||
let want = sel.read_region(Rect::from_xywh(x, 16, 1, 1))[0];
|
||||
assert!((want > 0.5) == (x < 32), "Color Range at {x}: {want}");
|
||||
}
|
||||
s.execute("select.deselect", json!({})).unwrap();
|
||||
// Red sampled, blue then subtracted: only red changes.
|
||||
s.execute("image.adjustments.replaceColor", shift(json!({"points": [[4, 16]], "subtractPoints": [[24, 16]], "order": ["+", "-"]}))).unwrap();
|
||||
assert!(changed(rgba(&s, 4, 16), before.0) > 0.1);
|
||||
assert_eq!(rgba(&s, 24, 16), before.1, "the subtracted blue stays");
|
||||
assert_eq!(rgba(&s, 40, 16), before.2);
|
||||
// Bad samples are refused, naming this command.
|
||||
let err = s.execute("image.adjustments.replaceColor", shift(json!({"points": [[4, 16]], "subtractPoints": [[999, 16]]}))).unwrap_err();
|
||||
assert!(err.to_string().contains("replaceColor") && err.to_string().contains("outside"), "{err}");
|
||||
}
|
||||
|
||||
/// #6095: ⌘I in Quick Mask inverts the whole mask, not only where it was white, so leaving Quick
|
||||
/// Mask selects everything but the original rectangle, whichever way the colour reads. With
|
||||
/// Color Indicates Selected Areas and nothing selected, it selects everything. A black alpha
|
||||
|
||||
@@ -762,7 +762,7 @@ fn handle_dialog_canvas_input(app: &mut PhotocraftApp, input: DialogCanvasInput<
|
||||
if curves_picking {
|
||||
crate::adjust_dialog::sample_at(app, d[0], d[1]);
|
||||
} else if replace_picking {
|
||||
crate::filter_dialog::color_eyedropper_sample(app, d[0], d[1]);
|
||||
crate::filter_dialog::color_eyedropper_sample(app, d[0], d[1], input.ctx.input(|i| i.modifiers));
|
||||
} else {
|
||||
crate::color_picker_ui::sample_at(app, d[0], d[1]);
|
||||
}
|
||||
|
||||
@@ -401,7 +401,7 @@ fn radio(ui: &mut egui::Ui, on: bool, label: &str) -> egui::Response {
|
||||
}
|
||||
|
||||
/// Eyedropper button: the pipette icon with a +/− badge; labelled for accessibility.
|
||||
fn eyedropper(ui: &mut egui::Ui, badge: &str, selected: bool, label: &str) -> egui::Response {
|
||||
pub(crate) fn eyedropper(ui: &mut egui::Ui, badge: &str, selected: bool, label: &str) -> egui::Response {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let r = crate::icons::button(ui, "pipette", 26.0, selected, label);
|
||||
if !badge.is_empty() {
|
||||
|
||||
@@ -784,16 +784,26 @@ pub(crate) fn color_eyedropper_armed(app: &PhotocraftApp) -> bool {
|
||||
/// Sample the merged image at document point (x, y) into the armed dialog's "From" colour.
|
||||
/// Returns true when the click was consumed, even off-image or over transparency, where nothing
|
||||
/// changes and the eyedropper stays armed (like the Curves picker).
|
||||
pub(crate) fn color_eyedropper_sample(app: &mut PhotocraftApp, x: f64, y: f64) -> bool {
|
||||
/// With the Add to Sample eyedropper (or Shift) the click adds a sample, with Subtract from
|
||||
/// Sample (or Alt) it subtracts one, as in Photoshop and Select › Color Range (#6280).
|
||||
pub(crate) fn color_eyedropper_sample(app: &mut PhotocraftApp, x: f64, y: f64, mods: egui::Modifiers) -> bool {
|
||||
let Some(id) = app.ui.dialogs.last().filter(|d| color_eyedropper_owns(&d.fields)).map(|d| d.id) else {
|
||||
return false;
|
||||
};
|
||||
if let Some(rgb) = crate::canvas::composite_color(app, x, y)
|
||||
&& let Some(d) = app.ui.dialog_mut(id)
|
||||
{
|
||||
d.fields.insert("color".into(), json!(crate::color_picker_ui::hex(rgb)));
|
||||
// Where it was picked: Localized Color Clusters match the colour near this point (#4457).
|
||||
d.fields.insert("points".into(), json!([[x.floor(), y.floor()]]));
|
||||
let Some(rgb) = crate::canvas::composite_color(app, x, y) else { return true };
|
||||
let Some(mut f) = app.ui.dialogs.last().map(|d| d.fields.clone()) else { return true };
|
||||
let sample = !mods.shift && !mods.alt && f.get("__tool").and_then(Value::as_str).is_none_or(|t| t == "sample");
|
||||
if sample {
|
||||
f.insert("color".into(), json!(crate::color_picker_ui::hex(rgb)));
|
||||
}
|
||||
// Where it was picked: Localized Color Clusters match the colour near this point (#4457);
|
||||
// added and subtracted samples go to `points` / `subtractPoints` in click order.
|
||||
crate::color_range_ui::pick(app, &mut f, [x, y], mods);
|
||||
if let Some(order) = f.get("__order").cloned() {
|
||||
f.insert("order".into(), order);
|
||||
}
|
||||
if let Some(d) = app.ui.dialog_mut(id) {
|
||||
d.fields = f;
|
||||
}
|
||||
true
|
||||
}
|
||||
@@ -1035,9 +1045,19 @@ pub fn body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
|
||||
// PhotoCraft's Color Picker, as everywhere else (#2144).
|
||||
clicked = crate::widgets::color_swatch_button(ui, egui::Color32::from_rgb(bytes[0], bytes[1], bytes[2]), &name).clicked();
|
||||
if eyedropper {
|
||||
// Photoshop's three eyedroppers (#6280), right to left here: Subtract
|
||||
// from Sample, Add to Sample, Eyedropper. Clicking the chosen one again
|
||||
// puts it away.
|
||||
let armed = f.get(COLOR_EYEDROPPER).and_then(Value::as_bool).unwrap_or(false);
|
||||
if crate::icons::button(ui, "pipette", 24.0, armed, "Pick Color From Image").clicked() {
|
||||
f.insert(COLOR_EYEDROPPER.into(), json!(!armed));
|
||||
let tool = f.get("__tool").and_then(Value::as_str).unwrap_or("sample").to_string();
|
||||
for (id, badge, label) in
|
||||
[("subtract", "−", tl!("Subtract from Sample")), ("add", "+", tl!("Add to Sample")), ("sample", "", tl!("Pick Color From Image"))]
|
||||
{
|
||||
let on = armed && tool == id;
|
||||
if crate::color_range_ui::eyedropper(ui, badge, on, label).clicked() {
|
||||
f.insert(COLOR_EYEDROPPER.into(), json!(!on));
|
||||
f.insert("__tool".into(), json!(id));
|
||||
}
|
||||
}
|
||||
}
|
||||
ui.label(egui::RichText::new(crate::color_picker_ui::hex(rgb)).color(t.text_dim).monospace());
|
||||
|
||||
@@ -1549,18 +1549,44 @@ fn replace_color_eyedropper_samples_the_image() {
|
||||
assert_eq!(app.ui.dialogs.last().unwrap().fields["color"], json!("#ff0000"), "the dialog starts from the foreground colour");
|
||||
// #6279: armed as it opens, so the first click on the image samples.
|
||||
assert!(color_eyedropper_armed(&app));
|
||||
assert!(color_eyedropper_sample(&mut app, 4.0, 4.0));
|
||||
assert!(color_eyedropper_sample(&mut app, 4.0, 4.0, egui::Modifiers::NONE));
|
||||
assert_eq!(app.ui.dialogs.last().unwrap().fields["color"], json!("#00ff00"));
|
||||
// #4457: where it was picked, for Localized Color Clusters.
|
||||
assert_eq!(app.ui.dialogs.last().unwrap().fields["points"], json!([[4.0, 4.0]]));
|
||||
// Off-image: the click is consumed, nothing changes and the eyedropper stays armed.
|
||||
assert!(color_eyedropper_sample(&mut app, -1.0, 4.0));
|
||||
assert!(color_eyedropper_sample(&mut app, -1.0, 4.0, egui::Modifiers::NONE));
|
||||
assert_eq!(app.ui.dialogs.last().unwrap().fields["color"], json!("#00ff00"));
|
||||
assert!(color_eyedropper_armed(&app));
|
||||
// Disarmed, the dialog never claims the click.
|
||||
app.ui.dialog_mut(id).unwrap().fields.insert(COLOR_EYEDROPPER.into(), json!(false));
|
||||
assert!(!color_eyedropper_armed(&app));
|
||||
assert!(!color_eyedropper_sample(&mut app, 4.0, 4.0));
|
||||
assert!(!color_eyedropper_sample(&mut app, 4.0, 4.0, egui::Modifiers::NONE));
|
||||
}
|
||||
|
||||
/// #6280: Shift-click adds a sample and Alt-click subtracts one (as do the + and − eyedroppers),
|
||||
/// in click order, as in Photoshop; a plain click starts over from the clicked colour.
|
||||
#[test]
|
||||
fn replace_color_shift_and_alt_clicks_add_and_subtract_samples() {
|
||||
let mut s = photocraft_engine::Session::new();
|
||||
s.execute("file.new", json!({"width": 16, "height": 16, "background": "#00ff00"})).unwrap();
|
||||
let mut app = PhotocraftApp::new(s, crate::Services::default());
|
||||
open(&mut app, REPLACE_COLOR).unwrap();
|
||||
let field = |app: &PhotocraftApp, k: &str| app.ui.dialogs.last().unwrap().fields.get(k).cloned().unwrap_or(Value::Null);
|
||||
assert!(color_eyedropper_sample(&mut app, 2.0, 2.0, egui::Modifiers::NONE));
|
||||
assert!(color_eyedropper_sample(&mut app, 5.0, 5.0, egui::Modifiers::SHIFT));
|
||||
assert!(color_eyedropper_sample(&mut app, 9.0, 9.0, egui::Modifiers::ALT));
|
||||
assert_eq!(field(&app, "points"), json!([[2.0, 2.0], [5.0, 5.0]]));
|
||||
assert_eq!(field(&app, "subtractPoints"), json!([[9.0, 9.0]]));
|
||||
assert_eq!(field(&app, "order"), json!(["+", "+", "-"]));
|
||||
assert_eq!(field(&app, "color"), json!("#00ff00"), "the From colour is the plain sample's");
|
||||
// The − eyedropper subtracts without a modifier; a plain click with the plain one starts over.
|
||||
let id = app.ui.dialogs.last().unwrap().id;
|
||||
app.ui.dialog_mut(id).unwrap().fields.insert("__tool".into(), json!("subtract"));
|
||||
assert!(color_eyedropper_sample(&mut app, 3.0, 3.0, egui::Modifiers::NONE));
|
||||
assert_eq!(field(&app, "subtractPoints"), json!([[9.0, 9.0], [3.0, 3.0]]));
|
||||
app.ui.dialog_mut(id).unwrap().fields.insert("__tool".into(), json!("sample"));
|
||||
assert!(color_eyedropper_sample(&mut app, 7.0, 7.0, egui::Modifiers::NONE));
|
||||
assert_eq!((field(&app, "points"), field(&app, "subtractPoints")), (json!([[7.0, 7.0]]), json!([])));
|
||||
}
|
||||
|
||||
/// #4143: the eyedropper button disarms and re-arms the canvas picker (#6279: it opens armed).
|
||||
@@ -1641,7 +1667,7 @@ fn replace_color_localized_color_clusters() {
|
||||
h.run_steps(3);
|
||||
let _ = h.get_by_label("Localized Color Clusters");
|
||||
h.state_mut().ui.dialog_mut(id).unwrap().fields.insert(COLOR_EYEDROPPER.into(), json!(true));
|
||||
assert!(color_eyedropper_sample(h.state_mut(), 2.0, 5.0));
|
||||
assert!(color_eyedropper_sample(h.state_mut(), 2.0, 5.0, egui::Modifiers::NONE));
|
||||
h.get_by_label("Localized Color Clusters").click();
|
||||
h.run_steps(2);
|
||||
let f = h.state().ui.dialogs.last().unwrap().fields.clone();
|
||||
|
||||
Reference in new issue
Block a user