plotive 0.2.0

Simple data plotting library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! Style definitions for lines, fills, markers, and themes.
mod catppuccin;
pub(crate) mod defaults;
pub mod series;
pub mod theme;

use crate::style::series::Palette;
use crate::style::theme::Theme;
use crate::{Color, ColorU8, ResolveColor, render};

/// Overall style definition for figures
///
/// The style gathers together two main components:
/// - The theme, which defines colors for the figure background, foreground, grid lines, and legend.
/// - The palette, which defines colors for data series.
#[derive(Debug, Clone, PartialEq)]
pub struct Style {
    /// Theme used for the figure
    theme: Theme,
    /// Palette used for series colors
    palette: Palette,
}

impl Default for Style {
    fn default() -> Self {
        Style::light()
    }
}

impl Style {
    /// Create a new style with the given theme and palette
    pub const fn new(theme: Theme, palette: Palette) -> Self {
        Style { theme, palette }
    }

    /// Create a black and white monochrome style
    /// If you use this with multiple series, consider styling the series lines with different patterns to distinguish them
    pub const fn black_white() -> Self {
        Style {
            theme: Theme::Light,
            palette: Palette::Black,
        }
    }

    /// Create the default light style, using a light theme and standard palette
    pub const fn light() -> Self {
        Style {
            theme: Theme::Light,
            palette: Palette::Standard,
        }
    }

    /// Create the default dark style, using a dark theme and pastel palette
    pub const fn dark() -> Self {
        Style {
            theme: Theme::Dark,
            palette: Palette::Pastel,
        }
    }

    /// Create a light theme with Okabe & Ito colorblind-safe palette
    pub const fn okabe_ito() -> Self {
        Style {
            theme: Theme::Light,
            palette: Palette::OkabeIto,
        }
    }

    /// Create a light theme with Paul Tol's bright colorblind-safe palette
    pub const fn tol_bright() -> Self {
        Style {
            theme: Theme::Light,
            palette: Palette::TolBright,
        }
    }

    /// Create a Catppuccin Mocha theme and palette
    pub const fn catppuccin_mocha() -> Self {
        Style {
            theme: Theme::CatppuccinMocha,
            palette: Palette::CatppuccinMocha,
        }
    }

    /// Create a Catppuccin Macchiato theme and palette
    pub const fn catppuccin_macchiato() -> Self {
        Style {
            theme: Theme::CatppuccinMacchiato,
            palette: Palette::CatppuccinMacchiato,
        }
    }

    /// Create a Catppuccin Frappe theme and palette
    pub const fn catppuccin_frappe() -> Self {
        Style {
            theme: Theme::CatppuccinFrappe,
            palette: Palette::CatppuccinFrappe,
        }
    }

    /// Create a Catppuccin Latte theme and palette
    pub const fn catppuccin_latte() -> Self {
        Style {
            theme: Theme::CatppuccinLatte,
            palette: Palette::CatppuccinLatte,
        }
    }

    /// Theme used for the figure
    pub const fn theme(&self) -> &Theme {
        &self.theme
    }

    /// Palette used for series colors
    pub const fn palette(&self) -> &Palette {
        &self.palette
    }
}

impl ResolveColor<theme::Color> for Style {
    fn resolve_color(&self, col: &theme::Color) -> ColorU8 {
        self.theme().resolve_color(col)
    }
}

impl ResolveColor<series::IndexColor> for Style {
    fn resolve_color(&self, col: &series::IndexColor) -> ColorU8 {
        self.palette.get(*col)
    }
}

impl ResolveColor<series::AutoColor> for (&Style, usize) {
    fn resolve_color(&self, _col: &series::AutoColor) -> ColorU8 {
        self.0.palette.get(series::IndexColor(self.1))
    }
}

impl ResolveColor<series::Color> for (&Style, usize) {
    fn resolve_color(&self, col: &series::Color) -> ColorU8 {
        match col {
            series::Color::Auto => self.0.palette.get(series::IndexColor(self.1)),
            series::Color::Index(idx) => self.0.palette.get(*idx),
            series::Color::Fixed(c) => *c,
        }
    }
}

/// Dash pattern for dashed lines
/// A dash pattern is a sequence of lengths that specify the lengths of
/// alternating dashes and gaps.
///
/// The lengths are relative to the line width.
/// So a pattern will scale with the line width and remain visually consistent.
#[derive(Debug, Clone, PartialEq)]
pub struct Dash(pub Vec<f32>);

impl Default for Dash {
    fn default() -> Self {
        Dash(vec![5.0, 5.0])
    }
}

/// Line pattern defines how the line is drawn
#[derive(Debug, Clone, PartialEq)]
pub enum LinePattern {
    /// Solid line
    Solid,
    /// Dashed line. The pattern is relative to the line width.
    Dash(Dash),
    /// Dotted line. Equivalent to Dash(1.0, 1.0)
    Dot,
}

impl Default for LinePattern {
    fn default() -> Self {
        LinePattern::Solid
    }
}

impl From<Dash> for LinePattern {
    fn from(dash: Dash) -> Self {
        LinePattern::Dash(dash)
    }
}

/// Stroke style definition. Defines how lines are stroked.
///
/// The color is a generic parameter to support different color resolution strategies,
/// such as fixed colors, theme-based colors, or series-based colors.
#[derive(Debug, Clone, PartialEq)]
pub struct Stroke<C: Color> {
    /// Line color
    pub color: C,
    /// Line width in figure units
    pub width: f32,
    /// Line pattern
    pub pattern: LinePattern,
    /// Line opacity (0.0 to 1.0)
    pub opacity: Option<f32>,
}

const DOT_DASH: &[f32] = &[1.0, 1.0];

impl<C: Color> Stroke<C> {
    /// Set the line width in figure units, returning self for chaining
    pub fn with_width(self, width: f32) -> Self {
        Stroke { width, ..self }
    }

    /// Set the line opacity (0.0 to 1.0), returning self for chaining
    pub fn with_opacity(self, opacity: f32) -> Self {
        Stroke {
            opacity: Some(opacity),
            ..self
        }
    }

    /// Set the line pattern, returning self for chaining
    pub fn with_pattern(self, pattern: LinePattern) -> Self {
        Stroke { pattern, ..self }
    }

    /// Convert to a renderable stroke, resolving colors using the provided resolver
    pub fn as_stroke<'a, R>(&'a self, rc: &R) -> render::Stroke<'a>
    where
        R: ResolveColor<C>,
    {
        let color = if let Some(opacity) = self.opacity {
            self.color.resolve(rc).with_opacity(opacity)
        } else {
            self.color.resolve(rc)
        };

        let pattern = match &self.pattern {
            LinePattern::Solid => render::LinePattern::Solid,
            LinePattern::Dash(Dash(a)) => render::LinePattern::Dash(a.as_slice()),
            LinePattern::Dot => render::LinePattern::Dash(DOT_DASH),
        };

        render::Stroke {
            color,
            width: self.width,
            pattern,
        }
    }
}

impl<C: Color> From<C> for Stroke<C> {
    fn from(color: C) -> Self {
        Stroke {
            width: 1.0,
            color,
            pattern: LinePattern::default(),
            opacity: None,
        }
    }
}

impl<C: Color> From<(C, f32)> for Stroke<C> {
    fn from((color, width): (C, f32)) -> Self {
        Stroke {
            color,
            width,
            pattern: LinePattern::default(),
            opacity: None,
        }
    }
}

impl<C: Color> From<(C, f32, LinePattern)> for Stroke<C> {
    fn from((color, width, pattern): (C, f32, LinePattern)) -> Self {
        Stroke {
            color,
            width,
            pattern,
            opacity: None,
        }
    }
}

impl<C: Color> From<(C, f32, Dash)> for Stroke<C> {
    fn from((color, width, dash): (C, f32, Dash)) -> Self {
        Stroke {
            color,
            width,
            pattern: LinePattern::Dash(dash),
            opacity: None,
        }
    }
}

/// Fill style definition
/// The color is a generic parameter to support different color resolution strategies,
/// such as fixed colors, theme based colors, or series-based colors.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Fill<C: Color> {
    /// Solid fill
    Solid {
        /// Fill color
        color: C,
        /// Fill opacity (0.0 to 1.0)
        opacity: Option<f32>,
    },
}

impl<C> Default for Fill<C>
where
    C: Color + Default,
{
    fn default() -> Self {
        Fill::Solid {
            color: C::default(),
            opacity: None,
        }
    }
}

impl<C: Color> Fill<C> {
    /// Set the fill opacity (0.0 to 1.0), returning self for chaining
    pub fn with_opacity(self, opacity: f32) -> Self {
        match self {
            Fill::Solid { color, .. } => Fill::Solid {
                color,
                opacity: Some(opacity),
            },
        }
    }

    /// Convert to a renderable paint, resolving colors using the provided resolver
    pub fn as_paint<R>(&self, rc: &R) -> render::Paint
    where
        R: ResolveColor<C>,
    {
        match self {
            Fill::Solid {
                color,
                opacity: None,
            } => render::Paint::Solid(color.resolve(rc)),
            Fill::Solid {
                color,
                opacity: Some(opacity),
            } => render::Paint::Solid(color.resolve(rc).with_opacity(*opacity)),
        }
    }
}

impl<C: Color> From<C> for Fill<C> {
    fn from(color: C) -> Self {
        Fill::Solid {
            color,
            opacity: None,
        }
    }
}

/// Shape of a marker, used in scatter plots
#[derive(Debug, Clone, Copy, Default)]
pub enum MarkerShape {
    /// Circle marker (the default)
    #[default]
    Circle,
    /// Square marker
    Square,
    ///  Diamond marker
    Diamond,
    ///  Cross marker
    Cross,
    ///  Plus marker
    Plus,
    ///  Upward pointing triangle marker
    TriangleUp,
    ///  Downward pointing triangle marker
    TriangleDown,
}

/// Size of a marker, used in scatter plots
#[derive(Debug, Clone, Copy)]
pub struct MarkerSize(pub f32);

impl Default for MarkerSize {
    fn default() -> Self {
        MarkerSize(defaults::MARKER_SIZE)
    }
}

impl From<f32> for MarkerSize {
    fn from(size: f32) -> Self {
        MarkerSize(size)
    }
}

/// Marker style definition, used in scatter plots
#[derive(Debug, Clone)]
pub struct Marker<C: Color> {
    /// Marker size
    pub size: MarkerSize,
    /// Marker shape
    pub shape: MarkerShape,
    /// Marker fill style
    pub fill: Option<Fill<C>>,
    /// Marker stroke style
    pub stroke: Option<Stroke<C>>,
}

impl<C> Default for Marker<C>
where
    C: Color + Default,
{
    fn default() -> Self {
        Marker {
            size: MarkerSize::default(),
            shape: MarkerShape::default(),
            fill: Some(Fill::default()),
            stroke: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ColorU8;
    use crate::style::theme;

    #[test]
    fn test_color_resolve() {
        let style = Style::light();

        let theme_line: theme::Stroke = (theme::Color::Theme(theme::Col::LegendBorder), 2.0).into();
        let stroke = theme_line.as_stroke(&style);
        assert_eq!(stroke.color, ColorU8::from_html(b"#000000"));

        let series_line: Stroke<series::IndexColor> = (series::IndexColor(2), 2.0).into();
        let stroke = series_line.as_stroke(&style);
        assert_eq!(stroke.color, ColorU8::from_html(b"#2ca02c"));

        let series_line: Stroke<series::AutoColor> = (series::AutoColor, 2.0).into();
        let stroke = series_line.as_stroke(&(&style, 2));
        assert_eq!(stroke.color, ColorU8::from_html(b"#2ca02c"));

        let fixed_color: Stroke<ColorU8> = (ColorU8::from_html(b"#123456"), 2.0).into();
        let stroke = fixed_color.as_stroke(&());
        assert_eq!(stroke.color, ColorU8::from_html(b"#123456"));
    }
}