use zenith_scene::GradientPaint;
#[derive(Debug, Clone, PartialEq)]
pub(super) struct AxialGradient {
pub(super) coords: [f32; 4],
pub(super) stops: Vec<(f32, [f32; 3])>,
}
pub(super) fn resolve(
x: f64,
y: f64,
w: f64,
h: f64,
gradient: &GradientPaint,
) -> Option<AxialGradient> {
if gradient.stops.len() < 2 {
return None;
}
let theta = gradient.angle_deg.to_radians();
let (dir_x, dir_y) = (theta.cos(), theta.sin());
let (cx, cy) = (x + w / 2.0, y + h / 2.0);
let line_len = (w * dir_x).abs() + (h * dir_y).abs();
let half = line_len / 2.0;
let coords = [
(cx - dir_x * half) as f32,
(cy - dir_y * half) as f32,
(cx + dir_x * half) as f32,
(cy + dir_y * half) as f32,
];
let stops: Vec<(f32, [f32; 3])> = gradient
.stops
.iter()
.map(|s| {
(
(s.offset as f32).clamp(0.0, 1.0),
[
f32::from(s.color.r) / 255.0,
f32::from(s.color.g) / 255.0,
f32::from(s.color.b) / 255.0,
],
)
})
.collect();
Some(AxialGradient { coords, stops })
}