session_rust/
color.rs

1use serde::{ser::Serialize as SerTrait, Deserialize, Serialize};
2use std::fmt;
3use uuid::Uuid;
4
5/// A color with RGBA values and JSON serialization support.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7#[serde(tag = "type", rename = "Color")]
8pub struct Color {
9    pub guid: String,
10    pub name: String,
11    pub r: u8,
12    pub g: u8,
13    pub b: u8,
14    pub a: u8,
15}
16
17impl Color {
18    /// Create new color.
19    pub fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
20        Color {
21            guid: Uuid::new_v4().to_string(),
22            name: "Color".to_string(),
23            r,
24            g,
25            b,
26            a,
27        }
28    }
29
30    /// Create white color.
31    pub fn white() -> Self {
32        let mut color = Color::new(255, 255, 255, 255);
33        color.name = "white".to_string();
34        color
35    }
36
37    /// Create black color.
38    pub fn black() -> Self {
39        let mut color = Color::new(0, 0, 0, 255);
40        color.name = "black".to_string();
41        color
42    }
43
44    /// Convert to float array [0-1].
45    pub fn to_float_array(&self) -> [f32; 4] {
46        [
47            self.r as f32 / 255.0,
48            self.g as f32 / 255.0,
49            self.b as f32 / 255.0,
50            self.a as f32 / 255.0,
51        ]
52    }
53
54    /// Create from float values [0-1].
55    pub fn from_float(r: f32, g: f32, b: f32, a: f32) -> Self {
56        Color::new(
57            (r * 255.0).round() as u8,
58            (g * 255.0).round() as u8,
59            (b * 255.0).round() as u8,
60            (a * 255.0).round() as u8,
61        )
62    }
63
64    ///////////////////////////////////////////////////////////////////////////////////////////
65    // JSON
66    ///////////////////////////////////////////////////////////////////////////////////////////
67
68    /// Serialize to JSON string (for cross-language compatibility)
69    pub fn to_json_data(&self) -> Result<String, Box<dyn std::error::Error>> {
70        let mut buf = Vec::new();
71        let formatter = serde_json::ser::PrettyFormatter::with_indent(b"    ");
72        let mut ser = serde_json::Serializer::with_formatter(&mut buf, formatter);
73        SerTrait::serialize(self, &mut ser)?;
74        Ok(String::from_utf8(buf)?)
75    }
76
77    /// Deserialize from JSON string (for cross-language compatibility)
78    pub fn from_json_data(json_data: &str) -> Result<Self, Box<dyn std::error::Error>> {
79        Ok(serde_json::from_str(json_data)?)
80    }
81
82    /// Serialize to JSON file
83    pub fn to_json(&self, filepath: &str) -> Result<(), Box<dyn std::error::Error>> {
84        let json = self.to_json_data()?;
85        std::fs::write(filepath, json)?;
86        Ok(())
87    }
88
89    /// Deserialize from JSON file
90    pub fn from_json(filepath: &str) -> Result<Self, Box<dyn std::error::Error>> {
91        let json = std::fs::read_to_string(filepath)?;
92        Self::from_json_data(&json)
93    }
94}
95
96impl Default for Color {
97    fn default() -> Self {
98        Self::white()
99    }
100}
101
102impl fmt::Display for Color {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        write!(
105            f,
106            "Color(r={}, g={}, b={}, a={}, name={})",
107            self.r, self.g, self.b, self.a, self.name
108        )
109    }
110}