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
prelude! {}
pub use base::rand::{
rngs::SmallRng,
{Rng, SeedableRng},
};
lazy_static! {
static ref RNG: sync::RwLock<SmallRng> = sync::RwLock::new(
SmallRng::seed_from_u64(42u64)
);
}
macro_rules! rng {
() => {
RNG.write().expect("failed to retrieve color RNG")
};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Serialize, Deserialize)]
pub struct Color {
pub r: u8,
pub g: u8,
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 {
pub fn new(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b }
}
pub const BLACK: Self = Self { r: 0, g: 0, b: 0 };
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))
}
pub fn to_plotters(&self) -> palette::rgb::Rgb<palette::encoding::srgb::Srgb, u8> {
palette::rgb::Rgb::new(self.r, self.g, self.b)
}
pub fn from_hue(hue: f32, saturation: f32, lightness: f32) -> Self {
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 }
}
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()
}
pub fn random() -> Self {
Self::from_hue(rng!().gen::<f32>() * 360f32, 1.0, 0.5)
}
pub fn random_until(pred: impl Fn(&Color) -> bool) -> Self {
let mut color = Self::random();
while !pred(&color) {
color = Self::random()
}
color
}
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)
}
}