mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
feat: add Indexed Color matte control
This commit is contained in:
5 files changed
+193
-2
No files matched your search
@@ -255,6 +255,32 @@ fn parse_dither(s: &str) -> Option<Dither> {
|
||||
})
|
||||
}
|
||||
|
||||
fn indexed_matte(s: &Session, p: &Value) -> Result<Option<[f32; 3]>> {
|
||||
const CMD: &str = "image.mode.indexedColor";
|
||||
let rgba = |color: [f32; 4]| [color[0], color[1], color[2]];
|
||||
let matte = match p.get("matte") {
|
||||
None | Some(Value::Null) => return Ok(None),
|
||||
Some(Value::String(value)) => match value.as_str() {
|
||||
"none" => return Ok(None),
|
||||
"white" => [1.0; 3],
|
||||
"black" => [0.0; 3],
|
||||
"foreground" => rgba(s.tools.foreground),
|
||||
"background" => rgba(s.tools.background),
|
||||
value if value.len() == 7 && value.starts_with('#') => p
|
||||
.get("matte")
|
||||
.and_then(parse_color)
|
||||
.ok_or_else(|| bad(CMD, "matte must be none, foreground, background, white, black or #rrggbb"))?
|
||||
.map(|v| f32::from(v) / 255.0),
|
||||
_ => return Err(bad(CMD, "matte must be none, foreground, background, white, black or #rrggbb")),
|
||||
},
|
||||
_ => return Err(bad(CMD, "matte must be a colour string")),
|
||||
};
|
||||
if matte.iter().any(|v| !v.is_finite() || !(0.0..=1.0).contains(v)) {
|
||||
return Err(bad(CMD, "matte channels must be finite and in 0..=1"));
|
||||
}
|
||||
Ok(Some(matte))
|
||||
}
|
||||
|
||||
fn indexed_color(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
const CMD: &str = "image.mode.indexedColor";
|
||||
let kind = PaletteKind::from_id(str_or(p, "palette", "selective"))
|
||||
@@ -273,8 +299,23 @@ fn indexed_color(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
};
|
||||
let amount = num(p, "amount", 75.0).clamp(0.0, 100.0) / 100.0;
|
||||
let transparency = p.get("transparency").and_then(Value::as_bool).unwrap_or(true);
|
||||
let matte = indexed_matte(s, p)?;
|
||||
let (pal_len, transparent) = s.edit("Indexed Color", |doc, active| {
|
||||
let mut px = composite(doc, !transparency);
|
||||
let mut px = composite(doc, !transparency && matte.is_none());
|
||||
if let Some(matte) = matte {
|
||||
for pixel in &mut px {
|
||||
let alpha = pixel[3].clamp(0.0, 1.0);
|
||||
if alpha == 1.0 {
|
||||
continue;
|
||||
}
|
||||
if !transparency || alpha > 0.0 {
|
||||
for (channel, background) in pixel[..3].iter_mut().zip(matte) {
|
||||
*channel = *channel * alpha + background * (1.0 - alpha);
|
||||
}
|
||||
pixel[3] = 1.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
let has_clear = transparency && px.iter().any(|q| q[3] < 0.5);
|
||||
let mut pal = quantize::build_palette(&px, kind, if has_clear { colors.saturating_sub(1).max(2) } else { colors }, forced).map_err(|e| bad(CMD, e))?;
|
||||
let transparent = if has_clear && pal.len() < 256 {
|
||||
@@ -689,7 +730,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"image.mode.indexedColor",
|
||||
"Indexed Color…",
|
||||
["Image", "Mode"],
|
||||
r##"{"palette":"selective|perceptual|adaptive|exact|systemMac|systemWindows|web|uniform"="selective","colors":2..256=256,"forced":"blackWhite|none|primaries|web"="blackWhite","transparency":bool=true,"dither":"diffusion|none|pattern|noise"="diffusion","amount":0..100=75} (flattens)"##,
|
||||
r##"{"palette":"selective|perceptual|adaptive|exact|systemMac|systemWindows|web|uniform"="selective","colors":2..256=256,"forced":"blackWhite|none|primaries|web"="blackWhite","transparency":bool=true,"matte":color,"dither":"diffusion|none|pattern|noise"="diffusion","amount":0..100=75} (flattens; matte: none|foreground|background|white|black|#rrggbb)"##,
|
||||
|s| mode_is(
|
||||
s,
|
||||
&[ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Indexed, ColorMode::Duotone, ColorMode::Cmyk, ColorMode::Lab],
|
||||
|
||||
@@ -247,6 +247,61 @@ fn indexed_color_palettes_and_dithers() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_matte_blends_edges_without_replacing_clear_pixels() {
|
||||
for depth in [8, 16, 32] {
|
||||
for (matte, color) in [
|
||||
("black", [0.0, 0.0, 0.0]),
|
||||
("white", [1.0, 1.0, 1.0]),
|
||||
("#0000ff", [0.0, 0.0, 1.0]),
|
||||
("foreground", [0.0, 0.0, 1.0]),
|
||||
("background", [0.0, 1.0, 0.0]),
|
||||
] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 4, "height": 1, "depth": depth, "background": "transparent"})).unwrap();
|
||||
s.execute("tools.setColors", json!({"foreground": "#0000ff", "background": "#00ff00"})).unwrap();
|
||||
paint(&mut s, |x, _| match x {
|
||||
0 => [0.0, 1.0, 0.0, 1.0],
|
||||
1 => [1.0, 0.0, 0.0, 0.75],
|
||||
2 => [1.0, 0.0, 0.0, 0.25],
|
||||
_ => [0.0; 4],
|
||||
});
|
||||
let source = [1, 2].map(|x| doc(&s).layers[0].surface().unwrap().rgba(x, 0));
|
||||
let expected = source.map(|pixel| [0, 1, 2].map(|i| pixel[i] * pixel[3] + color[i] * (1.0 - pixel[3])));
|
||||
s.execute("image.mode.indexedColor", json!({"palette": "exact", "forced": "none", "dither": "none", "matte": matte})).unwrap();
|
||||
let surface = doc(&s).layers[0].surface().unwrap();
|
||||
let edges = [1, 2].map(|x| surface.rgba(x, 0));
|
||||
for (edge, expected) in edges.iter().zip(expected) {
|
||||
for i in 0..3 {
|
||||
assert!((edge[i] - expected[i]).abs() <= 1.0 / 255.0, "{depth}-bit {matte}: {edge:?} vs {expected:?}");
|
||||
}
|
||||
assert_eq!(edge[3], 1.0);
|
||||
}
|
||||
assert_eq!(surface.rgba(3, 0)[3], 0.0, "{matte}: fully transparent pixels stay clear");
|
||||
assert_eq!(surface.rgba(0, 0), [0.0, 1.0, 0.0, 1.0]);
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
assert_eq!([1, 2].map(|x| doc(&s).layers[0].surface().unwrap().rgba(x, 0)), source);
|
||||
s.execute("edit.redo", json!({})).unwrap();
|
||||
assert_eq!([1, 2].map(|x| doc(&s).layers[0].surface().unwrap().rgba(x, 0)), edges);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_matte_without_transparency_fills_clear_pixels_and_rejects_bad_input_atomically() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 2, "height": 1, "background": "transparent"})).unwrap();
|
||||
let before = s.active().unwrap().revision;
|
||||
for matte in [json!(false), json!(12), json!("#gg0000"), json!("#000000ff"), json!("unknown")] {
|
||||
assert!(s.execute("image.mode.indexedColor", json!({"matte": matte})).is_err(), "{matte}");
|
||||
assert_eq!(s.active().unwrap().revision, before);
|
||||
assert_eq!(doc(&s).mode, ColorMode::Rgb);
|
||||
}
|
||||
s.execute("image.mode.indexedColor", json!({"palette": "exact", "forced": "none", "dither": "none", "transparency": false, "matte": "#0000ff"})).unwrap();
|
||||
assert_eq!(doc(&s).layers[0].surface().unwrap().rgba(0, 0), [0.0, 0.0, 1.0, 1.0]);
|
||||
assert_eq!(doc(&s).color_table.as_ref().unwrap().transparent, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_exact_transparency_and_sources() {
|
||||
// Two colours + transparency: exact palette with a transparent entry.
|
||||
|
||||
@@ -840,6 +840,44 @@ fn replace_color_result(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn indexed_matte_row(ui: &mut egui::Ui, fields: &mut Map<String, Value>) {
|
||||
let value = fields.get("matte").and_then(Value::as_str).unwrap_or("none");
|
||||
let mut choice = if value.starts_with('#') { "custom" } else { value };
|
||||
let color = crate::color_picker_ui::parse_hex(value).unwrap_or([1.0; 3]);
|
||||
let mut changed = false;
|
||||
let mut pick = false;
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(egui::RichText::new(tl!("Matte")).color(Tokens::get(ui.ctx()).text_dim));
|
||||
changed = crate::widgets::dropdown(
|
||||
ui,
|
||||
"indexed-matte",
|
||||
"Matte",
|
||||
&mut choice,
|
||||
&[
|
||||
("none", "None"),
|
||||
("foreground", "Foreground Color"),
|
||||
("background", "Background Color"),
|
||||
("white", "White"),
|
||||
("black", "Black"),
|
||||
("custom", "Other…"),
|
||||
],
|
||||
170.0,
|
||||
);
|
||||
if choice == "custom" {
|
||||
let bytes = color.map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8);
|
||||
pick = crate::widgets::color_swatch_button(ui, egui::Color32::from_rgb(bytes[0], bytes[1], bytes[2]), tl!("Matte")).clicked();
|
||||
}
|
||||
});
|
||||
let custom = choice == "custom";
|
||||
if changed {
|
||||
let next = if custom { crate::color_picker_ui::hex(color) } else { choice.to_owned() };
|
||||
fields.insert("matte".into(), json!(next));
|
||||
}
|
||||
if custom && (changed || pick) {
|
||||
crate::color_picker_ui::request_field(fields, "matte", &crate::color_picker_ui::title_for("Matte"));
|
||||
}
|
||||
}
|
||||
|
||||
/// Dialog body for filter commands.
|
||||
pub fn body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
@@ -856,6 +894,10 @@ pub fn body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
|
||||
crate::select_mask_view::controls(ui, f);
|
||||
}
|
||||
for p in parse_spec(&spec) {
|
||||
if cmd == "image.mode.indexedColor" && p.key == "matte" {
|
||||
indexed_matte_row(ui, f);
|
||||
continue;
|
||||
}
|
||||
if zoom_center::owns(&cmd, f) && matches!(p.key.as_str(), "centerX" | "centerY") {
|
||||
if p.key == "centerX" {
|
||||
zoom_center::row(ui, f);
|
||||
|
||||
@@ -1642,3 +1642,47 @@ fn replace_color_localized_color_clusters() {
|
||||
assert!(px(2)[1] > 0.5, "near the point: shifted to green {:?}", px(2));
|
||||
assert!(px(38)[0] > 0.9 && px(38)[1] < 0.1, "far away: still red {:?}", px(38));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_matte_dropdown_and_picker_change_the_applied_edge_color() {
|
||||
use egui::accesskit::Role;
|
||||
use egui_kittest::{Harness, kittest::Queryable};
|
||||
|
||||
for (choice, blue) in [("Black", 0.0), ("Other…", 0.25)] {
|
||||
let mut session = photocraft_engine::Session::new();
|
||||
session.execute("file.new", json!({"width": 2, "height": 1, "depth": 32, "background": "transparent"})).unwrap();
|
||||
session
|
||||
.edit("fixture", |doc, active| {
|
||||
let bounds = doc.bounds();
|
||||
doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().write_region(bounds, &[1.0, 0.0, 0.0, 0.75, 0.0, 0.0, 0.0, 0.0]);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let mut app = PhotocraftApp::new(session, crate::Services::default());
|
||||
let mut harness = Harness::builder().with_size(egui::vec2(900.0, 1000.0)).build_ui_state(|ui, app| crate::dialogs::show(app, ui.ctx()), &mut app);
|
||||
PhotocraftApp::setup_context(&harness.ctx, crate::theme::ThemeKind::ALL[0]);
|
||||
let id = open(harness.state_mut(), "image.mode.indexedColor").unwrap();
|
||||
harness.state_mut().ui.dialog_mut(id).unwrap().fields.insert("palette".into(), json!("exact"));
|
||||
harness.run_steps(3);
|
||||
harness.query_all_by_role(Role::ComboBox).find(|node| node.value().as_deref() == Some("None")).expect("Matte dropdown").click();
|
||||
harness.run_steps(2);
|
||||
harness.get_by_label(choice).click();
|
||||
harness.run_steps(3);
|
||||
if choice == "Other…" {
|
||||
let picker = harness.state().ui.dialogs.last().unwrap().id;
|
||||
assert_ne!(picker, id);
|
||||
harness.state_mut().ui.dialog_mut(picker).unwrap().fields.insert("color".into(), json!("#0000ff"));
|
||||
crate::dialogs::confirm(harness.state_mut(), picker).unwrap();
|
||||
harness.state_mut().ui.dialogs.retain(|dialog| dialog.id != picker);
|
||||
harness.run_steps(3);
|
||||
}
|
||||
harness.get_by_label("OK").click();
|
||||
harness.run_steps(3);
|
||||
let doc = &harness.state().session.active().unwrap().doc;
|
||||
assert_eq!(doc.mode, photocraft_color::ColorMode::Indexed);
|
||||
let surface = doc.layers[0].surface().unwrap();
|
||||
let edge = surface.rgba(0, 0);
|
||||
assert!((edge[0] - 0.75).abs() <= 1.0 / 255.0 && edge[1] == 0.0 && (edge[2] - blue).abs() <= 1.0 / 255.0 && edge[3] == 1.0, "{choice}: {edge:?}");
|
||||
assert_eq!(surface.rgba(1, 0)[3], 0.0);
|
||||
}
|
||||
}
|
||||
@@ -661,6 +661,15 @@ except Place, which is a job that `poll_jobs` steps on the UI thread (a *coopera
|
||||
|
||||
## Indexed Color timings
|
||||
|
||||
The stock Indexed Color dialog exposes Matte: None, Foreground Color, Background Color,
|
||||
White, Black and Other… (the shared Color Picker). Automation accepts `matte` as `none`,
|
||||
`foreground`, `background`, `white`, `black` or `#rrggbb`. A selected matte blends all
|
||||
partially transparent edges; with Transparency on, fully clear pixels stay transparent.
|
||||
With Transparency off, the selected matte also fills clear pixels. None retains the existing
|
||||
hard-edged transparency/default flattening behavior. Indexed conversion retains the document's
|
||||
ICC profile and encoded RGB sample representation; matte colours are interpreted in the document
|
||||
RGB coordinate space. This follows Adobe's documented
|
||||
[Indexed Color conversion options](https://helpx.adobe.com/photoshop/desktop/adjust-color/color-modes/conversion-options-for-indexed-color-images.html).
|
||||
`cargo run --release -p photocraft-engine --example indexed_color_perf -- image.png` measures
|
||||
palette construction, Floyd–Steinberg diffusion, and the complete full-resolution CPU preview
|
||||
path (proxy copy, engine command, result composition; excludes GPU upload/presentation).
|
||||
|
||||
Reference in new issue
Block a user