Color Wheel: the contrast row takes its chip from the ratio's answer

With the background sampled, the row asks color.contrast for the ratio first and shows the background in its answer, so a changed selection or revision renders the background once. The background is read on its own only when there is no ratio, so the chip still shows it for a paint with no color.
This commit is contained in:
dotCooCoo
2026-11-18 01:58:47 -08:00
parent 7da444db69
commit ce20395f42
2 changed files with 91 additions and 52 deletions
+41 -35
View File
@@ -370,49 +370,55 @@ fn background(app: &mut VectorcraftApp, ui: &Ui) -> Option<Color> {
Color::from_hex(&hex?)
}
/// The `color.contrast` answer against `pinned`, else against the color behind the selection
/// (`against: "below"`), kept per [`RatioKey`].
fn ratio_answer(app: &mut VectorcraftApp, ui: &Ui, pinned: Option<Color>) -> Option<Value> {
let st = app.session.active();
let key: RatioKey = (
pinned.map_or_else(|| "below".to_string(), |bg| format!("{bg:?}")),
format!("{:?}", super::active_paint(app)),
st.map(|d| (d.uid, d.revision)),
st.map(|d| d.selection.objects.clone()).unwrap_or_default(),
st.and_then(picked_part),
app.session.fill_active,
app.session.appearance_item(),
app.session.selected_stop(),
app.session.selected_freeform_point(),
app.session.prefs.black_on_screen.clone(),
under(),
app.session.fill_mesh_points(),
);
match pstate::<Option<(RatioKey, Option<Value>)>>(ui.ctx(), RATIO) {
Some((k, r)) if k == key => r,
_ => {
let against = pinned.as_ref().map_or_else(|| json!("below"), color_json);
let r = app.session.execute("color.contrast", &json!({ "against": against })).ok();
set_pstate(ui.ctx(), RATIO, Some((key, r.clone())));
r
}
}
}
/// The contrast row: the background's chip, the ratio cut off at two decimals and the AA and AAA
/// results for the chosen category; a dash when there is no background or no color to measure.
/// Without a pinned background the row asks `color.contrast` for the color behind the selection
/// (`against: "below"`), also during an interaction, so translucent groups and layers count as the
/// command counts them; the chip then shows the background in the answer, else the pinned color or
/// the one kept by [`background`].
/// command counts them; the chip then shows the background in the answer. Only when there is no
/// answer is the background read on its own ([`background`]), so the chip still shows it.
fn contrast_row(app: &mut VectorcraftApp, ui: &mut Ui) {
let category = app.ui.color_wheel.contrast_for;
let pinned = app.ui.color_wheel.contrast_background.is_some();
let against = background(app, ui);
let sampled = !pinned;
let answer = against.as_ref().and_then(|bg| {
let st = app.session.active();
let key: RatioKey = (
if sampled { "below".to_string() } else { format!("{bg:?}") },
format!("{:?}", super::active_paint(app)),
st.map(|d| (d.uid, d.revision)),
st.map(|d| d.selection.objects.clone()).unwrap_or_default(),
st.and_then(picked_part),
app.session.fill_active,
app.session.appearance_item(),
app.session.selected_stop(),
app.session.selected_freeform_point(),
app.session.prefs.black_on_screen.clone(),
under(),
app.session.fill_mesh_points(),
);
match pstate::<Option<(RatioKey, Option<Value>)>>(ui.ctx(), RATIO) {
Some((k, r)) if k == key => r,
_ => {
let against = if sampled { json!("below") } else { color_json(bg) };
let r = app.session.execute("color.contrast", &json!({ "against": against })).ok();
set_pstate(ui.ctx(), RATIO, Some((key, r.clone())));
r
}
}
});
let pinned = app.ui.color_wheel.contrast_background;
let answer = ratio_answer(app, ui, pinned);
// The background the ratio was measured against.
let measured = answer.as_ref().filter(|_| pinned.is_none()).and_then(|r| r["against"].as_str()).and_then(Color::from_hex);
let chip = match pinned.or(measured) {
Some(c) => Some(c),
None => background(app, ui),
};
let resp = ui
.horizontal(|ui| {
let (rect, _) = ui.allocate_exact_size(vec2(CHIP, CHIP), egui::Sense::hover());
// The chip shows the background the ratio was measured against.
let measured = answer.as_ref().filter(|_| sampled).and_then(|r| r["against"].as_str()).and_then(Color::from_hex);
match measured.or(against) {
match chip {
Some(c) => widgets::swatch_tile(ui, rect, &Paint::solid(c), false, false),
None => widgets::swatch_tile(ui, rect, &Paint::None, false, false),
}
@@ -433,7 +439,7 @@ fn contrast_row(app: &mut VectorcraftApp, ui: &mut Ui) {
"WCAG 2.2 contrast minimums: normal text AA 4.5:1 (1.4.3) and AAA 7:1 (1.4.6), large text AA 3:1 and AAA 4.5:1, graphics and controls AA 3:1 (1.4.11)."
)
.to_string();
if !pinned {
if pinned.is_none() {
tip.push(' ');
tip.push_str(tl!("The background is read at the center of the selection."));
}
@@ -148,15 +148,27 @@ fn the_row_follows_the_default_fill_with_nothing_selected() {
assert!(shows(&h, "1.00:1"), "white on the pinned white");
}
/// While a drag on the wheel is in progress the color behind the selection is not read again,
/// although every preview raises the document's revision.
type Under = (vectorcraft_color::cms::ColorSettings, u64);
/// The background read on its own, with what it was read for.
type Behind = Option<((u64, u64, Vec<vectorcraft_doc::NodeId>, Option<Vec<u32>>, String, Under), Option<String>)>;
type Ratio = Option<(super::color_wheel::RatioKey, Option<Value>)>;
fn behind(h: &Harness<'static, VectorcraftApp>) -> Behind {
pstate::<Behind>(&h.ctx, "color-wheel-behind")
}
/// The background in the row's kept answer.
fn measured(h: &Harness<'static, VectorcraftApp>) -> Option<String> {
pstate::<Ratio>(&h.ctx, "color-wheel-ratio").and_then(|(_, r)| r).and_then(|r| r["against"].as_str().map(str::to_string))
}
/// During a drag on the wheel every preview raises the document's revision, and the row asks again
/// for the ratio, whose answer gives the chip: the background is not read on its own as well.
#[test]
fn a_drag_on_the_wheel_keeps_the_background() {
type Under = (vectorcraft_color::cms::ColorSettings, u64);
type Kept = Option<((u64, u64, Vec<vectorcraft_doc::NodeId>, Option<Vec<u32>>, String, Under), Option<String>)>;
fn a_drag_on_the_wheel_reads_the_background_only_with_the_ratio() {
let mut h = harness(app("#336699"));
let read = |h: &Harness<'static, VectorcraftApp>| pstate::<Kept>(&h.ctx, "color-wheel-behind").map(|(k, _)| k.1);
let before = read(&h).expect("the background was read");
assert_eq!(measured(&h).as_deref(), Some("#ffffff"));
assert!(behind(&h).is_none(), "the answer gave the background");
let sq = h.get_by_label("Saturation and brightness").rect();
press(&mut h, sq.center(), true);
for i in 1..4 {
@@ -164,19 +176,41 @@ fn a_drag_on_the_wheel_keeps_the_background() {
h.run_steps(1);
}
assert!(h.state().session.in_interaction(), "the drag previews on an interaction");
assert!(h.state().session.active().unwrap().revision > before, "the previews raised the revision");
assert_eq!(read(&h), Some(before), "the background was not read again during the drag");
press(&mut h, sq.center() + vec2(24.0, 0.0), false);
h.run_steps(2);
assert!(read(&h).unwrap() > before, "after the drag it is read for the new revision");
assert!(behind(&h).is_none(), "the background was not read on its own during or after the drag");
assert_eq!(measured(&h).as_deref(), Some("#ffffff"));
}
/// An interaction keeps the background only for the same document and selection: another
/// selection is read again.
/// With no color to measure the row has no ratio, and the chip still shows the background, read on
/// its own; during an interaction that is kept only for the same document and selection.
#[test]
fn a_paint_with_no_color_still_shows_the_background() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 100, "height": 200})).unwrap();
app.run("paint.setFill", json!({"color": "#ffff00"})).unwrap();
app.run("object.lock", json!({})).unwrap();
let id = |v: Value| v["id"].as_u64().unwrap();
let b = id(app.run("shape.rectangle", json!({"x": 130, "y": 20, "width": 40, "height": 40})).unwrap());
app.run("paint.setFill", json!({"none": true})).unwrap();
app.run("shape.rectangle", json!({"x": 20, "y": 20, "width": 40, "height": 40})).unwrap();
app.run("paint.setFill", json!({"none": true})).unwrap();
app.run("ui.colorWheel", json!({"contrast": true})).unwrap();
let mut h = harness(app);
assert!(shows(&h, "—"), "a fill of None has no ratio");
let hex = |h: &Harness<'static, VectorcraftApp>| behind(h).and_then(|(_, hex)| hex);
assert_eq!(hex(&h).as_deref(), Some("#ffff00"));
h.state_mut().run("select.set", json!({"ids": [b]})).unwrap();
h.state_mut().session.begin_interaction("test").unwrap();
h.run_steps(2);
assert_eq!(hex(&h).as_deref(), Some("#ffffff"), "the other selection's background is read during the interaction");
}
/// An interaction keeps no answer for another selection: the row asks again, and the chip shows
/// the other selection's background.
#[test]
fn an_interaction_reads_the_background_again_for_another_selection() {
type Under = (vectorcraft_color::cms::ColorSettings, u64);
type Kept = Option<((u64, u64, Vec<vectorcraft_doc::NodeId>, Option<Vec<u32>>, String, Under), Option<String>)>;
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 100, "height": 200})).unwrap();
@@ -188,12 +222,11 @@ fn an_interaction_reads_the_background_again_for_another_selection() {
app.run("paint.setFill", json!({"color": "#000000"})).unwrap();
app.run("ui.colorWheel", json!({"contrast": true})).unwrap();
let mut h = harness(app);
let hex = |h: &Harness<'static, VectorcraftApp>| pstate::<Kept>(&h.ctx, "color-wheel-behind").and_then(|(_, hex)| hex);
assert_eq!(hex(&h).as_deref(), Some("#ffff00"));
assert_eq!(measured(&h).as_deref(), Some("#ffff00"));
h.state_mut().run("select.set", json!({"ids": [b]})).unwrap();
h.state_mut().session.begin_interaction("test").unwrap();
h.run_steps(2);
assert_eq!(hex(&h).as_deref(), Some("#ffffff"), "the other selection's background is read during the interaction");
assert_eq!(measured(&h).as_deref(), Some("#ffffff"), "the other selection's background is read during the interaction");
}
/// Without a pin the row asks for the color behind the selection, so a translucent group holding