Merge #18048 into the merge queue batch

Fill: colour fills in Monotone documents take the reference's grey value (#17478)
This commit is contained in:
David Mann committed 2026-11-30 14:56:00 +01:00
commit 2556678e2f
6 files changed
+192 -13

No files matched your search

+15 -4
View File
@@ -840,16 +840,18 @@ pub fn document_profile(doc: &Document) -> Arc<Profile> {
/// so pixel conversions such as Image › Mode › Grayscale keep using the document's profile. Other
/// modes, and a failed conversion, keep the colour. So do Grayscale documents outside the default
/// gray working space, which #11940 did not measure: a Working Gray other than Dot Gain 20 %, or a
/// document tagged with a gray profile other than that one.
/// document tagged with a gray profile other than that one. An 8-bit Monotone document (one ink)
/// takes the colour's gray with a gamma 2.2 curve instead, which fits the reference's values there
/// (RGB 200,30,30 / 30,30,200 / 140,140,140 become 104 / 64 / 139; sGray gives 104 / 61 / 140),
/// not its luma 81 (#17478). Duotones with more inks and 16-bit Duotones were not measured.
pub fn swatch_in_doc(s: &Session, c: [f32; 4]) -> [f32; 4] {
let Some(st) = s.active().filter(|st| st.doc.mode == ColorMode::Grayscale) else { return c };
let Some((st, target)) = s.active().and_then(|st| swatch_gray(&st.doc).map(|t| (st, t))) else { return c };
let default_gray = Builtin::DotGain20Compat;
if s.color().settings.working_gray != default_gray.id() || document_profile(&st.doc).content_hash() != default_gray.profile().content_hash() {
return c;
}
let opts = craft_cms::TransformOptions { intent: Intent::RelativeColorimetric, bpc: true, ..Default::default() };
let sgray = Arc::new(Builtin::SGray.profile().clone());
let Ok(t) = craft_cms::cached(&working_profile(ColorMode::Rgb), &sgray, opts) else { return c };
let Ok(t) = craft_cms::cached(&working_profile(ColorMode::Rgb), &Arc::new(target.profile().clone()), opts) else { return c };
let mut out = [0.0f32; 16];
if t.eval(&[c[0], c[1], c[2]], &mut out).is_err() {
return c;
@@ -863,6 +865,15 @@ pub fn swatch_in_doc(s: &Session, c: [f32; 4]) -> [f32; 4] {
}
}
/// The gray [`swatch_in_doc`] converts swatches to: sGray in Grayscale, gamma 2.2 in 8-bit Monotone.
fn swatch_gray(doc: &Document) -> Option<Builtin> {
match doc.mode {
ColorMode::Grayscale => Some(Builtin::SGray),
ColorMode::Duotone if doc.depth == SampleType::U8 && doc.duotone.as_ref().is_some_and(|d| d.inks.len() == 1) => Some(Builtin::GrayGamma22),
_ => None,
}
}
/// Profile describing the compositor's RGB output for this document (see the module docs).
pub fn composite_profile(doc: &Document) -> Arc<Profile> {
match mode_space(doc.mode) {
+14 -5
View File
@@ -14,6 +14,7 @@ use photocraft_geom::TILE_SIZE;
use photocraft_raster::Surface;
use serde_json::{Value, json};
use crate::color_cmds::swatch_in_doc;
use crate::commands::{cmyk_param, color_param, layer_param, parse_blend};
use crate::content_sampling::{content_aware, content_aware_options};
pub use crate::pixel_fill::greyed_modes;
@@ -88,16 +89,18 @@ pub fn fill(s: &mut Session, p: &Value) -> Result<Value> {
let mut out = json!({ "contents": contents });
let restore = contents == "history";
let source = match contents {
"foreground" => color_source(s, s.tools.foreground, 0),
// As the document takes the swatch: its gray profile's value in a Grayscale document (#11940).
"background" => color_source(s, crate::color_cmds::swatch_in_doc(s, s.tools.background), 1),
// As the document takes the swatch: its gray profile's value in a Grayscale document (#11940, #17469).
"foreground" => color_source(s, swatch_in_doc(s, s.tools.foreground), 0),
"background" => color_source(s, swatch_in_doc(s, s.tools.background), 1),
"color" => {
let rgba = color_param(CMD, p, "color", s.tools.foreground)?;
let picked = color_param(CMD, p, "color", s.tools.foreground)?;
// The picked inks are matched on the colour as picked; only the written value is converted.
let rgba = swatch_in_doc(s, picked);
match cmyk_param(CMD, p, "colorCmyk")? {
Some(cmyk) if rgba[3] >= 1.0 => Source::Cmyk { rgba, cmyk },
Some(_) => return Err(bad("`colorCmyk` requires an opaque color")),
None if p.get("color").is_none_or(Value::is_null) => color_source(s, rgba, 0),
None => match s.tools.matching_cmyk([rgba[0], rgba[1], rgba[2]]) {
None => match s.tools.matching_cmyk([picked[0], picked[1], picked[2]]) {
Some(cmyk) if rgba[3] >= 1.0 => Source::Cmyk { rgba, cmyk },
_ => Source::Color(rgba),
},
@@ -191,6 +194,12 @@ mod tests;
#[cfg(test)]
mod mode_by_document_tests;
#[cfg(test)]
mod grayscale_color_tests;
#[cfg(test)]
mod duotone_grey_value_tests;
#[cfg(test)]
#[path = "fill_dissolve_opacity_rounding_tests.rs"]
mod fill_dissolve_opacity_rounding_tests;
@@ -0,0 +1,61 @@
//! #17478: in an 8-bit Monotone document Edit › Fill with a colour writes the colour's grey (gamma 2.2, which fits the
//! reference's values), not its luma: Background 30,30,200 gives about 64 (the reference measured 64), not 49. A
//! Grayscale document's sGray would give 61. Opacity keeps its linear blend. Duotones with more inks and 16-bit
//! Duotones were not measured and keep the luma.
use super::*;
use serde_json::json;
const RED: [f32; 4] = [200.0 / 255.0, 30.0 / 255.0, 30.0 / 255.0, 1.0];
const BLUE: [f32; 4] = [30.0 / 255.0, 30.0 / 255.0, 200.0 / 255.0, 1.0];
/// A white 200×100 document of `mode` (`monotone`, `duotone` or `grayscale`) at `depth` bits, selection
/// 50,25-150,75, foreground red and background blue.
fn session(mode: &str, depth: u32) -> Session {
let mut s = crate::session();
s.execute("file.new", json!({"width": 200, "height": 100, "mode": "grayscale", "depth": depth, "background": "white"})).unwrap();
if mode != "grayscale" {
s.execute("image.mode.duotone", json!({"type": mode})).unwrap();
}
s.execute("select.rect", json!({"x": 50, "y": 25, "width": 100, "height": 50, "antiAlias": false})).unwrap();
s.tools.foreground = RED;
s.tools.background = BLUE;
s
}
/// The grey level (0..=255) at the selection's centre after `edit.fill` with `params`.
fn filled(mode: &str, depth: u32, params: serde_json::Value) -> i32 {
let mut s = session(mode, depth);
s.execute("edit.fill", params).unwrap();
let v = s.execute("document.pixel", json!({"x": 100, "y": 50})).unwrap();
(v[0].as_f64().unwrap() * 255.0).round() as i32
}
#[test]
fn monotone_fills_give_the_measured_grey_values() {
for (params, want) in [
(json!({"contents": "foreground"}), 104),
(json!({"contents": "background"}), 64),
(json!({"contents": "color", "color": "#8c8c8c"}), 139),
(json!({"contents": "gray"}), 128),
(json!({"contents": "black"}), 0),
] {
let got = filled("monotone", 8, params.clone());
assert!((got - want).abs() <= 1, "{params}: {got}, the reference gives {want}");
}
}
#[test]
fn monotone_opacity_stays_a_linear_blend() {
let half = filled("monotone", 8, json!({"contents": "black", "opacity": 50}));
assert!((127..=128).contains(&half), "Black at 50 %: {half}");
let under = filled("monotone", 8, json!({"contents": "black", "opacity": 49}));
assert!((129..=131).contains(&under), "Black at 49 %: {under}");
}
#[test]
fn unmeasured_duotones_keep_the_luma() {
// Rec.601 luma of 200,30,30 is 81; the grey working space gives about 104.
assert_eq!(filled("duotone", 8, json!({"contents": "foreground"})), 81, "two inks");
assert_eq!(filled("monotone", 16, json!({"contents": "foreground"})), 81, "16-bit Monotone");
}
@@ -0,0 +1,95 @@
//! #17469: in a Grayscale document Edit › Fill with Color or Foreground Color writes the colour as the document takes
//! it, the same grey Background Color already writes (#11940): RGB 0, 0, 255 gives about 69 (the reference measured
//! 69), not its luma 29. Other modes keep the colour; Black, White and 50% Gray stay exact.
use super::*;
use serde_json::json;
const BLUE: [f32; 4] = [0.0, 0.0, 1.0, 1.0];
const GREEN_200: [f32; 4] = [0.0, 200.0 / 255.0, 0.0, 1.0];
/// A white 64×32 document of `mode` at `depth` bits.
fn session(mode: &str, depth: u32) -> Session {
let mut s = crate::session();
s.execute("file.new", json!({"width": 64, "height": 32, "mode": mode, "depth": depth, "background": "white"})).unwrap();
s
}
fn pixel(s: &mut Session) -> [f32; 4] {
let v = s.execute("document.pixel", json!({"x": 50, "y": 20})).unwrap();
let a: Vec<f32> = v.as_array().unwrap().iter().map(|c| c.as_f64().unwrap() as f32).collect();
[a[0], a[1], a[2], a[3]]
}
fn level(c: f32) -> i32 {
(c * 255.0).round() as i32
}
/// The pixel after filling a fresh document with `params`, the foreground and background both set to `c`.
fn filled(mode: &str, depth: u32, c: [f32; 4], params: serde_json::Value) -> [f32; 4] {
let mut s = session(mode, depth);
s.tools.foreground = c;
s.tools.background = c;
s.execute("edit.fill", params).unwrap();
pixel(&mut s)
}
fn hex(c: [f32; 4]) -> String {
format!("#{:02x}{:02x}{:02x}", level(c[0]), level(c[1]), level(c[2]))
}
#[test]
fn gray_8_color_fill_writes_the_converted_gray_not_the_luma() {
let p = filled("grayscale", 8, [1.0; 4], json!({"contents": "color", "color": "#0000ff"}));
// The luma grey is 29; the colour as the document takes it is about 69 (sGray gives 70).
assert!((level(p[0]) - 69).abs() <= 1, "filled pixel {p:?}");
assert_eq!(level(p[0]), level(p[2]), "the grey is written as equal channels");
}
#[test]
fn color_and_foreground_equal_background_color_at_every_gray_depth() {
for depth in [8, 16, 32] {
for c in [BLUE, GREEN_200] {
let bg = filled("grayscale", depth, c, json!({"contents": "background"}));
let fg = filled("grayscale", depth, c, json!({"contents": "foreground"}));
let color = filled("grayscale", depth, c, json!({"contents": "color", "color": hex(c)}));
// The old `{"color": …}` form without a colour takes the foreground.
let default = filled("grayscale", depth, c, json!({}));
for (what, got) in [("foreground", fg), ("color", color), ("default color", default)] {
assert!((got[0] - bg[0]).abs() < 0.5 / 255.0, "{depth}-bit {}: {what} {got:?}, background {bg:?}", hex(c));
}
}
}
}
#[test]
fn other_modes_keep_the_colour_as_it_is() {
for c in [BLUE, GREEN_200] {
let p = filled("rgb", 8, [1.0; 4], json!({"contents": "color", "color": hex(c)}));
assert_eq!([level(p[0]), level(p[1]), level(p[2])], [level(c[0]), level(c[1]), level(c[2])], "RGB color {p:?}");
let q = filled("rgb", 8, c, json!({"contents": "foreground"}));
assert_eq!(q, p, "RGB foreground");
for mode in ["cmyk", "lab"] {
let before = filled(mode, 8, c, json!({"contents": "background"}));
for params in [json!({"contents": "color", "color": hex(c)}), json!({"contents": "foreground"})] {
let got = filled(mode, 8, c, params.clone());
assert!(got.iter().zip(before).all(|(a, b)| (a - b).abs() < 0.5 / 255.0), "{mode} {params}: {got:?} vs {before:?}");
}
}
}
}
#[test]
fn black_white_and_gray_stay_exact_in_gray_documents() {
for depth in [8, 16, 32] {
for (contents, want) in [("black", 0.0), ("white", 1.0)] {
let p = filled("grayscale", depth, BLUE, json!({"contents": contents}));
assert_eq!(p[0], want, "{depth}-bit {contents}");
}
let g = filled("grayscale", depth, BLUE, json!({"contents": "gray"}));
assert!((g[0] - 128.0 / 255.0).abs() < 0.5 / 255.0, "{depth}-bit 50% Gray {g:?}");
let black = filled("grayscale", depth, [1.0; 4], json!({"contents": "color", "color": "#000000"}));
let white = filled("grayscale", depth, [0.0, 0.0, 0.0, 1.0], json!({"contents": "color", "color": "#ffffff"}));
assert_eq!((black[0], white[0]), (0.0, 1.0), "{depth}-bit Color black and white");
}
}
@@ -68,10 +68,13 @@ fn solid_color_fill_matches_edit_fill() {
s.execute("layer.newFillLayer.solidColor", json!({"color": "#0000ff"})).unwrap();
assert_in_mode(&s, cm, mode);
let got = pixel(&mut s, 10, 10);
let want = painted(mode, depth, BLUE);
assert!(close(got, want, 0.01), "{mode}/{depth}: fill layer {got:?}, painted {want:?}");
if cm == ColorMode::Grayscale {
// Edit › Fill converts a colour through the document's grey as the reference does (#17469); the fill
// layer was not measured and keeps its luma grey.
assert!((got[0] - 0.114).abs() < 0.01 && got[0] == got[1] && got[1] == got[2], "{mode}/{depth}: {got:?}");
} else {
let want = painted(mode, depth, BLUE);
assert!(close(got, want, 0.01), "{mode}/{depth}: fill layer {got:?}, painted {want:?}");
}
}
}
+2 -2
View File
@@ -11,7 +11,7 @@ use crate::PhotocraftApp;
use craft::imaging::ui::transform_preview::StylePlan;
/// A 200 × 160 document with a 40 × 20 blue block at (60, 60) on its own layer, wearing a 4 px
/// red outside Stroke.
/// yellow outside Stroke (yellow stays apart from the blue in every mode, also as a Grayscale grey).
fn app(mode: &str, depth: u32) -> Box<PhotocraftApp> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 200, "height": 160, "mode": mode, "depth": depth})).unwrap();
@@ -19,7 +19,7 @@ fn app(mode: &str, depth: u32) -> Box<PhotocraftApp> {
app.run("select.rect", json!({"x": 60, "y": 60, "width": 40, "height": 20})).unwrap();
app.run("edit.fill", json!({"color": "#0000ff"})).unwrap();
app.run("select.deselect", json!({})).unwrap();
app.run("layer.layerStyle.stroke", json!({"size": 4, "color": "#ff0000", "position": "outside"})).unwrap();
app.run("layer.layerStyle.stroke", json!({"size": 4, "color": "#ffff00", "position": "outside"})).unwrap();
app.sync_views();
app
}