use super::Color;
use crate::types::GradientType;
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ColorStop {
pub offset: f64,
pub color: Color,
}
impl ColorStop {
pub fn new(offset: f64, color: Color) -> Self {
Self {
offset: offset.clamp(0.0, 1.0),
color,
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Gradient {
pub gradient_type: GradientType,
pub rotation: f64,
pub color_stops: Vec<ColorStop>,
}
impl Gradient {
pub fn linear(color_stops: Vec<ColorStop>) -> Self {
Self {
gradient_type: GradientType::Linear,
rotation: 0.0,
color_stops,
}
}
pub fn linear_rotated(rotation: f64, color_stops: Vec<ColorStop>) -> Self {
Self {
gradient_type: GradientType::Linear,
rotation,
color_stops,
}
}
pub fn radial(color_stops: Vec<ColorStop>) -> Self {
Self {
gradient_type: GradientType::Radial,
rotation: 0.0,
color_stops,
}
}
pub fn simple_linear(start: Color, end: Color) -> Self {
Self::linear(vec![
ColorStop::new(0.0, start),
ColorStop::new(1.0, end),
])
}
pub fn simple_radial(center: Color, edge: Color) -> Self {
Self::radial(vec![
ColorStop::new(0.0, center),
ColorStop::new(1.0, edge),
])
}
}
impl Default for Gradient {
fn default() -> Self {
Self::simple_linear(Color::BLACK, Color::BLACK)
}
}