use crate::style::Color;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum GradientKind {
#[default]
Linear,
Radial,
Conic,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LinearGradient {
pub kind: GradientKind,
pub start: (f32, f32),
pub end: (f32, f32),
pub stops: [GradientStop; 4],
pub stop_count: u32,
}
pub type Gradient = LinearGradient;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GradientStop {
pub color: Color,
pub offset: f32,
}
impl LinearGradient {
fn build(
kind: GradientKind,
start: (f32, f32),
end: (f32, f32),
stops: &[(Color, f32)],
) -> Self {
let mut s = Self {
kind,
start,
end,
stops: [GradientStop {
color: Color::TRANSPARENT,
offset: 0.0,
}; 4],
stop_count: stops.len().min(4) as u32,
};
for (i, &(color, offset)) in stops.iter().take(4).enumerate() {
s.stops[i] = GradientStop { color, offset };
}
s
}
pub fn new(start: (f32, f32), end: (f32, f32), stops: &[(Color, f32)]) -> Self {
Self::build(GradientKind::Linear, start, end, stops)
}
pub fn linear(start: (f32, f32), end: (f32, f32), stops: &[(Color, f32)]) -> Self {
Self::build(GradientKind::Linear, start, end, stops)
}
pub fn radial(center: (f32, f32), radius_point: (f32, f32), stops: &[(Color, f32)]) -> Self {
Self::build(GradientKind::Radial, center, radius_point, stops)
}
pub fn conic(center: (f32, f32), angle_ref: (f32, f32), stops: &[(Color, f32)]) -> Self {
Self::build(GradientKind::Conic, center, angle_ref, stops)
}
}