1use core::fmt;
8use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
9
10#[repr(C)]
18#[derive(Debug, Clone, Copy, PartialEq)]
19pub struct NiColor {
20 pub red: f32,
22 pub green: f32,
24 pub blue: f32,
26}
27
28impl NiColor {
29 #[inline]
37 pub const fn new(red: f32, green: f32, blue: f32) -> Self {
38 Self { red, green, blue }
39 }
40
41 #[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 #[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 #[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
91impl 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
164impl 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#[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 #[inline]
184 pub const fn new(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
185 Self { red, green, blue, alpha }
186 }
187
188 #[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 #[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
210impl 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}