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
use std::convert::From;
#[derive(Clone, Copy, Default, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RoundedRectRadii {
pub top_left: f64,
pub top_right: f64,
pub bottom_right: f64,
pub bottom_left: f64,
}
impl RoundedRectRadii {
pub const fn new(top_left: f64, top_right: f64, bottom_right: f64, bottom_left: f64) -> Self {
RoundedRectRadii {
top_left,
top_right,
bottom_right,
bottom_left,
}
}
pub const fn from_single_radius(radius: f64) -> Self {
RoundedRectRadii {
top_left: radius,
top_right: radius,
bottom_right: radius,
bottom_left: radius,
}
}
pub fn abs(&self) -> Self {
RoundedRectRadii::new(
self.top_left.abs(),
self.top_right.abs(),
self.bottom_right.abs(),
self.bottom_left.abs(),
)
}
pub fn clamp(&self, max: f64) -> Self {
RoundedRectRadii::new(
self.top_left.min(max),
self.top_right.min(max),
self.bottom_right.min(max),
self.bottom_left.min(max),
)
}
pub fn is_finite(&self) -> bool {
self.top_left.is_finite()
&& self.top_right.is_finite()
&& self.bottom_right.is_finite()
&& self.bottom_left.is_finite()
}
pub fn is_nan(&self) -> bool {
self.top_left.is_nan()
|| self.top_right.is_nan()
|| self.bottom_right.is_nan()
|| self.bottom_left.is_nan()
}
pub fn as_single_radius(&self) -> Option<f64> {
let epsilon = 1e-9;
if (self.top_left - self.top_right).abs() < epsilon
&& (self.top_right - self.bottom_right).abs() < epsilon
&& (self.bottom_right - self.bottom_left).abs() < epsilon
{
Some(self.top_left)
} else {
None
}
}
}
impl From<f64> for RoundedRectRadii {
fn from(radius: f64) -> Self {
RoundedRectRadii::from_single_radius(radius)
}
}
impl From<(f64, f64, f64, f64)> for RoundedRectRadii {
fn from(radii: (f64, f64, f64, f64)) -> Self {
RoundedRectRadii::new(radii.0, radii.1, radii.2, radii.3)
}
}