diff --git a/crates/ui-egui/src/dialogs/color_picker.rs b/crates/ui-egui/src/dialogs/color_picker.rs index 1e74be421d..f33eed336b 100644 --- a/crates/ui-egui/src/dialogs/color_picker.rs +++ b/crates/ui-egui/src/dialogs/color_picker.rs @@ -328,17 +328,18 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { /// The Color Picker with `key` on `color`, with the eyedropper, the gamut warning, the document's /// swatches and libraries, and with `cmyk` (a CMYK document) the CMYK inks. `inks`: the profiles of the -/// document the picker edits when that is not the front document; it shows the color, tests the gamut -/// and fills the CMYK fields through them. `None`: the active profiles. +/// document the picker edits when that is not the front document; it shows a CMYK color, tests the +/// gamut and fills the CMYK fields through them. `None`: the active profiles. fn picker(app: &VectorcraftApp, key: &str, color: &Color, cmyk: bool, inks: Option<&Arc>) -> ColorPickerDialog { - let active = cms::active(); let mut dialog = ColorPickerDialog::new(key, tl!("Color Picker"), srgba_in(color, inks)) .eyedropper(true) .add_to_swatches(false) .gamut(inks.map_or_else(|| gamut(app), |i| gamut_of(i.clone()))) .working_rgb(WorkingRgb) .swatch_view(swatch_view(app)); - if cmyk && let Some(converter) = working_cmyk(inks.unwrap_or(&active)) { + if let Some(cms) = picker_cmyk(cmyk, inks) + && let Some(converter) = working_cmyk(&cms) + { dialog = dialog.cmyk(&converter); } if matches!(color, Color::Cmyk { .. } | Color::Lab { .. }) { @@ -347,6 +348,13 @@ fn picker(app: &VectorcraftApp, key: &str, color: &Color, cmyk: bool, inks: Opti dialog } +/// The color-management context whose CMYK profile the picker's CMYK fields convert through: `inks` +/// whenever given, in an RGB document too, else the active profiles in a CMYK document (`cmyk`). +/// `None`: the picker's built-in coated CMYK. +fn picker_cmyk(cmyk: bool, inks: Option<&Arc>) -> Option> { + inks.cloned().or_else(|| cmyk.then(cms::active)) +} + /// Opens the Color Picker a running script's `app.showColorPicker` waits at, on `color`; `cmyk`: /// the run's own active document is in CMYK, which the session does not show between slices; /// `profiles`: the RGB and CMYK profiles that document is assigned, which OK converts the pick through. It @@ -377,10 +385,12 @@ fn script_inks(profiles: &(Option, Option)) -> Arc { cms::for_document(profiles.0.as_deref(), profiles.1.as_deref()) } -/// `color` as display sRGB through `inks` (`None`: the active profiles, as [`srgba`]). +/// `color` as display sRGB: a CMYK color through the CMYK profile of `inks`, anything else as +/// [`srgba`] shows it. An RGB color keeps the active RGB profile, which the picker's RGB fields +/// ([`WorkingRgb`]) read its channel values in. fn srgba_in(color: &Color, inks: Option<&Arc>) -> Srgba { match (inks, color) { - (Some(inks), Color::Rgb { .. } | Color::Cmyk { .. }) => { + (Some(inks), Color::Cmyk { .. }) => { let [r, g, b] = inks.display_rgb(color); Srgba::rgb(r, g, b) } diff --git a/crates/ui-egui/src/dialogs/color_picker/tests.rs b/crates/ui-egui/src/dialogs/color_picker/tests.rs index 14041cbce2..9b9a6ab715 100644 --- a/crates/ui-egui/src/dialogs/color_picker/tests.rs +++ b/crates/ui-egui/src/dialogs/color_picker/tests.rs @@ -509,3 +509,22 @@ fn a_script_picker_shows_cmyk_in_the_scripts_document_profile() { let inks = script_inks(&(None, Some(cms::FOGRA52_CMYK.to_string()))); assert_eq!(inks.settings().cmyk, cms::FOGRA52_CMYK, "the CMYK fields and gamut use the script document's profile"); } + +#[test] +fn a_script_picker_in_an_rgb_document_fills_cmyk_fields_in_the_scripts_profile() { + // An RGB document can carry its own CMYK profile; the picker's CMYK fields convert through it. + let inks = script_inks(&(None, Some(cms::FOGRA52_CMYK.to_string()))); + let fields = picker_cmyk(false, Some(&inks)).map(|c| c.settings().cmyk.clone()); + assert_eq!(fields.as_deref(), Some(cms::FOGRA52_CMYK), "the script document's CMYK profile, in an RGB document too"); + assert!(picker_cmyk(false, None).is_none(), "the front RGB document keeps the built-in CMYK fields"); +} + +#[test] +fn a_script_picker_opens_an_rgb_color_as_its_rgb_fields_read_it() { + // The RGB fields read channel values in the working RGB profile, so the opening swatch matches them. + let inks = script_inks(&(Some(cms::WIDE_GAMUT_RGB.to_string()), None)); + let color = Color::rgb(0.5, 0.3, 0.2); + let [r, g, b] = inks.display_rgb(&color); + assert_ne!(Srgba::rgb(r, g, b), srgba(&color), "the two RGB profiles show the color differently"); + assert_eq!(srgba_in(&color, Some(&inks)), srgba(&color), "the opening color and the RGB fields use one profile"); +} diff --git a/docs/mcp.md b/docs/mcp.md index 4407509e2b..7f3d55e255 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -4704,7 +4704,9 @@ the color's `typename` can differ from the picker's model and from the color giv picker convert from the RGB profile of the document the window shows (the working RGB profile when it shows none) to the RGB profile of the run's active document, and in a CMYK document they then separate through that document's CMYK profile. Hex, screen-sample and HSB picks, and typed values that Only Web Colors snaps, start in display sRGB and convert only through the active -document's profiles. An answer taken from `answers` +document's profiles. The picker's CMYK fields, its gamut warning and the swatch of a CMYK color it opens on use the +CMYK profile of the run's active document, in an RGB document too; an RGB color opens as the document the window +shows draws it, the profile its RGB fields read. An answer taken from `answers` keeps the color model it was given. The picker opens on the color as given, whatever Only Web Colors was left at in another picker. OK with both the color and its model unchanged, and Cancel, return a new object of the color the script gave. The picker shows no preview on the selection and has no Stop button: the status bar's Stop and quitting