Skip to main content

ferroui_base/media/immutable/
immutable_linear_gradient_brush.rs

1use crate::media::immutable::immutable_gradient_brush::immutable_gradient_brush_interfaces;
2use crate::media::immutable::{ImmutableGradientBrush, ImmutableGradientStop, ImmutableTransform};
3use crate::media::{GradientSpreadMethod, ILinearGradientBrush, LinearGradientBrush};
4use crate::RelativePoint;
5use std::rc::Rc;
6
7/// A brush that draws with a linear gradient.
8#[derive(Clone)]
9pub struct ImmutableLinearGradientBrush {
10    base: ImmutableGradientBrush,
11    start_point: RelativePoint,
12    end_point: RelativePoint,
13}
14
15immutable_gradient_brush_interfaces!(
16    ImmutableLinearGradientBrush,
17    as_linear_gradient_brush,
18    crate::media::ILinearGradientBrush
19);
20
21impl ImmutableLinearGradientBrush {
22    /// Creates a brush. `transform_origin` defaults to the top left corner,
23    /// `start_point` to the top left and `end_point` to the bottom right.
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        start_point: Option<RelativePoint>,
32        end_point: Option<RelativePoint>,
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            start_point: start_point.unwrap_or(RelativePoint::TOP_LEFT),
45            end_point: end_point.unwrap_or(RelativePoint::BOTTOM_RIGHT),
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, None, None)
53    }
54
55    /// Creates an immutable copy of `source`.
56    pub fn from_brush(source: &LinearGradientBrush) -> Self {
57        Self {
58            base: ImmutableGradientBrush::from_brush(source),
59            start_point: source.start_point(),
60            end_point: source.end_point(),
61        }
62    }
63
64    /// The start point for the gradient.
65    #[inline]
66    pub fn start_point(&self) -> RelativePoint {
67        self.start_point
68    }
69
70    /// The end point for the gradient.
71    #[inline]
72    pub fn end_point(&self) -> RelativePoint {
73        self.end_point
74    }
75}
76
77impl ILinearGradientBrush for ImmutableLinearGradientBrush {
78    #[inline]
79    fn start_point(&self) -> RelativePoint {
80        self.start_point
81    }
82
83    #[inline]
84    fn end_point(&self) -> RelativePoint {
85        self.end_point
86    }
87}