winio_primitive/
canvas.rs

1use crate::{Color, RelativePoint, RelativeSize};
2
3/// Brush with single solid color.
4#[derive(Debug, Clone)]
5pub struct SolidColorBrush {
6    /// The color of the brush.
7    pub color: Color,
8}
9
10impl SolidColorBrush {
11    /// Create [`SolidColorBrush`] with color.
12    pub fn new(color: Color) -> Self {
13        Self { color }
14    }
15}
16
17/// A transition point in a gradient.
18#[derive(Debug, PartialEq, Clone, Copy)]
19pub struct GradientStop {
20    /// Color of the stop.
21    pub color: Color,
22    /// Relative position of the stop, from 0 to 1.
23    pub pos: f64,
24}
25
26impl GradientStop {
27    /// Create [`GradientStop`].
28    pub fn new(color: Color, pos: f64) -> Self {
29        Self { color, pos }
30    }
31}
32
33impl From<(Color, f64)> for GradientStop {
34    fn from((color, pos): (Color, f64)) -> Self {
35        Self::new(color, pos)
36    }
37}
38
39/// Linear gradient brush.
40#[derive(Debug, Clone)]
41pub struct LinearGradientBrush {
42    /// The gradient stops.
43    pub stops: Vec<GradientStop>,
44    /// The relative start position.
45    pub start: RelativePoint,
46    /// The relative end position.
47    pub end: RelativePoint,
48}
49
50impl LinearGradientBrush {
51    /// Create [`LinearGradientBrush`].
52    pub fn new(
53        stops: impl IntoIterator<Item = GradientStop>,
54        start: RelativePoint,
55        end: RelativePoint,
56    ) -> Self {
57        Self {
58            stops: stops.into_iter().collect(),
59            start,
60            end,
61        }
62    }
63}
64
65/// Radial gradient brush.
66#[derive(Debug, Clone)]
67pub struct RadialGradientBrush {
68    /// The gradient stops.
69    pub stops: Vec<GradientStop>,
70    /// The relative origin position.
71    pub origin: RelativePoint,
72    /// The relative center position.
73    pub center: RelativePoint,
74    /// The relative radius.
75    pub radius: RelativeSize,
76}
77
78impl RadialGradientBrush {
79    /// Create [`RadialGradientBrush`].
80    pub fn new(
81        stops: impl IntoIterator<Item = GradientStop>,
82        origin: RelativePoint,
83        center: RelativePoint,
84        radius: RelativeSize,
85    ) -> Self {
86        Self {
87            stops: stops.into_iter().collect(),
88            origin,
89            center,
90            radius,
91        }
92    }
93}
94
95/// Pen with specified brush.
96#[derive(Debug, Clone)]
97pub struct BrushPen<B> {
98    /// The inner brush.
99    pub brush: B,
100    /// The width of the pen.
101    pub width: f64,
102}
103
104impl<B> BrushPen<B> {
105    /// Create [`BrushPen`] with brush and pen width.
106    pub fn new(brush: B, width: f64) -> Self {
107        Self { brush, width }
108    }
109}