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
152
153
154
use crate::*;

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

unsafe impl Send for SolidColorBrush {}
unsafe impl Sync for SolidColorBrush {}

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

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

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

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

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

impl GradientStopCollection {
    pub(crate) fn new<T>(dc: &ID2D1DeviceContext, 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_WRAP)?
        };
        Ok(Self(collection))
    }
}

unsafe impl Send for GradientStopCollection {}
unsafe impl Sync for GradientStopCollection {}

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

unsafe impl Send for LinearGradientBrush {}
unsafe impl Sync for LinearGradientBrush {}

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

unsafe impl Send for RadialGradientBrush {}
unsafe impl Sync for RadialGradientBrush {}

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

impl Brush {
    #[inline]
    pub(crate) fn solid_color(dc: &ID2D1DeviceContext, color: impl Into<Rgba>) -> Result<Self> {
        let color: D2D1_COLOR_F = Inner(color.into()).into();
        let brush = unsafe { dc.CreateSolidColorBrush(&color, std::ptr::null())? };
        Ok(Self::SolidColor(SolidColorBrush(brush)))
    }

    #[inline]
    pub(crate) fn linear_gradient(
        dc: &ID2D1DeviceContext,
        start: impl Into<Point>,
        end: impl Into<Point>,
        stop_collection: &GradientStopCollection,
    ) -> Result<Self> {
        let start = start.into();
        let end = end.into();
        let brush = unsafe {
            dc.CreateLinearGradientBrush(
                &D2D1_LINEAR_GRADIENT_BRUSH_PROPERTIES {
                    startPoint: Inner(start).into(),
                    endPoint: Inner(end).into(),
                },
                std::ptr::null(),
                &stop_collection.0,
            )?
        };
        Ok(Self::LinearGradient(LinearGradientBrush(brush)))
    }

    #[inline]
    pub(crate) fn radial_gradient(
        dc: &ID2D1DeviceContext,
        center: impl Into<Point>,
        offset: impl Into<Point>,
        radius: impl Into<Vector>,
        stop_collection: &GradientStopCollection,
    ) -> Result<Self> {
        let center = center.into();
        let offset = offset.into();
        let radius = radius.into();
        let brush = unsafe {
            dc.CreateRadialGradientBrush(
                &D2D1_RADIAL_GRADIENT_BRUSH_PROPERTIES {
                    center: Inner(center).into(),
                    gradientOriginOffset: Inner(offset).into(),
                    radiusX: radius.x,
                    radiusY: radius.y,
                },
                std::ptr::null(),
                &stop_collection.0,
            )?
        };
        Ok(Self::RadialGradient(RadialGradientBrush(brush)))
    }

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