use crate::easing::Easing;
use crate::utils;
use crate::Event;
pub enum Color {
Static(usize, i32, utils::Color),
Dynamic(usize, Easing, i32, i32, utils::Color, utils::Color),
}
impl Event for Color {
fn to_line(&self) -> String {
match self {
Color::Static(depth, time, color) => {
format!(
"{} C,{},{},,{},{},{}",
" ".repeat(*depth),
Easing::Linear.id(),
time,
color.r(),
color.g(),
color.b(),
)
}
Color::Dynamic(depth, easing, start_time, end_time, start_color, end_color) => {
format!(
"{} C,{},{},{},{},{},{},{},{},{}",
" ".repeat(*depth),
easing.id(),
start_time,
end_time,
start_color.r(),
start_color.g(),
start_color.b(),
end_color.r(),
end_color.g(),
end_color.b(),
)
}
}
}
fn set_depth(&mut self, depth: usize) {
match self {
Color::Static(ref mut current_depth, ..) => *current_depth = depth,
Color::Dynamic(ref mut current_depth, ..) => *current_depth = depth,
}
}
fn get_start_time(&self) -> i32 {
match self {
Color::Static(_, start_time, _) => *start_time,
Color::Dynamic(_, _, start_time, ..) => *start_time,
}
}
fn get_end_time(&self) -> i32 {
match self {
Color::Static(_, end_time, _) => *end_time,
Color::Dynamic(_, _, _, end_time, ..) => *end_time,
}
}
}
impl Into<Color> for (i32, utils::Color) {
fn into(self) -> Color {
Color::Static(0, self.0, self.1)
}
}
impl Into<Color> for (i32, i32, i32, i32) {
fn into(self) -> Color {
Color::Static(0, self.0, utils::Color::from(self.1, self.2, self.3))
}
}
impl Into<Color> for (i32, i32, utils::Color, utils::Color) {
fn into(self) -> Color {
Color::Dynamic(0, Easing::Linear, self.0, self.1, self.2, self.3)
}
}
impl Into<Color> for (i32, i32, i32, i32, i32, i32, i32, i32) {
fn into(self) -> Color {
Color::Dynamic(
0,
Easing::Linear,
self.0,
self.1,
utils::Color::from(self.2, self.3, self.4),
utils::Color::from(self.5, self.6, self.7),
)
}
}
impl Into<Color> for (Easing, i32, i32, utils::Color, utils::Color) {
fn into(self) -> Color {
Color::Dynamic(0, self.0, self.1, self.2, self.3, self.4)
}
}
impl Into<Color> for (Easing, i32, i32, i32, i32, i32, i32, i32, i32) {
fn into(self) -> Color {
Color::Dynamic(
0,
self.0,
self.1,
self.2,
utils::Color::from(self.3, self.4, self.5),
utils::Color::from(self.6, self.7, self.8),
)
}
}
#[cfg(test)]
mod tests {
use crate::{event::*, Easing};
#[test]
fn to_line_static() {
let mut color_event_depth: Color = (100, 0, 0, 0).into();
color_event_depth.set_depth(2);
assert_eq!(color_event_depth.to_line(), " C,0,100,,0,0,0");
}
#[test]
fn to_line_dynamic() {
let color_event: Color = (0, 1000, 0, 0, 0, 255, 255, 255).into();
assert_eq!(color_event.to_line(), " C,0,0,1000,0,0,0,255,255,255");
let color_event_easing: Color = (Easing::QuadOut, 0, 1000, 0, 0, 0, 255, 255, 255).into();
assert_eq!(color_event_easing.to_line(), " C,4,0,1000,0,0,0,255,255,255");
}
}