firefly_rust/graphics/
types.rs

1/// The RGB value of a color in the palette.
2#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
3pub struct RGB {
4    /// Red component.
5    pub r: u8,
6    /// Green component.
7    pub g: u8,
8    /// Blue component.
9    pub b: u8,
10}
11
12/// Style of a shape.
13#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
14pub struct Style {
15    /// The color to use to fill the shape.
16    pub fill_color: Color,
17
18    /// The color to use for the shape stroke.
19    pub stroke_color: Color,
20
21    /// The width of the shape stroke.
22    ///
23    /// If zero, a solid shape without a stroke will be drawn.
24    pub stroke_width: i32,
25}
26
27impl Default for Style {
28    fn default() -> Self {
29        Self {
30            fill_color: Color::None,
31            stroke_color: Color::None,
32            stroke_width: 1,
33        }
34    }
35}
36
37impl Style {
38    /// Convert the style to a line style.
39    ///
40    /// [`LineStyle`] is the same as [Style] except it doesn't have a fill color.
41    #[must_use]
42    pub const fn as_line_style(&self) -> LineStyle {
43        LineStyle {
44            color: self.stroke_color,
45            width: self.stroke_width,
46        }
47    }
48}
49
50/// The same as [Style] but without a fill color (only stroke color and width).
51#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
52pub struct LineStyle {
53    /// The line stroke color.
54    pub color: Color,
55    /// The line stroke width.
56    pub width: i32,
57}
58
59impl From<Style> for LineStyle {
60    fn from(value: Style) -> Self {
61        Self {
62            color: value.stroke_color,
63            width: value.stroke_width,
64        }
65    }
66}
67
68/// A pointer to a color in the color palette.
69#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
70pub enum Color {
71    /// No color (100% transparency).
72    None,
73    /// Black color: #1A1C2C.
74    Black,
75    /// Purple color: #5D275D.
76    Purple,
77    /// Red color: #B13E53.
78    Red,
79    /// Orange color: #EF7D57.
80    Orange,
81    /// Yellow color: #FFCD75.
82    Yellow,
83    /// Light green color: #A7F070.
84    LightGreen,
85    /// Green color: #38B764.
86    Green,
87    /// Dark green color: #257179.
88    DarkGreen,
89    /// Dark blue color: #29366F.
90    DarkBlue,
91    /// Blue color: #3B5DC9.
92    Blue,
93    /// Light blue color: #41A6F6.
94    LightBlue,
95    /// Cyan color: #73EFF7.
96    Cyan,
97    /// White color: #F4F4F4.
98    White,
99    /// Light gray color: #94B0C2.
100    LightGray,
101    /// Gray color: #566C86.
102    Gray,
103    /// Dark gray color: #333C57.
104    DarkGray,
105}
106
107impl Default for Color {
108    fn default() -> Self {
109        Self::None
110    }
111}
112
113impl TryFrom<usize> for Color {
114    type Error = ();
115
116    fn try_from(value: usize) -> Result<Self, Self::Error> {
117        match value {
118            0 => Ok(Color::None),
119            1 => Ok(Color::Black),
120            2 => Ok(Color::Purple),
121            3 => Ok(Color::Red),
122            4 => Ok(Color::Orange),
123            5 => Ok(Color::Yellow),
124            6 => Ok(Color::LightGreen),
125            7 => Ok(Color::Green),
126            8 => Ok(Color::DarkGreen),
127            9 => Ok(Color::DarkBlue),
128            10 => Ok(Color::Blue),
129            11 => Ok(Color::LightBlue),
130            12 => Ok(Color::Cyan),
131            13 => Ok(Color::White),
132            14 => Ok(Color::LightGray),
133            15 => Ok(Color::Gray),
134            16 => Ok(Color::DarkGray),
135            _ => Err(()),
136        }
137    }
138}
139
140impl From<Color> for i32 {
141    fn from(value: Color) -> Self {
142        match value {
143            Color::None => 0,
144            Color::Black => 1,
145            Color::Purple => 2,
146            Color::Red => 3,
147            Color::Orange => 4,
148            Color::Yellow => 5,
149            Color::LightGreen => 6,
150            Color::Green => 7,
151            Color::DarkGreen => 8,
152            Color::DarkBlue => 9,
153            Color::Blue => 10,
154            Color::LightBlue => 11,
155            Color::Cyan => 12,
156            Color::White => 13,
157            Color::LightGray => 14,
158            Color::Gray => 15,
159            Color::DarkGray => 16,
160        }
161    }
162}