1use strata_primitives::geom::Point;
4
5#[derive(Debug, Clone, Copy, PartialEq)]
7pub enum FilterOp {
8 Blur(f32),
10 Brightness(f32),
12 Contrast(f32),
14 Grayscale(f32),
16 Invert(f32),
18 Opacity(f32),
20 Saturate(f32),
22 Sepia(f32),
24 DropShadow {
26 offset_x: f32,
27 offset_y: f32,
28 blur_radius: f32,
29 color: crate::types::Color,
30 },
31 HueRotate(f32),
33}
34
35pub type FilterList = BackdropFilter;
37
38#[derive(Debug, Clone, PartialEq, Default)]
40pub struct BackdropFilter {
41 pub operations: Vec<FilterOp>,
42}
43
44impl BackdropFilter {
45 pub fn new(operations: Vec<FilterOp>) -> Self {
46 Self { operations }
47 }
48
49 pub fn blur(radius: f32) -> Self {
51 Self {
52 operations: vec![FilterOp::Blur(radius)],
53 }
54 }
55
56 pub fn is_empty(&self) -> bool {
57 self.operations.is_empty()
58 }
59}
60
61#[derive(Debug, Clone, PartialEq, Default)]
63pub enum ClipPath {
64 #[default]
65 None,
66 Inset {
68 top: f32,
69 right: f32,
70 bottom: f32,
71 left: f32,
72 border_radius: f32,
73 },
74 Circle {
76 radius: f32,
77 center_x: f32,
78 center_y: f32,
79 },
80 Ellipse {
82 radius_x: f32,
83 radius_y: f32,
84 center_x: f32,
85 center_y: f32,
86 },
87 Polygon(Vec<Point<f32>>),
89 Path(String),
91}
92
93impl ClipPath {
94 pub fn inset(top: f32, right: f32, bottom: f32, left: f32, border_radius: f32) -> Self {
95 Self::Inset {
96 top,
97 right,
98 bottom,
99 left,
100 border_radius,
101 }
102 }
103
104 pub fn circle(radius: f32, center_x: f32, center_y: f32) -> Self {
105 Self::Circle {
106 radius,
107 center_x,
108 center_y,
109 }
110 }
111
112 pub fn ellipse(radius_x: f32, radius_y: f32, center_x: f32, center_y: f32) -> Self {
113 Self::Ellipse {
114 radius_x,
115 radius_y,
116 center_x,
117 center_y,
118 }
119 }
120
121 pub fn polygon(vertices: Vec<Point<f32>>) -> Self {
122 Self::Polygon(vertices)
123 }
124
125 pub fn path(path_str: impl Into<String>) -> Self {
126 Self::Path(path_str.into())
127 }
128
129 pub fn is_none(&self) -> bool {
130 matches!(self, ClipPath::None)
131 }
132
133 pub fn contains_point(&self, x: f32, y: f32, container_w: f32, container_h: f32) -> bool {
135 match self {
136 ClipPath::None => true,
137 ClipPath::Inset {
138 top,
139 right,
140 bottom,
141 left,
142 ..
143 } => {
144 x >= *left
145 && x <= (container_w - *right)
146 && y >= *top
147 && y <= (container_h - *bottom)
148 }
149 ClipPath::Circle {
150 radius,
151 center_x,
152 center_y,
153 } => {
154 let dx = x - *center_x;
155 let dy = y - *center_y;
156 (dx * dx + dy * dy) <= (radius * radius)
157 }
158 ClipPath::Ellipse {
159 radius_x,
160 radius_y,
161 center_x,
162 center_y,
163 } => {
164 if *radius_x <= 0.0 || *radius_y <= 0.0 {
165 return false;
166 }
167 let norm_x = (x - *center_x) / radius_x;
168 let norm_y = (y - *center_y) / radius_y;
169 (norm_x * norm_x + norm_y * norm_y) <= 1.0
170 }
171 ClipPath::Polygon(vertices) => {
172 if vertices.len() < 3 {
173 return true;
174 }
175 let mut inside = false;
177 let mut j = vertices.len() - 1;
178 for i in 0..vertices.len() {
179 let pi = vertices[i];
180 let pj = vertices[j];
181
182 if ((pi.y > y) != (pj.y > y))
183 && (x < (pj.x - pi.x) * (y - pi.y) / (pj.y - pi.y + 1e-6) + pi.x)
184 {
185 inside = !inside;
186 }
187 j = i;
188 }
189 inside
190 }
191 ClipPath::Path(_) => true, }
193 }
194}
195
196pub fn interpolate_clip_path(from: &ClipPath, to: &ClipPath, t: f32) -> ClipPath {
198 let t = t.clamp(0.0, 1.0);
199 match (from, to) {
200 (
201 ClipPath::Inset {
202 top: t1,
203 right: r1,
204 bottom: b1,
205 left: l1,
206 border_radius: br1,
207 },
208 ClipPath::Inset {
209 top: t2,
210 right: r2,
211 bottom: b2,
212 left: l2,
213 border_radius: br2,
214 },
215 ) => ClipPath::Inset {
216 top: t1 + (t2 - t1) * t,
217 right: r1 + (r2 - r1) * t,
218 bottom: b1 + (b2 - b1) * t,
219 left: l1 + (l2 - l1) * t,
220 border_radius: br1 + (br2 - br1) * t,
221 },
222 (
223 ClipPath::Circle {
224 radius: r1,
225 center_x: x1,
226 center_y: y1,
227 },
228 ClipPath::Circle {
229 radius: r2,
230 center_x: x2,
231 center_y: y2,
232 },
233 ) => ClipPath::Circle {
234 radius: r1 + (r2 - r1) * t,
235 center_x: x1 + (x2 - x1) * t,
236 center_y: y1 + (y2 - y1) * t,
237 },
238 (
239 ClipPath::Ellipse {
240 radius_x: rx1,
241 radius_y: ry1,
242 center_x: cx1,
243 center_y: cy1,
244 },
245 ClipPath::Ellipse {
246 radius_x: rx2,
247 radius_y: ry2,
248 center_x: cx2,
249 center_y: cy2,
250 },
251 ) => ClipPath::Ellipse {
252 radius_x: rx1 + (rx2 - rx1) * t,
253 radius_y: ry1 + (ry2 - ry1) * t,
254 center_x: cx1 + (cx2 - cx1) * t,
255 center_y: cy1 + (cy2 - cy1) * t,
256 },
257 (ClipPath::Polygon(v1), ClipPath::Polygon(v2)) => {
258 if v1.len() == v2.len() {
259 let mut out = Vec::with_capacity(v1.len());
260 for (p1, p2) in v1.iter().zip(v2.iter()) {
261 out.push(Point::new(
262 p1.x + (p2.x - p1.x) * t,
263 p1.y + (p2.y - p1.y) * t,
264 ));
265 }
266 ClipPath::Polygon(out)
267 } else if t < 0.5 {
268 from.clone()
269 } else {
270 to.clone()
271 }
272 }
273 _ => {
274 if t < 0.5 {
275 from.clone()
276 } else {
277 to.clone()
278 }
279 }
280 }
281}