Fill: Color and Foreground Color convert to the Grayscale document's grey (#17469)

In a Grayscale document Edit > Fill with Color or Foreground Color wrote the
colour's plain luma (blue gave 29). Both now go through swatch_in_doc as
Background Color already did (blue gives 70; the reference measured 69).
Picked CMYK inks are still matched on the colour as picked, and other modes
are unchanged.

The fill-layer test no longer expects a Grayscale Solid Color layer to equal
Edit > Fill; the layer was not measured and keeps its luma grey.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-11-30 00:56:57 +01:00
1 parent 6517a37a30
commit 57e7779ed8
3 files changed
+111 -7

No files matched your search

+11 -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;
@@ -85,16 +86,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),
},
@@ -172,6 +175,9 @@ mod tests;
#[cfg(test)]
mod mode_by_document_tests;
#[cfg(test)]
mod grayscale_color_tests;
#[cfg(test)]
#[path = "fill_dissolve_opacity_rounding_tests.rs"]
mod fill_dissolve_opacity_rounding_tests;
@@ -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:?}");
}
}
}