mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 19:51:26 -05:00
Color Picker: a script's picker shows color in its own document's profiles
app.showColorPicker opened the picker with the front document's color profiles: the opening color, the CMYK fields and the out-of-gamut test used the active context, while OK converted the pick through the script's document. With two documents assigned different profiles, the picker showed and separated colors in the wrong space. The script picker now draws its opening color, fills its CMYK fields and tests the gamut through the profiles of the script's document. Pickers opened from the panels keep using the active profiles.
This commit is contained in:
@@ -34,7 +34,7 @@ use vectorcraft_engine::cmd::swatchlib;
|
||||
use vectorcraft_engine::script::DialogAnswer;
|
||||
|
||||
use super::preview::LivePreview;
|
||||
use crate::panels::color::{WorkingRgb, color_of_values, gamut, model_values};
|
||||
use crate::panels::color::{WorkingRgb, color_of_values, gamut, gamut_of, model_values};
|
||||
use crate::panels::swatches::{DOCUMENT_SWATCHES, colour_libraries};
|
||||
use crate::panels::{color_json, srgba};
|
||||
use crate::{VectorcraftApp, screen_color};
|
||||
@@ -236,10 +236,10 @@ fn library_index(app: &VectorcraftApp, limit: &str) -> Option<usize> {
|
||||
colour_libraries(app).iter().position(|lib| lib.id == limit).map(|i| i + 1)
|
||||
}
|
||||
|
||||
/// The working CMYK profile as the picker's CMYK converter, so its CMYK fields follow the document's
|
||||
/// The CMYK profile of `inks` as the picker's CMYK converter, so its CMYK fields follow the document's
|
||||
/// inks; `None` (the picker's built-in coated CMYK) when the profile can't link to sRGB.
|
||||
fn working_cmyk() -> Option<CmykConverter> {
|
||||
let bytes = cms::icc_bytes(&cms::active().settings().cmyk).ok()?;
|
||||
fn working_cmyk(inks: &cms::Cms) -> Option<CmykConverter> {
|
||||
let bytes = cms::icc_bytes(&inks.settings().cmyk).ok()?;
|
||||
CmykConverter::new(Arc::new(Profile::parse(&bytes).ok()?)).ok()
|
||||
}
|
||||
|
||||
@@ -295,7 +295,7 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
|
||||
(None, false) => "fill",
|
||||
};
|
||||
let cmyk = app.session.active().is_some_and(|s| s.doc.color_mode == ColorMode::Cmyk);
|
||||
let mut dialog = picker(app, key, &color, cmyk);
|
||||
let mut dialog = picker(app, key, &color, cmyk, None);
|
||||
// The host replaces a picker only for the same document and artboard.
|
||||
if let Some((document, _, index, _)) = artboard {
|
||||
dialog = dialog.instance((document, index));
|
||||
@@ -327,15 +327,18 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
|
||||
}
|
||||
|
||||
/// 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 working CMYK inks.
|
||||
fn picker(app: &VectorcraftApp, key: &str, color: &Color, cmyk: bool) -> ColorPickerDialog {
|
||||
let mut dialog = ColorPickerDialog::new(key, tl!("Color Picker"), srgba(color))
|
||||
/// 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.
|
||||
fn picker(app: &VectorcraftApp, key: &str, color: &Color, cmyk: bool, inks: Option<&Arc<cms::Cms>>) -> 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(gamut(app))
|
||||
.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() {
|
||||
if cmyk && let Some(converter) = working_cmyk(inks.unwrap_or(&active)) {
|
||||
dialog = dialog.cmyk(&converter);
|
||||
}
|
||||
if matches!(color, Color::Cmyk { .. } | Color::Lab { .. }) {
|
||||
@@ -352,14 +355,15 @@ pub(crate) fn open_for_script(app: &mut VectorcraftApp, color: Color, cmyk: bool
|
||||
let Some(dialogs) = app.dialogs() else { return };
|
||||
// The given colour's own values, so that OK can tell an untouched pick from another model's.
|
||||
// The Only Web Colors setting of earlier pickers is not recalled, so the colour opens as given.
|
||||
let dialog = picker(app, SCRIPT_KEY, &color, cmyk).kept(model_values(&color)).web_colors_remembered(false);
|
||||
let inks = script_inks(&profiles);
|
||||
let dialog = picker(app, SCRIPT_KEY, &color, cmyk, Some(&inks)).kept(model_values(&color)).web_colors_remembered(false);
|
||||
let id = dialogs.with(|h| h.open(dialog));
|
||||
app.color_dialogs.pickers.retain(|o| o.id != id);
|
||||
let target = Target::Script { color, cmyk, profiles };
|
||||
app.color_dialogs.pickers.push(Open {
|
||||
id,
|
||||
target,
|
||||
start: srgba(&color),
|
||||
start: srgba_in(&color, Some(&inks)),
|
||||
keep: false,
|
||||
preview: LivePreview::default(),
|
||||
view: Vec::new(),
|
||||
@@ -367,6 +371,23 @@ pub(crate) fn open_for_script(app: &mut VectorcraftApp, color: Color, cmyk: bool
|
||||
});
|
||||
}
|
||||
|
||||
/// The color-management context whose CMYK profile a script's picker shows its CMYK fields in: the
|
||||
/// script's document assigned `profiles`, which may not be the window's front document.
|
||||
fn script_inks(profiles: &(Option<String>, Option<String>)) -> Arc<cms::Cms> {
|
||||
cms::for_document(profiles.0.as_deref(), profiles.1.as_deref())
|
||||
}
|
||||
|
||||
/// `color` as display sRGB through `inks` (`None`: the active profiles, as [`srgba`]).
|
||||
fn srgba_in(color: &Color, inks: Option<&Arc<cms::Cms>>) -> Srgba {
|
||||
match (inks, color) {
|
||||
(Some(inks), Color::Rgb { .. } | Color::Cmyk { .. }) => {
|
||||
let [r, g, b] = inks.display_rgb(color);
|
||||
Srgba::rgb(r, g, b)
|
||||
}
|
||||
_ => srgba(color),
|
||||
}
|
||||
}
|
||||
|
||||
/// The key of the Color Picker a script opens.
|
||||
pub(crate) const SCRIPT_KEY: &str = "script";
|
||||
|
||||
|
||||
@@ -494,3 +494,18 @@ fn the_canvas_stays_open_under_the_picker() {
|
||||
step(&mut app, &ctx, vec![]);
|
||||
assert_eq!(app.session.doc().unwrap().selection, selected, "the click under the picker leaves the selection");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_script_picker_shows_cmyk_in_the_scripts_document_profile() {
|
||||
// The front document keeps the working CMYK profile; the script's document is assigned another.
|
||||
let (mut app, _ctx) = app();
|
||||
assert_ne!(cms::active().settings().cmyk, cms::FOGRA52_CMYK, "the front document uses another CMYK profile");
|
||||
let color = Color::Cmyk { c: 1.0, m: 0.0, y: 0.0, k: 0.0 };
|
||||
open_for_script(&mut app, color, true, (None, Some(cms::FOGRA52_CMYK.to_string())));
|
||||
let open = app.color_dialogs.pickers.last().unwrap();
|
||||
let [r, g, b] = cms::for_document(None, Some(cms::FOGRA52_CMYK)).display_rgb(&color);
|
||||
assert_eq!(open.start, Srgba::rgb(r, g, b), "the picker shows the color through the script document's profile");
|
||||
assert_ne!(open.start, srgba(&color), "the front document's profile shows the color differently");
|
||||
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");
|
||||
}
|
||||
|
||||
@@ -180,6 +180,20 @@ pub(crate) fn gamut(_app: &VectorcraftApp) -> Gamut<'static> {
|
||||
})
|
||||
}
|
||||
|
||||
/// The printable test of [`gamut`] in the CMYK space of `inks` instead of the active one, for a
|
||||
/// picker that edits a document other than the front one.
|
||||
pub(crate) fn gamut_of(inks: std::sync::Arc<cms::Cms>) -> Gamut<'static> {
|
||||
Gamut::new(move |c: Srgba| {
|
||||
// The picker samples display sRGB: the same color in the document's RGB space.
|
||||
let color = crate::screen_color::sampled(&inks, [c.r, c.g, c.b]);
|
||||
if !inks.out_of_gamut(&color) {
|
||||
return c;
|
||||
}
|
||||
let [r, g, b] = inks.display_rgb(&inks.convert(&color, cms::Model::Cmyk, inks.settings().intent));
|
||||
Srgba::new(r, g, b, c.a)
|
||||
})
|
||||
}
|
||||
|
||||
/// Serde reader for [`crate::UiState::color_panel`]: junk gives the default, so an unknown value
|
||||
/// doesn't lose the rest of the preferences.
|
||||
pub fn deserialize_panel_state<'de, D: serde::Deserializer<'de>>(d: D) -> Result<ColorPanelState, D::Error> {
|
||||
|
||||
Reference in New Issue
Block a user