Skip to main content

inset_embedder/geometry/
mod.rs

1//! Flutter counterpart: the pure-Dart value layer of `dart:ui`, which lives in
2//! the engine at `engine/src/flutter/lib/ui`.
3
4mod clip;
5mod color;
6#[allow(clippy::module_inception)]
7mod geometry;
8mod lerp;
9mod math;
10mod rrect;
11mod shadow;
12
13pub use clip::*;
14pub use color::*;
15pub use geometry::*;
16pub use lerp::*;
17pub use math::*;
18pub use rrect::*;
19pub use shadow::*;
20pub use valo::Matrix as Matrix4;
21pub use valo_geometry::MatrixKind;
22
23mod matrix;
24pub use matrix::*;
25
26impl From<Color> for valo::Color {
27    fn from(color: Color) -> valo::Color {
28        valo::Color {
29            r: color.r as f32,
30            g: color.g as f32,
31            b: color.b as f32,
32            a: color.a as f32,
33        }
34    }
35}
36
37impl From<valo::Color> for Color {
38    fn from(color: valo::Color) -> Color {
39        Color::from(
40            color.a as f64,
41            color.r as f64,
42            color.g as f64,
43            color.b as f64,
44            ColorSpace::Srgb,
45        )
46    }
47}
48
49impl From<Rect> for valo::Rect {
50    fn from(rect: Rect) -> valo::Rect {
51        valo::Rect::from_ltrb(
52            rect.left as f32,
53            rect.top as f32,
54            rect.right as f32,
55            rect.bottom as f32,
56        )
57    }
58}
59
60impl From<Offset> for valo::Point {
61    fn from(offset: Offset) -> valo::Point {
62        valo::Point {
63            x: offset.dx() as f32,
64            y: offset.dy() as f32,
65        }
66    }
67}
68
69impl From<valo::Point> for Offset {
70    fn from(point: valo::Point) -> Offset {
71        Offset::new(point.x as f64, point.y as f64)
72    }
73}
74
75impl From<Size> for valo::Size {
76    fn from(size: Size) -> valo::Size {
77        valo::Size {
78            width: size.width() as f32,
79            height: size.height() as f32,
80        }
81    }
82}
83
84/// Per-corner elliptical radii in valo's order (clockwise from top-left).
85pub fn rrect_radii_elliptical(rrect: RRect) -> [[f32; 2]; 4] {
86    corner_radii_elliptical([
87        [rrect.tl_radius_x, rrect.tl_radius_y],
88        [rrect.tr_radius_x, rrect.tr_radius_y],
89        [rrect.br_radius_x, rrect.br_radius_y],
90        [rrect.bl_radius_x, rrect.bl_radius_y],
91    ])
92}
93
94/// Per-corner elliptical radii in valo's order (clockwise from top-left).
95pub fn rsuperellipse_radii_elliptical(rse: RSuperellipse) -> [[f32; 2]; 4] {
96    corner_radii_elliptical([
97        [rse.tl_radius_x, rse.tl_radius_y],
98        [rse.tr_radius_x, rse.tr_radius_y],
99        [rse.br_radius_x, rse.br_radius_y],
100        [rse.bl_radius_x, rse.bl_radius_y],
101    ])
102}
103
104fn corner_radii_elliptical(radii: [[f64; 2]; 4]) -> [[f32; 2]; 4] {
105    radii.map(|[x, y]| [x as f32, y as f32])
106}
107
108#[cfg(test)]
109mod valo_interop_tests {
110    use super::*;
111
112    #[test]
113    fn color_channels_narrow_to_f32() {
114        let valo_color = valo::Color::from(Color::from_argb(255, 255, 0, 128));
115        assert_eq!(valo_color.a, 1.0);
116        assert_eq!(valo_color.r, 1.0);
117        assert_eq!(valo_color.g, 0.0);
118        assert!((valo_color.b - 128.0 / 255.0).abs() < 1e-6);
119    }
120
121    #[test]
122    fn rect_narrows_to_f32_sides() {
123        let valo_rect = valo::Rect::from(Rect::from_ltrb(1.5, 2.5, 3.5, 4.5));
124        assert_eq!(
125            (valo_rect.x, valo_rect.y, valo_rect.width, valo_rect.height),
126            (1.5, 2.5, 2.0, 2.0)
127        );
128    }
129}