Skip to main content

lichtung_plot/theme/
mark_paint.rs

1//! Validated compound paints, independent of backend geometry and operation state.
2
3use crate::render::backend::{FillStyle, LineStyle, PointOutline, PointStyle, StrokePattern};
4use crate::{Error, PointShape, Result};
5
6use super::{color_from_rgb, Color, RelativeSpacing};
7
8fn validate_width(width: f32) -> Result<()> {
9    if width.is_finite() && width > 0.0 {
10        Ok(())
11    } else {
12        Err(Error::Data(
13            "present stroke width must be finite and positive CSS pixels".into(),
14        ))
15    }
16}
17
18// Solid outlines and solid bracket strokes share scalar validation, not roles.
19macro_rules! solid_paint {
20    ($name:ident, $documentation:literal) => {
21        #[doc = $documentation]
22        #[derive(Debug, Clone, PartialEq)]
23        pub struct $name {
24            pub(crate) paint: Color,
25            pub(crate) width: f32,
26        }
27
28        impl $name {
29            /// Construct a complete paint and positive finite CSS stroke width.
30            pub fn new(color: Color, width: f32) -> Result<Self> {
31                let value = Self {
32                    paint: color,
33                    width,
34                };
35                value.validate()?;
36                Ok(value)
37            }
38
39            pub(crate) fn validate(&self) -> Result<()> {
40                validate_width(self.get_width())
41            }
42
43            /// Inspect the complete stroke paint.
44            pub fn get_color(&self) -> Color {
45                self.paint
46            }
47            /// Inspect the positive width in CSS pixels.
48            pub fn get_width(&self) -> f32 {
49                self.width
50            }
51
52            /// Replace only the complete stroke paint.
53            pub fn color(self, color: Color) -> Result<Self> {
54                Self::new(color, self.width)
55            }
56
57            /// Replace only the positive CSS width.
58            pub fn width(self, width: f32) -> Result<Self> {
59                Self::new(self.paint, width)
60            }
61        }
62    };
63}
64
65solid_paint!(
66    SolidStrokeTheme,
67    "Complete solid bracket stroke; its topology has no pattern edit."
68);
69solid_paint!(
70    OutlineTheme,
71    "Complete independent outline paint and positive CSS width."
72);
73
74/// Complete stroke with the renderer's one closed dash vocabulary.
75#[derive(Debug, Clone, PartialEq)]
76pub struct StrokeTheme {
77    pub(crate) paint: Color,
78    pub(crate) width: f32,
79    pub(crate) pattern: StrokePattern,
80}
81
82impl StrokeTheme {
83    pub(crate) fn validate(&self) -> Result<()> {
84        validate_width(self.get_width())
85    }
86    /// Construct a complete stroke with positive finite CSS width and a closed pattern.
87    pub fn new(color: Color, width: f32, pattern: StrokePattern) -> Result<Self> {
88        validate_width(width)?;
89        Ok(Self {
90            paint: color,
91            width,
92            pattern,
93        })
94    }
95
96    /// Inspect the complete stroke paint.
97    pub fn get_color(&self) -> Color {
98        self.paint
99    }
100    /// Inspect the positive CSS width.
101    pub fn get_width(&self) -> f32 {
102        self.width
103    }
104    /// Inspect the closed stroke pattern.
105    pub fn get_pattern(&self) -> StrokePattern {
106        self.pattern
107    }
108
109    /// Replace only the complete stroke paint.
110    pub fn color(self, color: Color) -> Result<Self> {
111        Self::new(color, self.width, self.pattern)
112    }
113    /// Replace only the positive CSS width.
114    pub fn width(self, width: f32) -> Result<Self> {
115        Self::new(self.paint, width, self.pattern)
116    }
117    /// Replace only the closed stroke pattern.
118    pub fn pattern(self, pattern: StrokePattern) -> Result<Self> {
119        Self::new(self.paint, self.width, pattern)
120    }
121
122    pub(crate) fn backend_style(&self) -> LineStyle {
123        LineStyle {
124            color: self.get_color(),
125            width: self.get_width(),
126            pattern: self.get_pattern(),
127        }
128    }
129}
130
131/// A point outline owns one exact relationship to the selected point radius.
132#[derive(Debug, Clone, PartialEq, Eq)]
133pub struct PointOutlineTheme {
134    pub(crate) color: Color,
135    pub(crate) width_ratio: RelativeSpacing,
136}
137
138impl PointOutlineTheme {
139    /// Construct independent outline paint and a positive radius-relative width.
140    pub fn new(color: Color, width_ratio: RelativeSpacing) -> Result<Self> {
141        if width_ratio.is_zero() {
142            return Err(Error::Data(
143                "point outline width ratio must be positive".into(),
144            ));
145        }
146        Ok(Self { color, width_ratio })
147    }
148
149    fn validate(&self) -> Result<()> {
150        if self.width_ratio.is_zero() {
151            Err(Error::Data(
152                "point outline width ratio must be positive".into(),
153            ))
154        } else {
155            Ok(())
156        }
157    }
158
159    /// Inspect the independent outline paint.
160    pub fn get_color(&self) -> Color {
161        self.color
162    }
163    /// Borrow the exact positive width-to-radius ratio.
164    pub fn get_width_ratio(&self) -> &RelativeSpacing {
165        &self.width_ratio
166    }
167    /// Replace only the independent outline paint.
168    pub fn color(self, color: Color) -> Result<Self> {
169        Self::new(color, self.width_ratio)
170    }
171    /// Replace only the positive radius-relative width.
172    pub fn width_ratio(self, width_ratio: RelativeSpacing) -> Result<Self> {
173        Self::new(self.color, width_ratio)
174    }
175
176    pub(crate) fn publication() -> Result<Self> {
177        Self::new(Color::rgb(255, 255, 255), RelativeSpacing::new(0.1)?)
178    }
179
180    /// Materialize the operational outline by sharing the same immutable ratio.
181    pub(crate) fn backend_style(&self) -> PointOutline {
182        PointOutline {
183            color: self.get_color(),
184            width_ratio: self.get_width_ratio().clone(),
185        }
186    }
187}
188
189/// Fixed paint or mapped RGB with an independent outline alpha.
190#[derive(Debug, Clone, PartialEq)]
191pub struct DataOutlineTheme {
192    choice: DataOutline,
193}
194
195#[derive(Debug, Clone, PartialEq)]
196enum DataOutline {
197    Fixed(OutlineTheme),
198    Mapped { width: f32, alpha: u8 },
199}
200
201impl DataOutlineTheme {
202    /// Select a complete independent fixed outline.
203    pub fn fixed(stroke: OutlineTheme) -> Self {
204        Self {
205            choice: DataOutline::Fixed(stroke),
206        }
207    }
208
209    /// Select mapped RGB with positive CSS width and an independent byte alpha.
210    pub fn mapped(width: f32, alpha: u8) -> Result<Self> {
211        validate_width(width)?;
212        Ok(Self {
213            choice: DataOutline::Mapped { width, alpha },
214        })
215    }
216
217    /// Borrow the fixed outline, or None for mapped RGB.
218    pub fn fixed_stroke(&self) -> Option<&OutlineTheme> {
219        match &self.choice {
220            DataOutline::Fixed(stroke) => Some(stroke),
221            DataOutline::Mapped { .. } => None,
222        }
223    }
224    /// Inspect the mapped width, or None for a fixed outline.
225    pub fn mapped_width(&self) -> Option<f32> {
226        match self.choice {
227            DataOutline::Fixed(_) => None,
228            DataOutline::Mapped { width, .. } => Some(width),
229        }
230    }
231    /// Inspect mapped alpha, or None for a fixed outline.
232    pub fn mapped_alpha(&self) -> Option<u8> {
233        match self.choice {
234            DataOutline::Fixed(_) => None,
235            DataOutline::Mapped { alpha, .. } => Some(alpha),
236        }
237    }
238
239    fn validate(&self) -> Result<()> {
240        match &self.choice {
241            DataOutline::Fixed(stroke) => stroke.validate(),
242            DataOutline::Mapped { width, .. } => validate_width(*width),
243        }
244    }
245
246    fn operands(&self, mapped: Color) -> (Color, f32) {
247        if let Some(stroke) = self.fixed_stroke() {
248            (stroke.get_color(), stroke.get_width())
249        } else {
250            (
251                mapped.alpha(self.mapped_alpha().expect("validated mapped outline")),
252                self.mapped_width().expect("validated mapped outline"),
253            )
254        }
255    }
256}
257
258/// Mapped fill alpha and a structural optional outline.
259#[derive(Debug, Clone, PartialEq)]
260pub struct DataFillTheme {
261    pub(crate) fill_alpha: u8,
262    pub(crate) outline: Option<DataOutlineTheme>,
263}
264
265impl DataFillTheme {
266    pub(crate) fn validate(&self) -> Result<()> {
267        if let Some(outline) = self.get_outline() {
268            outline.validate()?;
269        }
270        Ok(())
271    }
272    /// Construct mapped fill alpha and its independently owned optional outline.
273    pub fn new(fill_alpha: u8, outline: Option<DataOutlineTheme>) -> Result<Self> {
274        let value = Self {
275            fill_alpha,
276            outline,
277        };
278        value.validate()?;
279        Ok(value)
280    }
281    /// Inspect fill alpha, including transparent and opaque endpoints.
282    pub fn get_fill_alpha(&self) -> u8 {
283        self.fill_alpha
284    }
285    /// Borrow the optional independent outline.
286    pub fn get_outline(&self) -> Option<&DataOutlineTheme> {
287        self.outline.as_ref()
288    }
289    /// Replace only fill alpha.
290    pub fn fill_alpha(self, fill_alpha: u8) -> Result<Self> {
291        Self::new(fill_alpha, self.outline)
292    }
293    /// Replace the complete optional outline without changing fill alpha.
294    pub fn outline(self, outline: Option<DataOutlineTheme>) -> Result<Self> {
295        Self::new(self.fill_alpha, outline)
296    }
297    pub(crate) fn backend_style(&self, rgb: (u8, u8, u8)) -> FillStyle {
298        let color = color_from_rgb(rgb);
299        let (stroke, width) = self
300            .get_outline()
301            .map_or((Color::rgba(0, 0, 0, 0), 0.0), |outline| {
302                outline.operands(color)
303            });
304        FillStyle::new(color.alpha(self.get_fill_alpha()), stroke, width)
305    }
306}
307
308/// Complete fixed fill paint and an independent structural optional outline.
309#[derive(Debug, Clone, PartialEq)]
310pub struct FixedFillTheme {
311    pub(crate) paint: Color,
312    pub(crate) outline: Option<OutlineTheme>,
313}
314
315impl FixedFillTheme {
316    pub(crate) fn validate(&self) -> Result<()> {
317        if let Some(outline) = self.get_outline() {
318            outline.validate()?;
319        }
320        Ok(())
321    }
322    /// Construct complete fixed fill and independent optional outline paints.
323    pub fn new(color: Color, outline: Option<OutlineTheme>) -> Result<Self> {
324        let value = Self {
325            paint: color,
326            outline,
327        };
328        value.validate()?;
329        Ok(value)
330    }
331    /// Inspect the complete fill paint.
332    pub fn get_color(&self) -> Color {
333        self.paint
334    }
335    /// Borrow the optional independent outline.
336    pub fn get_outline(&self) -> Option<&OutlineTheme> {
337        self.outline.as_ref()
338    }
339    /// Replace only the complete fill paint.
340    pub fn color(self, color: Color) -> Result<Self> {
341        Self::new(color, self.outline)
342    }
343    /// Replace the complete optional outline without changing fill paint.
344    pub fn outline(self, outline: Option<OutlineTheme>) -> Result<Self> {
345        Self::new(self.paint, outline)
346    }
347    pub(crate) fn backend_style(&self) -> FillStyle {
348        self.get_outline().map_or_else(
349            || FillStyle::new(self.get_color(), Color::rgba(0, 0, 0, 0), 0.0),
350            |outline| outline.backend_style(self.get_color()),
351        )
352    }
353}
354
355impl OutlineTheme {
356    pub(crate) fn backend_style(&self, fill: Color) -> FillStyle {
357        FillStyle::new(fill, self.get_color(), self.get_width())
358    }
359}
360
361/// Shared point appearance before selecting its final integral radius.
362#[derive(Debug, Clone, PartialEq)]
363pub struct PointTheme {
364    pub(crate) fill_alpha: u8,
365    pub(crate) shape: PointShape,
366    pub(crate) outline: Option<PointOutlineTheme>,
367}
368
369impl PointTheme {
370    pub(crate) fn validate(&self) -> Result<()> {
371        if let Some(outline) = self.get_outline() {
372            outline.validate()?;
373        }
374        Ok(())
375    }
376    /// Construct mapped fill alpha, closed shape and an independent optional outline.
377    pub fn new(
378        fill_alpha: u8,
379        shape: PointShape,
380        outline: Option<PointOutlineTheme>,
381    ) -> Result<Self> {
382        let value = Self {
383            fill_alpha,
384            shape,
385            outline,
386        };
387        value.validate()?;
388        Ok(value)
389    }
390    /// Inspect the mapped point fill alpha.
391    pub fn get_fill_alpha(&self) -> u8 {
392        self.fill_alpha
393    }
394    /// Inspect the closed point shape.
395    pub fn get_shape(&self) -> PointShape {
396        self.shape
397    }
398    /// Borrow the optional independent outline.
399    pub fn get_outline(&self) -> Option<&PointOutlineTheme> {
400        self.outline.as_ref()
401    }
402    /// Replace only mapped fill alpha.
403    pub fn fill_alpha(self, fill_alpha: u8) -> Result<Self> {
404        Self::new(fill_alpha, self.shape, self.outline)
405    }
406    /// Replace only the closed point shape.
407    pub fn shape(self, shape: PointShape) -> Result<Self> {
408        Self::new(self.fill_alpha, shape, self.outline)
409    }
410    /// Replace the optional independent outline without changing fill or shape.
411    pub fn outline(self, outline: Option<PointOutlineTheme>) -> Result<Self> {
412        Self::new(self.fill_alpha, self.shape, outline)
413    }
414    pub(crate) fn backend_style(
415        &self,
416        rgb: (u8, u8, u8),
417        radius: u32,
418        outline_visible: bool,
419    ) -> PointStyle {
420        PointStyle {
421            color: color_from_rgb(rgb).alpha(self.get_fill_alpha()),
422            radius,
423            shape: self.get_shape(),
424            outline: self
425                .get_outline()
426                .filter(|_| outline_visible)
427                .map(PointOutlineTheme::backend_style),
428        }
429    }
430}
431
432/// Data-colored line with one independent alpha and the closed pattern input.
433#[derive(Debug, Clone, PartialEq)]
434pub struct DataLineTheme {
435    pub(crate) width: f32,
436    pub(crate) alpha: u8,
437    pub(crate) pattern: StrokePattern,
438}
439
440impl DataLineTheme {
441    pub(crate) fn validate(&self) -> Result<()> {
442        validate_width(self.get_width())
443    }
444    /// Construct positive finite CSS width, mapped byte alpha and a closed pattern.
445    pub fn new(width: f32, alpha: u8, pattern: StrokePattern) -> Result<Self> {
446        let value = Self {
447            width,
448            alpha,
449            pattern,
450        };
451        value.validate()?;
452        Ok(value)
453    }
454    /// Inspect the positive CSS width.
455    pub fn get_width(&self) -> f32 {
456        self.width
457    }
458    /// Inspect mapped stroke alpha.
459    pub fn get_alpha(&self) -> u8 {
460        self.alpha
461    }
462    /// Inspect the closed stroke pattern.
463    pub fn get_pattern(&self) -> StrokePattern {
464        self.pattern
465    }
466    /// Replace only the positive CSS width.
467    pub fn width(self, width: f32) -> Result<Self> {
468        Self::new(width, self.alpha, self.pattern)
469    }
470    /// Replace only mapped stroke alpha.
471    pub fn alpha(self, alpha: u8) -> Result<Self> {
472        Self::new(self.width, alpha, self.pattern)
473    }
474    /// Replace only the closed stroke pattern.
475    pub fn pattern(self, pattern: StrokePattern) -> Result<Self> {
476        Self::new(self.width, self.alpha, pattern)
477    }
478    pub(crate) fn backend_style(&self, rgb: (u8, u8, u8)) -> LineStyle {
479        LineStyle {
480            color: color_from_rgb(rgb).alpha(self.get_alpha()),
481            width: self.get_width(),
482            pattern: self.get_pattern(),
483        }
484    }
485}