Skip to main content

ferroui_base/media/immutable/
immutable_conic_gradient_brush.rs

1use crate::media::immutable::immutable_gradient_brush::immutable_gradient_brush_interfaces;
2use crate::media::immutable::{ImmutableGradientBrush, ImmutableGradientStop, ImmutableTransform};
3use crate::media::{ConicGradientBrush, GradientSpreadMethod, IConicGradientBrush};
4use crate::RelativePoint;
5use std::rc::Rc;
6
7/// A brush that draws with a sweep gradient.
8#[derive(Clone)]
9pub struct ImmutableConicGradientBrush {
10    base: ImmutableGradientBrush,
11    center: RelativePoint,
12    angle: f64,
13}
14
15immutable_gradient_brush_interfaces!(
16    ImmutableConicGradientBrush,
17    as_conic_gradient_brush,
18    crate::media::IConicGradientBrush
19);
20
21impl ImmutableConicGradientBrush {
22    /// Creates a brush. `transform_origin` defaults to the top left corner
23    /// and `center` to the center.
24    #[allow(clippy::too_many_arguments)]
25    pub fn new(
26        gradient_stops: &[ImmutableGradientStop],
27        opacity: f64,
28        transform: Option<Rc<ImmutableTransform>>,
29        transform_origin: Option<RelativePoint>,
30        spread_method: GradientSpreadMethod,
31        center: Option<RelativePoint>,
32        angle: f64,
33        relative_transform: Option<Rc<ImmutableTransform>>,
34    ) -> Self {
35        Self {
36            base: ImmutableGradientBrush::new(
37                gradient_stops,
38                opacity,
39                transform,
40                transform_origin,
41                spread_method,
42                relative_transform,
43            ),
44            center: center.unwrap_or(RelativePoint::CENTER),
45            angle,
46        }
47    }
48
49    /// Creates a fully opaque brush with the given stops and default values
50    /// for everything else.
51    pub fn from_stops(gradient_stops: &[ImmutableGradientStop]) -> Self {
52        Self::new(gradient_stops, 1.0, None, None, GradientSpreadMethod::Pad, None, 0.0, None)
53    }
54
55    /// Creates an immutable copy of `source`.
56    pub fn from_brush(source: &ConicGradientBrush) -> Self {
57        Self { base: ImmutableGradientBrush::from_brush(source), center: source.center(), angle: source.angle() }
58    }
59
60    /// The center point for the gradient.
61    #[inline]
62    pub fn center(&self) -> RelativePoint {
63        self.center
64    }
65
66    /// The starting angle of the gradient's sweep, in degrees.
67    #[inline]
68    pub fn angle(&self) -> f64 {
69        self.angle
70    }
71}
72
73impl IConicGradientBrush for ImmutableConicGradientBrush {
74    #[inline]
75    fn center(&self) -> RelativePoint {
76        self.center
77    }
78
79    #[inline]
80    fn angle(&self) -> f64 {
81        self.angle
82    }
83}