Skip to main content

lichtung_plot/theme/
mark.rs

1//! Complete validated mark roles, independent of axes and scientific presence.
2
3use super::component::validated_component;
4use super::{
5    AccentPalette, Color, DataFillTheme, DataLineTheme, DataOutlineTheme, FixedFillTheme,
6    OpaqueColor, OutlineTheme, PointOutlineTheme, PointTheme, RelativeSpacing, StrokeTheme,
7};
8use crate::render::backend::{PointStyle, StrokePattern};
9use crate::{Error, PointShape, Result};
10
11fn positive_radius(radius: u32) -> Result<()> {
12    if radius == 0 {
13        Err(Error::Data(
14            "point radius must be positive CSS pixels".into(),
15        ))
16    } else {
17        Ok(())
18    }
19}
20
21fn fraction(value: &RelativeSpacing, limit: &RelativeSpacing, positive: bool) -> Result<()> {
22    if (positive && value.is_zero()) || value.compare(limit).is_gt() {
23        Err(Error::Data(
24            "mark relationship is outside its admitted fraction".into(),
25        ))
26    } else {
27        Ok(())
28    }
29}
30
31validated_component!(SizedPointTheme {
32    borrow appearance: PointTheme => get_appearance,
33    copy radius: u32 => get_radius,
34} validate |value| {
35    value.get_appearance().validate()?;
36    positive_radius(value.get_radius())?;
37});
38
39impl SizedPointTheme {
40    pub(crate) fn backend_style(&self, color: (u8, u8, u8), outline_visible: bool) -> PointStyle {
41        self.get_appearance()
42            .backend_style(color, self.get_radius(), outline_visible)
43    }
44}
45
46validated_component!(SupplementalPointTheme {
47    copy radius: u32 => get_radius,
48    copy fill_alpha: u8 => get_fill_alpha,
49} validate |value| { positive_radius(value.get_radius())?; });
50
51validated_component!(PointRadii {
52    copy scatter: u32 => get_scatter,
53    copy path: u32 => get_path,
54    copy trajectory: u32 => get_trajectory,
55    copy regression: u32 => get_regression,
56    copy qq: u32 => get_qq,
57    copy marginal: u32 => get_marginal,
58    copy matrix: u32 => get_matrix,
59    copy interval_estimate: u32 => get_interval_estimate,
60    copy dumbbell: u32 => get_dumbbell,
61    copy interaction: u32 => get_interaction,
62    copy beeswarm: u32 => get_beeswarm,
63    copy box_flier: u32 => get_box_flier,
64} validate |value| {
65    for radius in [value.get_scatter(), value.get_path(), value.get_trajectory(), value.get_regression(),
66        value.get_qq(), value.get_marginal(), value.get_matrix(), value.get_interval_estimate(),
67        value.get_dumbbell(), value.get_interaction(), value.get_beeswarm(), value.get_box_flier()] {
68        positive_radius(radius)?;
69    }
70});
71
72validated_component!(PointRoles {
73    borrow primary: PointTheme => get_primary,
74    borrow raw_overlay: SizedPointTheme => get_raw_overlay,
75    borrow mandatory_strip: SizedPointTheme => get_mandatory_strip,
76    borrow radii: PointRadii => get_radii,
77    borrow supplemental: SupplementalPointTheme => get_supplemental,
78} validate |value| {
79    value.get_primary().validate()?;
80    value.get_raw_overlay().validate()?;
81    value.get_mandatory_strip().validate()?;
82    value.get_radii().validate()?;
83    value.get_supplemental().validate()?;
84});
85
86validated_component!(BodyFillRoles {
87    borrow bar_body: DataFillTheme => get_bar_body,
88    borrow box_body: DataFillTheme => get_box_body,
89    borrow violin_body: DataFillTheme => get_violin_body,
90    borrow inner_summary: FixedFillTheme => get_inner_summary,
91    borrow histogram_bin: DataFillTheme => get_histogram_bin,
92    borrow waterfall_bar: DataFillTheme => get_waterfall_bar,
93} validate |value| {
94    for fill in [value.get_bar_body(), value.get_box_body(), value.get_violin_body(),
95        value.get_histogram_bin(), value.get_waterfall_bar()] { fill.validate()?; }
96    value.get_inner_summary().validate()?;
97});
98
99validated_component!(BandFillRoles {
100    borrow line_band: DataFillTheme => get_line_band,
101    borrow regression_band: DataFillTheme => get_regression_band,
102} validate |value| {
103    value.get_line_band().validate()?;
104    value.get_regression_band().validate()?;
105});
106
107validated_component!(DensityFillRoles {
108    borrow density_area: DataFillTheme => get_density_area,
109    borrow marginal_density: DataFillTheme => get_marginal_density,
110    borrow matrix_density: DataFillTheme => get_matrix_density,
111} validate |value| {
112    for fill in [value.get_density_area(), value.get_marginal_density(), value.get_matrix_density()] {
113        fill.validate()?;
114    }
115});
116
117validated_component!(FillRoles {
118    borrow bodies: BodyFillRoles => get_bodies,
119    borrow bands: BandFillRoles => get_bands,
120    borrow densities: DensityFillRoles => get_densities,
121} validate |value| {
122    value.get_bodies().validate()?;
123    value.get_bands().validate()?;
124    value.get_densities().validate()?;
125});
126
127validated_component!(DataLineRoles {
128    borrow series_line: DataLineTheme => get_series_line,
129    borrow path_line: DataLineTheme => get_path_line,
130    borrow density_line: DataLineTheme => get_density_line,
131    borrow interaction_interval: DataLineTheme => get_interaction_interval,
132    borrow interaction_connector: DataLineTheme => get_interaction_connector,
133} validate |value| {
134    for line in [value.get_series_line(), value.get_path_line(), value.get_density_line(),
135        value.get_interaction_interval(), value.get_interaction_connector()] { line.validate()?; }
136});
137
138validated_component!(NeutralPaintRoles {
139    copy path: OpaqueColor => get_path,
140    copy box_flier: OpaqueColor => get_box_flier,
141} validate |_value| {});
142
143validated_component!(ReferenceStrokeRoles {
144    borrow bar_zero: StrokeTheme => get_bar_zero,
145    borrow waterfall_reference: StrokeTheme => get_waterfall_reference,
146    borrow beeswarm_reference: StrokeTheme => get_beeswarm_reference,
147    borrow point_interval_reference: StrokeTheme => get_point_interval_reference,
148    borrow estimate_reference: StrokeTheme => get_estimate_reference,
149    borrow qq_reference: StrokeTheme => get_qq_reference,
150} validate |value| {
151    for stroke in [value.get_bar_zero(), value.get_waterfall_reference(), value.get_beeswarm_reference(),
152        value.get_point_interval_reference(), value.get_estimate_reference(), value.get_qq_reference()] {
153        stroke.validate()?;
154    }
155});
156
157validated_component!(ConnectorStrokeRoles {
158    borrow waterfall_connector: StrokeTheme => get_waterfall_connector,
159    borrow dumbbell_connector: StrokeTheme => get_dumbbell_connector,
160} validate |value| {
161    value.get_waterfall_connector().validate()?;
162    value.get_dumbbell_connector().validate()?;
163});
164
165validated_component!(SummaryStrokeRoles {
166    borrow uncertainty: StrokeTheme => get_uncertainty,
167    borrow summary: StrokeTheme => get_summary,
168    borrow estimate_interval: StrokeTheme => get_estimate_interval,
169    borrow median: StrokeTheme => get_median,
170} validate |value| {
171    for stroke in [value.get_uncertainty(), value.get_summary(), value.get_estimate_interval(), value.get_median()] {
172        stroke.validate()?;
173    }
174});
175
176validated_component!(CurveStrokeRoles {
177    borrow histogram_curve: StrokeTheme => get_histogram_curve,
178    borrow histogram_rug: StrokeTheme => get_histogram_rug,
179    borrow regression_fit: StrokeTheme => get_regression_fit,
180} validate |value| {
181    for stroke in [value.get_histogram_curve(), value.get_histogram_rug(), value.get_regression_fit()] {
182        stroke.validate()?;
183    }
184});
185
186validated_component!(StrokeRoles {
187    borrow references: ReferenceStrokeRoles => get_references,
188    borrow connectors: ConnectorStrokeRoles => get_connectors,
189    borrow summaries: SummaryStrokeRoles => get_summaries,
190    borrow curves: CurveStrokeRoles => get_curves,
191} validate |value| {
192    value.get_references().validate()?;
193    value.get_connectors().validate()?;
194    value.get_summaries().validate()?;
195    value.get_curves().validate()?;
196});
197
198validated_component!(CellMarkRoles {
199    copy fill_alpha: u8 => get_fill_alpha,
200    optional cell_outline: OutlineTheme => get_cell_outline,
201    copy missing_fill: Color => get_missing_fill,
202    optional missing_row_outline: OutlineTheme => get_missing_row_outline,
203    optional missing_key_outline: OutlineTheme => get_missing_key_outline,
204    borrow missing_cross: StrokeTheme => get_missing_cross,
205    borrow missing_cross_inset: RelativeSpacing => get_missing_cross_inset,
206} validate |value| {
207    for outline in [value.get_cell_outline(), value.get_missing_row_outline(), value.get_missing_key_outline()]
208        .into_iter().flatten() { outline.validate()?; }
209    value.get_missing_cross().validate()?;
210    if value.get_missing_cross_inset().compare(&RelativeSpacing::ratio(1, 2)?).is_ge() {
211        return Err(Error::Data("missing-cell cross inset must be smaller than one half".into()));
212    }
213});
214
215validated_component!(CategoryMarkGeometry {
216    borrow vertical_body_breadth: RelativeSpacing => get_vertical_body_breadth,
217    borrow horizontal_body_breadth: RelativeSpacing => get_horizontal_body_breadth,
218    borrow vertical_group_span: RelativeSpacing => get_vertical_group_span,
219    borrow horizontal_group_span: RelativeSpacing => get_horizontal_group_span,
220    borrow uncertainty_cap_ratio: RelativeSpacing => get_uncertainty_cap_ratio,
221} validate |value| {
222    let one = RelativeSpacing::ratio(1, 1)?;
223    for extent in [value.get_vertical_body_breadth(), value.get_horizontal_body_breadth(),
224        value.get_vertical_group_span(), value.get_horizontal_group_span()] {
225        fraction(extent, &one, true)?;
226    }
227    fraction(value.get_uncertainty_cap_ratio(), &RelativeSpacing::ratio(1, 2)?, true)?;
228});
229
230validated_component!(MarkExtentTheme {
231    borrow categories: CategoryMarkGeometry => get_categories,
232    borrow waterfall_body_category_breadth: RelativeSpacing => get_waterfall_body_category_breadth,
233    copy point_interval_cap_half_height: u32 => get_point_interval_cap_half_height,
234    borrow sampled_rug_height: RelativeSpacing => get_sampled_rug_height,
235    borrow marginal_density_fraction: RelativeSpacing => get_marginal_density_fraction,
236    borrow matrix_density_fraction: RelativeSpacing => get_matrix_density_fraction,
237} validate |value| {
238    value.get_categories().validate()?;
239    positive_radius(value.get_point_interval_cap_half_height())?;
240    let one = RelativeSpacing::ratio(1, 1)?;
241    for extent in [value.get_waterfall_body_category_breadth(), value.get_marginal_density_fraction(),
242        value.get_matrix_density_fraction()] { fraction(extent, &one, true)?; }
243});
244
245impl CategoryMarkGeometry {
246    pub(crate) fn group_span(&self, horizontal: bool) -> f64 {
247        if horizontal {
248            self.get_horizontal_group_span()
249        } else {
250            self.get_vertical_group_span()
251        }
252        .as_f64()
253    }
254
255    pub(crate) fn body_breadth(&self, horizontal: bool) -> f64 {
256        if horizontal {
257            self.get_horizontal_body_breadth()
258        } else {
259            self.get_vertical_body_breadth()
260        }
261        .as_f64()
262    }
263}
264
265validated_component!(MarkTheme {
266    borrow accents: AccentPalette => get_accents,
267    borrow points: PointRoles => get_points,
268    borrow fills: FillRoles => get_fills,
269    borrow data_lines: DataLineRoles => get_data_lines,
270    borrow neutral_paints: NeutralPaintRoles => get_neutral_paints,
271    borrow strokes: StrokeRoles => get_strokes,
272    borrow cells: CellMarkRoles => get_cells,
273    borrow extents: MarkExtentTheme => get_extents,
274} validate |value| {
275    value.get_points().validate()?;
276    value.get_fills().validate()?;
277    value.get_data_lines().validate()?;
278    value.get_neutral_paints().validate()?;
279    value.get_strokes().validate()?;
280    value.get_cells().validate()?;
281    value.get_extents().validate()?;
282});
283
284impl MarkTheme {
285    #[expect(
286        clippy::too_many_lines,
287        reason = "one construction table retains the approved complete built-in mark defaults"
288    )]
289    pub(crate) fn builtin(
290        accents: AccentPalette,
291        foreground: OpaqueColor,
292        axis_width: f32,
293    ) -> Result<Self> {
294        let paint = Color::from(foreground);
295        let stroke = |width, pattern| StrokeTheme::new(paint, width, pattern);
296        let filled = |alpha, width| {
297            DataFillTheme::new(
298                alpha,
299                Some(DataOutlineTheme::fixed(OutlineTheme::new(paint, width)?)),
300            )
301        };
302        let point_appearance = || {
303            PointTheme::new(
304                209,
305                PointShape::Circle,
306                Some(PointOutlineTheme::publication()?),
307            )
308        };
309        let relative = RelativeSpacing::new;
310        Self::new(
311            accents,
312            PointRoles::new(
313                point_appearance()?,
314                SizedPointTheme::new(point_appearance()?, 2)?,
315                SizedPointTheme::new(point_appearance()?, 2)?,
316                PointRadii::new(3, 3, 4, 3, 3, 3, 2, 4, 4, 4, 3, 2)?,
317                SupplementalPointTheme::new(4, 204)?,
318            )?,
319            FillRoles::new(
320                BodyFillRoles::new(
321                    filled(255, 1.0)?,
322                    filled(255, 1.0)?,
323                    filled(178, 1.0)?,
324                    FixedFillTheme::new(
325                        Color::rgb(255, 255, 255),
326                        Some(OutlineTheme::new(paint, 1.0)?),
327                    )?,
328                    filled(209, 0.8)?,
329                    filled(255, 0.8)?,
330                )?,
331                BandFillRoles::new(DataFillTheme::new(46, None)?, DataFillTheme::new(64, None)?)?,
332                DensityFillRoles::new(
333                    DataFillTheme::new(56, None)?,
334                    DataFillTheme::new(115, Some(DataOutlineTheme::mapped(1.0, 255)?))?,
335                    DataFillTheme::new(102, Some(DataOutlineTheme::mapped(1.0, 255)?))?,
336                )?,
337            )?,
338            DataLineRoles::new(
339                DataLineTheme::new(1.8, 255, StrokePattern::Solid)?,
340                DataLineTheme::new(1.4, 255, StrokePattern::Solid)?,
341                DataLineTheme::new(1.6, 255, StrokePattern::Solid)?,
342                DataLineTheme::new(1.2, 255, StrokePattern::Solid)?,
343                DataLineTheme::new(1.8, 255, StrokePattern::Solid)?,
344            )?,
345            NeutralPaintRoles::new(foreground, foreground)?,
346            StrokeRoles::new(
347                ReferenceStrokeRoles::new(
348                    stroke(0.8, StrokePattern::Solid)?,
349                    stroke(0.7, StrokePattern::MediumDash)?,
350                    stroke(0.8, StrokePattern::Solid)?,
351                    stroke(0.8, StrokePattern::Solid)?,
352                    stroke(1.0, StrokePattern::MediumDash)?,
353                    stroke(1.2, StrokePattern::Solid)?,
354                )?,
355                ConnectorStrokeRoles::new(
356                    stroke(0.7, StrokePattern::ShortDash)?,
357                    stroke(1.2, StrokePattern::Solid)?,
358                )?,
359                SummaryStrokeRoles::new(
360                    stroke(axis_width, StrokePattern::Solid)?,
361                    stroke(axis_width, StrokePattern::Solid)?,
362                    stroke(1.4, StrokePattern::Solid)?,
363                    stroke(1.5, StrokePattern::Solid)?,
364                )?,
365                CurveStrokeRoles::new(
366                    stroke(1.0, StrokePattern::Solid)?,
367                    stroke(0.7, StrokePattern::Solid)?,
368                    stroke(1.5, StrokePattern::Solid)?,
369                )?,
370            )?,
371            CellMarkRoles::new(
372                255,
373                Some(OutlineTheme::new(Color::rgb(255, 255, 255), 0.5)?),
374                Color::rgb(224, 224, 224),
375                Some(OutlineTheme::new(Color::rgb(255, 255, 255), 0.5)?),
376                Some(OutlineTheme::new(Color::rgb(120, 120, 120), 0.8)?),
377                StrokeTheme::new(Color::rgb(120, 120, 120), 1.0, StrokePattern::Solid)?,
378                relative(0.16)?,
379            )?,
380            MarkExtentTheme::new(
381                CategoryMarkGeometry::new(
382                    relative(0.6)?,
383                    relative(0.55)?,
384                    relative(0.76)?,
385                    relative(0.74)?,
386                    relative(0.2)?,
387                )?,
388                relative(0.6)?,
389                4,
390                relative(0.035)?,
391                relative(0.90)?,
392                relative(0.85)?,
393            )?,
394        )
395    }
396}