Fill: colour fills in 8-bit Monotone documents take the grey value (#17478)

swatch_in_doc now converts swatches in an 8-bit Monotone document as well,
through a gamma 2.2 grey, which fits the reference application's measured
values there (200,30,30 -> 104, 30,30,200 -> 64, 140,140,140 -> 139) where
sGray (the Grayscale document's curve) gives 61 and 140. Duotones with more
inks and 16-bit Duotones were not measured and keep the luma.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-11-30 02:32:04 +01:00
1 parent 51c85c8f1e
commit 8ddbee2c25
3 files changed
+79 -4

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) {
+3
View File
@@ -178,6 +178,9 @@ 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");
}