summaryrefslogtreecommitdiff
path: root/rust/crates/animation/src/channel_layout.rs
blob: 59bbc3f67c45c2a81b455bfe97ab2b7ea22de744 (plain) (blame)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use scene::ParamValue;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

use crate::color::{LinearRgba, parse_color_to_linear_rgba};
use crate::types::AnimationInterpolation;

#[derive(Serialize, Deserialize, Clone, Copy, Debug, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum ChannelValueKind {
    Scalar,
    Discrete,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ChannelComponentDefinition {
    pub key: String,
    pub value_kind: ChannelValueKind,
    pub default_interpolation: AnimationInterpolation,
}

#[derive(Clone, Debug, PartialEq)]
pub enum ChannelLayout {
    Leaf(ChannelComponentDefinition),
    Composite(Vec<ChannelComponentDefinition>),
}

impl ChannelLayout {
    pub fn components(&self) -> &[ChannelComponentDefinition] {
        match self {
            ChannelLayout::Leaf(component) => std::slice::from_ref(component),
            ChannelLayout::Composite(components) => components,
        }
    }

    pub fn decompose(&self, value: &ParamValue) -> Option<BTreeMap<String, ParamValue>> {
        match self {
            ChannelLayout::Leaf(component) => {
                let mut values = BTreeMap::new();
                values.insert(component.key.clone(), value.clone());
                Some(values)
            }
            ChannelLayout::Composite(_) => {
                let ParamValue::String(color) = value else {
                    return None;
                };
                let LinearRgba { r, g, b, a } = parse_color_to_linear_rgba(color)?;
                let mut values = BTreeMap::new();
                values.insert("r".to_string(), ParamValue::Number(r));
                values.insert("g".to_string(), ParamValue::Number(g));
                values.insert("b".to_string(), ParamValue::Number(b));
                values.insert("a".to_string(), ParamValue::Number(a));
                Some(values)
            }
        }
    }
}

pub fn number_channel_layout() -> ChannelLayout {
    ChannelLayout::Leaf(ChannelComponentDefinition {
        key: "value".to_string(),
        value_kind: ChannelValueKind::Scalar,
        default_interpolation: AnimationInterpolation::Linear,
    })
}

pub fn boolean_channel_layout() -> ChannelLayout {
    ChannelLayout::Leaf(ChannelComponentDefinition {
        key: "value".to_string(),
        value_kind: ChannelValueKind::Discrete,
        default_interpolation: AnimationInterpolation::Hold,
    })
}

pub fn string_channel_layout() -> ChannelLayout {
    ChannelLayout::Leaf(ChannelComponentDefinition {
        key: "value".to_string(),
        value_kind: ChannelValueKind::Discrete,
        default_interpolation: AnimationInterpolation::Hold,
    })
}

pub fn color_channel_layout() -> ChannelLayout {
    let component = |key: &str| ChannelComponentDefinition {
        key: key.to_string(),
        value_kind: ChannelValueKind::Scalar,
        default_interpolation: AnimationInterpolation::Linear,
    };
    ChannelLayout::Composite(vec![
        component("r"),
        component("g"),
        component("b"),
        component("a"),
    ])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn leaf_decompose_wraps_value_under_component_key() {
        let layout = number_channel_layout();
        let values = layout.decompose(&ParamValue::Number(3.0)).unwrap();
        assert_eq!(values.get("value"), Some(&ParamValue::Number(3.0)));
    }

    #[test]
    fn color_decompose_parses_hex_into_linear_components() {
        let layout = color_channel_layout();
        let values = layout
            .decompose(&ParamValue::String("#ff0000".to_string()))
            .unwrap();
        assert_eq!(values.get("r"), Some(&ParamValue::Number(1.0)));
        assert_eq!(values.get("g"), Some(&ParamValue::Number(0.0)));
        assert_eq!(values.get("a"), Some(&ParamValue::Number(1.0)));
        assert!(
            layout
                .decompose(&ParamValue::Number(1.0))
                .is_none()
        );
    }
}