summaryrefslogtreecommitdiffstats
path: root/graphics/src/gradient.rs
blob: e1984854eb07c1ea7a84fe84a201beeb2667eba8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! For creating a Gradient.
mod linear;

pub use crate::gradient::linear::{Linear, Position, Location};
use crate::widget::canvas::frame::Transform;
use crate::Color;
use crate::widget::canvas::{Fill, fill};

#[derive(Debug, Clone, PartialEq)]
/// A fill which transitions colors progressively along a direction, either linearly, radially (TBD),
/// or conically (TBD).
pub enum Gradient {
    /// A linear gradient interpolates colors along a direction from its [`start`] to its [`end`]
    /// point.
    Linear(Linear),
}

#[derive(Debug, Clone, Copy, PartialEq)]
/// A point along the gradient vector where the specified [`color`] is unmixed.
pub struct ColorStop {
    /// Offset along the gradient vector.
    pub offset: f32,
    /// The color of the gradient at the specified [`offset`].
    pub color: Color,
}

impl Gradient {
    /// Creates a new linear [`linear::Builder`].
    pub fn linear(position: impl Into<Position>) -> linear::Builder {
        linear::Builder::new(position.into())
    }

    /// Modifies the start & end stops of the gradient to have a proper transform value.
    pub(crate) fn transform(mut self, transform: &Transform) -> Self {
        match &mut self {
            Gradient::Linear(linear) => {
                linear.start = transform.transform_point(linear.start);
                linear.end = transform.transform_point(linear.end);
            }
        }
        self
    }
}

impl<'a> Into<Fill<'a>> for &'a Gradient {
    fn into(self) -> Fill<'a> {
        Fill {
            style: fill::Style::Gradient(self),
            .. Default::default()
        }
    }
}