use serde::Deserialize;
use crate::GradientCoordinates;
use windows::Win32::{
Foundation::RECT,
Graphics::Direct2D::{
Common::{D2D1_GRADIENT_STOP, D2D_POINT_2F},
ID2D1LinearGradientBrush,
},
};
#[allow(dead_code)]
pub trait GradientImpl {
fn update_start_end_points(&self, window_rect: &RECT);
}
#[derive(Debug, Clone, PartialEq)]
pub struct Gradient {
pub direction: GradientCoordinates,
pub gradient_stops: Vec<D2D1_GRADIENT_STOP>,
pub brush: Option<ID2D1LinearGradientBrush>,
}
impl GradientImpl for Gradient {
fn update_start_end_points(&self, window_rect: &RECT) {
let width = (window_rect.right - window_rect.left) as f32;
let height = (window_rect.bottom - window_rect.top) as f32;
let start_point = D2D_POINT_2F {
x: self.direction.start[0] * width,
y: self.direction.start[1] * height,
};
let end_point = D2D_POINT_2F {
x: self.direction.end[0] * width,
y: self.direction.end[1] * height,
};
if let Some(ref id2d1_brush) = self.brush {
unsafe {
id2d1_brush.SetStartPoint(start_point);
id2d1_brush.SetEndPoint(end_point)
};
}
}
}
#[derive(Debug, Clone, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum GradientDirection {
Direction(String),
Coordinates(GradientCoordinates),
}
impl From<&str> for GradientDirection {
fn from(s: &str) -> Self {
Self::Direction(s.to_string())
}
}
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct ColorMapping {
pub colors: Vec<String>,
pub direction: GradientDirection,
}
pub trait ColorMappingImpl {
fn new(colors: &[&str], direction: GradientDirection) -> Self;
}
impl ColorMappingImpl for ColorMapping {
fn new(colors: &[&str], direction: GradientDirection) -> Self {
Self {
colors: colors.iter().map(|&s| s.to_string()).collect(),
direction,
}
}
}