kurbo/
rounded_rect_radii.rs1use core::convert::From;
7
8#[allow(unused_imports)] #[cfg(not(feature = "std"))]
10use crate::common::FloatFuncs;
11
12#[derive(Clone, Copy, Default, Debug, PartialEq)]
21#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
22#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
23pub struct RoundedRectRadii {
24 pub top_left: f64,
26 pub top_right: f64,
28 pub bottom_right: f64,
30 pub bottom_left: f64,
32}
33
34impl RoundedRectRadii {
35 #[inline(always)]
39 pub const fn new(top_left: f64, top_right: f64, bottom_right: f64, bottom_left: f64) -> Self {
40 RoundedRectRadii {
41 top_left,
42 top_right,
43 bottom_right,
44 bottom_left,
45 }
46 }
47
48 #[inline(always)]
51 pub const fn from_single_radius(radius: f64) -> Self {
52 RoundedRectRadii {
53 top_left: radius,
54 top_right: radius,
55 bottom_right: radius,
56 bottom_left: radius,
57 }
58 }
59
60 pub fn abs(&self) -> Self {
62 RoundedRectRadii::new(
63 self.top_left.abs(),
64 self.top_right.abs(),
65 self.bottom_right.abs(),
66 self.bottom_left.abs(),
67 )
68 }
69
70 pub fn clamp(&self, max: f64) -> Self {
72 RoundedRectRadii::new(
73 self.top_left.min(max),
74 self.top_right.min(max),
75 self.bottom_right.min(max),
76 self.bottom_left.min(max),
77 )
78 }
79
80 pub fn is_finite(&self) -> bool {
82 self.top_left.is_finite()
83 && self.top_right.is_finite()
84 && self.bottom_right.is_finite()
85 && self.bottom_left.is_finite()
86 }
87
88 pub fn is_nan(&self) -> bool {
90 self.top_left.is_nan()
91 || self.top_right.is_nan()
92 || self.bottom_right.is_nan()
93 || self.bottom_left.is_nan()
94 }
95
96 pub fn as_single_radius(&self) -> Option<f64> {
99 let epsilon = 1e-9;
100
101 if (self.top_left - self.top_right).abs() < epsilon
102 && (self.top_right - self.bottom_right).abs() < epsilon
103 && (self.bottom_right - self.bottom_left).abs() < epsilon
104 {
105 Some(self.top_left)
106 } else {
107 None
108 }
109 }
110}
111
112impl From<f64> for RoundedRectRadii {
113 #[inline(always)]
114 fn from(radius: f64) -> Self {
115 RoundedRectRadii::from_single_radius(radius)
116 }
117}
118
119impl From<(f64, f64, f64, f64)> for RoundedRectRadii {
120 #[inline(always)]
121 fn from(radii: (f64, f64, f64, f64)) -> Self {
122 RoundedRectRadii::new(radii.0, radii.1, radii.2, radii.3)
123 }
124}