From 522a4fc7d6ded04fd142f1fc8bb4919ddb98cc3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Fri, 27 Nov 2026 13:28:33 -0500 Subject: [PATCH 1/2] fix(ui): start Ink from the colours shown under a legend, colorbar, table or chart (#2953) --- crates/ui-egui/src/layoutview/format.rs | 44 +++++--- crates/ui-egui/src/layoutview/tests.rs | 127 +++++++++++++++++++++--- docs/user-guide/layouts.md | 7 +- 3 files changed, 150 insertions(+), 28 deletions(-) diff --git a/crates/ui-egui/src/layoutview/format.rs b/crates/ui-egui/src/layoutview/format.rs index d952fedb02..8b86c3c556 100644 --- a/crates/ui-egui/src/layoutview/format.rs +++ b/crates/ui-egui/src/layoutview/format.rs @@ -21,8 +21,7 @@ use frame::*; use legend::*; use north::*; use rustgis_engine::map::layout::{ - Background, ElementKind, INK, LAYOUT_TEXT_SIZE, Layout, MAX_BACKGROUND_PADDING, MAX_BACKGROUND_RADIUS, MAX_BACKGROUND_WIDTH, - Orientation, + Element, ElementKind, INK, LAYOUT_TEXT_SIZE, Layout, MAX_BACKGROUND_PADDING, MAX_BACKGROUND_RADIUS, MAX_BACKGROUND_WIDTH, Orientation, }; use rustgis_engine::map::symbology::Dash; use scalebar::*; @@ -253,8 +252,7 @@ pub(super) fn properties(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout, li: let mut on = ink.is_some(); let hint = tl!("Its own colour for its text and lines; off, the default dark grey"); if ui.checkbox(&mut on, "").labelled_by(input_label.id).on_hover_text(hint).changed() { - let page = layout.paper.unwrap_or(rustgis_engine::map::Rgba::rgb(0xFF, 0xFF, 0xFF)); - update(app, json!({"color": on.then(|| first_ink(e.background.as_ref(), page).hex())})); + update(app, json!({"color": on.then(|| first_ink(layout, &e).hex())})); } let Some(current) = ink else { return }; let mut c = crate::theme::c32(current); @@ -382,18 +380,40 @@ pub(super) fn choice(ui: &mut Ui, salt: (&str, u32), label: egui::Id, current: & } /// The ink an element starts with when Ink is turned on (#2257): white over what shows under its -/// text, its background drawn on the page (`page`, the page's colour, #2855), when that is dark, -/// else the default dark grey, so its text never disappears. -pub(super) fn first_ink(background: Option<&Background>, page: rustgis_engine::map::Rgba) -> rustgis_engine::map::Rgba { - // No background: nothing drawn over the page, so the page itself shows under the text. - let fill = background.map_or(rustgis_engine::map::Rgba::new(0, 0, 0, 0), |b| b.fill); - let a = u16::from(fill.a()); - let over = |c: u8, under: u8| u8::try_from((u16::from(c) * a + u16::from(under) * (255 - a)) / 255).unwrap_or(u8::MAX); - let shown = Color32::from_rgb(over(fill.r(), page.r()), over(fill.g(), page.g()), over(fill.b(), page.b())); +/// text when that is dark, else the default dark grey, so its text never disappears. What shows is +/// read at the centre of its box: the page's colour (#2855), the fills drawn there by the visible +/// elements under it (#2953), then its own background. +pub(super) fn first_ink(layout: &Layout, e: &Element) -> rustgis_engine::map::Rgba { + let centre = (e.x + e.w / 2.0, e.y + e.h / 2.0); + let page = layout.paper.unwrap_or(rustgis_engine::map::Rgba::rgb(0xFF, 0xFF, 0xFF)); + let under = layout.elements.iter().take_while(|u| u.id != e.id).filter(|u| u.visible); + let fills = under.flat_map(|u| fills_at(u, centre)).chain(e.background.map(|b| b.fill)); + let shown = fills.fold(Color32::from_rgb(page.r(), page.g(), page.b()), |under, fill| { + let a = u16::from(fill.a()); + let over = |c: u8, under: u8| u8::try_from((u16::from(c) * a + u16::from(under) * (255 - a)) / 255).unwrap_or(u8::MAX); + Color32::from_rgb(over(fill.r(), under.r()), over(fill.g(), under.g()), over(fill.b(), under.b())) + }); let white = rustgis_engine::map::Rgba::rgb(0xFF, 0xFF, 0xFF); if crate::theme::contrast(shown, Color32::WHITE) > crate::theme::contrast(shown, crate::theme::c32(INK)) { white } else { INK } } +/// The fills `e` draws at a page point (mm), the lower first: its background's, rounded corners +/// counted as square, then a rectangle's or an ellipse's own. Maps, pictures, text and lines have +/// no one fill to read. +fn fills_at(e: &Element, (x, y): (f64, f64)) -> impl Iterator { + let (x, y) = e.unturned(x, y); + let inside = |(bx, by, bw, bh): (f64, f64, f64, f64)| x >= bx && x <= bx + bw && y >= by && y <= by + bh; + let background = e.background.filter(|_| inside(e.background_box())).map(|b| b.fill); + // The point from the box's centre, in half widths and half heights: within 1 of it, the ellipse. + let from_centre = (((x - e.x) / e.w) * 2.0 - 1.0).hypot(((y - e.y) / e.h) * 2.0 - 1.0); + let shape = match e.kind { + ElementKind::Rectangle { fill, .. } if inside((e.x, e.y, e.w, e.h)) => Some(fill), + ElementKind::Ellipse { fill, .. } if from_centre <= 1.0 => Some(fill), + _ => None, + }; + background.into_iter().chain(shape) +} + /// A row of a layout element's text size in points, within [`LAYOUT_TEXT_SIZE`], labelled `label` /// (#2581): the size it settles on when it differs from `current`. pub(super) fn text_size_row(ui: &mut egui::Ui, label: impl Into, key: egui::Id, current: f64) -> Option { diff --git a/crates/ui-egui/src/layoutview/tests.rs b/crates/ui-egui/src/layoutview/tests.rs index fe1c8fc825..e56de03380 100644 --- a/crates/ui-egui/src/layoutview/tests.rs +++ b/crates/ui-egui/src/layoutview/tests.rs @@ -4,6 +4,8 @@ use super::{ }; use crate::RustGisApp; use egui::{Event, Key, Modifiers, PointerButton, Pos2, Rect, vec2}; +use rustgis_engine::map::Rgba; +use rustgis_engine::map::layout::{Background, ElementKind, INK, Layout}; use serde_json::json; use std::{cell::Cell, rc::Rc}; @@ -24,26 +26,125 @@ fn frame_approximations_are_said_once_a_kind() { assert_eq!(n, 1); } +/// A table at (10, 10, 40, 40) mm, centred on (30, 30), on a page of `paper`: the element whose +/// ink starts, over whatever `under` adds before it and under whatever `over` adds after it. +fn inked(paper: Rgba, background: Option, under: impl FnOnce(&mut Layout), over: impl FnOnce(&mut Layout)) -> Rgba { + let mut layout = Layout::new("Ink", 297.0, 210.0); + layout.paper = Some(paper); + under(&mut layout); + let id = layout.add("Table", (10.0, 10.0, 40.0, 40.0), ElementKind::Table { frame: 1, layer: "Cities".into(), rows: 3, color: None }); + layout.element_mut(id).unwrap().background = background.map(Background::filled); + over(&mut layout); + first_ink(&layout, layout.element(id).unwrap()) +} + +const PAGE: Rgba = Rgba::rgb(0xFF, 0xFF, 0xFF); +const NAVY: Rgba = Rgba::rgb(0x0B, 0x1B, 0x2B); +const DARK: Rgba = Rgba::rgb(0x1E, 0x29, 0x3B); +const CLEAR: Rgba = Rgba::new(0, 0, 0, 0); + +fn rectangle(fill: Rgba) -> ElementKind { + ElementKind::Rectangle { fill, outline: INK, width: 0.0, radius: 0.0, dash: None } +} + +fn nothing(_: &mut Layout) {} + +/// A rectangle of `fill` covering the whole table. +fn covering(fill: Rgba) -> impl FnOnce(&mut Layout) { + move |l| { + l.add("Box", (0.0, 0.0, 100.0, 100.0), rectangle(fill)); + } +} + /// Ink starts white only over a dark background, so turning it on never hides the text (#2257). #[test] fn ink_starts_readable_on_its_background() { - use rustgis_engine::map::Rgba; - use rustgis_engine::map::layout::{Background, INK}; - let white = Rgba::rgb(0xFF, 0xFF, 0xFF); - let on = |fill: Rgba| first_ink(Some(&Background { fill, ..serde_json::from_value(json!({})).unwrap() }), white); - assert_eq!(first_ink(None, white), INK); - assert_eq!(on(white), INK); - assert_eq!(on(Rgba::rgb(0x1E, 0x29, 0x3B)), white); + let on = |fill| inked(PAGE, Some(fill), nothing, nothing); + assert_eq!(inked(PAGE, None, nothing, nothing), INK); + assert_eq!(on(PAGE), INK); + assert_eq!(on(DARK), PAGE); // A dark fill seen through mostly: the page shows, so it reads as light. assert_eq!(on(Rgba::new(0x1E, 0x29, 0x3B, 0x20)), INK); - assert_eq!(on(Rgba::new(0x1E, 0x29, 0x3B, 0xE0)), white); + assert_eq!(on(Rgba::new(0x1E, 0x29, 0x3B, 0xE0)), PAGE); // On a dark page (#2855): white with no background, or over a fill the page shows through; // dark grey over a light fill. - let navy = Rgba::rgb(0x0B, 0x1B, 0x2B); - let on_navy = |fill: Rgba| first_ink(Some(&Background { fill, ..serde_json::from_value(json!({})).unwrap() }), navy); - assert_eq!(first_ink(None, navy), white); - assert_eq!(on_navy(Rgba::new(0xFF, 0xFF, 0xFF, 0x20)), white); - assert_eq!(on_navy(white), INK); + assert_eq!(inked(NAVY, None, nothing, nothing), PAGE); + assert_eq!(inked(NAVY, Some(Rgba::new(0xFF, 0xFF, 0xFF, 0x20)), nothing, nothing), PAGE); + assert_eq!(inked(NAVY, Some(PAGE), nothing, nothing), INK); +} + +/// The fills shown under an element, in drawing order, decide its starting ink (#2953). +#[test] +fn ink_starts_readable_over_the_elements_under_it() { + assert_eq!(inked(NAVY, None, covering(PAGE), nothing), INK, "a white box on a dark page"); + assert_eq!(inked(PAGE, None, covering(DARK), nothing), PAGE, "a dark box on a white page"); + assert_eq!(inked(PAGE, Some(PAGE), covering(DARK), nothing), INK, "its own background is over the box"); + assert_eq!(inked(PAGE, None, covering(Rgba::new(0x1E, 0x29, 0x3B, 0x20)), nothing), INK, "the page shows through"); + let two = |l: &mut Layout| { + covering(DARK)(l); + covering(PAGE)(l); + }; + assert_eq!(inked(PAGE, None, two, nothing), INK, "the later box is over the earlier one"); + assert_eq!(inked(PAGE, None, nothing, covering(DARK)), INK, "a box drawn over it isn't under its text"); + let hidden = |l: &mut Layout| { + covering(DARK)(l); + l.elements[0].visible = false; + }; + assert_eq!(inked(PAGE, None, hidden, nothing), INK, "a hidden box shows nothing"); + let aside = |l: &mut Layout| { + l.add("Box", (0.0, 0.0, 25.0, 25.0), rectangle(DARK)); + }; + assert_eq!(inked(PAGE, None, aside, nothing), INK, "a box short of the centre"); +} + +/// An ellipse covers only its own shape, and a background reaches as far as its padding and +/// turns with its text (#2953). +#[test] +fn ink_reads_each_shape_as_drawn() { + let ellipse = |rect| { + move |l: &mut Layout| { + l.add("Ellipse", rect, ElementKind::Ellipse { fill: DARK, outline: INK, width: 0.0, dash: None }); + } + }; + assert_eq!(inked(PAGE, None, ellipse((0.0, 0.0, 60.0, 60.0)), nothing), PAGE, "the centre is inside"); + // The centre (30, 30) is in this ellipse's box, near its corner, outside the ellipse. + assert_eq!(inked(PAGE, None, ellipse((28.0, 28.0, 60.0, 60.0)), nothing), INK, "only in its box"); + let padded = |padding| { + move |l: &mut Layout| { + let id = l.add("Box", (0.0, 0.0, 25.0, 60.0), rectangle(CLEAR)); + l.element_mut(id).unwrap().background = Some(Background { padding, ..Background::filled(DARK) }); + } + }; + assert_eq!(inked(PAGE, None, padded(10.0), nothing), PAGE, "reached by the padding"); + assert_eq!(inked(PAGE, None, padded(0.0), nothing), INK); + // 10 by 60 mm centred on (50, 30): turned a quarter, 60 by 10, over (30, 30). + let turned = |turn: f64| { + move |l: &mut Layout| { + let mut kind = ElementKind::text("N", 8.0, INK, false); + if let ElementKind::Text { rotation, .. } = &mut kind { + *rotation = turn; + } + let id = l.add("Text", (45.0, 0.0, 10.0, 60.0), kind); + l.element_mut(id).unwrap().background = Some(Background::filled(DARK)); + } + }; + assert_eq!(inked(PAGE, None, turned(90.0), nothing), PAGE, "turned over the centre"); + assert_eq!(inked(PAGE, None, turned(0.0), nothing), INK); +} + +/// A map or a picture under an element has no one fill to read: the page decides, as before. +#[test] +fn ink_reads_no_map_or_picture() { + let picture = |l: &mut Layout| { + l.add("Picture", (0.0, 0.0, 100.0, 100.0), ElementKind::Picture { image: String::new() }); + }; + assert_eq!(inked(PAGE, None, picture, nothing), INK); + assert_eq!(inked(NAVY, None, picture, nothing), PAGE); + let map = |l: &mut Layout| { + let kind = serde_json::from_value(json!({"MapFrame": {"map": "Map", "center": {"x": 0.0, "y": 0.0}, "scale": 1000000.0}})); + l.add("Map", (0.0, 0.0, 100.0, 100.0), kind.unwrap()); + }; + assert_eq!(inked(NAVY, None, map, nothing), PAGE); } /// In the browser, a page drawn while its frames' tiles were on their way is drawn again as diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 9ad89c65f7..217dd06e16 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -669,9 +669,10 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu map frame draws its own map and paper, so a background on one is refused. - **Ink**: a legend, a colorbar, a table and a chart draw their text and lines in dark grey; turn on **Ink** in the Format pane and pick its colour to read them on a dark page or over imagery. - It starts white over a dark background and dark grey otherwise, and turned off it is the dark - grey again. A legend's title, layer names and - class labels, a colorbar's title, outline, ticks and labels, a table's text and a chart's title + It starts white when the colour shown at the element's centre is dark, and dark grey otherwise: + that colour is the page's, with the backgrounds, rectangles and ellipses under the element and its + own background over it (maps and pictures aren't read). Turned off it is the dark grey again. A + legend's title, layer names and class labels, a colorbar's title, outline, ticks and labels, a table's text and a chart's title and labels take it; a chart's bars and a colorbar's colours keep theirs. A table with an ink draws no white cells of its own, only its header and every other row in a light tint of the ink, so it reads on whatever is behind it: give it a **Background** for a box. From a command: From a51047267229666cb3ee627ed45a6d2e9a15c43d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Fri, 27 Nov 2026 13:39:09 -0500 Subject: [PATCH 2/2] fix(ui): say what Ink's start reads in its comment, and name the colour below (#2953) --- crates/ui-egui/src/layoutview/format.rs | 6 +++--- docs/user-guide/layouts.md | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/ui-egui/src/layoutview/format.rs b/crates/ui-egui/src/layoutview/format.rs index 8b86c3c556..10984d2164 100644 --- a/crates/ui-egui/src/layoutview/format.rs +++ b/crates/ui-egui/src/layoutview/format.rs @@ -244,7 +244,7 @@ pub(super) fn properties(app: &mut RustGisApp, ui: &mut Ui, layout: &Layout, li: // A legend's, colorbar's, table's or chart's ink (#2257), on and off as a background // is: off is the default dark grey, which isn't the same as a dark grey ink (a table // keeps its white cells only without one). Turned on, it starts white over a dark - // background and dark grey otherwise, so its text never disappears. + // colour and dark grey otherwise (`first_ink`), so its text never disappears. if e.kind.takes_ink() { let ink = e.kind.ink(); let input_label = ui.label(tl!("Ink")); @@ -388,10 +388,10 @@ pub(super) fn first_ink(layout: &Layout, e: &Element) -> rustgis_engine::map::Rg let page = layout.paper.unwrap_or(rustgis_engine::map::Rgba::rgb(0xFF, 0xFF, 0xFF)); let under = layout.elements.iter().take_while(|u| u.id != e.id).filter(|u| u.visible); let fills = under.flat_map(|u| fills_at(u, centre)).chain(e.background.map(|b| b.fill)); - let shown = fills.fold(Color32::from_rgb(page.r(), page.g(), page.b()), |under, fill| { + let shown = fills.fold(Color32::from_rgb(page.r(), page.g(), page.b()), |below, fill| { let a = u16::from(fill.a()); let over = |c: u8, under: u8| u8::try_from((u16::from(c) * a + u16::from(under) * (255 - a)) / 255).unwrap_or(u8::MAX); - Color32::from_rgb(over(fill.r(), under.r()), over(fill.g(), under.g()), over(fill.b(), under.b())) + Color32::from_rgb(over(fill.r(), below.r()), over(fill.g(), below.g()), over(fill.b(), below.b())) }); let white = rustgis_engine::map::Rgba::rgb(0xFF, 0xFF, 0xFF); if crate::theme::contrast(shown, Color32::WHITE) > crate::theme::contrast(shown, crate::theme::c32(INK)) { white } else { INK } diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index 217dd06e16..fbe3a3e225 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -667,17 +667,17 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu `layout.update={"element":4,"background":{"fill":"#FFFFFFCC","radius":1,"padding":1.5}}`; the keys not given keep theirs, and `"background":null` removes it. `layout.addElement` takes it too. A map frame draws its own map and paper, so a background on one is refused. -- **Ink**: a legend, a colorbar, a table and a chart draw their text and lines in dark grey; turn - on **Ink** in the Format pane and pick its colour to read them on a dark page or over imagery. - It starts white when the colour shown at the element's centre is dark, and dark grey otherwise: - that colour is the page's, with the backgrounds, rectangles and ellipses under the element and its - own background over it (maps and pictures aren't read). Turned off it is the dark grey again. A - legend's title, layer names and class labels, a colorbar's title, outline, ticks and labels, a table's text and a chart's title - and labels take it; a chart's bars and a colorbar's colours keep theirs. A table with an ink - draws no white cells of its own, only its header and every other row in a light tint of the ink, - so it reads on whatever is behind it: give it a **Background** for a box. From a command: - `layout.update={"element":5,"color":"#FFFFFF"}`, and `"color":null` for the default; - `layout.addElement` takes it too. +- **Ink**: a legend, a colorbar, a table and a chart draw their text and lines in dark grey; turn on + **Ink** in the Format pane and pick its colour to read them on a dark page or over imagery. It + starts white when the colour shown at the element's centre is dark, and dark grey otherwise: that + colour is the page's, with the backgrounds, rectangles and ellipses under the element and its own + background over it (maps and pictures aren't read). Turned off it is the dark grey again. A + legend's title, layer names and class labels, a colorbar's title, outline, ticks and labels, a + table's text and a chart's title and labels take it; a chart's bars and a colorbar's colours keep + theirs. A table with an ink draws no white cells of its own, only its header and every other row + in a light tint of the ink, so it reads on whatever is behind it: give it a **Background** for a + box. From a command: `layout.update={"element":5,"color":"#FFFFFF"}`, and `"color":null` for the + default; `layout.addElement` takes it too. - **Chart frame**: a bar chart of a layer of the frame's map. Its properties pick the **Layer** and the numeric **Field**, and by default draw a bar per feature, the largest first, each labelled by the **Label** field. **Group by** a field draws a bar per value of that field