x-graphics 0.2.1

Graphics framework for X
Documentation
use windows::Win32::Graphics::Direct2D::Common::{D2D1_COLOR_F, D2D1_GRADIENT_STOP};

use crate::{
    geometry::FPoint,
    gradient::{GradientBackend, GradientData},
    paint::Color,
    Float,
};

#[derive(Clone, Debug)]
pub struct D2DGradient {
    data: GradientData,
    color_stops: Vec<D2D1_GRADIENT_STOP>,
}

impl GradientBackend for D2DGradient {
    fn new_linear(x1: Float, y1: Float, x2: Float, y2: Float) -> Self {
        Self {
            data: GradientData::Linear((FPoint::new(x1, y1), FPoint::new(x2, y2))),
            color_stops: Vec::new(),
        }
    }

    fn new_radial(x1: Float, y1: Float, x2: Float, y2: Float, r: Float) -> Self {
        Self {
            data: GradientData::Radial((FPoint::new(x1, y1), FPoint::new(x2, y2), r)),
            color_stops: Vec::new(),
        }
    }

    fn add_color_stop(&mut self, offset: Float, color: Color) -> bool {
        if offset.is_nan() || !(0.0..=1.0).contains(&offset) {
            return false;
        }

        let (r, g, b, a) = color.get_float_value();
        self.color_stops.push(D2D1_GRADIENT_STOP {
            position: offset,
            color: D2D1_COLOR_F {
                r,
                g,
                b,
                a,
            },
        });
        true
    }
}

impl D2DGradient {
    pub(super) fn data(&self) -> &GradientData {
        &self.data
    }

    pub(super) fn color_stops(&self) -> &Vec<D2D1_GRADIENT_STOP> {
        &self.color_stops
    }
}