1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use crate::utility::*;
use crate::*;
use windows::core::CanInto;
use windows::Win32::Graphics::Direct2D::*;

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct SolidColorBrush(ID2D1SolidColorBrush);

#[derive(Clone, Copy, Debug)]
pub struct GradientStop {
    pub position: f32,
    pub color: Rgba<f32>,
}

impl GradientStop {
    #[inline]
    pub fn new(position: f32, color: impl Into<Rgba<f32>>) -> Self {
        Self {
            position,
            color: color.into(),
        }
    }
}

impl From<GradientStop> for D2D1_GRADIENT_STOP {
    #[inline]
    fn from(src: GradientStop) -> Self {
        Self {
            position: src.position,
            color: Wrapper(src.color).into(),
        }
    }
}

impl<T> From<(f32, T)> for GradientStop
where
    T: Into<Rgba<f32>>,
{
    #[inline]
    fn from(src: (f32, T)) -> Self {
        Self {
            position: src.0,
            color: src.1.into(),
        }
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u32)]
pub enum GradientMode {
    Clamp = D2D1_EXTEND_MODE_CLAMP.0,
    Mirror = D2D1_EXTEND_MODE_MIRROR.0,
    Wrap = D2D1_EXTEND_MODE_WRAP.0,
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct GradientStopCollection(ID2D1GradientStopCollection);

impl GradientStopCollection {
    pub(crate) fn new<T>(dc: &ID2D1DeviceContext5, mode: GradientMode, stops: &[T]) -> Result<Self>
    where
        T: Into<GradientStop> + Clone,
    {
        let stops = stops
            .iter()
            .cloned()
            .map(|stop| stop.into().into())
            .collect::<Vec<_>>();
        let collection = unsafe {
            dc.CreateGradientStopCollection(&stops, D2D1_GAMMA_2_2, D2D1_EXTEND_MODE(mode as u32))?
        };
        Ok(Self(collection))
    }
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct LinearGradientBrush(ID2D1LinearGradientBrush);

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct RadialGradientBrush(ID2D1RadialGradientBrush);

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Brush {
    SolidColor(SolidColorBrush),
    LinearGradient(LinearGradientBrush),
    RadialGradient(RadialGradientBrush),
}

impl Brush {
    pub(crate) fn solid_color(
        dc: &ID2D1DeviceContext5,
        color: impl Into<Rgba<f32>>,
    ) -> Result<Self> {
        let color = Wrapper(color.into()).into();
        let brush = unsafe { dc.CreateSolidColorBrush(&color, None)? };
        Ok(Self::SolidColor(SolidColorBrush(brush)))
    }

    pub(crate) fn linear_gradient(
        dc: &ID2D1DeviceContext5,
        start: impl Into<Point<f32>>,
        end: impl Into<Point<f32>>,
        stops: &GradientStopCollection,
    ) -> Result<Self> {
        let start = start.into();
        let end = end.into();
        let brush = unsafe {
            dc.CreateLinearGradientBrush(
                &D2D1_LINEAR_GRADIENT_BRUSH_PROPERTIES {
                    startPoint: Wrapper(start).into(),
                    endPoint: Wrapper(end).into(),
                },
                None,
                &stops.0,
            )?
        };
        Ok(Self::LinearGradient(LinearGradientBrush(brush)))
    }

    pub(crate) fn radial_gradient(
        dc: &ID2D1DeviceContext5,
        ellipse: impl Into<Ellipse>,
        offset: impl Into<Point<f32>>,
        stops: &GradientStopCollection,
    ) -> Result<Self> {
        let ellipse = ellipse.into();
        let offset = offset.into();
        let brush = unsafe {
            dc.CreateRadialGradientBrush(
                &D2D1_RADIAL_GRADIENT_BRUSH_PROPERTIES {
                    center: Wrapper(ellipse.center).into(),
                    radiusX: ellipse.radius.x,
                    radiusY: ellipse.radius.y,
                    gradientOriginOffset: Wrapper(offset).into(),
                },
                None,
                &stops.0,
            )?
        };
        Ok(Self::RadialGradient(RadialGradientBrush(brush)))
    }

    #[inline]
    pub(crate) fn handle(&self) -> ID2D1Brush {
        match self {
            Self::SolidColor(b) => b.0.can_clone_into(),
            Self::LinearGradient(b) => b.0.can_clone_into(),
            Self::RadialGradient(b) => b.0.can_clone_into(),
        }
    }
}