2022-09-29 10:52:58 -07:00
|
|
|
//! For creating a Gradient.
|
2022-10-05 10:49:58 -07:00
|
|
|
mod linear;
|
|
|
|
|
|
2022-10-07 11:41:50 -07:00
|
|
|
pub use crate::gradient::linear::{Linear, Position, Location};
|
2022-10-06 16:57:38 -07:00
|
|
|
use crate::widget::canvas::frame::Transform;
|
2022-10-07 11:41:50 -07:00
|
|
|
use crate::Color;
|
|
|
|
|
use crate::widget::canvas::{Fill, fill};
|
2022-09-29 10:52:58 -07:00
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2022-09-30 10:27:00 -07:00
|
|
|
/// A fill which transitions colors progressively along a direction, either linearly, radially (TBD),
|
|
|
|
|
/// or conically (TBD).
|
2022-09-29 10:52:58 -07:00
|
|
|
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,
|
2022-09-30 10:27:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Gradient {
|
|
|
|
|
/// Creates a new linear [`linear::Builder`].
|
2022-10-07 11:41:50 -07:00
|
|
|
pub fn linear(position: impl Into<Position>) -> linear::Builder {
|
|
|
|
|
linear::Builder::new(position.into())
|
2022-09-30 10:27:00 -07:00
|
|
|
}
|
2022-10-06 16:57:38 -07:00
|
|
|
|
|
|
|
|
/// 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) => {
|
2022-10-07 11:41:50 -07:00
|
|
|
linear.start = transform.transform_point(linear.start);
|
|
|
|
|
linear.end = transform.transform_point(linear.end);
|
2022-10-06 16:57:38 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self
|
|
|
|
|
}
|
2022-09-30 10:27:00 -07:00
|
|
|
}
|
2022-10-07 11:41:50 -07:00
|
|
|
|
|
|
|
|
impl<'a> Into<Fill<'a>> for &'a Gradient {
|
|
|
|
|
fn into(self) -> Fill<'a> {
|
|
|
|
|
Fill {
|
|
|
|
|
style: fill::Style::Gradient(self),
|
|
|
|
|
.. Default::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|