Skip to main content

iced_m3/style/
shadow.rs

1use iced::{Color, Shadow, Vector};
2
3pub struct Elevation {
4    value: f32,
5}
6
7impl From<f32> for Elevation {
8    fn from(val: f32) -> Self {
9        Elevation {
10            value: val.clamp(0.0, 1.0),
11        }
12    }
13}
14
15impl Elevation {
16    /// 0 -> No elevation
17    /// 1 -> Max elevation
18    pub fn new(value: f32) -> Self {
19        Self {
20            value: value.clamp(0.0, 1.0),
21        }
22    }
23
24    fn alpha(&self) -> f32 {
25        self.value / 2.0
26    }
27
28    fn offset(&self) -> Vector {
29        Vector::new(0.0, self.value * 8.0)
30    }
31
32    fn blur_radius(&self) -> f32 {
33        self.value * 18.0
34    }
35}
36
37pub fn shadow<E: Into<Elevation>>(color: Color, elevation: E) -> Shadow {
38    let elevation = elevation.into();
39    Shadow {
40        color: color.scale_alpha(elevation.alpha()),
41        offset: elevation.offset(),
42        blur_radius: elevation.blur_radius(),
43    }
44}