lichtung-plot 0.1.0

Strongly typed scientific plotting with deterministic SVG and PNG output
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! Validated compound paints, independent of backend geometry and operation state.

use crate::render::backend::{FillStyle, LineStyle, PointOutline, PointStyle, StrokePattern};
use crate::{Error, PointShape, Result};

use super::{color_from_rgb, Color, RelativeSpacing};

fn validate_width(width: f32) -> Result<()> {
    if width.is_finite() && width > 0.0 {
        Ok(())
    } else {
        Err(Error::Data(
            "present stroke width must be finite and positive CSS pixels".into(),
        ))
    }
}

// Solid outlines and solid bracket strokes share scalar validation, not roles.
macro_rules! solid_paint {
    ($name:ident, $documentation:literal) => {
        #[doc = $documentation]
        #[derive(Debug, Clone, PartialEq)]
        pub struct $name {
            pub(crate) paint: Color,
            pub(crate) width: f32,
        }

        impl $name {
            /// Construct a complete paint and positive finite CSS stroke width.
            pub fn new(color: Color, width: f32) -> Result<Self> {
                let value = Self {
                    paint: color,
                    width,
                };
                value.validate()?;
                Ok(value)
            }

            pub(crate) fn validate(&self) -> Result<()> {
                validate_width(self.get_width())
            }

            /// Inspect the complete stroke paint.
            pub fn get_color(&self) -> Color {
                self.paint
            }
            /// Inspect the positive width in CSS pixels.
            pub fn get_width(&self) -> f32 {
                self.width
            }

            /// Replace only the complete stroke paint.
            pub fn color(self, color: Color) -> Result<Self> {
                Self::new(color, self.width)
            }

            /// Replace only the positive CSS width.
            pub fn width(self, width: f32) -> Result<Self> {
                Self::new(self.paint, width)
            }
        }
    };
}

solid_paint!(
    SolidStrokeTheme,
    "Complete solid bracket stroke; its topology has no pattern edit."
);
solid_paint!(
    OutlineTheme,
    "Complete independent outline paint and positive CSS width."
);

/// Complete stroke with the renderer's one closed dash vocabulary.
#[derive(Debug, Clone, PartialEq)]
pub struct StrokeTheme {
    pub(crate) paint: Color,
    pub(crate) width: f32,
    pub(crate) pattern: StrokePattern,
}

impl StrokeTheme {
    pub(crate) fn validate(&self) -> Result<()> {
        validate_width(self.get_width())
    }
    /// Construct a complete stroke with positive finite CSS width and a closed pattern.
    pub fn new(color: Color, width: f32, pattern: StrokePattern) -> Result<Self> {
        validate_width(width)?;
        Ok(Self {
            paint: color,
            width,
            pattern,
        })
    }

    /// Inspect the complete stroke paint.
    pub fn get_color(&self) -> Color {
        self.paint
    }
    /// Inspect the positive CSS width.
    pub fn get_width(&self) -> f32 {
        self.width
    }
    /// Inspect the closed stroke pattern.
    pub fn get_pattern(&self) -> StrokePattern {
        self.pattern
    }

    /// Replace only the complete stroke paint.
    pub fn color(self, color: Color) -> Result<Self> {
        Self::new(color, self.width, self.pattern)
    }
    /// Replace only the positive CSS width.
    pub fn width(self, width: f32) -> Result<Self> {
        Self::new(self.paint, width, self.pattern)
    }
    /// Replace only the closed stroke pattern.
    pub fn pattern(self, pattern: StrokePattern) -> Result<Self> {
        Self::new(self.paint, self.width, pattern)
    }

    pub(crate) fn backend_style(&self) -> LineStyle {
        LineStyle {
            color: self.get_color(),
            width: self.get_width(),
            pattern: self.get_pattern(),
        }
    }
}

/// A point outline owns one exact relationship to the selected point radius.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PointOutlineTheme {
    pub(crate) color: Color,
    pub(crate) width_ratio: RelativeSpacing,
}

impl PointOutlineTheme {
    /// Construct independent outline paint and a positive radius-relative width.
    pub fn new(color: Color, width_ratio: RelativeSpacing) -> Result<Self> {
        if width_ratio.is_zero() {
            return Err(Error::Data(
                "point outline width ratio must be positive".into(),
            ));
        }
        Ok(Self { color, width_ratio })
    }

    fn validate(&self) -> Result<()> {
        if self.width_ratio.is_zero() {
            Err(Error::Data(
                "point outline width ratio must be positive".into(),
            ))
        } else {
            Ok(())
        }
    }

    /// Inspect the independent outline paint.
    pub fn get_color(&self) -> Color {
        self.color
    }
    /// Borrow the exact positive width-to-radius ratio.
    pub fn get_width_ratio(&self) -> &RelativeSpacing {
        &self.width_ratio
    }
    /// Replace only the independent outline paint.
    pub fn color(self, color: Color) -> Result<Self> {
        Self::new(color, self.width_ratio)
    }
    /// Replace only the positive radius-relative width.
    pub fn width_ratio(self, width_ratio: RelativeSpacing) -> Result<Self> {
        Self::new(self.color, width_ratio)
    }

    pub(crate) fn publication() -> Result<Self> {
        Self::new(Color::rgb(255, 255, 255), RelativeSpacing::new(0.1)?)
    }

    /// Materialize the operational outline by sharing the same immutable ratio.
    pub(crate) fn backend_style(&self) -> PointOutline {
        PointOutline {
            color: self.get_color(),
            width_ratio: self.get_width_ratio().clone(),
        }
    }
}

/// Fixed paint or mapped RGB with an independent outline alpha.
#[derive(Debug, Clone, PartialEq)]
pub struct DataOutlineTheme {
    choice: DataOutline,
}

#[derive(Debug, Clone, PartialEq)]
enum DataOutline {
    Fixed(OutlineTheme),
    Mapped { width: f32, alpha: u8 },
}

impl DataOutlineTheme {
    /// Select a complete independent fixed outline.
    pub fn fixed(stroke: OutlineTheme) -> Self {
        Self {
            choice: DataOutline::Fixed(stroke),
        }
    }

    /// Select mapped RGB with positive CSS width and an independent byte alpha.
    pub fn mapped(width: f32, alpha: u8) -> Result<Self> {
        validate_width(width)?;
        Ok(Self {
            choice: DataOutline::Mapped { width, alpha },
        })
    }

    /// Borrow the fixed outline, or None for mapped RGB.
    pub fn fixed_stroke(&self) -> Option<&OutlineTheme> {
        match &self.choice {
            DataOutline::Fixed(stroke) => Some(stroke),
            DataOutline::Mapped { .. } => None,
        }
    }
    /// Inspect the mapped width, or None for a fixed outline.
    pub fn mapped_width(&self) -> Option<f32> {
        match self.choice {
            DataOutline::Fixed(_) => None,
            DataOutline::Mapped { width, .. } => Some(width),
        }
    }
    /// Inspect mapped alpha, or None for a fixed outline.
    pub fn mapped_alpha(&self) -> Option<u8> {
        match self.choice {
            DataOutline::Fixed(_) => None,
            DataOutline::Mapped { alpha, .. } => Some(alpha),
        }
    }

    fn validate(&self) -> Result<()> {
        match &self.choice {
            DataOutline::Fixed(stroke) => stroke.validate(),
            DataOutline::Mapped { width, .. } => validate_width(*width),
        }
    }

    fn operands(&self, mapped: Color) -> (Color, f32) {
        if let Some(stroke) = self.fixed_stroke() {
            (stroke.get_color(), stroke.get_width())
        } else {
            (
                mapped.alpha(self.mapped_alpha().expect("validated mapped outline")),
                self.mapped_width().expect("validated mapped outline"),
            )
        }
    }
}

/// Mapped fill alpha and a structural optional outline.
#[derive(Debug, Clone, PartialEq)]
pub struct DataFillTheme {
    pub(crate) fill_alpha: u8,
    pub(crate) outline: Option<DataOutlineTheme>,
}

impl DataFillTheme {
    pub(crate) fn validate(&self) -> Result<()> {
        if let Some(outline) = self.get_outline() {
            outline.validate()?;
        }
        Ok(())
    }
    /// Construct mapped fill alpha and its independently owned optional outline.
    pub fn new(fill_alpha: u8, outline: Option<DataOutlineTheme>) -> Result<Self> {
        let value = Self {
            fill_alpha,
            outline,
        };
        value.validate()?;
        Ok(value)
    }
    /// Inspect fill alpha, including transparent and opaque endpoints.
    pub fn get_fill_alpha(&self) -> u8 {
        self.fill_alpha
    }
    /// Borrow the optional independent outline.
    pub fn get_outline(&self) -> Option<&DataOutlineTheme> {
        self.outline.as_ref()
    }
    /// Replace only fill alpha.
    pub fn fill_alpha(self, fill_alpha: u8) -> Result<Self> {
        Self::new(fill_alpha, self.outline)
    }
    /// Replace the complete optional outline without changing fill alpha.
    pub fn outline(self, outline: Option<DataOutlineTheme>) -> Result<Self> {
        Self::new(self.fill_alpha, outline)
    }
    pub(crate) fn backend_style(&self, rgb: (u8, u8, u8)) -> FillStyle {
        let color = color_from_rgb(rgb);
        let (stroke, width) = self
            .get_outline()
            .map_or((Color::rgba(0, 0, 0, 0), 0.0), |outline| {
                outline.operands(color)
            });
        FillStyle::new(color.alpha(self.get_fill_alpha()), stroke, width)
    }
}

/// Complete fixed fill paint and an independent structural optional outline.
#[derive(Debug, Clone, PartialEq)]
pub struct FixedFillTheme {
    pub(crate) paint: Color,
    pub(crate) outline: Option<OutlineTheme>,
}

impl FixedFillTheme {
    pub(crate) fn validate(&self) -> Result<()> {
        if let Some(outline) = self.get_outline() {
            outline.validate()?;
        }
        Ok(())
    }
    /// Construct complete fixed fill and independent optional outline paints.
    pub fn new(color: Color, outline: Option<OutlineTheme>) -> Result<Self> {
        let value = Self {
            paint: color,
            outline,
        };
        value.validate()?;
        Ok(value)
    }
    /// Inspect the complete fill paint.
    pub fn get_color(&self) -> Color {
        self.paint
    }
    /// Borrow the optional independent outline.
    pub fn get_outline(&self) -> Option<&OutlineTheme> {
        self.outline.as_ref()
    }
    /// Replace only the complete fill paint.
    pub fn color(self, color: Color) -> Result<Self> {
        Self::new(color, self.outline)
    }
    /// Replace the complete optional outline without changing fill paint.
    pub fn outline(self, outline: Option<OutlineTheme>) -> Result<Self> {
        Self::new(self.paint, outline)
    }
    pub(crate) fn backend_style(&self) -> FillStyle {
        self.get_outline().map_or_else(
            || FillStyle::new(self.get_color(), Color::rgba(0, 0, 0, 0), 0.0),
            |outline| outline.backend_style(self.get_color()),
        )
    }
}

impl OutlineTheme {
    pub(crate) fn backend_style(&self, fill: Color) -> FillStyle {
        FillStyle::new(fill, self.get_color(), self.get_width())
    }
}

/// Shared point appearance before selecting its final integral radius.
#[derive(Debug, Clone, PartialEq)]
pub struct PointTheme {
    pub(crate) fill_alpha: u8,
    pub(crate) shape: PointShape,
    pub(crate) outline: Option<PointOutlineTheme>,
}

impl PointTheme {
    pub(crate) fn validate(&self) -> Result<()> {
        if let Some(outline) = self.get_outline() {
            outline.validate()?;
        }
        Ok(())
    }
    /// Construct mapped fill alpha, closed shape and an independent optional outline.
    pub fn new(
        fill_alpha: u8,
        shape: PointShape,
        outline: Option<PointOutlineTheme>,
    ) -> Result<Self> {
        let value = Self {
            fill_alpha,
            shape,
            outline,
        };
        value.validate()?;
        Ok(value)
    }
    /// Inspect the mapped point fill alpha.
    pub fn get_fill_alpha(&self) -> u8 {
        self.fill_alpha
    }
    /// Inspect the closed point shape.
    pub fn get_shape(&self) -> PointShape {
        self.shape
    }
    /// Borrow the optional independent outline.
    pub fn get_outline(&self) -> Option<&PointOutlineTheme> {
        self.outline.as_ref()
    }
    /// Replace only mapped fill alpha.
    pub fn fill_alpha(self, fill_alpha: u8) -> Result<Self> {
        Self::new(fill_alpha, self.shape, self.outline)
    }
    /// Replace only the closed point shape.
    pub fn shape(self, shape: PointShape) -> Result<Self> {
        Self::new(self.fill_alpha, shape, self.outline)
    }
    /// Replace the optional independent outline without changing fill or shape.
    pub fn outline(self, outline: Option<PointOutlineTheme>) -> Result<Self> {
        Self::new(self.fill_alpha, self.shape, outline)
    }
    pub(crate) fn backend_style(
        &self,
        rgb: (u8, u8, u8),
        radius: u32,
        outline_visible: bool,
    ) -> PointStyle {
        PointStyle {
            color: color_from_rgb(rgb).alpha(self.get_fill_alpha()),
            radius,
            shape: self.get_shape(),
            outline: self
                .get_outline()
                .filter(|_| outline_visible)
                .map(PointOutlineTheme::backend_style),
        }
    }
}

/// Data-colored line with one independent alpha and the closed pattern input.
#[derive(Debug, Clone, PartialEq)]
pub struct DataLineTheme {
    pub(crate) width: f32,
    pub(crate) alpha: u8,
    pub(crate) pattern: StrokePattern,
}

impl DataLineTheme {
    pub(crate) fn validate(&self) -> Result<()> {
        validate_width(self.get_width())
    }
    /// Construct positive finite CSS width, mapped byte alpha and a closed pattern.
    pub fn new(width: f32, alpha: u8, pattern: StrokePattern) -> Result<Self> {
        let value = Self {
            width,
            alpha,
            pattern,
        };
        value.validate()?;
        Ok(value)
    }
    /// Inspect the positive CSS width.
    pub fn get_width(&self) -> f32 {
        self.width
    }
    /// Inspect mapped stroke alpha.
    pub fn get_alpha(&self) -> u8 {
        self.alpha
    }
    /// Inspect the closed stroke pattern.
    pub fn get_pattern(&self) -> StrokePattern {
        self.pattern
    }
    /// Replace only the positive CSS width.
    pub fn width(self, width: f32) -> Result<Self> {
        Self::new(width, self.alpha, self.pattern)
    }
    /// Replace only mapped stroke alpha.
    pub fn alpha(self, alpha: u8) -> Result<Self> {
        Self::new(self.width, alpha, self.pattern)
    }
    /// Replace only the closed stroke pattern.
    pub fn pattern(self, pattern: StrokePattern) -> Result<Self> {
        Self::new(self.width, self.alpha, pattern)
    }
    pub(crate) fn backend_style(&self, rgb: (u8, u8, u8)) -> LineStyle {
        LineStyle {
            color: color_from_rgb(rgb).alpha(self.get_alpha()),
            width: self.get_width(),
            pattern: self.get_pattern(),
        }
    }
}