graphics_style/styles/color/
display.rs

1use super::*;
2use std::hash::{Hash, Hasher};
3
4impl Default for Color {
5    fn default() -> Self {
6        Self::new(0, 0, 0, 255)
7    }
8}
9
10impl Display for Color {
11    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
12        let (r, g, b, a) = self.view();
13        write!(f, "rgba({}, {}, {}, {})", r, g, b, a)
14    }
15}
16
17impl UpperHex for Color {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        if f.alternate() {
20            f.write_char('#')?;
21        }
22        let (r, g, b, a) = self.view();
23        write!(f, "{:02X}{:02X}{:02X}{:02X}", r, g, b, a)
24    }
25}
26
27impl LowerHex for Color {
28    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
29        if f.alternate() {
30            f.write_char('#')?;
31        }
32        let (r, g, b, a) = self.view();
33        write!(f, "{:02x}{:02x}{:02x}{:02x}", r, g, b, a)
34    }
35}
36
37impl Hash for Color {
38    fn hash<H: Hasher>(&self, state: &mut H) {
39        let (r, g, b, a) = self.view();
40        r.hash(state);
41        g.hash(state);
42        b.hash(state);
43        a.hash(state);
44    }
45}