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
use crate::xml::common::FromFormat;
use crate::xml::style::color::Color;

#[derive(Clone, Debug)]
pub enum FormatColor {
    Default,
    // RGB(String),
    Index(u8),
    Theme(u8, f64),
    RGB(u8, u8, u8),
}


impl Default for FormatColor {
    fn default() -> Self {
        Self::Default
    }
}


impl FromFormat<FormatColor> for Color {
    fn set_attrs_by_format(&mut self, format: &FormatColor) {
        match format {
            FormatColor::Default => *self = Color::default(),
            FormatColor::RGB(r, g, b) => *self = Color::from_rgb(*r, *g, *b),
            FormatColor::Index(id) => *self = Color::from_index(*id),
            FormatColor::Theme(theme, tint) => *self = Color::from_theme(*theme, *tint),
        }
    }

    fn set_format(&self, format: &mut FormatColor) {
        *format = if let Some(id) = self.indexed {
            FormatColor::Index(id)
        } else if let (Some(theme), tint) = (self.theme, self.tint) {
            FormatColor::Theme(theme, tint.unwrap_or_default())
        } else if let Some(color) = &self.rgb {
            let argb: Vec<u8> = color
                .chars()
                .collect::<Vec<char>>()
                .chunks(2)
                .map(|chunk| {
                    let hex_string: String = chunk.iter().collect();
                    u8::from_str_radix(&hex_string, 16).unwrap()
                })
                .collect();
            FormatColor::RGB(argb[1], argb[2], argb[3])
        } else {
            FormatColor::Default
        };
    }
}