ori_core/canvas/
border.rs

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
187
188
/// Radi of the corners on a rounded rectangle.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BorderRadius {
    /// The top left corner radius.
    pub top_left: f32,
    /// The top right corner radius.
    pub top_right: f32,
    /// The bottom right corner radius.
    pub bottom_right: f32,
    /// The bottom left corner radius.
    pub bottom_left: f32,
}

impl BorderRadius {
    /// A [`BorderRadius`] with zero radius on all corners.
    pub const ZERO: Self = Self::all(0.0);

    /// Create a new [`BorderRadius`].
    pub const fn new(top_left: f32, top_right: f32, bottom_right: f32, bottom_left: f32) -> Self {
        Self {
            top_left,
            top_right,
            bottom_right,
            bottom_left,
        }
    }

    /// Create a new [`BorderRadius`] with the same radius on all corners.
    pub const fn all(radius: f32) -> Self {
        Self {
            top_left: radius,
            top_right: radius,
            bottom_right: radius,
            bottom_left: radius,
        }
    }

    /// Get the maximum radius of the corners.
    pub fn max_element(&self) -> f32 {
        self.top_left
            .max(self.top_right)
            .max(self.bottom_right)
            .max(self.bottom_left)
    }

    /// Get the minimum radius of the corners.
    pub fn min_element(&self) -> f32 {
        self.top_left
            .min(self.top_right)
            .min(self.bottom_right)
            .min(self.bottom_left)
    }

    /// Expand the radius of the corners.
    pub fn expand(&self, radius: f32) -> Self {
        Self {
            top_left: self.top_left + radius,
            top_right: self.top_right + radius,
            bottom_right: self.bottom_right + radius,
            bottom_left: self.bottom_left + radius,
        }
    }
}

impl From<(f32, f32, f32, f32)> for BorderRadius {
    fn from((top_left, top_right, bottom_right, bottom_left): (f32, f32, f32, f32)) -> Self {
        Self::new(top_left, top_right, bottom_right, bottom_left)
    }
}

impl From<[f32; 4]> for BorderRadius {
    fn from([top_left, top_right, bottom_right, bottom_left]: [f32; 4]) -> Self {
        Self::new(top_left, top_right, bottom_right, bottom_left)
    }
}

impl From<f32> for BorderRadius {
    fn from(radius: f32) -> Self {
        Self::all(radius)
    }
}

impl From<BorderRadius> for [f32; 4] {
    fn from(radius: BorderRadius) -> Self {
        [
            radius.top_left,
            radius.top_right,
            radius.bottom_right,
            radius.bottom_left,
        ]
    }
}

/// The border width of a rounded rectangle.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BorderWidth {
    /// The top border width.
    pub top: f32,
    /// The right border width.
    pub right: f32,
    /// The bottom border width.
    pub bottom: f32,
    /// The left border width.
    pub left: f32,
}

impl BorderWidth {
    /// A [`BorderWidth`] with zero width on all borders.
    pub const ZERO: Self = Self::all(0.0);

    /// Create a new [`BorderWidth`].
    pub const fn new(top: f32, right: f32, bottom: f32, left: f32) -> Self {
        Self {
            top,
            right,
            bottom,
            left,
        }
    }

    /// Create a new [`BorderWidth`] with the same width on all borders.
    pub const fn all(width: f32) -> Self {
        Self {
            top: width,
            right: width,
            bottom: width,
            left: width,
        }
    }

    /// Get the maximum width of the borders.
    pub fn max_element(&self) -> f32 {
        self.top.max(self.right).max(self.bottom).max(self.left)
    }

    /// Get the minimum width of the borders.
    pub fn min_element(&self) -> f32 {
        self.top.min(self.right).min(self.bottom).min(self.left)
    }

    /// Expand the width of the borders.
    pub fn expand(&self, width: f32) -> Self {
        Self {
            top: self.top + width,
            right: self.right + width,
            bottom: self.bottom + width,
            left: self.left + width,
        }
    }
}

impl From<(f32, f32, f32, f32)> for BorderWidth {
    fn from((top, right, bottom, left): (f32, f32, f32, f32)) -> Self {
        Self::new(top, right, bottom, left)
    }
}

impl From<[f32; 4]> for BorderWidth {
    fn from([top, right, bottom, left]: [f32; 4]) -> Self {
        Self::new(top, right, bottom, left)
    }
}

impl From<(f32, f32)> for BorderWidth {
    fn from((horizontal, vertical): (f32, f32)) -> Self {
        Self::new(vertical, horizontal, vertical, horizontal)
    }
}

impl From<[f32; 2]> for BorderWidth {
    fn from([horizontal, vertical]: [f32; 2]) -> Self {
        Self::new(vertical, horizontal, vertical, horizontal)
    }
}

impl From<f32> for BorderWidth {
    fn from(width: f32) -> Self {
        Self::all(width)
    }
}

impl From<BorderWidth> for [f32; 4] {
    fn from(width: BorderWidth) -> Self {
        [width.top, width.right, width.bottom, width.left]
    }
}