summaryrefslogtreecommitdiff
path: root/material_theme/src/utils.rs
blob: 7efec9bf3e7757a5b02c53a876f3cf858e48089c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use iced_widget::core::{Color, Shadow, Vector};

pub const HOVERED_LAYER_OPACITY: f32 = 0.08;
pub const PRESSED_LAYER_OPACITY: f32 = 0.1;

pub const DISABLED_TEXT_OPACITY: f32 = 0.38;
pub const DISABLED_CONTAINER_OPACITY: f32 = 0.12;

pub fn elevation(elevation_level: u8) -> f32 {
    (match elevation_level {
        0 => 0.0,
        1 => 1.0,
        2 => 3.0,
        3 => 6.0,
        4 => 8.0,
        _ => 12.0,
    } as f32)
}

pub fn shadow_from_elevation(elevation: f32, color: Color) -> Shadow {
    Shadow {
        color,
        offset: Vector {
            x: 0.0,
            y: elevation,
        },
        blur_radius: (elevation) * (1.0 + 0.4_f32.powf(elevation)),
    }
}

pub fn mix(color1: Color, color2: Color, p2: f32) -> Color {
    if p2 <= 0.0 {
        return color1;
    } else if p2 >= 1.0 {
        return color2;
    }

    let p1 = 1.0 - p2;

    if color1.a != 1.0 || color2.a != 1.0 {
        let a = color1.a * p1 + color2.a * p2;
        if a > 0.0 {
            let c1 = color1.into_linear().map(|c| c * color1.a * p1);
            let c2 = color2.into_linear().map(|c| c * color2.a * p2);

            let [r, g, b] =
                [c1[0] + c2[0], c1[1] + c2[1], c1[2] + c2[2]].map(|u| u / a);

            return Color::from_linear_rgba(r, g, b, a);
        }
    }

    let c1 = color1.into_linear().map(|c| c * p1);
    let c2 = color2.into_linear().map(|c| c * p2);

    Color::from_linear_rgba(
        c1[0] + c2[0],
        c1[1] + c2[1],
        c1[2] + c2[2],
        c1[3] + c2[3],
    )
}

#[cfg(test)]
mod tests {
    use super::{Color, mix};

    #[test]
    fn mixing_works() {
        let base = Color::from_rgba(1.0, 0.0, 0.0, 0.7);
        let overlay = Color::from_rgba(0.0, 1.0, 0.0, 0.2);

        assert_eq!(
            mix(base, overlay, 0.75).into_rgba8(),
            Color::from_linear_rgba(0.53846, 0.46154, 0.0, 0.325).into_rgba8()
        );
    }
}