minetest_worldmapper/
color.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use image::{Pixel, Rgba};
use serde::de::{self, Unexpected, Visitor};
use serde::{Deserialize, Deserializer};
use std::fmt;

#[derive(Clone, Copy, Debug)]
pub struct Color(pub Rgba<u8>);

impl Default for Color {
    fn default() -> Self {
        Color(Rgba([0, 0, 0, 0]))
    }
}

impl Color {
    pub fn alpha(&self) -> u8 {
        self.0[3]
    }

    pub fn with_background(&self, other: &Color) -> Color {
        if self.alpha() == 255 {
            return *self;
        } else if self.alpha() == 0 {
            return *other;
        }
        let fore_alpha = self.alpha() as f32 / 255.0;
        let back_alpha = 1.0 - fore_alpha;
        let r = fore_alpha * (self.0[0] as f32) + back_alpha * (other.0[0] as f32);
        let g = fore_alpha * (self.0[1] as f32) + back_alpha * (other.0[1] as f32);
        let b = fore_alpha * (self.0[2] as f32) + back_alpha * (other.0[2] as f32);
        let a = fore_alpha + back_alpha * (other.alpha() as f32 / 255.0);
        Color(Rgba::from([r as u8, g as u8, b as u8, (255.0 * a) as u8]))
    }

    pub fn lighten_up(&mut self, by: u8) {
        self.0.apply_without_alpha(|c| c.saturating_add(by))
    }

    pub fn darken(&mut self, by: u8) {
        self.0.apply_without_alpha(|c| c.saturating_sub(by))
    }
}

struct ColorVisitor;

impl<'de> Visitor<'de> for ColorVisitor {
    type Value = Color;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(
            formatter,
            "a hexadecimal color string in the format 'abcdef' (rgb) or 'abcdef01' (rgba)"
        )
    }

    fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        let valid = ((s.len() == 6) || (s.len() == 8)) && s.chars().all(|c| c.is_ascii_hexdigit());
        if valid {
            // We ruled out a ParseIntError above, so can call unwrap
            let r = u8::from_str_radix(&s[0..2], 16).unwrap();
            let g = u8::from_str_radix(&s[2..4], 16).unwrap();
            let b = u8::from_str_radix(&s[4..6], 16).unwrap();
            let alpha: String = s.chars().skip(6).take(2).collect();
            // An error here can only mean there is no alpha provided, so we can discard the error
            let a = u8::from_str_radix(&alpha, 16).unwrap_or(255);
            Ok(Color(Rgba::from([r, g, b, a])))
        } else {
            Err(de::Error::invalid_value(Unexpected::Str(s), &self))
        }
    }
}

impl<'de> Deserialize<'de> for Color {
    fn deserialize<D>(deserializer: D) -> Result<Color, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(ColorVisitor)
    }
}