1#![allow(dead_code)]
2
3use serde::de::{self, Deserialize, Deserializer, Visitor};
4use std::fmt;
5use std::num::ParseIntError;
6
7pub fn rgb<C: From<Rgba>>(hex: u32) -> C {
8 let r = ((hex >> 16) & 0xFF) as f32 / 255.0;
9 let g = ((hex >> 8) & 0xFF) as f32 / 255.0;
10 let b = (hex & 0xFF) as f32 / 255.0;
11 Rgba { r, g, b, a: 1.0 }.into()
12}
13
14pub fn rgba(hex: u32) -> Rgba {
15 let r = ((hex >> 24) & 0xFF) as f32 / 255.0;
16 let g = ((hex >> 16) & 0xFF) as f32 / 255.0;
17 let b = ((hex >> 8) & 0xFF) as f32 / 255.0;
18 let a = (hex & 0xFF) as f32 / 255.0;
19 Rgba { r, g, b, a }
20}
21
22#[derive(Clone, Copy, Default)]
23pub struct Rgba {
24 pub r: f32,
25 pub g: f32,
26 pub b: f32,
27 pub a: f32,
28}
29
30impl fmt::Debug for Rgba {
31 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32 write!(f, "rgba({:#010x})", u32::from(*self))
33 }
34}
35
36impl Rgba {
37 pub fn blend(&self, other: Rgba) -> Self {
38 if other.a >= 1.0 {
39 return other;
40 } else if other.a <= 0.0 {
41 return *self;
42 } else {
43 return Rgba {
44 r: (self.r * (1.0 - other.a)) + (other.r * other.a),
45 g: (self.g * (1.0 - other.a)) + (other.g * other.a),
46 b: (self.b * (1.0 - other.a)) + (other.b * other.a),
47 a: self.a,
48 };
49 }
50 }
51}
52
53impl From<Rgba> for u32 {
54 fn from(rgba: Rgba) -> Self {
55 let r = (rgba.r * 255.0) as u32;
56 let g = (rgba.g * 255.0) as u32;
57 let b = (rgba.b * 255.0) as u32;
58 let a = (rgba.a * 255.0) as u32;
59 (r << 24) | (g << 16) | (b << 8) | a
60 }
61}
62
63struct RgbaVisitor;
64
65impl<'de> Visitor<'de> for RgbaVisitor {
66 type Value = Rgba;
67
68 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
69 formatter.write_str("a string in the format #rrggbb or #rrggbbaa")
70 }
71
72 fn visit_str<E: de::Error>(self, value: &str) -> Result<Rgba, E> {
73 if value.len() == 7 || value.len() == 9 {
74 let r = u8::from_str_radix(&value[1..3], 16).unwrap() as f32 / 255.0;
75 let g = u8::from_str_radix(&value[3..5], 16).unwrap() as f32 / 255.0;
76 let b = u8::from_str_radix(&value[5..7], 16).unwrap() as f32 / 255.0;
77 let a = if value.len() == 9 {
78 u8::from_str_radix(&value[7..9], 16).unwrap() as f32 / 255.0
79 } else {
80 1.0
81 };
82 Ok(Rgba { r, g, b, a })
83 } else {
84 Err(E::custom(
85 "Bad format for RGBA. Expected #rrggbb or #rrggbbaa.",
86 ))
87 }
88 }
89}
90
91impl<'de> Deserialize<'de> for Rgba {
92 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
93 deserializer.deserialize_str(RgbaVisitor)
94 }
95}
96
97impl From<Hsla> for Rgba {
98 fn from(color: Hsla) -> Self {
99 let h = color.h;
100 let s = color.s;
101 let l = color.l;
102
103 let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
104 let x = c * (1.0 - ((h * 6.0) % 2.0 - 1.0).abs());
105 let m = l - c / 2.0;
106 let cm = c + m;
107 let xm = x + m;
108
109 let (r, g, b) = match (h * 6.0).floor() as i32 {
110 0 | 6 => (cm, xm, m),
111 1 => (xm, cm, m),
112 2 => (m, cm, xm),
113 3 => (m, xm, cm),
114 4 => (xm, m, cm),
115 _ => (cm, m, xm),
116 };
117
118 Rgba {
119 r,
120 g,
121 b,
122 a: color.a,
123 }
124 }
125}
126
127impl TryFrom<&'_ str> for Rgba {
128 type Error = ParseIntError;
129
130 fn try_from(value: &'_ str) -> Result<Self, Self::Error> {
131 let r = u8::from_str_radix(&value[1..3], 16)? as f32 / 255.0;
132 let g = u8::from_str_radix(&value[3..5], 16)? as f32 / 255.0;
133 let b = u8::from_str_radix(&value[5..7], 16)? as f32 / 255.0;
134 let a = if value.len() > 7 {
135 u8::from_str_radix(&value[7..9], 16)? as f32 / 255.0
136 } else {
137 1.0
138 };
139
140 Ok(Rgba { r, g, b, a })
141 }
142}
143
144#[derive(Default, Copy, Clone, Debug, PartialEq)]
145#[repr(C)]
146pub struct Hsla {
147 pub h: f32,
148 pub s: f32,
149 pub l: f32,
150 pub a: f32,
151}
152
153impl Hsla {
154 pub fn to_rgb(self) -> Rgba {
155 self.into()
156 }
157}
158
159impl Eq for Hsla {}
160
161pub fn hsla(h: f32, s: f32, l: f32, a: f32) -> Hsla {
162 Hsla {
163 h: h.clamp(0., 1.),
164 s: s.clamp(0., 1.),
165 l: l.clamp(0., 1.),
166 a: a.clamp(0., 1.),
167 }
168}
169
170pub fn black() -> Hsla {
171 Hsla {
172 h: 0.,
173 s: 0.,
174 l: 0.,
175 a: 1.,
176 }
177}
178
179pub fn white() -> Hsla {
180 Hsla {
181 h: 0.,
182 s: 0.,
183 l: 1.,
184 a: 1.,
185 }
186}
187
188pub fn red() -> Hsla {
189 Hsla {
190 h: 0.,
191 s: 1.,
192 l: 0.5,
193 a: 1.,
194 }
195}
196
197impl Hsla {
198 /// Returns true if the HSLA color is fully transparent, false otherwise.
199 pub fn is_transparent(&self) -> bool {
200 self.a == 0.0
201 }
202
203 /// Blends `other` on top of `self` based on `other`'s alpha value. The resulting color is a combination of `self`'s and `other`'s colors.
204 ///
205 /// If `other`'s alpha value is 1.0 or greater, `other` color is fully opaque, thus `other` is returned as the output color.
206 /// If `other`'s alpha value is 0.0 or less, `other` color is fully transparent, thus `self` is returned as the output color.
207 /// Else, the output color is calculated as a blend of `self` and `other` based on their weighted alpha values.
208 ///
209 /// Assumptions:
210 /// - Alpha values are contained in the range [0, 1], with 1 as fully opaque and 0 as fully transparent.
211 /// - The relative contributions of `self` and `other` is based on `self`'s alpha value (`self.a`) and `other`'s alpha value (`other.a`), `self` contributing `self.a * (1.0 - other.a)` and `other` contributing it's own alpha value.
212 /// - RGB color components are contained in the range [0, 1].
213 /// - If `self` and `other` colors are out of the valid range, the blend operation's output and behavior is undefined.
214 pub fn blend(self, other: Hsla) -> Hsla {
215 let alpha = other.a;
216
217 if alpha >= 1.0 {
218 return other;
219 } else if alpha <= 0.0 {
220 return self;
221 } else {
222 let converted_self = Rgba::from(self);
223 let converted_other = Rgba::from(other);
224 let blended_rgb = converted_self.blend(converted_other);
225 return Hsla::from(blended_rgb);
226 }
227 }
228
229 /// Fade out the color by a given factor. This factor should be between 0.0 and 1.0.
230 /// Where 0.0 will leave the color unchanged, and 1.0 will completely fade out the color.
231 pub fn fade_out(&mut self, factor: f32) {
232 self.a *= 1.0 - factor.clamp(0., 1.);
233 }
234}
235
236// impl From<Hsla> for Rgba {
237// fn from(value: Hsla) -> Self {
238// let h = value.h;
239// let s = value.s;
240// let l = value.l;
241
242// let c = (1 - |2L - 1|) X s
243// }
244// }
245
246impl From<Rgba> for Hsla {
247 fn from(color: Rgba) -> Self {
248 let r = color.r;
249 let g = color.g;
250 let b = color.b;
251
252 let max = r.max(g.max(b));
253 let min = r.min(g.min(b));
254 let delta = max - min;
255
256 let l = (max + min) / 2.0;
257 let s = if l == 0.0 || l == 1.0 {
258 0.0
259 } else if l < 0.5 {
260 delta / (2.0 * l)
261 } else {
262 delta / (2.0 - 2.0 * l)
263 };
264
265 let h = if delta == 0.0 {
266 0.0
267 } else if max == r {
268 ((g - b) / delta).rem_euclid(6.0) / 6.0
269 } else if max == g {
270 ((b - r) / delta + 2.0) / 6.0
271 } else {
272 ((r - g) / delta + 4.0) / 6.0
273 };
274
275 Hsla {
276 h,
277 s,
278 l,
279 a: color.a,
280 }
281 }
282}
283
284impl<'de> Deserialize<'de> for Hsla {
285 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
286 where
287 D: Deserializer<'de>,
288 {
289 // First, deserialize it into Rgba
290 let rgba = Rgba::deserialize(deserializer)?;
291
292 // Then, use the From<Rgba> for Hsla implementation to convert it
293 Ok(Hsla::from(rgba))
294 }
295}