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