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