use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}
impl Color {
pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
pub const WHITE: Self = Self::new(1.0, 1.0, 1.0, 1.0);
pub const BLACK: Self = Self::new(0.0, 0.0, 0.0, 1.0);
pub const TRANSPARENT: Self = Self::new(0.0, 0.0, 0.0, 0.0);
}
impl Default for Color {
fn default() -> Self {
Self::WHITE
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Stroke {
pub color: Color,
pub width: f32,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn color_default_is_white() {
assert_eq!(Color::default(), Color::WHITE);
}
#[test]
fn color_constants_have_expected_channel_values() {
assert_eq!(Color::WHITE.r, 1.0);
assert_eq!(Color::BLACK.r, 0.0);
assert_eq!(Color::TRANSPARENT.a, 0.0);
}
#[test]
fn color_serde_round_trip() {
let original = Color::new(0.25, 0.5, 0.75, 1.0);
let json = serde_json::to_string(&original).unwrap();
let decoded: Color = serde_json::from_str(&json).unwrap();
assert_eq!(decoded, original);
}
#[test]
fn stroke_carries_color_and_width() {
let stroke = Stroke {
color: Color::BLACK,
width: 2.0,
};
assert_eq!(stroke.color, Color::BLACK);
assert_eq!(stroke.width, 2.0);
}
}