use iced::Color;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

pub mod color_serde {
    use super::*;

    pub fn serialize<S>(color: &Color, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let data = [color.r, color.g, color.b, color.a];
        data.serialize(serializer)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Color, D::Error>
    where
        D: Deserializer<'de>,
    {
        let data: [f32; 4] = Deserialize::deserialize(deserializer)?;
        Ok(Color::from_rgba(data[0], data[1], data[2], data[3]))
    }
}
