1use crate::arena::StrRef;
11use crate::solver::Region;
12use crate::style::Color;
13use crate::svg::math::{
14 abs_f32, atan2_f32, cbrt_f32, floor_f32, linear_to_srgb_f32, sin_cos_f32, sqrt_f32,
15 srgb_to_linear_f32, PI, TAU,
16};
17pub use crate::svg::Transform2D;
18
19#[inline(always)]
21pub(crate) fn canonical_f32_bits(v: f32) -> u32 {
22 if v == 0.0 {
23 0
24 } else if v.is_nan() {
25 0x7FC0_0000
26 } else {
27 v.to_bits()
28 }
29}
30
31#[inline(always)]
32pub(crate) fn mix_u8(h: &mut u32, b: u8) {
33 *h ^= b as u32;
34 *h = h.wrapping_mul(0x0100_0193);
35}
36
37#[inline(always)]
38pub(crate) fn mix_u32(h: &mut u32, v: u32) {
39 for b in v.to_le_bytes() {
40 mix_u8(h, b);
41 }
42}
43
44#[inline(always)]
45pub(crate) fn mix_f32(h: &mut u32, v: f32) {
46 mix_u32(h, canonical_f32_bits(v));
47}
48
49#[inline(always)]
50pub(crate) fn mix_transform(h: &mut u32, t: Transform2D) {
51 mix_f32(h, t.a);
52 mix_f32(h, t.b);
53 mix_f32(h, t.c);
54 mix_f32(h, t.d);
55 mix_f32(h, t.e);
56 mix_f32(h, t.f);
57}
58
59#[repr(C)]
65#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Hash)]
66pub struct CornerRadii {
67 pub tl: u8,
68 pub tr: u8,
69 pub br: u8,
70 pub bl: u8,
71}
72
73impl CornerRadii {
74 pub const ZERO: Self = Self::all(0);
76
77 #[inline(always)]
79 pub const fn all(r: u8) -> Self {
80 Self {
81 tl: r,
82 tr: r,
83 br: r,
84 bl: r,
85 }
86 }
87
88 #[inline(always)]
90 pub const fn new(tl: u8, tr: u8, br: u8, bl: u8) -> Self {
91 Self { tl, tr, br, bl }
92 }
93
94 #[inline(always)]
96 pub const fn top(r: u8) -> Self {
97 Self::new(r, r, 0, 0)
98 }
99
100 #[inline(always)]
102 pub const fn bottom(r: u8) -> Self {
103 Self::new(0, 0, r, r)
104 }
105
106 #[inline(always)]
108 pub const fn left(r: u8) -> Self {
109 Self::new(r, 0, 0, r)
110 }
111
112 #[inline(always)]
114 pub const fn right(r: u8) -> Self {
115 Self::new(0, r, r, 0)
116 }
117
118 #[inline(always)]
120 pub const fn is_uniform(self) -> bool {
121 self.tl == self.tr && self.tr == self.br && self.br == self.bl
122 }
123
124 #[inline(always)]
126 pub const fn is_zero(self) -> bool {
127 self.tl == 0 && self.tr == 0 && self.br == 0 && self.bl == 0
128 }
129
130 #[inline(always)]
132 pub const fn max_radius(self) -> u8 {
133 let a = if self.tl > self.tr { self.tl } else { self.tr };
134 let b = if self.br > self.bl { self.br } else { self.bl };
135 if a > b { a } else { b }
136 }
137
138 #[inline(always)]
140 pub const fn as_array(self) -> [u8; 4] {
141 [self.tl, self.tr, self.br, self.bl]
142 }
143
144 #[inline]
146 pub fn inverse_scaled(self, scale_x: f32, scale_y: f32) -> Self {
147 let eff_scale = scale_x.abs().min(scale_y.abs()).clamp(0.15, 8.0);
148 let inv = 1.0 / eff_scale;
149 let map = |r: u8| -> u8 { ((r as f32 * inv + 0.5) as i32).clamp(0, u8::MAX as i32) as u8 };
150 Self {
151 tl: map(self.tl),
152 tr: map(self.tr),
153 br: map(self.br),
154 bl: map(self.bl),
155 }
156 }
157}
158
159#[inline]
162pub fn compute_flip_transform(
163 layout_bounds: Region,
164 visual_bounds: Region,
165) -> Option<(Transform2D, f32, f32)> {
166 if layout_bounds == visual_bounds || layout_bounds.w <= 0 || layout_bounds.h <= 0 {
167 return None;
168 }
169 let lw = layout_bounds.w as f32;
170 let lh = layout_bounds.h as f32;
171 let vw = (visual_bounds.w.max(1)) as f32;
172 let vh = (visual_bounds.h.max(1)) as f32;
173 let sx = (vw / lw).clamp(0.05, 16.0);
174 let sy = (vh / lh).clamp(0.05, 16.0);
175 let lcx = (layout_bounds.x as f32) + lw * 0.5;
176 let lcy = (layout_bounds.y as f32) + lh * 0.5;
177 let vcx = (visual_bounds.x as f32) + vw * 0.5;
178 let vcy = (visual_bounds.y as f32) + vh * 0.5;
179 let tx = vcx - lcx;
180 let ty = vcy - lcy;
181 Some((
182 Transform2D::new(sx, 0.0, 0.0, sy, tx, ty),
183 sx,
184 sy,
185 ))
186}
187
188#[derive(Copy, Clone, Debug)]
194pub struct ColorStop {
195 pub offset: f32,
196 pub color: Color,
197}
198
199impl ColorStop {
200 #[inline(always)]
202 pub const fn new(offset: f32, color: Color) -> Self {
203 Self { offset, color }
204 }
205}
206
207impl PartialEq for ColorStop {
208 #[inline]
209 fn eq(&self, other: &Self) -> bool {
210 canonical_f32_bits(self.offset) == canonical_f32_bits(other.offset)
211 && self.color == other.color
212 }
213}
214
215impl Eq for ColorStop {}
216
217#[repr(u8)]
219#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Hash)]
220pub enum ExtendMode {
221 #[default]
223 Pad = 0,
224 Repeat = 1,
226 Reflect = 2,
228}
229
230#[repr(u8)]
232#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Hash)]
233pub enum GradientSpace {
234 #[default]
236 Srgb = 0,
237 LinearRgb = 1,
239 Oklab = 2,
241}
242
243#[derive(Copy, Clone, Debug)]
245pub enum Paint<'a> {
246 Solid(Color),
248 LinearGradient {
250 start: [f32; 2],
251 end: [f32; 2],
252 stops: &'a [ColorStop],
253 extend: ExtendMode,
254 space: GradientSpace,
255 },
256 RadialGradient {
258 center: [f32; 2],
259 radius: f32,
260 focal: [f32; 2],
261 focal_radius: f32,
262 stops: &'a [ColorStop],
263 extend: ExtendMode,
264 space: GradientSpace,
265 },
266 ConicGradient {
268 center: [f32; 2],
269 start_angle_deg: f32,
270 sweep_deg: f32,
271 stops: &'a [ColorStop],
272 extend: ExtendMode,
273 space: GradientSpace,
274 },
275}
276
277impl<'a> PartialEq for Paint<'a> {
278 fn eq(&self, other: &Self) -> bool {
279 match (*self, *other) {
280 (Self::Solid(a), Self::Solid(b)) => a == b,
281 (
282 Self::LinearGradient {
283 start: s1,
284 end: e1,
285 stops: st1,
286 extend: ex1,
287 space: sp1,
288 },
289 Self::LinearGradient {
290 start: s2,
291 end: e2,
292 stops: st2,
293 extend: ex2,
294 space: sp2,
295 },
296 ) => {
297 canonical_f32_bits(s1[0]) == canonical_f32_bits(s2[0])
298 && canonical_f32_bits(s1[1]) == canonical_f32_bits(s2[1])
299 && canonical_f32_bits(e1[0]) == canonical_f32_bits(e2[0])
300 && canonical_f32_bits(e1[1]) == canonical_f32_bits(e2[1])
301 && ex1 == ex2
302 && sp1 == sp2
303 && st1 == st2
304 }
305 (
306 Self::RadialGradient {
307 center: c1,
308 radius: r1,
309 focal: f1,
310 focal_radius: fr1,
311 stops: st1,
312 extend: ex1,
313 space: sp1,
314 },
315 Self::RadialGradient {
316 center: c2,
317 radius: r2,
318 focal: f2,
319 focal_radius: fr2,
320 stops: st2,
321 extend: ex2,
322 space: sp2,
323 },
324 ) => {
325 canonical_f32_bits(c1[0]) == canonical_f32_bits(c2[0])
326 && canonical_f32_bits(c1[1]) == canonical_f32_bits(c2[1])
327 && canonical_f32_bits(r1) == canonical_f32_bits(r2)
328 && canonical_f32_bits(f1[0]) == canonical_f32_bits(f2[0])
329 && canonical_f32_bits(f1[1]) == canonical_f32_bits(f2[1])
330 && canonical_f32_bits(fr1) == canonical_f32_bits(fr2)
331 && ex1 == ex2
332 && sp1 == sp2
333 && st1 == st2
334 }
335 (
336 Self::ConicGradient {
337 center: c1,
338 start_angle_deg: a1,
339 sweep_deg: sw1,
340 stops: st1,
341 extend: ex1,
342 space: sp1,
343 },
344 Self::ConicGradient {
345 center: c2,
346 start_angle_deg: a2,
347 sweep_deg: sw2,
348 stops: st2,
349 extend: ex2,
350 space: sp2,
351 },
352 ) => {
353 canonical_f32_bits(c1[0]) == canonical_f32_bits(c2[0])
354 && canonical_f32_bits(c1[1]) == canonical_f32_bits(c2[1])
355 && canonical_f32_bits(a1) == canonical_f32_bits(a2)
356 && canonical_f32_bits(sw1) == canonical_f32_bits(sw2)
357 && ex1 == ex2
358 && sp1 == sp2
359 && st1 == st2
360 }
361 _ => false,
362 }
363 }
364}
365
366impl<'a> Eq for Paint<'a> {}
367
368impl<'a> Paint<'a> {
369 #[inline]
371 pub fn linear_angle(angle_deg: f32, stops: &'a [ColorStop]) -> Self {
372 let rad = angle_deg * (PI / 180.0);
373 let (sin_a, cos_a) = sin_cos_f32(rad);
374 let dx = sin_a;
376 let dy = -cos_a;
377 let half_len = 0.5 * (abs_f32(dx) + abs_f32(dy));
378 let hx = dx * half_len;
379 let hy = dy * half_len;
380 Self::LinearGradient {
381 start: [0.5 - hx, 0.5 - hy],
382 end: [0.5 + hx, 0.5 + hy],
383 stops,
384 extend: ExtendMode::Pad,
385 space: GradientSpace::Srgb,
386 }
387 }
388
389 #[inline]
391 pub const fn radial(stops: &'a [ColorStop]) -> Self {
392 Self::RadialGradient {
393 center: [0.5, 0.5],
394 radius: 0.5,
395 focal: [0.5, 0.5],
396 focal_radius: 0.0,
397 stops,
398 extend: ExtendMode::Pad,
399 space: GradientSpace::Srgb,
400 }
401 }
402
403 #[inline]
405 pub const fn radial_focal(
406 center: [f32; 2],
407 radius: f32,
408 focal: [f32; 2],
409 focal_radius: f32,
410 stops: &'a [ColorStop],
411 ) -> Self {
412 Self::RadialGradient {
413 center,
414 radius,
415 focal,
416 focal_radius,
417 stops,
418 extend: ExtendMode::Pad,
419 space: GradientSpace::Srgb,
420 }
421 }
422
423 #[inline]
425 pub const fn conic(start_angle_deg: f32, stops: &'a [ColorStop]) -> Self {
426 Self::ConicGradient {
427 center: [0.5, 0.5],
428 start_angle_deg,
429 sweep_deg: 360.0,
430 stops,
431 extend: ExtendMode::Pad,
432 space: GradientSpace::Srgb,
433 }
434 }
435
436 #[inline]
438 pub const fn with_extend(mut self, mode: ExtendMode) -> Self {
439 match &mut self {
440 Self::LinearGradient { extend, .. }
441 | Self::RadialGradient { extend, .. }
442 | Self::ConicGradient { extend, .. } => *extend = mode,
443 Self::Solid(_) => {}
444 }
445 self
446 }
447
448 #[inline]
450 pub const fn with_space(mut self, color_space: GradientSpace) -> Self {
451 match &mut self {
452 Self::LinearGradient { space, .. }
453 | Self::RadialGradient { space, .. }
454 | Self::ConicGradient { space, .. } => *space = color_space,
455 Self::Solid(_) => {}
456 }
457 self
458 }
459
460 #[inline]
462 pub fn is_fully_transparent(&self) -> bool {
463 match *self {
464 Self::Solid(c) => c.a() == 0,
465 Self::LinearGradient { stops, .. }
466 | Self::RadialGradient { stops, .. }
467 | Self::ConicGradient { stops, .. } => {
468 stops.is_empty() || stops.iter().all(|s| s.color.a() == 0)
469 }
470 }
471 }
472
473 #[inline]
475 pub fn is_fully_opaque(&self) -> bool {
476 match *self {
477 Self::Solid(c) => c.a() == 255,
478 Self::LinearGradient { stops, .. }
479 | Self::RadialGradient { stops, .. }
480 | Self::ConicGradient { stops, .. } => {
481 !stops.is_empty() && stops.iter().all(|s| s.color.a() == 255)
482 }
483 }
484 }
485
486 pub fn mix_hash(&self, h: &mut u32) {
488 match *self {
489 Self::Solid(c) => {
490 mix_u8(h, 1);
491 mix_u32(h, c.0);
492 }
493 Self::LinearGradient {
494 start,
495 end,
496 stops,
497 extend,
498 space,
499 } => {
500 mix_u8(h, 2);
501 mix_f32(h, start[0]);
502 mix_f32(h, start[1]);
503 mix_f32(h, end[0]);
504 mix_f32(h, end[1]);
505 mix_u8(h, extend as u8);
506 mix_u8(h, space as u8);
507 mix_stops_hash(h, stops);
508 }
509 Self::RadialGradient {
510 center,
511 radius,
512 focal,
513 focal_radius,
514 stops,
515 extend,
516 space,
517 } => {
518 mix_u8(h, 3);
519 mix_f32(h, center[0]);
520 mix_f32(h, center[1]);
521 mix_f32(h, radius);
522 mix_f32(h, focal[0]);
523 mix_f32(h, focal[1]);
524 mix_f32(h, focal_radius);
525 mix_u8(h, extend as u8);
526 mix_u8(h, space as u8);
527 mix_stops_hash(h, stops);
528 }
529 Self::ConicGradient {
530 center,
531 start_angle_deg,
532 sweep_deg,
533 stops,
534 extend,
535 space,
536 } => {
537 mix_u8(h, 4);
538 mix_f32(h, center[0]);
539 mix_f32(h, center[1]);
540 mix_f32(h, start_angle_deg);
541 mix_f32(h, sweep_deg);
542 mix_u8(h, extend as u8);
543 mix_u8(h, space as u8);
544 mix_stops_hash(h, stops);
545 }
546 }
547 }
548
549 #[inline]
551 pub fn eval_gradient_t(&self, u: f32, v: f32) -> Option<f32> {
552 match *self {
553 Self::Solid(_) => Some(0.0),
554 Self::LinearGradient {
555 start, end, extend, ..
556 } => {
557 let dx = end[0] - start[0];
558 let dy = end[1] - start[1];
559 let denom = dx * dx + dy * dy;
560 if denom < 1e-8 {
561 return Some(0.0);
562 }
563 let t = ((u - start[0]) * dx + (v - start[1]) * dy) / denom;
564 Some(apply_extend(t, extend))
565 }
566 Self::RadialGradient {
567 center,
568 radius,
569 focal,
570 focal_radius,
571 extend,
572 ..
573 } => {
574 let r1 = radius.max(1e-5);
575 let r0 = focal_radius.max(0.0);
576 let cdx = center[0] - focal[0];
577 let cdy = center[1] - focal[1];
578 if abs_f32(cdx) < 1e-6 && abs_f32(cdy) < 1e-6 && r0 < 1e-6 {
579 let dx = u - center[0];
580 let dy = v - center[1];
581 let t = sqrt_f32(dx * dx + dy * dy) / r1;
582 return Some(apply_extend(t, extend));
583 }
584 let px = u - focal[0];
586 let py = v - focal[1];
587 let dr = r1 - r0;
588 let a = cdx * cdx + cdy * cdy - dr * dr;
589 let b = -2.0 * (px * cdx + py * cdy + r0 * dr);
590 let c_term = px * px + py * py - r0 * r0;
591 let t = if abs_f32(a) < 1e-6 {
592 if abs_f32(b) < 1e-6 {
593 return None;
594 }
595 -c_term / b
596 } else {
597 let disc = b * b - 4.0 * a * c_term;
598 if disc < 0.0 {
599 return None;
600 }
601 let sd = sqrt_f32(disc);
602 let r_a = (-b + sd) / (2.0 * a);
603 let r_b = (-b - sd) / (2.0 * a);
604 let (t_hi, t_lo) = if r_a >= r_b { (r_a, r_b) } else { (r_b, r_a) };
605 if r0 + t_hi * dr >= 0.0 {
606 t_hi
607 } else if r0 + t_lo * dr >= 0.0 {
608 t_lo
609 } else {
610 return None;
611 }
612 };
613 Some(apply_extend(t, extend))
614 }
615 Self::ConicGradient {
616 center,
617 start_angle_deg,
618 sweep_deg,
619 extend,
620 ..
621 } => {
622 let dx = u - center[0];
623 let dy = v - center[1];
624 let mut angle = atan2_f32(dx, -dy);
626 if angle < 0.0 {
627 angle += TAU;
628 }
629 let start_rad_raw = start_angle_deg * (PI / 180.0);
630 let start_rad = start_rad_raw - floor_f32(start_rad_raw / TAU) * TAU;
631 let mut rel = angle - start_rad;
632 if rel < 0.0 {
633 rel += TAU;
634 }
635 let sweep_rad = (abs_f32(sweep_deg).max(1e-3) * (PI / 180.0)).min(TAU);
636 let t = rel / sweep_rad;
637 Some(apply_extend(t, extend))
638 }
639 }
640 }
641}
642
643#[inline]
644fn mix_stops_hash(h: &mut u32, stops: &[ColorStop]) {
645 mix_u32(h, stops.len() as u32);
646 for s in stops {
647 mix_f32(h, s.offset);
648 mix_u32(h, s.color.0);
649 }
650}
651
652pub fn gradient_ramp_hash(stops: &[ColorStop], space: GradientSpace, opacity: u8) -> u64 {
654 let mut h: u64 = 0xcbf2_9ce4_8422_2325;
655 let mix = |h: &mut u64, v: u64| {
656 *h ^= v;
657 *h = h.wrapping_mul(0x0100_0000_01b3);
658 };
659 mix(&mut h, space as u64);
660 mix(&mut h, opacity as u64);
661 mix(&mut h, stops.len() as u64);
662 for s in stops {
663 mix(&mut h, canonical_f32_bits(s.offset) as u64);
664 mix(&mut h, s.color.0 as u64);
665 }
666 h
667}
668
669#[inline(always)]
671pub fn apply_extend(t: f32, mode: ExtendMode) -> f32 {
672 match mode {
673 ExtendMode::Pad => t.clamp(0.0, 1.0),
674 ExtendMode::Repeat => {
675 let f = t - floor_f32(t);
676 f.clamp(0.0, 1.0)
677 }
678 ExtendMode::Reflect => {
679 let half = t * 0.5;
680 let f = (half - floor_f32(half)) * 2.0;
681 let r = if f <= 1.0 { f } else { 2.0 - f };
682 r.clamp(0.0, 1.0)
683 }
684 }
685}
686
687#[inline]
689pub fn linear_srgb_to_oklab(r: f32, g: f32, b: f32) -> (f32, f32, f32) {
690 let l = 0.412_221_46 * r + 0.536_332_55 * g + 0.051_445_995 * b;
691 let m = 0.211_903_5 * r + 0.680_699_5 * g + 0.107_396_96 * b;
692 let s = 0.088_302_46 * r + 0.281_718_85 * g + 0.629_978_7 * b;
693 let l_ = cbrt_f32(l);
694 let m_ = cbrt_f32(m);
695 let s_ = cbrt_f32(s);
696 (
697 0.210_454_26 * l_ + 0.793_617_8 * m_ - 0.004_072_047 * s_,
698 1.977_998_5 * l_ - 2.428_592_2 * m_ + 0.450_593_7 * s_,
699 0.025_904_037 * l_ + 0.782_771_77 * m_ - 0.808_675_77 * s_,
700 )
701}
702
703#[inline]
705pub fn oklab_to_linear_srgb(l: f32, a: f32, b: f32) -> (f32, f32, f32) {
706 let l_ = l + 0.396_337_78 * a + 0.215_803_76 * b;
707 let m_ = l - 0.105_561_346 * a - 0.063_854_17 * b;
708 let s_ = l - 0.089_484_18 * a - 1.291_485_5 * b;
709 let l3 = l_ * l_ * l_;
710 let m3 = m_ * m_ * m_;
711 let s3 = s_ * s_ * s_;
712 let r = 4.076_741_7 * l3 - 3.307_711_6 * m3 + 0.230_969_94 * s3;
713 let g = -1.268_438 * l3 + 2.609_757_4 * m3 - 0.341_319_38 * s3;
714 let bl = -0.004_196_086_3 * l3 - 0.703_418_6 * m3 + 1.707_614_7 * s3;
715 (r.clamp(0.0, 1.0), g.clamp(0.0, 1.0), bl.clamp(0.0, 1.0))
716}
717
718#[inline]
720pub fn interpolate_colors(c0: Color, c1: Color, t: f32, space: GradientSpace) -> Color {
721 let t = t.clamp(0.0, 1.0);
722 let inv = 1.0 - t;
723 let a0 = (c0.a() as f32) * (1.0 / 255.0);
724 let a1 = (c1.a() as f32) * (1.0 / 255.0);
725 let a = a0 * inv + a1 * t;
726 if a <= 1e-5 {
727 return Color::TRANSPARENT;
728 }
729 let (r0, g0, b0) = (
730 (c0.r() as f32) * (1.0 / 255.0),
731 (c0.g() as f32) * (1.0 / 255.0),
732 (c0.b() as f32) * (1.0 / 255.0),
733 );
734 let (r1, g1, b1) = (
735 (c1.r() as f32) * (1.0 / 255.0),
736 (c1.g() as f32) * (1.0 / 255.0),
737 (c1.b() as f32) * (1.0 / 255.0),
738 );
739
740 let (r, g, b) = match space {
741 GradientSpace::Srgb => {
742 let pr = (r0 * a0) * inv + (r1 * a1) * t;
744 let pg = (g0 * a0) * inv + (g1 * a1) * t;
745 let pb = (b0 * a0) * inv + (b1 * a1) * t;
746 (pr / a, pg / a, pb / a)
747 }
748 GradientSpace::LinearRgb => {
749 let lr0 = srgb_to_linear_f32(r0);
750 let lg0 = srgb_to_linear_f32(g0);
751 let lb0 = srgb_to_linear_f32(b0);
752 let lr1 = srgb_to_linear_f32(r1);
753 let lg1 = srgb_to_linear_f32(g1);
754 let lb1 = srgb_to_linear_f32(b1);
755 let pr = (lr0 * a0) * inv + (lr1 * a1) * t;
756 let pg = (lg0 * a0) * inv + (lg1 * a1) * t;
757 let pb = (lb0 * a0) * inv + (lb1 * a1) * t;
758 (
759 linear_to_srgb_f32(pr / a),
760 linear_to_srgb_f32(pg / a),
761 linear_to_srgb_f32(pb / a),
762 )
763 }
764 GradientSpace::Oklab => {
765 let (l0, oa0, ob0) = linear_srgb_to_oklab(
766 srgb_to_linear_f32(r0),
767 srgb_to_linear_f32(g0),
768 srgb_to_linear_f32(b0),
769 );
770 let (l1, oa1, ob1) = linear_srgb_to_oklab(
771 srgb_to_linear_f32(r1),
772 srgb_to_linear_f32(g1),
773 srgb_to_linear_f32(b1),
774 );
775 let w0 = inv * a0;
777 let w1 = t * a1;
778 let wsum = w0 + w1;
779 let (l, oa, ob) = if wsum > 1e-6 {
780 (
781 (l0 * w0 + l1 * w1) / wsum,
782 (oa0 * w0 + oa1 * w1) / wsum,
783 (ob0 * w0 + ob1 * w1) / wsum,
784 )
785 } else {
786 (l0 * inv + l1 * t, oa0 * inv + oa1 * t, ob0 * inv + ob1 * t)
787 };
788 let (lr, lg, lb) = oklab_to_linear_srgb(l, oa, ob);
789 (
790 linear_to_srgb_f32(lr),
791 linear_to_srgb_f32(lg),
792 linear_to_srgb_f32(lb),
793 )
794 }
795 };
796
797 Color::rgba(
798 (r.clamp(0.0, 1.0) * 255.0 + 0.5) as u8,
799 (g.clamp(0.0, 1.0) * 255.0 + 0.5) as u8,
800 (b.clamp(0.0, 1.0) * 255.0 + 0.5) as u8,
801 (a.clamp(0.0, 1.0) * 255.0 + 0.5) as u8,
802 )
803}
804
805pub fn sample_gradient_stops(
807 stops: &[ColorStop],
808 t: f32,
809 space: GradientSpace,
810 opacity: u8,
811) -> Color {
812 if stops.is_empty() || opacity == 0 {
813 return Color::TRANSPARENT;
814 }
815 if stops.len() == 1 {
816 return stops[0].color.scale_alpha(opacity);
817 }
818 let t = t.clamp(0.0, 1.0);
819 let first = stops[0];
821 let last = stops[stops.len() - 1];
822 if t <= first.offset {
823 return first.color.scale_alpha(opacity);
824 }
825 if t >= last.offset {
826 return last.color.scale_alpha(opacity);
827 }
828 for i in 0..(stops.len() - 1) {
829 let s0 = stops[i];
830 let s1 = stops[i + 1];
831 if t <= s1.offset {
832 let span = s1.offset - s0.offset;
833 let local_t = if span <= 1e-6 {
834 1.0
835 } else {
836 ((t - s0.offset) / span).clamp(0.0, 1.0)
837 };
838 return interpolate_colors(s0.color, s1.color, local_t, space).scale_alpha(opacity);
839 }
840 }
841 last.color.scale_alpha(opacity)
842}
843
844pub fn bake_gradient_lut<const N: usize>(
846 stops: &[ColorStop],
847 space: GradientSpace,
848 opacity: u8,
849 out: &mut [[u8; 4]; N],
850) {
851 let denom = (N.saturating_sub(1)).max(1) as f32;
852 for (i, slot) in out.iter_mut().enumerate() {
853 let t = (i as f32) / denom;
854 let c = sample_gradient_stops(stops, t, space, opacity);
855 *slot = [c.r(), c.g(), c.b(), c.a()];
856 }
857}
858
859#[repr(u8)]
865#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Hash)]
866pub enum BlendMode {
867 #[default]
870 SrcOver = 0,
871 Clear = 1,
873 Src = 2,
875 Dst = 3,
877 SrcIn = 4,
879 DstIn = 5,
881 SrcOut = 6,
883 DstOut = 7,
885 SrcAtop = 8,
887 DstAtop = 9,
889 Xor = 10,
891 Plus = 11,
893
894 Multiply = 12,
897 Screen = 13,
899 Overlay = 14,
901 Darken = 15,
903 Lighten = 16,
905 ColorDodge = 17,
907 ColorBurn = 18,
909 HardLight = 19,
911 SoftLight = 20,
913 Difference = 21,
915 Exclusion = 22,
917
918 Hue = 23,
921 Saturation = 24,
923 Color = 25,
925 Luminosity = 26,
927}
928
929impl BlendMode {
930 #[inline(always)]
932 pub const fn is_normal(self) -> bool {
933 matches!(self, Self::SrcOver)
934 }
935
936 #[inline(always)]
938 pub const fn requires_backdrop_read(self) -> bool {
939 !matches!(self, Self::SrcOver)
940 }
941}
942
943#[inline(always)]
944fn lum(c: [f32; 3]) -> f32 {
945 0.3 * c[0] + 0.59 * c[1] + 0.11 * c[2]
946}
947
948#[inline]
949fn clip_color(mut c: [f32; 3]) -> [f32; 3] {
950 let l = lum(c);
951 let n = c[0].min(c[1]).min(c[2]);
952 let x = c[0].max(c[1]).max(c[2]);
953 if n < 0.0 && (l - n) > 1e-6 {
954 let f = l / (l - n);
955 c[0] = l + (c[0] - l) * f;
956 c[1] = l + (c[1] - l) * f;
957 c[2] = l + (c[2] - l) * f;
958 }
959 if x > 1.0 && (x - l) > 1e-6 {
960 let f = (1.0 - l) / (x - l);
961 c[0] = l + (c[0] - l) * f;
962 c[1] = l + (c[1] - l) * f;
963 c[2] = l + (c[2] - l) * f;
964 }
965 [c[0].clamp(0.0, 1.0), c[1].clamp(0.0, 1.0), c[2].clamp(0.0, 1.0)]
966}
967
968#[inline]
969fn set_lum(c: [f32; 3], l: f32) -> [f32; 3] {
970 let d = l - lum(c);
971 clip_color([c[0] + d, c[1] + d, c[2] + d])
972}
973
974#[inline(always)]
975fn sat(c: [f32; 3]) -> f32 {
976 c[0].max(c[1]).max(c[2]) - c[0].min(c[1]).min(c[2])
977}
978
979#[inline]
980fn set_sat(c: [f32; 3], s: f32) -> [f32; 3] {
981 let mut idx = [0usize, 1, 2];
982 if c[idx[0]] > c[idx[1]] {
983 idx.swap(0, 1);
984 }
985 if c[idx[1]] > c[idx[2]] {
986 idx.swap(1, 2);
987 }
988 if c[idx[0]] > c[idx[1]] {
989 idx.swap(0, 1);
990 }
991 let mut out = [0.0f32; 3];
992 let min_v = c[idx[0]];
993 let mid_v = c[idx[1]];
994 let max_v = c[idx[2]];
995 if max_v > min_v {
996 out[idx[1]] = ((mid_v - min_v) * s) / (max_v - min_v);
997 out[idx[2]] = s;
998 }
999 out
1000}
1001
1002#[inline]
1003fn blend_channel_separable(cb: f32, cs: f32, mode: BlendMode) -> f32 {
1004 match mode {
1005 BlendMode::Multiply => cb * cs,
1006 BlendMode::Screen => cb + cs - cb * cs,
1007 BlendMode::Overlay => {
1008 if cb <= 0.5 {
1009 2.0 * cb * cs
1010 } else {
1011 1.0 - 2.0 * (1.0 - cb) * (1.0 - cs)
1012 }
1013 }
1014 BlendMode::Darken => cb.min(cs),
1015 BlendMode::Lighten => cb.max(cs),
1016 BlendMode::ColorDodge => {
1017 if cb <= 0.0 {
1018 0.0
1019 } else if cs >= 1.0 {
1020 1.0
1021 } else {
1022 (cb / (1.0 - cs)).min(1.0)
1023 }
1024 }
1025 BlendMode::ColorBurn => {
1026 if cb >= 1.0 {
1027 1.0
1028 } else if cs <= 0.0 {
1029 0.0
1030 } else {
1031 1.0 - ((1.0 - cb) / cs).min(1.0)
1032 }
1033 }
1034 BlendMode::HardLight => {
1035 if cs <= 0.5 {
1036 2.0 * cb * cs
1037 } else {
1038 1.0 - 2.0 * (1.0 - cb) * (1.0 - cs)
1039 }
1040 }
1041 BlendMode::SoftLight => {
1042 if cs <= 0.5 {
1043 cb - (1.0 - 2.0 * cs) * cb * (1.0 - cb)
1044 } else {
1045 let d = if cb <= 0.25 {
1046 ((16.0 * cb - 12.0) * cb + 4.0) * cb
1047 } else {
1048 sqrt_f32(cb)
1049 };
1050 cb + (2.0 * cs - 1.0) * (d - cb)
1051 }
1052 }
1053 BlendMode::Difference => abs_f32(cb - cs),
1054 BlendMode::Exclusion => cb + cs - 2.0 * cb * cs,
1055 _ => cs,
1056 }
1057}
1058
1059#[inline]
1061pub fn blend_premul_f32(src_pm: [f32; 4], dst_pm: [f32; 4], mode: BlendMode) -> [f32; 4] {
1062 let sa = src_pm[3].clamp(0.0, 1.0);
1063 let da = dst_pm[3].clamp(0.0, 1.0);
1064 let inv_sa = 1.0 - sa;
1065 let inv_da = 1.0 - da;
1066
1067 match mode {
1068 BlendMode::SrcOver => [
1069 src_pm[0] + dst_pm[0] * inv_sa,
1070 src_pm[1] + dst_pm[1] * inv_sa,
1071 src_pm[2] + dst_pm[2] * inv_sa,
1072 sa + da * inv_sa,
1073 ],
1074 BlendMode::Clear => [0.0; 4],
1075 BlendMode::Src => src_pm,
1076 BlendMode::Dst => dst_pm,
1077 BlendMode::SrcIn => [
1078 src_pm[0] * da,
1079 src_pm[1] * da,
1080 src_pm[2] * da,
1081 sa * da,
1082 ],
1083 BlendMode::DstIn => [
1084 dst_pm[0] * sa,
1085 dst_pm[1] * sa,
1086 dst_pm[2] * sa,
1087 da * sa,
1088 ],
1089 BlendMode::SrcOut => [
1090 src_pm[0] * inv_da,
1091 src_pm[1] * inv_da,
1092 src_pm[2] * inv_da,
1093 sa * inv_da,
1094 ],
1095 BlendMode::DstOut => [
1096 dst_pm[0] * inv_sa,
1097 dst_pm[1] * inv_sa,
1098 dst_pm[2] * inv_sa,
1099 da * inv_sa,
1100 ],
1101 BlendMode::SrcAtop => [
1102 src_pm[0] * da + dst_pm[0] * inv_sa,
1103 src_pm[1] * da + dst_pm[1] * inv_sa,
1104 src_pm[2] * da + dst_pm[2] * inv_sa,
1105 da,
1106 ],
1107 BlendMode::DstAtop => [
1108 dst_pm[0] * sa + src_pm[0] * inv_da,
1109 dst_pm[1] * sa + src_pm[1] * inv_da,
1110 dst_pm[2] * sa + src_pm[2] * inv_da,
1111 sa,
1112 ],
1113 BlendMode::Xor => [
1114 src_pm[0] * inv_da + dst_pm[0] * inv_sa,
1115 src_pm[1] * inv_da + dst_pm[1] * inv_sa,
1116 src_pm[2] * inv_da + dst_pm[2] * inv_sa,
1117 sa * inv_da + da * inv_sa,
1118 ],
1119 BlendMode::Plus => [
1120 (src_pm[0] + dst_pm[0]).min(1.0),
1121 (src_pm[1] + dst_pm[1]).min(1.0),
1122 (src_pm[2] + dst_pm[2]).min(1.0),
1123 (sa + da).min(1.0),
1124 ],
1125 _ => {
1126 let cs = if sa > 1e-6 {
1129 [
1130 (src_pm[0] / sa).clamp(0.0, 1.0),
1131 (src_pm[1] / sa).clamp(0.0, 1.0),
1132 (src_pm[2] / sa).clamp(0.0, 1.0),
1133 ]
1134 } else {
1135 [0.0; 3]
1136 };
1137 let cb = if da > 1e-6 {
1138 [
1139 (dst_pm[0] / da).clamp(0.0, 1.0),
1140 (dst_pm[1] / da).clamp(0.0, 1.0),
1141 (dst_pm[2] / da).clamp(0.0, 1.0),
1142 ]
1143 } else {
1144 [0.0; 3]
1145 };
1146 let blended = match mode {
1147 BlendMode::Hue => set_lum(set_sat(cs, sat(cb)), lum(cb)),
1148 BlendMode::Saturation => set_lum(set_sat(cb, sat(cs)), lum(cb)),
1149 BlendMode::Color => set_lum(cs, lum(cb)),
1150 BlendMode::Luminosity => set_lum(cb, lum(cs)),
1151 _ => [
1152 blend_channel_separable(cb[0], cs[0], mode),
1153 blend_channel_separable(cb[1], cs[1], mode),
1154 blend_channel_separable(cb[2], cs[2], mode),
1155 ],
1156 };
1157 let sada = sa * da;
1158 let ao = (sa + da * inv_sa).clamp(0.0, 1.0);
1159 [
1160 (src_pm[0] * inv_da + dst_pm[0] * inv_sa + sada * blended[0]).clamp(0.0, 1.0),
1161 (src_pm[1] * inv_da + dst_pm[1] * inv_sa + sada * blended[1]).clamp(0.0, 1.0),
1162 (src_pm[2] * inv_da + dst_pm[2] * inv_sa + sada * blended[2]).clamp(0.0, 1.0),
1163 ao,
1164 ]
1165 }
1166 }
1167}
1168
1169#[inline(always)]
1171pub fn color_to_premul_f32(c: Color) -> [f32; 4] {
1172 const INV: f32 = 1.0 / 255.0;
1173 let a = (c.a() as f32) * INV;
1174 [
1175 (c.r() as f32) * INV * a,
1176 (c.g() as f32) * INV * a,
1177 (c.b() as f32) * INV * a,
1178 a,
1179 ]
1180}
1181
1182#[inline(always)]
1184pub fn premul_f32_to_color(pm: [f32; 4]) -> Color {
1185 let a = pm[3].clamp(0.0, 1.0);
1186 if a <= 1e-5 {
1187 return Color::TRANSPARENT;
1188 }
1189 let inv_a = 1.0 / a;
1190 Color::rgba(
1191 ((pm[0] * inv_a).clamp(0.0, 1.0) * 255.0 + 0.5) as u8,
1192 ((pm[1] * inv_a).clamp(0.0, 1.0) * 255.0 + 0.5) as u8,
1193 ((pm[2] * inv_a).clamp(0.0, 1.0) * 255.0 + 0.5) as u8,
1194 (a * 255.0 + 0.5) as u8,
1195 )
1196}
1197
1198#[inline]
1200pub fn blend_colors(src: Color, dst: Color, mode: BlendMode) -> Color {
1201 premul_f32_to_color(blend_premul_f32(
1202 color_to_premul_f32(src),
1203 color_to_premul_f32(dst),
1204 mode,
1205 ))
1206}
1207
1208#[repr(C)]
1214#[derive(Copy, Clone, Debug)]
1215pub struct ColorMatrix(pub [f32; 20]);
1216
1217impl PartialEq for ColorMatrix {
1218 fn eq(&self, other: &Self) -> bool {
1219 for i in 0..20 {
1220 if canonical_f32_bits(self.0[i]) != canonical_f32_bits(other.0[i]) {
1221 return false;
1222 }
1223 }
1224 true
1225 }
1226}
1227
1228impl Eq for ColorMatrix {}
1229
1230impl Default for ColorMatrix {
1231 #[inline(always)]
1232 fn default() -> Self {
1233 Self::IDENTITY
1234 }
1235}
1236
1237impl ColorMatrix {
1238 pub const IDENTITY: Self = Self([
1240 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, ]);
1245
1246 #[inline]
1248 pub fn grayscale(amount: f32) -> Self {
1249 let t = amount.clamp(0.0, 1.0);
1250 let inv = 1.0 - t;
1251 let (lr, lg, lb) = (0.2126 * t, 0.7152 * t, 0.0722 * t);
1252 Self([
1253 lr + inv, lg, lb, 0.0, 0.0,
1254 lr, lg + inv, lb, 0.0, 0.0,
1255 lr, lg, lb + inv, 0.0, 0.0,
1256 0.0, 0.0, 0.0, 1.0, 0.0,
1257 ])
1258 }
1259
1260 #[inline]
1262 pub fn sepia(amount: f32) -> Self {
1263 let t = amount.clamp(0.0, 1.0);
1264 let inv = 1.0 - t;
1265 Self([
1266 inv + 0.393 * t, 0.769 * t, 0.189 * t, 0.0, 0.0,
1267 0.349 * t, inv + 0.686 * t, 0.168 * t, 0.0, 0.0,
1268 0.272 * t, 0.534 * t, inv + 0.131 * t, 0.0, 0.0,
1269 0.0, 0.0, 0.0, 1.0, 0.0,
1270 ])
1271 }
1272
1273 #[inline]
1275 pub fn saturate(amount: f32) -> Self {
1276 let s = amount.max(0.0);
1277 let inv = 1.0 - s;
1278 let (lr, lg, lb) = (0.2126 * inv, 0.7152 * inv, 0.0722 * inv);
1279 Self([
1280 lr + s, lg, lb, 0.0, 0.0,
1281 lr, lg + s, lb, 0.0, 0.0,
1282 lr, lg, lb + s, 0.0, 0.0,
1283 0.0, 0.0, 0.0, 1.0, 0.0,
1284 ])
1285 }
1286
1287 #[inline]
1289 pub fn brightness(amount: f32) -> Self {
1290 let b = amount.max(0.0);
1291 Self([
1292 b, 0.0, 0.0, 0.0, 0.0,
1293 0.0, b, 0.0, 0.0, 0.0,
1294 0.0, 0.0, b, 0.0, 0.0,
1295 0.0, 0.0, 0.0, 1.0, 0.0,
1296 ])
1297 }
1298
1299 #[inline]
1301 pub fn contrast(amount: f32) -> Self {
1302 let c = amount.max(0.0);
1303 let bias = 0.5 * (1.0 - c);
1304 Self([
1305 c, 0.0, 0.0, 0.0, bias,
1306 0.0, c, 0.0, 0.0, bias,
1307 0.0, 0.0, c, 0.0, bias,
1308 0.0, 0.0, 0.0, 1.0, 0.0,
1309 ])
1310 }
1311
1312 #[inline]
1314 pub fn hue_rotate(deg: f32) -> Self {
1315 let (s, c) = sin_cos_f32(deg * (PI / 180.0));
1316 Self([
1317 0.213 + c * 0.787 - s * 0.213, 0.715 - c * 0.715 - s * 0.715, 0.072 - c * 0.072 + s * 0.928, 0.0, 0.0,
1318 0.213 - c * 0.213 + s * 0.143, 0.715 + c * 0.285 + s * 0.140, 0.072 - c * 0.072 - s * 0.283, 0.0, 0.0,
1319 0.213 - c * 0.213 - s * 0.787, 0.715 - c * 0.715 + s * 0.715, 0.072 + c * 0.928 + s * 0.072, 0.0, 0.0,
1320 0.0, 0.0, 0.0, 1.0, 0.0,
1321 ])
1322 }
1323
1324 #[inline]
1326 pub fn invert(amount: f32) -> Self {
1327 let t = amount.clamp(0.0, 1.0);
1328 let scale = 1.0 - 2.0 * t;
1329 Self([
1330 scale, 0.0, 0.0, 0.0, t,
1331 0.0, scale, 0.0, 0.0, t,
1332 0.0, 0.0, scale, 0.0, t,
1333 0.0, 0.0, 0.0, 1.0, 0.0,
1334 ])
1335 }
1336
1337 #[inline]
1339 pub fn then(self, next: Self) -> Self {
1340 let a = &next.0;
1341 let b = &self.0;
1342 let mut out = [0.0f32; 20];
1343 for row in 0..4 {
1344 let r = row * 5;
1345 for col in 0..4 {
1346 out[r + col] = a[r] * b[col]
1347 + a[r + 1] * b[5 + col]
1348 + a[r + 2] * b[10 + col]
1349 + a[r + 3] * b[15 + col];
1350 }
1351 out[r + 4] = a[r] * b[4]
1352 + a[r + 1] * b[9]
1353 + a[r + 2] * b[14]
1354 + a[r + 3] * b[19]
1355 + a[r + 4];
1356 }
1357 Self(out)
1358 }
1359
1360 #[inline]
1362 pub fn apply_straight(&self, rgba: [f32; 4]) -> [f32; 4] {
1363 let m = &self.0;
1364 let r = m[0] * rgba[0] + m[1] * rgba[1] + m[2] * rgba[2] + m[3] * rgba[3] + m[4];
1365 let g = m[5] * rgba[0] + m[6] * rgba[1] + m[7] * rgba[2] + m[8] * rgba[3] + m[9];
1366 let b = m[10] * rgba[0] + m[11] * rgba[1] + m[12] * rgba[2] + m[13] * rgba[3] + m[14];
1367 let a = m[15] * rgba[0] + m[16] * rgba[1] + m[17] * rgba[2] + m[18] * rgba[3] + m[19];
1368 [
1369 r.clamp(0.0, 1.0),
1370 g.clamp(0.0, 1.0),
1371 b.clamp(0.0, 1.0),
1372 a.clamp(0.0, 1.0),
1373 ]
1374 }
1375
1376 #[inline]
1378 pub fn apply_premul(&self, pm: [f32; 4]) -> [f32; 4] {
1379 let a = pm[3].clamp(0.0, 1.0);
1380 let straight = if a > 1e-5 {
1381 [
1382 (pm[0] / a).clamp(0.0, 1.0),
1383 (pm[1] / a).clamp(0.0, 1.0),
1384 (pm[2] / a).clamp(0.0, 1.0),
1385 a,
1386 ]
1387 } else {
1388 [0.0; 4]
1389 };
1390 let out = self.apply_straight(straight);
1391 [out[0] * out[3], out[1] * out[3], out[2] * out[3], out[3]]
1392 }
1393
1394 pub fn mix_hash(&self, h: &mut u32) {
1396 for &v in &self.0 {
1397 mix_f32(h, v);
1398 }
1399 }
1400}
1401
1402#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
1404pub struct DropShadow {
1405 pub blur: u8,
1406 pub offset_x: i8,
1407 pub offset_y: i8,
1408 pub color: Color,
1409}
1410
1411impl DropShadow {
1412 #[inline(always)]
1413 pub const fn new(blur: u8, offset_x: i8, offset_y: i8, color: Color) -> Self {
1414 Self {
1415 blur,
1416 offset_x,
1417 offset_y,
1418 color,
1419 }
1420 }
1421
1422 #[inline(always)]
1423 pub const fn is_active(self) -> bool {
1424 self.color.a() > 0 && (self.blur > 0 || self.offset_x != 0 || self.offset_y != 0)
1425 }
1426}
1427
1428#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
1430pub struct LayerFilter {
1431 pub blur_radius: u8,
1433 pub color_matrix: Option<ColorMatrix>,
1435 pub drop_shadow: Option<DropShadow>,
1437}
1438
1439impl LayerFilter {
1440 pub const NONE: Self = Self {
1441 blur_radius: 0,
1442 color_matrix: None,
1443 drop_shadow: None,
1444 };
1445
1446 #[inline(always)]
1447 pub const fn is_empty(&self) -> bool {
1448 self.blur_radius == 0 && self.color_matrix.is_none() && self.drop_shadow.is_none()
1449 }
1450
1451 #[inline]
1453 pub const fn outset(&self) -> (i16, i16, i16, i16) {
1454 let mut l = (self.blur_radius as i16).saturating_mul(2);
1455 let mut t = l;
1456 let mut r = l;
1457 let mut b = l;
1458 if let Some(ds) = self.drop_shadow {
1459 if ds.color.a() > 0 {
1460 let db = (ds.blur as i16).saturating_mul(2);
1461 let sl = db.saturating_sub(ds.offset_x as i16);
1462 let sr = db.saturating_add(ds.offset_x as i16);
1463 let st = db.saturating_sub(ds.offset_y as i16);
1464 let sb = db.saturating_add(ds.offset_y as i16);
1465 if sl > l { l = sl; }
1466 if sr > r { r = sr; }
1467 if st > t { t = st; }
1468 if sb > b { b = sb; }
1469 }
1470 }
1471 (l, t, r, b)
1472 }
1473
1474 pub fn mix_hash(&self, h: &mut u32) {
1475 mix_u8(h, self.blur_radius);
1476 if let Some(cm) = &self.color_matrix {
1477 mix_u8(h, 1);
1478 cm.mix_hash(h);
1479 } else {
1480 mix_u8(h, 0);
1481 }
1482 if let Some(ds) = self.drop_shadow {
1483 mix_u8(h, 1);
1484 mix_u8(h, ds.blur);
1485 mix_u8(h, ds.offset_x as u8);
1486 mix_u8(h, ds.offset_y as u8);
1487 mix_u32(h, ds.color.0);
1488 } else {
1489 mix_u8(h, 0);
1490 }
1491 }
1492}
1493
1494#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
1496pub struct BackdropFilter {
1497 pub blur_radius: u8,
1499 pub color_matrix: Option<ColorMatrix>,
1501 pub tint: Color,
1503 pub noise_amount: u8,
1505}
1506
1507impl BackdropFilter {
1508 pub const NONE: Self = Self {
1509 blur_radius: 0,
1510 color_matrix: None,
1511 tint: Color::TRANSPARENT,
1512 noise_amount: 0,
1513 };
1514
1515 #[inline(always)]
1517 pub const fn blur(radius: u8) -> Self {
1518 Self {
1519 blur_radius: radius,
1520 color_matrix: None,
1521 tint: Color::TRANSPARENT,
1522 noise_amount: 0,
1523 }
1524 }
1525
1526 #[inline]
1528 pub fn acrylic(blur_radius: u8, tint: Color) -> Self {
1529 Self {
1530 blur_radius,
1531 color_matrix: Some(ColorMatrix::saturate(1.45)),
1532 tint,
1533 noise_amount: 12,
1534 }
1535 }
1536
1537 #[inline(always)]
1538 pub const fn is_empty(&self) -> bool {
1539 self.blur_radius == 0
1540 && self.color_matrix.is_none()
1541 && self.tint.a() == 0
1542 && self.noise_amount == 0
1543 }
1544
1545 pub fn mix_hash(&self, h: &mut u32) {
1546 mix_u8(h, self.blur_radius);
1547 mix_u32(h, self.tint.0);
1548 mix_u8(h, self.noise_amount);
1549 if let Some(cm) = &self.color_matrix {
1550 mix_u8(h, 1);
1551 cm.mix_hash(h);
1552 } else {
1553 mix_u8(h, 0);
1554 }
1555 }
1556}
1557
1558#[repr(transparent)]
1564#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Hash)]
1565pub struct ExternalTextureId(pub u32);
1566
1567#[derive(Copy, Clone, Debug)]
1569pub struct ExternalTextureSpec {
1570 pub id: ExternalTextureId,
1572 pub uv_rect: [f32; 4],
1574 pub radii: CornerRadii,
1576 pub opacity: u8,
1578 pub premultiplied: bool,
1580 pub blend_mode: BlendMode,
1582 pub content_revision: u64,
1584 pub fallback_bg: Color,
1586}
1587
1588impl ExternalTextureSpec {
1589 #[inline(always)]
1591 pub const fn new(id: ExternalTextureId) -> Self {
1592 Self {
1593 id,
1594 uv_rect: [0.0, 0.0, 1.0, 1.0],
1595 radii: CornerRadii::ZERO,
1596 opacity: 255,
1597 premultiplied: true,
1598 blend_mode: BlendMode::SrcOver,
1599 content_revision: 0,
1600 fallback_bg: Color::rgb(12, 18, 28),
1601 }
1602 }
1603
1604 #[inline(always)]
1605 pub const fn with_uv(mut self, uv_rect: [f32; 4]) -> Self {
1606 self.uv_rect = uv_rect;
1607 self
1608 }
1609
1610 #[inline(always)]
1611 pub const fn with_radii(mut self, radii: CornerRadii) -> Self {
1612 self.radii = radii;
1613 self
1614 }
1615
1616 #[inline(always)]
1617 pub const fn with_opacity(mut self, opacity: u8) -> Self {
1618 self.opacity = opacity;
1619 self
1620 }
1621
1622 #[inline(always)]
1623 pub const fn with_premultiplied(mut self, premultiplied: bool) -> Self {
1624 self.premultiplied = premultiplied;
1625 self
1626 }
1627
1628 #[inline(always)]
1629 pub const fn with_blend_mode(mut self, blend_mode: BlendMode) -> Self {
1630 self.blend_mode = blend_mode;
1631 self
1632 }
1633
1634 #[inline(always)]
1635 pub const fn with_revision(mut self, rev: u64) -> Self {
1636 self.content_revision = rev;
1637 self
1638 }
1639
1640 #[inline(always)]
1641 pub const fn with_fallback_bg(mut self, color: Color) -> Self {
1642 self.fallback_bg = color;
1643 self
1644 }
1645
1646 pub fn mix_hash(&self, h: &mut u32) {
1647 mix_u32(h, self.id.0);
1648 for v in self.uv_rect {
1649 mix_u32(h, canonical_f32_bits(v));
1650 }
1651 mix_u8(h, self.radii.tl);
1652 mix_u8(h, self.radii.tr);
1653 mix_u8(h, self.radii.br);
1654 mix_u8(h, self.radii.bl);
1655 mix_u8(h, self.opacity);
1656 mix_u8(h, if self.premultiplied { 1 } else { 0 });
1657 mix_u8(h, self.blend_mode as u8);
1658 mix_u32(h, self.content_revision as u32);
1659 mix_u32(h, (self.content_revision >> 32) as u32);
1660 mix_u32(h, self.fallback_bg.0);
1661 }
1662}
1663
1664impl PartialEq for ExternalTextureSpec {
1665 fn eq(&self, other: &Self) -> bool {
1666 self.id == other.id
1667 && canonical_f32_bits(self.uv_rect[0]) == canonical_f32_bits(other.uv_rect[0])
1668 && canonical_f32_bits(self.uv_rect[1]) == canonical_f32_bits(other.uv_rect[1])
1669 && canonical_f32_bits(self.uv_rect[2]) == canonical_f32_bits(other.uv_rect[2])
1670 && canonical_f32_bits(self.uv_rect[3]) == canonical_f32_bits(other.uv_rect[3])
1671 && self.radii == other.radii
1672 && self.opacity == other.opacity
1673 && self.premultiplied == other.premultiplied
1674 && self.blend_mode == other.blend_mode
1675 && self.content_revision == other.content_revision
1676 && self.fallback_bg == other.fallback_bg
1677 }
1678}
1679
1680impl Eq for ExternalTextureSpec {}
1681
1682#[derive(Copy, Clone, Debug)]
1686pub struct LayoutExtras<'a> {
1687 pub aspect_ratio: f32,
1689 pub grid_col_span: u8,
1691 pub grid_template_fr: &'a [u16],
1693 pub sticky_top: Option<i16>,
1695 pub rtl: bool,
1697 pub scroll_chaining: bool,
1699}
1700
1701impl<'a> LayoutExtras<'a> {
1702 pub const EMPTY: Self = Self {
1703 aspect_ratio: 0.0,
1704 grid_col_span: 1,
1705 grid_template_fr: &[],
1706 sticky_top: None,
1707 rtl: false,
1708 scroll_chaining: true,
1709 };
1710
1711 #[inline(always)]
1712 pub fn is_empty(&self) -> bool {
1713 self.aspect_ratio <= 0.0
1714 && self.grid_col_span <= 1
1715 && self.grid_template_fr.is_empty()
1716 && self.sticky_top.is_none()
1717 && !self.rtl
1718 && self.scroll_chaining
1719 }
1720}
1721
1722#[derive(Copy, Clone, Debug, Default)]
1723pub struct PaintEffects<'a> {
1724 pub bg_paint: Option<Paint<'a>>,
1725 pub corner_radii: Option<CornerRadii>,
1726 pub blend_mode: BlendMode,
1727 pub isolate: bool,
1728 pub filter: LayerFilter,
1729 pub backdrop_filter: BackdropFilter,
1730 pub transform: Option<Transform2D>,
1731 pub clip_path: Option<StrRef<'a>>,
1732 pub external_texture: Option<&'a ExternalTextureSpec>,
1733 pub layout_ext: Option<&'a LayoutExtras<'a>>,
1734}
1735
1736impl<'a> PaintEffects<'a> {
1737 pub const EMPTY: Self = Self {
1738 bg_paint: None,
1739 corner_radii: None,
1740 blend_mode: BlendMode::SrcOver,
1741 isolate: false,
1742 filter: LayerFilter::NONE,
1743 backdrop_filter: BackdropFilter::NONE,
1744 transform: None,
1745 clip_path: None,
1746 external_texture: None,
1747 layout_ext: None,
1748 };
1749
1750 #[inline]
1752 pub fn is_empty(&self) -> bool {
1753 self.bg_paint.is_none()
1754 && self.corner_radii.is_none()
1755 && self.blend_mode.is_normal()
1756 && !self.isolate
1757 && self.filter.is_empty()
1758 && self.backdrop_filter.is_empty()
1759 && self.transform.is_none()
1760 && self.clip_path.is_none()
1761 && self.external_texture.is_none()
1762 && self.layout_ext.is_none()
1763 }
1764
1765 #[inline]
1767 pub fn needs_layer(&self, has_children: bool, opacity: u8) -> bool {
1768 !self.filter.is_empty()
1769 || !self.backdrop_filter.is_empty()
1770 || self.clip_path.is_some()
1771 || (!self.blend_mode.is_normal() && has_children)
1772 || (self.isolate && opacity < 255)
1773 || (self.transform.is_some() && has_children)
1774 }
1775}
1776
1777#[derive(Copy, Clone, Debug)]
1779pub struct StyledQuad<'a> {
1780 pub paint: Paint<'a>,
1781 pub border_color: Color,
1782 pub border_width: u8,
1783 pub radii: CornerRadii,
1784 pub opacity: u8,
1785 pub blend_mode: BlendMode,
1786 pub transform: Option<Transform2D>,
1787}
1788
1789impl<'a> PartialEq for StyledQuad<'a> {
1790 fn eq(&self, other: &Self) -> bool {
1791 self.paint == other.paint
1792 && self.border_color == other.border_color
1793 && self.border_width == other.border_width
1794 && self.radii == other.radii
1795 && self.opacity == other.opacity
1796 && self.blend_mode == other.blend_mode
1797 && match (self.transform, other.transform) {
1798 (None, None) => true,
1799 (Some(a), Some(b)) => {
1800 canonical_f32_bits(a.a) == canonical_f32_bits(b.a)
1801 && canonical_f32_bits(a.b) == canonical_f32_bits(b.b)
1802 && canonical_f32_bits(a.c) == canonical_f32_bits(b.c)
1803 && canonical_f32_bits(a.d) == canonical_f32_bits(b.d)
1804 && canonical_f32_bits(a.e) == canonical_f32_bits(b.e)
1805 && canonical_f32_bits(a.f) == canonical_f32_bits(b.f)
1806 }
1807 _ => false,
1808 }
1809 }
1810}
1811
1812impl<'a> Eq for StyledQuad<'a> {}
1813
1814impl<'a> StyledQuad<'a> {
1815 pub fn mix_hash(&self, h: &mut u32) {
1816 self.paint.mix_hash(h);
1817 mix_u32(h, self.border_color.0);
1818 mix_u8(h, self.border_width);
1819 mix_u8(h, self.radii.tl);
1820 mix_u8(h, self.radii.tr);
1821 mix_u8(h, self.radii.br);
1822 mix_u8(h, self.radii.bl);
1823 mix_u8(h, self.opacity);
1824 mix_u8(h, self.blend_mode as u8);
1825 if let Some(t) = self.transform {
1826 mix_u8(h, 1);
1827 mix_transform(h, t);
1828 } else {
1829 mix_u8(h, 0);
1830 }
1831 }
1832}
1833
1834#[derive(Copy, Clone, Debug)]
1836pub struct LayerSpec<'a> {
1837 pub bounds: Region,
1839 pub clip_radii: CornerRadii,
1841 pub clip_path: Option<StrRef<'a>>,
1843 pub opacity: u8,
1845 pub blend_mode: BlendMode,
1847 pub filter: LayerFilter,
1849 pub backdrop_filter: BackdropFilter,
1851 pub transform: Option<Transform2D>,
1853}
1854
1855impl<'a> PartialEq for LayerSpec<'a> {
1856 fn eq(&self, other: &Self) -> bool {
1857 self.bounds == other.bounds
1858 && self.clip_radii == other.clip_radii
1859 && self.clip_path == other.clip_path
1860 && self.opacity == other.opacity
1861 && self.blend_mode == other.blend_mode
1862 && self.filter == other.filter
1863 && self.backdrop_filter == other.backdrop_filter
1864 && match (self.transform, other.transform) {
1865 (None, None) => true,
1866 (Some(a), Some(b)) => {
1867 canonical_f32_bits(a.a) == canonical_f32_bits(b.a)
1868 && canonical_f32_bits(a.b) == canonical_f32_bits(b.b)
1869 && canonical_f32_bits(a.c) == canonical_f32_bits(b.c)
1870 && canonical_f32_bits(a.d) == canonical_f32_bits(b.d)
1871 && canonical_f32_bits(a.e) == canonical_f32_bits(b.e)
1872 && canonical_f32_bits(a.f) == canonical_f32_bits(b.f)
1873 }
1874 _ => false,
1875 }
1876 }
1877}
1878
1879impl<'a> Eq for LayerSpec<'a> {}
1880
1881impl<'a> LayerSpec<'a> {
1882 pub fn expanded_bounds(&self) -> Region {
1884 let mut reg = if let Some(t) = self.transform {
1885 let cx = self.bounds.x as f32 + self.bounds.w as f32 * 0.5;
1886 let cy = self.bounds.y as f32 + self.bounds.h as f32 * 0.5;
1887 t.centered_at(cx, cy).transform_region(self.bounds)
1888 } else {
1889 self.bounds
1890 };
1891 let (fl, ft, fr, fb) = self.filter.outset();
1892 let bd_pad = (self.backdrop_filter.blur_radius as i16).saturating_mul(2);
1893 let l = fl.max(bd_pad);
1894 let t = ft.max(bd_pad);
1895 let r = fr.max(bd_pad);
1896 let b = fb.max(bd_pad);
1897 if l > 0 || t > 0 || r > 0 || b > 0 {
1898 reg = Region::new(
1899 reg.x.saturating_sub(l),
1900 reg.y.saturating_sub(t),
1901 reg.w.saturating_add(l).saturating_add(r),
1902 reg.h.saturating_add(t).saturating_add(b),
1903 );
1904 }
1905 reg
1906 }
1907
1908 pub fn mix_hash(&self, h: &mut u32) {
1909 mix_u32(h, ((self.bounds.x as u16 as u32) << 16) | (self.bounds.y as u16 as u32));
1910 mix_u32(h, ((self.bounds.w as u16 as u32) << 16) | (self.bounds.h as u16 as u32));
1911 mix_u8(h, self.clip_radii.tl);
1912 mix_u8(h, self.clip_radii.tr);
1913 mix_u8(h, self.clip_radii.br);
1914 mix_u8(h, self.clip_radii.bl);
1915 mix_u8(h, self.opacity);
1916 mix_u8(h, self.blend_mode as u8);
1917 self.filter.mix_hash(h);
1918 self.backdrop_filter.mix_hash(h);
1919 if let Some(cp) = self.clip_path {
1920 mix_u8(h, 1);
1921 for &b in cp.as_str().as_bytes() {
1922 mix_u8(h, b);
1923 }
1924 } else {
1925 mix_u8(h, 0);
1926 }
1927 if let Some(t) = self.transform {
1928 mix_u8(h, 1);
1929 mix_transform(h, t);
1930 } else {
1931 mix_u8(h, 0);
1932 }
1933 }
1934}