rootvg_tessellation/
fill.rs

1// The following code was copied and modified from
2// https://github.com/iced-rs/iced/blob/31d1d5fecbef50fa319cabd5d4194f1e4aaefa21/graphics/src/geometry/fill.rs
3// Iced license (MIT): https://github.com/iced-rs/iced/blob/31d1d5fecbef50fa319cabd5d4194f1e4aaefa21/LICENSE
4
5use rootvg_core::color::{PackedSrgb, RGB8, RGBA8};
6
7#[cfg(feature = "gradient")]
8use rootvg_core::gradient::PackedGradient;
9
10/// The style used to fill geometry.
11#[derive(Debug, Clone)]
12pub struct Fill {
13    /// The color or gradient of the fill.
14    ///
15    /// By default, it is set to [`FillStyle::Solid`] with [`Color::BLACK`].
16    pub style: FillStyle,
17
18    /// The fill rule defines how to determine what is inside and what is
19    /// outside of a shape.
20    ///
21    /// See the [SVG specification][1] for more details.
22    ///
23    /// By default, it is set to `NonZero`.
24    ///
25    /// [1]: https://www.w3.org/TR/SVG/painting.html#FillRuleProperty
26    pub rule: FillRule,
27}
28
29impl Default for Fill {
30    fn default() -> Self {
31        Self {
32            style: FillStyle::Solid(PackedSrgb([0.0, 0.0, 0.0, 1.0])),
33            rule: FillRule::NonZero,
34        }
35    }
36}
37
38impl From<PackedSrgb> for Fill {
39    fn from(color: PackedSrgb) -> Fill {
40        Fill {
41            style: FillStyle::Solid(color),
42            ..Fill::default()
43        }
44    }
45}
46
47impl From<RGB8> for Fill {
48    fn from(color: RGB8) -> Fill {
49        Fill {
50            style: FillStyle::Solid(color.into()),
51            ..Fill::default()
52        }
53    }
54}
55
56impl From<RGBA8> for Fill {
57    fn from(color: RGBA8) -> Fill {
58        Fill {
59            style: FillStyle::Solid(color.into()),
60            ..Fill::default()
61        }
62    }
63}
64
65#[cfg(feature = "gradient")]
66impl From<PackedGradient> for Fill {
67    fn from(gradient: PackedGradient) -> Self {
68        Fill {
69            style: FillStyle::Gradient(gradient),
70            ..Default::default()
71        }
72    }
73}
74
75/// The fill rule defines how to determine what is inside and what is outside of
76/// a shape.
77///
78/// See the [SVG specification][1].
79///
80/// [1]: https://www.w3.org/TR/SVG/painting.html#FillRuleProperty
81#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
82pub enum FillRule {
83    #[default]
84    NonZero,
85    EvenOdd,
86}
87
88/// The coloring style of some drawing.
89#[derive(Debug, Clone, PartialEq)]
90pub enum FillStyle {
91    /// A solid [`Color`].
92    Solid(PackedSrgb),
93
94    #[cfg(feature = "gradient")]
95    /// A [`PackedGradient`] color.
96    Gradient(PackedGradient),
97}
98
99impl From<PackedSrgb> for FillStyle {
100    fn from(color: PackedSrgb) -> Self {
101        Self::Solid(color)
102    }
103}
104
105#[cfg(feature = "gradient")]
106impl From<PackedGradient> for FillStyle {
107    fn from(gradient: PackedGradient) -> Self {
108        Self::Gradient(gradient)
109    }
110}
111
112impl From<RGB8> for FillStyle {
113    fn from(color: RGB8) -> Self {
114        Self::Solid(color.into())
115    }
116}
117
118impl From<RGBA8> for FillStyle {
119    fn from(color: RGBA8) -> Self {
120        Self::Solid(color.into())
121    }
122}