Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Color: Dot Gain 20%-compatible default working gray, as in Photoshop (#4610)

MrCloudy24062d40070d2
diff --git a/crates/cms/src/builtin.rs b/crates/cms/src/builtin.rs
index b102562f4..9d42fef3f 100644
--- a/crates/cms/src/builtin.rs
+++ b/crates/cms/src/builtin.rs
@@ -32,6 +32,10 @@ pub enum Builtin {
GrayGamma22,
/// Gray with the sRGB transfer function (sGray): gray `v` looks like RGB `(v, v, v)` in sRGB.
SGray,
+ /// Gray matching Photoshop's default working gray (Dot Gain 20 %) with our own curve: a pure
+ /// gamma of 1.732, fitted on Photoshop 27.11's RGB → Grayscale (sRGB 64 / 128 / 192 and pure red
+ /// become 46 / 105 / 177 / 107, all within 1 level). Not Adobe's profile.
+ DotGain20Compat,
/// CIE L*a*b* D50 identity (device Lab = PCS Lab, ICC v4 encoding).
LabD50,
/// Synthetic coated-offset CMYK (300 % total ink, medium GCR); see [`crate::synth`].
@@ -39,7 +43,7 @@ pub enum Builtin {
}
impl Builtin {
- pub const ALL: [Builtin; 10] = [
+ pub const ALL: [Builtin; 11] = [
Builtin::Srgb,
Builtin::DisplayP3,
Builtin::AdobeRgbCompat,
@@ -48,6 +52,7 @@ impl Builtin {
Builtin::Rec2020,
Builtin::GrayGamma22,
Builtin::SGray,
+ Builtin::DotGain20Compat,
Builtin::LabD50,
Builtin::CoatedCmyk,
];
@@ -63,6 +68,7 @@ impl Builtin {
Builtin::Rec2020 => "rec2020",
Builtin::GrayGamma22 => "gray-gamma-2.2",
Builtin::SGray => "sgray",
+ Builtin::DotGain20Compat => "dot-gain-20-compat",
Builtin::LabD50 => "lab-d50",
Builtin::CoatedCmyk => "coated-cmyk",
}
@@ -79,6 +85,7 @@ impl Builtin {
Builtin::Rec2020 => "Rec. ITU-R BT.2020 (Photocraft)",
Builtin::GrayGamma22 => "Gray Gamma 2.2 (Photocraft)",
Builtin::SGray => "sGray (sRGB tone curve, Photocraft)",
+ Builtin::DotGain20Compat => "Dot Gain 20%-compatible (gamma 1.732, Photocraft)",
Builtin::LabD50 => "Lab D50 identity (Photocraft)",
Builtin::CoatedCmyk => crate::synth::DESCRIPTION,
}
@@ -96,6 +103,7 @@ impl Builtin {
"rec2020" | "bt2020" => Some(Builtin::Rec2020),
"gray" | "graygamma22" | "gamma22" => Some(Builtin::GrayGamma22),
"sgray" | "graysrgb" => Some(Builtin::SGray),
+ "dotgain20" | "dotgain20compat" | "dotgain" => Some(Builtin::DotGain20Compat),
"lab" | "labd50" | "cielab" => Some(Builtin::LabD50),
"cmyk" | "coatedcmyk" | "defaultcmyk" | "photocraftcoatedcmyk" => Some(Builtin::CoatedCmyk),
_ => None,
@@ -105,18 +113,19 @@ impl Builtin {
/// The profile (built once per process).
pub fn profile(self) -> &'static Profile {
- static CELLS: [OnceLock<Profile>; 10] = [const { OnceLock::new() }; 10];
+ static CELLS: [OnceLock<Profile>; 11] = [const { OnceLock::new() }; 11];
// `ALL` lists the variants in declaration order (checked by `all_in_declaration_order`),
// so the discriminant is the cell index.
CELLS[self as usize].get_or_init(|| build(self))
}
}
-/// The default profile for a colour space: sRGB, sGray, the coated CMYK profile, Lab D50.
+/// The default profile for a colour space: sRGB, Dot Gain 20 %-compatible gray (Photoshop's default
+/// working gray), the coated CMYK profile, Lab D50.
pub fn default_for(cs: ColorSpace) -> Option<&'static Profile> {
Some(match cs {
ColorSpace::Rgb => Builtin::Srgb.profile(),
- ColorSpace::Gray => Builtin::SGray.profile(),
+ ColorSpace::Gray => Builtin::DotGain20Compat.profile(),
ColorSpace::Cmyk => Builtin::CoatedCmyk.profile(),
ColorSpace::Lab => Builtin::LabD50.profile(),
_ => return None,
@@ -268,6 +277,7 @@ fn build(b: Builtin) -> Profile {
}
Builtin::GrayGamma22 => gray(b.description(), Curve::Gamma(2.2)),
Builtin::SGray => gray(b.description(), srgb_trc()),
+ Builtin::DotGain20Compat => gray(b.description(), Curve::Gamma(1.732)),
Builtin::LabD50 => lab(),
// The shipped bytes are `synth::coated_cmyk()` (checked by the `regen` test); rebuild
// them rather than crash if they ever fail to parse.
diff --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rs
index 11aa53bd8..bdb839fa3 100644
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@ -872,7 +872,9 @@ fn distressing_and_remove_white_shapes_import_and_play() {
assert!(!d.is_background(l.id), "Layer From Background");
let surf = l.surface().unwrap();
assert!(surf.sample_channel(2, 2, 1) < 0.01, "white is cleared");
- assert!((surf.sample_channel(20, 15, 1) - (1.0 - 32.0 / 255.0)).abs() < 0.02, "the ink keeps its darkness as opacity");
+ // #202020 is about 22 in the default (Dot Gain 20 %-compatible) working gray.
+ let ink = surf.sample_channel(20, 15, 1);
+ assert!((ink - (1.0 - 22.0 / 255.0)).abs() < 0.02, "the ink keeps its darkness as opacity: {ink}");
// An image that opened as a normal layer has no Background: the step leaves it as it is.
let mut s = Session::new();
s.execute("file.new", json!({"width": 40, "height": 30, "background": "transparent"})).unwrap();
diff --git a/crates/engine/src/color_cmds.rs b/crates/engine/src/color_cmds.rs
index 794b1b550..4cf438da0 100644
--- a/crates/engine/src/color_cmds.rs
+++ b/crates/engine/src/color_cmds.rs
@@ -133,7 +133,8 @@ impl Default for ColorSettings {
Self {
working_rgb: Builtin::Srgb.id().into(),
working_cmyk: Builtin::CoatedCmyk.id().into(),
- working_gray: Builtin::SGray.id().into(),
+ // Photoshop's default working gray is Dot Gain 20 %.
+ working_gray: Builtin::DotGain20Compat.id().into(),
policy_rgb: Policy::Preserve,
policy_cmyk: Policy::Preserve,
policy_gray: Policy::Preserve,
@@ -1062,7 +1063,7 @@ pub fn specs() -> Vec<CommandSpec> {
"edit.assignProfile",
"Assign Profile…",
["Edit"],
- r##"{"profile":"working|srgb|display-p3|adobe-rgb-compat|prophoto-compat|linear-srgb|rec2020|gray-gamma-2.2|sgray|lab-d50|coated-cmyk|none" (or a path to an .icc file)}"##,
+ r##"{"profile":"working|srgb|display-p3|adobe-rgb-compat|prophoto-compat|linear-srgb|rec2020|gray-gamma-2.2|sgray|dot-gain-20-compat|lab-d50|coated-cmyk|none" (or a path to an .icc file)}"##,
has_doc,
assign_profile,
true
@@ -1080,7 +1081,7 @@ pub fn specs() -> Vec<CommandSpec> {
"edit.colorSettings",
"Color Settings…",
["Edit"],
- r##"{"workingRgb":"srgb|display-p3|adobe-rgb-compat|prophoto-compat|linear-srgb|rec2020","workingCmyk":"coated-cmyk","workingGray":"sgray|gray-gamma-2.2","policyRgb":"preserve|convert|off","policyCmyk":"preserve|convert|off","policyGray":"preserve|convert|off","askOnMismatch":bool=true,"askOnPaste":bool=true,"askOnMissing":bool=false,"intent":"relative|perceptual|saturation|absolute","blendTextGamma":1.0..2.2|bool=1.45,"bpc":bool=true,"dither":bool=true,"monitorProfile":"auto|srgb|display-p3|adobe-rgb-compat|prophoto-compat|rec2020","reset":bool=false} (working spaces and the monitor profile also accept .icc paths; monitor `auto` = the main display's profile when the platform provides it, else sRGB; the reply's `monitorStatus` says which profile is in use and why: source auto|manual|fallback, reason)"##,
+ r##"{"workingRgb":"srgb|display-p3|adobe-rgb-compat|prophoto-compat|linear-srgb|rec2020","workingCmyk":"coated-cmyk","workingGray":"dot-gain-20-compat|sgray|gray-gamma-2.2","policyRgb":"preserve|convert|off","policyCmyk":"preserve|convert|off","policyGray":"preserve|convert|off","askOnMismatch":bool=true,"askOnPaste":bool=true,"askOnMissing":bool=false,"intent":"relative|perceptual|saturation|absolute","blendTextGamma":1.0..2.2|bool=1.45,"bpc":bool=true,"dither":bool=true,"monitorProfile":"auto|srgb|display-p3|adobe-rgb-compat|prophoto-compat|rec2020","reset":bool=false} (working spaces and the monitor profile also accept .icc paths; monitor `auto` = the main display's profile when the platform provides it, else sRGB; the reply's `monitorStatus` says which profile is in use and why: source auto|manual|fallback, reason)"##,
always,
color_settings,
true,
@@ -1280,7 +1281,10 @@ mod tests {
let t = s.color.display_transform(doc(&s)).unwrap();
let mut o = [0.0f32; 3];
t.eval(&[0.5, 0.5, 0.5], &mut o);
- assert!(o.iter().all(|v| (v - 0.5).abs() < 2e-3), "sGray displays as sRGB gray: {o:?}");
+ // The default working gray (Dot Gain 20 %-compatible, gamma 1.732) shows gray 0.5 as the
+ // sRGB value of linear 0.5^1.732, as Photoshop does: lighter than sGray's 0.5.
+ let want = photocraft_color::convert::linear_to_srgb(0.5f32.powf(1.732));
+ assert!(o.iter().all(|v| (v - want).abs() < 2e-3), "Dot Gain 20 % gray displays as {want}: {o:?}");
let s = session("cmyk", 8);
assert_eq!(composite_profile(doc(&s)).description, "sRGB IEC61966-2.1");
}
diff --git a/crates/engine/src/display_color_tests.rs b/crates/engine/src/display_color_tests.rs
index 2ce0e0cfb..e40dca9bf 100644
--- a/crates/engine/src/display_color_tests.rs
+++ b/crates/engine/src/display_color_tests.rs
@@ -80,8 +80,11 @@ fn srgb_is_the_identity_fast_path() {
assert!(s.color.canvas_lut(&d, 9).unwrap().is_none(), "no GPU LUT either");
assert!(close(shown(&s, &d), [204.0, 127.5, 76.5], 0.6));
}
- // Untagged gray (sGray), CMYK (shown in sRGB) and Lab composites are sRGB too.
- let g = Document::with_background("g", Size::new(4, 4), ColorMode::Grayscale, SampleType::U8, Color::gray(0.3));
+ // sGray-tagged gray and CMYK (shown in sRGB) composites are sRGB too; untagged gray takes the
+ // default working gray (Dot Gain 20 %-compatible), which is not.
+ let mut g = Document::with_background("g", Size::new(4, 4), ColorMode::Grayscale, SampleType::U8, Color::gray(0.3));
+ assert!(!s.color.canvas_display(&g).unwrap().is_identity());
+ g.icc_profile = Some(Builtin::SGray.profile().to_bytes());
assert!(s.color.canvas_display(&g).unwrap().is_identity());
assert!(s.color.canvas_display(&cmyk_doc(None, [0.1, 0.5, 0.5, 0.0])).unwrap().is_identity());
// Cached: the same Arc for the same profiles.
@@ -195,7 +198,8 @@ fn gray_exr_round_trip_is_linear() {
let mut s = Session::new();
// An sGray document exported to EXR is linearised (#2386); re-opened it is tagged linear gray
// and displays as before.
- let d = Document::with_background("t", Size::new(8, 8), ColorMode::Grayscale, SampleType::U8, Color::gray(128.0 / 255.0));
+ let mut d = Document::with_background("t", Size::new(8, 8), ColorMode::Grayscale, SampleType::U8, Color::gray(128.0 / 255.0));
+ d.icc_profile = Some(Builtin::SGray.profile().to_bytes());
let r = photocraft_io::export(&d, "x.exr", &photocraft_io::ExportOptions::default()).unwrap();
let back = photocraft_io::import("x.exr", &r.bytes).unwrap().document;
let icc = back.icc_profile.clone().expect("tagged");
diff --git a/crates/engine/src/fill_layer_mode_tests.rs b/crates/engine/src/fill_layer_mode_tests.rs
index 69f786baa..4bcc25ef2 100644
--- a/crates/engine/src/fill_layer_mode_tests.rs
+++ b/crates/engine/src/fill_layer_mode_tests.rs
@@ -122,6 +122,9 @@ fn rgb_documents_keep_rgb_fill_colours() {
#[test]
fn gray_fill_converts_with_the_document() {
let mut s = session("gray", 16);
+ // Tagged sGray, where a gray value reads the same as RGB, so the before/after pixels compare
+ // directly (an untagged gray document takes the Dot Gain 20 %-compatible default).
+ s.execute("edit.assignProfile", json!({"profile": "sgray"})).unwrap();
s.execute("layer.newFillLayer.solidColor", json!({"color": "#0000ff"})).unwrap();
let before = pixel(&mut s, 10, 10);
s.execute("image.mode.rgb", json!({})).unwrap();
diff --git a/crates/engine/src/image_cmds.rs b/crates/engine/src/image_cmds.rs
index c56a91277..15a71376b 100644
--- a/crates/engine/src/image_cmds.rs
+++ b/crates/engine/src/image_cmds.rs
@@ -1408,6 +1408,29 @@ mod tests {
}
}
+ #[test]
+ fn grayscale_conversion_matches_photoshop() {
+ // Photoshop 27.11, default Color Settings (working gray Dot Gain 20 %): Image > Mode >
+ // Grayscale turns sRGB 64 / 128 / 192 grey and pure red into 46 / 105 / 177 / 107.
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 4, "height": 1, "background": "white"})).unwrap();
+ s.edit("paint", |doc, _| {
+ let l = doc.layers.first_mut().unwrap();
+ let surf = l.surface_mut().unwrap();
+ for (x, c) in [[64.0, 64.0, 64.0], [128.0, 128.0, 128.0], [192.0, 192.0, 192.0], [255.0, 0.0, 0.0]].iter().enumerate() {
+ surf.fill_rect(Rect::new(x as i32, 0, x as i32 + 1, 1), &[c[0] / 255.0, c[1] / 255.0, c[2] / 255.0, 1.0]);
+ }
+ Ok(())
+ })
+ .unwrap();
+ s.execute("image.mode.grayscale", json!({})).unwrap();
+ let surf = doc(&s).layers[0].surface().unwrap();
+ for (x, want) in [46.0, 105.0, 177.0, 107.0].iter().enumerate() {
+ let got = surf.pixel(x as i32, 0)[0] * 255.0;
+ assert!((got - want).abs() <= 1.5, "pixel {x}: {got} vs {want}");
+ }
+ }
+
#[test]
fn mode_and_depth_conversions() {
let mut s = session();
diff --git a/crates/ui-egui/src/chrome_ui.rs b/crates/ui-egui/src/chrome_ui.rs
index a0970ae5f..b20e913a7 100644
--- a/crates/ui-egui/src/chrome_ui.rs
+++ b/crates/ui-egui/src/chrome_ui.rs
@@ -339,7 +339,7 @@ mod tests {
let mut gray = doc();
gray.mode = photocraft_doc::ColorMode::Grayscale;
gray.icc_profile = Some(photocraft_engine::color_cmds::working_profile(gray.mode).to_bytes());
- assert_eq!(profile_name(&gray), "sGray (sRGB tone curve, Photocraft)");
+ assert_eq!(profile_name(&gray), "Dot Gain 20%-compatible (gamma 1.732, Photocraft)");
let mut custom = (*photocraft_engine::color_cmds::working_profile(photocraft_doc::ColorMode::Cmyk)).clone();
custom.description = "PhotoCraft Studio CMYK".into();