commonlibsse_ng\re\n/
NiColor.rs

1//! # NiColor and NiColorA Module
2//!
3//! This module provides two color structs, `NiColor` and `NiColorA`, representing RGB and RGBA colors respectively.
4//! It includes support for arithmetic operations, hex and integer conversion, and display formatting.
5//!
6
7use core::fmt;
8use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
9
10/// Represents an RGB color with `f32` components for red, green, and blue.
11///
12/// # Example
13/// ```
14/// # use commonlibsse_ng::re::NiColor::NiColor;
15/// let color = NiColor::new(0.5, 0.3, 0.8);
16/// ```
17#[repr(C)]
18#[derive(Debug, Clone, Copy, PartialEq)]
19pub struct NiColor {
20    /// Red component (range: 0.0..=1.0)
21    pub red: f32,
22    /// Green component (range: 0.0..=1.0)
23    pub green: f32,
24    /// Blue component (range: 0.0..=1.0)
25    pub blue: f32,
26}
27
28impl NiColor {
29    /// Creates a new `NiColor` with the specified RGB values.
30    ///
31    /// # Example
32    /// ```
33    /// # use commonlibsse_ng::re::NiColor::NiColor;
34    /// let color = NiColor::new(0.5, 0.3, 0.8);
35    /// ```
36    #[inline]
37    pub const fn new(red: f32, green: f32, blue: f32) -> Self {
38        Self { red, green, blue }
39    }
40
41    /// Creates a `NiColor` from a 32-bit hex value in the format `0xRRGGBB`.
42    ///
43    /// # Example
44    /// ```
45    /// # use commonlibsse_ng::re::NiColor::NiColor;
46    /// let color = NiColor::from_hex(0xFF5733);
47    /// ```
48    #[inline]
49    pub const fn from_hex(hex: u32) -> Self {
50        Self {
51            red: ((hex >> 16) & 0xFF) as f32 / 255.0,
52            green: ((hex >> 8) & 0xFF) as f32 / 255.0,
53            blue: (hex & 0xFF) as f32 / 255.0,
54        }
55    }
56
57    /// Converts the color to a 32-bit integer in the format `0xRRGGBB`.
58    ///
59    /// # Example
60    /// ```
61    /// # use commonlibsse_ng::re::NiColor::NiColor;
62    /// let color = NiColor::new(1.0, 0.5, 0.0);
63    /// assert_eq!(color.to_u32(), 0xFF7F00);
64    /// ```
65    #[inline]
66    pub fn to_u32(&self) -> u32 {
67        (((self.red * 255.0) as u32) << 16)
68            | (((self.green * 255.0) as u32) << 8)
69            | (self.blue * 255.0) as u32
70    }
71
72    /// Converts the color to a hex string in the format `#RRGGBB`.
73    ///
74    /// # Example
75    /// ```
76    /// # use commonlibsse_ng::re::NiColor::NiColor;
77    /// let color = NiColor::new(1.0, 0.5, 0.0);
78    /// assert_eq!(color.to_hex(), "#FF7F00");
79    /// ```
80    #[inline]
81    pub fn to_hex(&self) -> String {
82        format!(
83            "#{:02X}{:02X}{:02X}",
84            (self.red * 255.0) as u32,
85            (self.green * 255.0) as u32,
86            (self.blue * 255.0) as u32
87        )
88    }
89}
90
91// Arithmetic operators for `NiColor`
92impl Add for NiColor {
93    type Output = Self;
94
95    #[inline]
96    fn add(self, rhs: Self) -> Self::Output {
97        Self { red: self.red + rhs.red, green: self.green + rhs.green, blue: self.blue + rhs.blue }
98    }
99}
100
101impl AddAssign for NiColor {
102    #[inline]
103    fn add_assign(&mut self, rhs: Self) {
104        self.red += rhs.red;
105        self.green += rhs.green;
106        self.blue += rhs.blue;
107    }
108}
109
110impl Sub for NiColor {
111    type Output = Self;
112
113    #[inline]
114    fn sub(self, rhs: Self) -> Self::Output {
115        Self { red: self.red - rhs.red, green: self.green - rhs.green, blue: self.blue - rhs.blue }
116    }
117}
118
119impl SubAssign for NiColor {
120    #[inline]
121    fn sub_assign(&mut self, rhs: Self) {
122        self.red -= rhs.red;
123        self.green -= rhs.green;
124        self.blue -= rhs.blue;
125    }
126}
127
128impl Mul<f32> for NiColor {
129    type Output = Self;
130
131    #[inline]
132    fn mul(self, rhs: f32) -> Self::Output {
133        Self { red: self.red * rhs, green: self.green * rhs, blue: self.blue * rhs }
134    }
135}
136
137impl MulAssign<f32> for NiColor {
138    #[inline]
139    fn mul_assign(&mut self, rhs: f32) {
140        self.red *= rhs;
141        self.green *= rhs;
142        self.blue *= rhs;
143    }
144}
145
146impl Div<f32> for NiColor {
147    type Output = Self;
148
149    #[inline]
150    fn div(self, rhs: f32) -> Self::Output {
151        Self { red: self.red / rhs, green: self.green / rhs, blue: self.blue / rhs }
152    }
153}
154
155impl DivAssign<f32> for NiColor {
156    #[inline]
157    fn div_assign(&mut self, rhs: f32) {
158        self.red /= rhs;
159        self.green /= rhs;
160        self.blue /= rhs;
161    }
162}
163
164// Display formatting for `NiColor`
165impl fmt::Display for NiColor {
166    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167        write!(f, "({:.1}, {:.1}, {:.1})", self.red, self.green, self.blue)
168    }
169}
170
171/// Represents an RGBA color with `f32` components for red, green, blue, and alpha.
172#[repr(C)]
173#[derive(Debug, Clone, Copy, PartialEq)]
174pub struct NiColorA {
175    pub red: f32,
176    pub green: f32,
177    pub blue: f32,
178    pub alpha: f32,
179}
180
181impl NiColorA {
182    /// Creates a new `NiColorA` with the specified RGBA values.
183    #[inline]
184    pub const fn new(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
185        Self { red, green, blue, alpha }
186    }
187
188    /// Converts the color to a 32-bit integer in the format `0xRRGGBBAA`.
189    #[inline]
190    pub fn to_u32(&self) -> u32 {
191        (((self.red * 255.0) as u32) << 24)
192            | (((self.green * 255.0) as u32) << 16)
193            | (((self.blue * 255.0) as u32) << 8)
194            | (self.alpha * 255.0) as u32
195    }
196
197    /// Converts the color to a hex string in the format `#RRGGBBAA`.
198    #[inline]
199    pub fn to_hex(&self) -> String {
200        format!(
201            "#{:02X}{:02X}{:02X}{:02X}",
202            (self.red * 255.0) as u32,
203            (self.green * 255.0) as u32,
204            (self.blue * 255.0) as u32,
205            (self.alpha * 255.0) as u32
206        )
207    }
208}
209
210// Arithmetic operators for `NiColorA`
211impl Add for NiColorA {
212    type Output = Self;
213
214    #[inline]
215    fn add(self, rhs: Self) -> Self::Output {
216        Self {
217            red: self.red + rhs.red,
218            green: self.green + rhs.green,
219            blue: self.blue + rhs.blue,
220            alpha: self.alpha + rhs.alpha,
221        }
222    }
223}
224
225impl Mul<f32> for NiColorA {
226    type Output = Self;
227
228    #[inline]
229    fn mul(self, rhs: f32) -> Self::Output {
230        Self {
231            red: self.red * rhs,
232            green: self.green * rhs,
233            blue: self.blue * rhs,
234            alpha: self.alpha * rhs,
235        }
236    }
237}
238
239impl fmt::Display for NiColorA {
240    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
241        write!(f, "({:.1}, {:.1}, {:.1}, {:.1})", self.red, self.green, self.blue, self.alpha)
242    }
243}
244
245impl From<NiColor> for NiColorA {
246    #[inline]
247    fn from(color: NiColor) -> Self {
248        Self { red: color.red, green: color.green, blue: color.blue, alpha: 0.0 }
249    }
250}
251
252impl From<NiColorA> for NiColor {
253    #[inline]
254    fn from(color: NiColorA) -> Self {
255        Self { red: color.red, green: color.green, blue: color.blue }
256    }
257}