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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! This module represents mostly Color related helpers.
//!
//! SSA Colors start with `&H` and can be found in multiple forms:
//!
//! `&HRR`,`&HGGRR`,`&HBBGGRR` or `&HAABBGGRR`.
//!
//! VTT Colors start with `#` and are the usual ARGB or RGB hex formats.
use core::panic;
use serde::Deserialize;
use serde::Serialize;
use std::error::Error;
use std::fmt;
use std::str::FromStr;

/// Generic ARGB color struct.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Color {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}
pub const RED: Color = Color {
    r: 255,
    g: 0,
    b: 0,
    a: 255,
};
pub const BLUE: Color = Color {
    r: 0,
    g: 0,
    b: 255,
    a: 255,
};
pub const TRANSPARENT: Color = Color {
    r: 0,
    g: 0,
    b: 0,
    a: 0,
};
pub const GREEN: Color = Color {
    r: 0,
    g: 255,
    b: 0,
    a: 255,
};
pub const WHITE: Color = Color {
    r: 255,
    g: 255,
    b: 255,
    a: 255,
};
pub const WHITET: Color = Color {
    r: 255,
    g: 255,
    b: 255,
    a: 0,
};
pub const BLACK: Color = Color {
    r: 0,
    g: 0,
    b: 0,
    a: 255,
};
pub const YELLOW: Color = Color {
    r: 227,
    g: 193,
    b: 41,
    a: 255,
};
pub const ORANGE: Color = Color {
    r: 227,
    g: 111,
    b: 4,
    a: 255,
};
pub const PINK: Color = Color {
    r: 255,
    g: 192,
    b: 203,
    a: 255,
};
impl Error for Color {}
impl Default for Color {
    fn default() -> Self {
        WHITE
    }
}

impl FromStr for Color {
    type Err = std::num::ParseIntError;
    /// Parses the color from a string depending on the type of color encountered.
    fn from_str(str: &str) -> Result<Self, <Color as FromStr>::Err> {
        if str.starts_with('#') {
            if str.len() == 3 {
                Ok(Color {
                    r: u8::from_str_radix(&str[1..3], 16)?,
                    g: 0,
                    b: 0,
                    a: 255,
                })
            } else if str.len() == 5 {
                Ok(Color {
                    r: u8::from_str_radix(&str[1..3], 16)?,
                    g: u8::from_str_radix(&str[3..5], 16)?,
                    b: 0,
                    a: 255,
                })
            } else if str.len() == 7 {
                Ok(Color {
                    r: u8::from_str_radix(&str[1..3], 16)?,
                    g: u8::from_str_radix(&str[3..5], 16)?,
                    b: u8::from_str_radix(&str[5..7], 16)?,
                    a: 255,
                })
            } else if str.len() == 9 {
                Ok(Color {
                    r: u8::from_str_radix(&str[3..5], 16)?,
                    g: u8::from_str_radix(&str[5..7], 16)?,
                    b: u8::from_str_radix(&str[7..9], 16)?,
                    a: u8::from_str_radix(&str[1..3], 16)?,
                })
            } else {
                panic!("No Color Detected")
            }
        } else if str.starts_with('&') {
            if str.len() == 4 {
                Ok(Color {
                    r: u8::from_str_radix(&str[2..4], 16)?,
                    g: 0,
                    b: 0,
                    a: 255,
                })
            } else if str.len() == 6 {
                Ok(Color {
                    r: u8::from_str_radix(&str[4..6], 16)?,
                    g: u8::from_str_radix(&str[2..4], 16)?,
                    b: 0,
                    a: 255,
                })
            } else if str.len() == 8 {
                Ok(Color {
                    a: 255,
                    b: u8::from_str_radix(&str[2..4], 16)?,
                    g: u8::from_str_radix(&str[4..6], 16)?,
                    r: u8::from_str_radix(&str[6..8], 16)?,
                })
            } else if str.len() == 10 {
                Ok(Color {
                    a: u8::from_str_radix(&str[2..4], 16)?,
                    b: u8::from_str_radix(&str[4..6], 16)?,
                    g: u8::from_str_radix(&str[6..8], 16)?,
                    r: u8::from_str_radix(&str[8..10], 16)?,
                })
            } else {
                panic!("No Color Detected")
            }
        } else {
            panic!("No Color Detected")
        }
    }
}

/// Used to easily display and move colors between representation types.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ColorType {
    SSAColor(Color),
    VTTColor(Color),
    SSAColor0A(Color),
    VTTColor0A(Color),
}

impl ColorType {
    pub fn get_color(&self) -> Color {
        match self {
            Self::VTTColor(color) => *color,
            Self::SSAColor(color) => *color,
            Self::SSAColor0A(color) => *color,
            Self::VTTColor0A(color) => *color,
        }
    }
}

impl fmt::Display for ColorType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::SSAColor(val) => write!(
                f,
                "&H{:0>2X}{:0>2X}{:0>2X}{:0>2X}",
                val.a, val.b, val.g, val.r
            ),
            Self::VTTColor(val) => write!(
                f,
                "#{:0>2X}{:0>2X}{:0>2X}{:0>2X}",
                val.a, val.r, val.g, val.b
            ),
            Self::SSAColor0A(val) => write!(f, "&H{:0>2X}{:0>2X}{:0>2X}", val.b, val.g, val.r),
            Self::VTTColor0A(val) => write!(f, "#{:0>2X}{:0>2X}{:0>2X}", val.r, val.g, val.b),
        }
    }
}

impl fmt::Display for Color {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "#{:0>2X}{:0>2X}{:0>2X}{:0>2X}",
            self.a, self.r, self.g, self.b
        )
    }
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum Alignment {
    BottomLeft = 1,
    BottomCenter = 2,
    BottomRight = 3,
    MiddleLeft = 4,
    MiddleCenter = 5,
    MiddleRight = 6,
    TopLeft = 7,
    TopCenter = 8,
    TopRight = 9,
}

impl Alignment {
    pub fn infer_from_str(str: &str) -> Result<Self, &'static str> {
        match str {
            "1" => Ok(Alignment::BottomLeft),
            "2" => Ok(Alignment::BottomCenter),
            "3" => Ok(Alignment::BottomRight),
            "4" => Ok(Alignment::MiddleLeft),
            "5" => Ok(Alignment::MiddleCenter),
            "6" => Ok(Alignment::MiddleRight),
            "7" => Ok(Alignment::TopLeft),
            "8" => Ok(Alignment::TopCenter),
            "9" => Ok(Alignment::TopRight),
            &_ => Err("ParseIntError"),
        }
    }
}