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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use bytemuck::{Pod, Zeroable};

use rootvg_core::color::PackedSrgb;
use rootvg_core::math::{Point, Rect, Size};

use crate::border::Border;
use crate::shadow::Shadow;
use crate::Radius;

/// A quad primitive with a solid background.
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct SolidQuad {
    /// The bounds of the quad in logical points.
    pub bounds: Rect,
    /// The background color of the quad
    pub bg_color: PackedSrgb,
    /// The [`Border`] of the quad
    pub border: Border,
    /// The shadow of the quad
    pub shadow: Shadow,
}

impl SolidQuad {
    pub fn packed(&self) -> SolidQuadPrimitive {
        SolidQuadPrimitive {
            color: self.bg_color,
            position: self.bounds.origin.into(),
            size: self.bounds.size.into(),
            border_color: self.border.color,
            border_radius: self.border.radius.into(),
            border_width: self.border.width,
            shadow_color: self.shadow.color,
            shadow_offset: self.shadow.offset.into(),
            shadow_blur_radius: self.shadow.blur_radius,
        }
    }

    pub fn builder(size: Size) -> SolidQuadBuilder {
        SolidQuadBuilder::new(size)
    }
}

pub struct SolidQuadBuilder {
    quad: SolidQuad,
}

impl SolidQuadBuilder {
    pub fn new(size: Size) -> Self {
        Self {
            quad: SolidQuad {
                bounds: Rect {
                    origin: Point::new(0.0, 0.0),
                    size,
                },
                ..Default::default()
            },
        }
    }

    pub fn position(mut self, position: Point) -> Self {
        self.quad.bounds.origin = position;
        self
    }

    pub fn bg_color(mut self, color: impl Into<PackedSrgb>) -> Self {
        self.quad.bg_color = color.into();
        self
    }

    pub fn border_color(mut self, color: impl Into<PackedSrgb>) -> Self {
        self.quad.border.color = color.into();
        self
    }

    pub fn border_width(mut self, width: f32) -> Self {
        self.quad.border.width = width;
        self
    }

    pub fn border_radius(mut self, radius: impl Into<Radius>) -> Self {
        self.quad.border.radius = radius.into();
        self
    }

    pub fn border(mut self, border: Border) -> Self {
        self.quad.border = border;
        self
    }

    pub fn shadow_color(mut self, color: impl Into<PackedSrgb>) -> Self {
        self.quad.shadow.color = color.into();
        self
    }

    pub fn shadow_offset(mut self, offset: Point) -> Self {
        self.quad.shadow.offset = offset;
        self
    }

    pub fn shadow_blur_radius(mut self, blur_radius: f32) -> Self {
        self.quad.shadow.blur_radius = blur_radius;
        self
    }

    pub fn shadow(mut self, shadow: Shadow) -> Self {
        self.quad.shadow = shadow;
        self
    }

    pub fn build(self) -> SolidQuad {
        self.quad
    }
}

/// A quad primitive with a solid background, packed into a format for
/// use in rendering.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct SolidQuadPrimitive {
    /// The background color data of the quad.
    pub color: PackedSrgb,

    /// The position of the [`Quad`] in logical points.
    pub position: [f32; 2],

    /// The size of the [`Quad`] in logical points.
    pub size: [f32; 2],

    /// The border color of the [`Quad`], in __linear RGB__.
    pub border_color: PackedSrgb,

    /// The border radii of the [`Quad`] in logical points.
    pub border_radius: [f32; 4],

    /// The border width of the [`Quad`] in logical points.
    pub border_width: f32,

    /// The shadow color of the [`Quad`].
    pub shadow_color: PackedSrgb,

    /// The shadow offset of the [`Quad`] in logical points.
    pub shadow_offset: [f32; 2],

    /// The shadow blur radius of the [`Quad`] in logical points.
    pub shadow_blur_radius: f32,
}

impl SolidQuadPrimitive {
    pub fn new(quad: &SolidQuad) -> Self {
        Self {
            color: quad.bg_color,
            position: quad.bounds.origin.into(),
            size: quad.bounds.size.into(),
            border_color: quad.border.color,
            border_radius: quad.border.radius.into(),
            border_width: quad.border.width,
            shadow_color: quad.shadow.color,
            shadow_offset: quad.shadow.offset.into(),
            shadow_blur_radius: quad.shadow.blur_radius,
        }
    }
}

impl From<SolidQuad> for SolidQuadPrimitive {
    fn from(q: SolidQuad) -> SolidQuadPrimitive {
        q.packed()
    }
}

impl<'a> From<&'a SolidQuad> for SolidQuadPrimitive {
    fn from(q: &'a SolidQuad) -> SolidQuadPrimitive {
        q.packed()
    }
}

impl From<SolidQuadBuilder> for SolidQuadPrimitive {
    fn from(q: SolidQuadBuilder) -> SolidQuadPrimitive {
        q.build().packed()
    }
}

impl From<SolidQuadBuilder> for SolidQuad {
    fn from(q: SolidQuadBuilder) -> SolidQuad {
        q.build()
    }
}