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