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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*<LICENSE>
    This file is part of Memthol.

    Copyright (C) 2020 OCamlPro.

    Memthol is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Memthol is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Memthol.  If not, see <https://www.gnu.org/licenses/>.
*/

//! Color handling.

prelude! {}

pub use base::rand::{
    rngs::SmallRng,
    {Rng, SeedableRng},
};

lazy_static! {
    /// Color RNG.
    static ref RNG: sync::RwLock<SmallRng> = sync::RwLock::new(
        SmallRng::seed_from_u64(42u64)
    );
}

/// Convenience macro giving write-access to the global RNG.
macro_rules! rng {
    () => {
        RNG.write().expect("failed to retrieve color RNG")
    };
}

/// RGBA color.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Serialize, Deserialize)]
pub struct Color {
    /// Red component.
    pub r: u8,
    /// Green component.
    pub g: u8,
    /// Blue component.
    pub b: u8,
}

impl plotters_backend::BackendStyle for Color {
    fn color(&self) -> plotters_backend::BackendColor {
        plotters_backend::BackendColor {
            alpha: 1.0,
            rgb: (self.r, self.g, self.b),
        }
    }
}
impl plotters::style::Color for Color {
    fn rgb(&self) -> (u8, u8, u8) {
        (self.r, self.g, self.b)
    }
    fn alpha(&self) -> f64 {
        1.0
    }
}
impl fmt::Display for Color {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "#{:0>2x}{:0>2x}{:0>2x}", self.r, self.g, self.b)
    }
}

impl Color {
    /// Color constructor.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use charts::color::Color;
    /// let color = Color::new(0xff, 0x00, 0x00);
    /// assert_eq!(&color.to_string(), "#ff0000")
    /// ```
    pub fn new(r: u8, g: u8, b: u8) -> Self {
        Self { r, g, b }
    }

    /// Black.
    pub const BLACK: Self = Self { r: 0, g: 0, b: 0 };

    /// Constructs a color from a string.
    ///
    /// - only accepts RGB strings starting with `#`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use charts::color::Color;
    /// let color = Color::from_str("#ff0000").unwrap();
    /// assert_eq!(&color.to_string(), "#ff0000")
    /// ```
    pub fn from_str<Str: AsRef<str>>(text: Str) -> Res<Self> {
        let text = text.as_ref();

        macro_rules! fail {
            () => {
                bail!("illegal RGB color string `{}`", text)
            };
            ($e:expr) => {
                if let Ok(res) = $e {
                    res
                } else {
                    fail!()
                }
            };
        }

        if text.len() != 7 || &text[0..1] != "#" {
            fail!()
        }

        let (r, g, b) = (
            fail!(u8::from_str_radix(&text[1..3], 16)),
            fail!(u8::from_str_radix(&text[3..5], 16)),
            fail!(u8::from_str_radix(&text[5..7], 16)),
        );

        Ok(Self::new(r, g, b))
    }

    /// Turns itself in a `plotters`-compliant color.
    pub fn to_plotters(&self) -> palette::rgb::Rgb<palette::encoding::srgb::Srgb, u8> {
        palette::rgb::Rgb::new(self.r, self.g, self.b)
    }

    /// Constructor from hue quantity.
    pub fn from_hue(hue: f32, saturation: f32, lightness: f32) -> Self {
        // Dumb application of https://en.wikipedia.org/wiki/HSL_and_HSV#Color_conversion_formulae.
        let hue = hue % 360.;
        let saturation = if saturation < 0.0 {
            0.0
        } else if 1.0 < saturation {
            1.0
        } else {
            saturation
        };
        let lightness = if lightness < 0.0 {
            0.0
        } else if 1.0 < lightness {
            1.0
        } else {
            lightness
        };

        let first_chroma = (1.0 - (2.0 * lightness - 1.).abs()) * saturation;
        let hue_prime = hue / 60.;
        let x = first_chroma * (1. - ((hue_prime % 2.) - 1.).abs());

        let (r, g, b) = if hue_prime <= 1. {
            (first_chroma, x, 0.)
        } else if hue_prime <= 2. {
            (x, first_chroma, 0.)
        } else if hue_prime <= 3. {
            (0., first_chroma, x)
        } else if hue_prime <= 4. {
            (0., x, first_chroma)
        } else if hue_prime <= 5. {
            (x, 0., first_chroma)
        } else if hue_prime <= 6. {
            (first_chroma, 0., x)
        } else {
            panic!("illegal `hue_prime` value {}", hue_prime)
        };

        let m = lightness - (first_chroma / 2.);

        let (r, g, b) = ((r + m) * 255., (g + m) * 255., (b + m) * 255.);
        let (r, g, b) = (r as u8, g as u8, b as u8);

        Self { r, g, b }
    }

    /// Constructs `n` random colors evenly spread on the color wheel.
    ///
    /// - the starting point on the color wheel is random.
    ///
    /// # Guarantees
    ///
    /// - `Color::randoms(n).len() == n`
    pub fn randoms(n: usize) -> Vec<Self> {
        if n == 0 {
            return vec![];
        }

        let inc = 360. / (n as f32);
        let mut current = rng!().gen::<f32>() * 360f32;

        (0..n)
            .into_iter()
            .map(|_| {
                let color = Self::from_hue(current, 1.0, 0.5);
                current += inc;
                color
            })
            .collect()
    }

    /// Constructs a random color.
    ///
    /// - `dark` indicates whether the random color should be relatively dark.
    pub fn random() -> Self {
        Self::from_hue(rng!().gen::<f32>() * 360f32, 1.0, 0.5)
    }

    /// Keeps on constructing colors until the input predicate is true.
    pub fn random_until(pred: impl Fn(&Color) -> bool) -> Self {
        let mut color = Self::random();
        while !pred(&color) {
            color = Self::random()
        }
        color
    }

    /// Returns true if two colors are very similar.
    ///
    /// "Very similar" here means that all components are less than `15u8` apart.
    pub fn is_similar_to(&self, other: &Self) -> bool {
        macro_rules! check {
            ($lft:expr, $rgt:expr) => {
                15u8 >= if $lft >= $rgt {
                    $lft - $rgt
                } else {
                    $rgt - $lft
                }
            };
        }
        check!(self.r, other.r) && check!(self.g, other.g) && check!(self.b, other.b)
    }
}