Skip to main content

egui_charts/drawings/domain/
pitchfork.rs

1//! Saved level and background settings for the pitchfork tools.
2
3use crate::model::LineStyle;
4use serde::{Deserialize, Serialize};
5
6/// A multiple of the median-to-tine distance, mirrored on both sides of the median.
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8pub struct PitchforkLevel {
9    /// Distance multiple: 0 is the median and 1 is the pair of tines.
10    pub value: f32,
11    /// Whether the level and its adjacent bands are drawn.
12    pub visible: bool,
13    /// Line colour as RGBA.
14    pub color: [u8; 4],
15    /// Stroke style of this level.
16    pub line_style: LineStyle,
17    /// Stroke width in points. `None` inherits the drawing width, as older files do.
18    #[serde(default)]
19    pub width: Option<f32>,
20}
21
22/// The pitchfork's level lines and the fill between adjacent levels.
23#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
24pub struct PitchforkConfig {
25    /// Levels, ordered by distance when rendered; negative and non-finite values are ignored.
26    pub levels: Vec<PitchforkLevel>,
27    /// Whether bands between adjacent visible levels are filled.
28    pub show_background: bool,
29    /// Band opacity, from 0 to 255.
30    pub background_opacity: u8,
31}
32
33impl Default for PitchforkConfig {
34    /// The median in red and nine levels in their own colours, of which 0.5 and 1 are shown,
35    /// with bands at 20%.
36    fn default() -> Self {
37        use super::drawing::{BLUE, CYAN, PINK, PURPLE, RED, TEAL};
38        let level = |value, color, visible| PitchforkLevel {
39            value,
40            visible,
41            color,
42            line_style: LineStyle::Solid,
43            width: None,
44        };
45        Self {
46            levels: vec![
47                level(0.0, RED, true),
48                level(0.25, [255, 183, 77, 255], false),
49                level(0.382, [129, 199, 132, 255], false),
50                level(0.5, TEAL, true),
51                level(0.618, TEAL, false),
52                level(0.75, CYAN, false),
53                level(1.0, BLUE, true),
54                level(1.5, PURPLE, false),
55                level(1.75, PINK, false),
56                level(2.0, [247, 124, 128, 255], false),
57            ],
58            show_background: true,
59            background_opacity: 51,
60        }
61    }
62}
63
64impl PitchforkConfig {
65    /// The levels of pitchforks saved without a config: 0, 0.5, 1 and 1.5, all shown, with
66    /// faint bands. Renderers draw them in the drawing colour.
67    pub(crate) fn legacy() -> Self {
68        Self {
69            levels: [0.0, 0.5, 1.0, 1.5]
70                .map(|value| PitchforkLevel {
71                    value,
72                    visible: true,
73                    color: [33, 150, 243, 255],
74                    line_style: LineStyle::Solid,
75                    width: None,
76                })
77                .to_vec(),
78            show_background: true,
79            background_opacity: 20,
80        }
81    }
82}