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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
use std::f32::consts::PI;
use utils::{degrees, fmod, turns};
#[derive(PartialEq, Copy, Clone, Debug)]
pub enum Color {
Rgba(f32, f32, f32, f32),
Hsla(f32, f32, f32, f32),
}
pub type Colour = Color;
#[inline]
pub fn rgba(r: f32, g: f32, b: f32, a: f32) -> Color {
Color::Rgba(r, g, b, a)
}
#[inline]
pub fn rgb(r: f32, g: f32, b: f32) -> Color {
Color::Rgba(r, g, b, 1.0)
}
#[inline]
pub fn rgba_bytes(r: u8, g: u8, b: u8, a: f32) -> Color {
Color::Rgba(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, a)
}
#[inline]
pub fn rgb_bytes(r: u8, g: u8, b: u8) -> Color {
rgba_bytes(r, g, b, 1.0)
}
#[inline]
pub fn hsla(hue: f32, saturation: f32, lightness: f32, alpha: f32) -> Color {
Color::Hsla(hue - turns((hue / (2.0 * PI)).floor()), saturation, lightness, alpha)
}
#[inline]
pub fn hsl(hue: f32, saturation: f32, lightness: f32) -> Color {
hsla(hue, saturation, lightness, 1.0)
}
pub fn grayscale(p: f32) -> Color {
Color::Hsla(0.0, 0.0, 1.0-p, 1.0)
}
pub fn greyscale(p: f32) -> Color {
Color::Hsla(0.0, 0.0, 1.0-p, 1.0)
}
fn clampf32(f: f32) -> f32 {
if f < 0.0 { 0.0 } else if f > 1.0 { 1.0 } else { f }
}
impl Color {
pub fn complement(self) -> Color {
match self {
Color::Hsla(h, s, l, a) => hsla(h + degrees(180.0), s, l, a),
Color::Rgba(r, g, b, a) => {
let (h, s, l) = rgb_to_hsl(r, g, b);
hsla(h + degrees(180.0), s, l, a)
},
}
}
pub fn luminance(&self) -> f32 {
match *self {
Color::Rgba(r, g, b, _) => (r + g + b) / 3.0,
Color::Hsla(_, _, l, _) => l,
}
}
pub fn plain_contrast(self) -> Color {
match self {
Color::Hsla(h, s, l, _) => {
let (r, g, b) = hsl_to_rgb(h, s, l);
rgb(r, g, b).plain_contrast()
},
Color::Rgba(r, g, b, _) => {
let l = 0.2126 * r
+ 0.7152 * g
+ 0.0722 * b;
if l > 0.5 { BLACK } else { WHITE }
}
}
}
pub fn to_hsl(self) -> Hsla {
match self {
Color::Hsla(h, s, l, a) => Hsla(h, s, l, a),
Color::Rgba(r, g, b, a) => {
let (h, s, l) = rgb_to_hsl(r, g, b);
Hsla(h, s, l, a)
},
}
}
pub fn to_rgb(self) -> Rgba {
match self {
Color::Rgba(r, g, b, a) => Rgba(r, g, b, a),
Color::Hsla(h, s, l, a) => {
let (r, g, b) = hsl_to_rgb(h, s, l);
Rgba(r, g, b, a)
},
}
}
pub fn to_fsa(self) -> [f32; 4] {
let Rgba(r, g, b, a) = self.to_rgb();
[r, g, b, a]
}
pub fn to_byte_fsa(self) -> [u8; 4] {
let Rgba(r, g, b, a) = self.to_rgb();
[f32_to_byte(r), f32_to_byte(g), f32_to_byte(b), f32_to_byte(a)]
}
pub fn with_luminance(self, l: f32) -> Color {
let Hsla(h, s, _, a) = self.to_hsl();
Color::Hsla(h, s, l, a)
}
pub fn alpha(self, alpha: f32) -> Color {
match self {
Color::Rgba(r, g, b, a) => Color::Rgba(r, g, b, a * alpha),
Color::Hsla(h, s, l, a) => Color::Hsla(h, s, l, a * alpha),
}
}
pub fn with_alpha(self, a: f32) -> Color {
match self {
Color::Rgba(r, g, b, _) => Color::Rgba(r, g, b, a),
Color::Hsla(h, s, l, _) => Color::Hsla(h, s, l, a),
}
}
pub fn highlighted(self) -> Color {
let luminance = self.luminance();
let Rgba(r, g, b, a) = self.to_rgb();
let (r, g, b) = {
if luminance > 0.8 { (r - 0.2, g - 0.2, b - 0.2) }
else if luminance < 0.2 { (r + 0.2, g + 0.2, b + 0.2) }
else {
(clampf32((1.0 - r) * 0.5 * r + r),
clampf32((1.0 - g) * 0.1 * g + g),
clampf32((1.0 - b) * 0.1 * b + b))
}
};
let a = clampf32((1.0 - a) * 0.5 + a);
rgba(r, g, b, a)
}
pub fn clicked(&self) -> Color {
let luminance = self.luminance();
let Rgba(r, g, b, a) = self.to_rgb();
let (r, g, b) = {
if luminance > 0.8 { (r , g - 0.2, b - 0.2) }
else if luminance < 0.2 { (r + 0.4, g + 0.2, b + 0.2) }
else {
(clampf32((1.0 - r) * 0.75 + r),
clampf32((1.0 - g) * 0.25 + g),
clampf32((1.0 - b) * 0.25 + b))
}
};
let a = clampf32((1.0 - a) * 0.75 + a);
rgba(r, g, b, a)
}
pub fn invert(self) -> Color {
let Rgba(r, g, b, a) = self.to_rgb();
rgba((r - 1.0).abs(), (g - 1.0).abs(), (b - 1.0).abs(), a)
}
pub fn red(&self) -> f32 {
let Rgba(r, _, _, _) = self.to_rgb();
r
}
pub fn green(&self) -> f32 {
let Rgba(_, g, _, _) = self.to_rgb();
g
}
pub fn blue(&self) -> f32 {
let Rgba(_, _, b, _) = self.to_rgb();
b
}
pub fn set_red(&mut self, r: f32) {
let Rgba(_, g, b, a) = self.to_rgb();
*self = rgba(r, g, b, a);
}
pub fn set_green(&mut self, g: f32) {
let Rgba(r, _, b, a) = self.to_rgb();
*self = rgba(r, g, b, a);
}
pub fn set_blue(&mut self, b: f32) {
let Rgba(r, g, _, a) = self.to_rgb();
*self = rgba(r, g, b, a);
}
}
#[derive(Copy, Clone, Debug)]
pub struct Hsla(pub f32, pub f32, pub f32, pub f32);
#[derive(Copy, Clone, Debug)]
pub struct Rgba(pub f32, pub f32, pub f32, pub f32);
#[inline]
pub fn f32_to_byte(c: f32) -> u8 { (c * 255.0) as u8 }
pub fn rgb_to_hsl(r: f32, g: f32, b: f32) -> (f32, f32, f32) {
let c_max = r.max(g).max(b);
let c_min = r.min(g).min(b);
let c = c_max - c_min;
let hue = if c == 0.0 {
0.0
} else {
degrees(60.0) * if c_max == r { fmod(((g - b) / c), 6) }
else if c_max == g { ((b - r) / c) + 2.0 }
else { ((r - g) / c) + 4.0 }
};
let lightness = (c_max + c_min) / 2.0;
let saturation = if lightness == 0.0 { 0.0 }
else { c / (1.0 - (2.0 * lightness - 1.0).abs()) };
(hue, saturation, lightness)
}
pub fn hsl_to_rgb(hue: f32, saturation: f32, lightness: f32) -> (f32, f32, f32) {
let chroma = (1.0 - (2.0 * lightness - 1.0).abs()) * saturation;
let hue = hue / degrees(60.0);
let x = chroma * (1.0 - (fmod(hue, 2) - 1.0).abs());
let (r, g, b) = match hue {
hue if hue < 0.0 => (0.0, 0.0, 0.0),
hue if hue < 1.0 => (chroma, x, 0.0),
hue if hue < 2.0 => (x, chroma, 0.0),
hue if hue < 3.0 => (0.0, chroma, x),
hue if hue < 4.0 => (0.0, x, chroma),
hue if hue < 5.0 => (x, 0.0, chroma),
hue if hue < 6.0 => (chroma, 0.0, x),
_ => (0.0, 0.0, 0.0),
};
let m = lightness - chroma / 2.0;
(r + m, g + m, b + m)
}
#[derive(Clone, Debug)]
pub enum Gradient {
Linear((f64, f64), (f64, f64), Vec<(f64, Color)>),
Radial((f64, f64), f64, (f64, f64), f64, Vec<(f64, Color)>),
}
pub fn linear(start: (f64, f64), end: (f64, f64), colors: Vec<(f64, Color)>) -> Gradient {
Gradient::Linear(start, end, colors)
}
pub fn radial(start: (f64, f64), start_r: f64,
end: (f64, f64), end_r: f64,
colors: Vec<(f64, Color)>) -> Gradient {
Gradient::Radial(start, start_r, end, end_r, colors)
}
macro_rules! make_color {
($r:expr, $g:expr, $b:expr) => ( Color::Rgba($r as f32 / 255.0, $g as f32 / 255.0, $b as f32 / 255.0, 1.0));
($r:expr, $g:expr, $b:expr, $a:expr) => ( Color::Rgba($r as f32 / 255.0, $g as f32 / 255.0, $b as f32 / 255.0, $a as f32 / 255.0));
}
pub const LIGHT_RED : Color = make_color!(239, 41, 41);
pub const RED : Color = make_color!(204, 0, 0);
pub const DARK_RED : Color = make_color!(164, 0, 0);
pub const LIGHT_ORANGE : Color = make_color!(252, 175, 62);
pub const ORANGE : Color = make_color!(245, 121, 0);
pub const DARK_ORANGE : Color = make_color!(206, 92, 0);
pub const LIGHT_YELLOW : Color = make_color!(252, 233, 79);
pub const YELLOW : Color = make_color!(237, 212, 0);
pub const DARK_YELLOW : Color = make_color!(196, 160, 0);
pub const LIGHT_GREEN : Color = make_color!(138, 226, 52);
pub const GREEN : Color = make_color!(115, 210, 22);
pub const DARK_GREEN : Color = make_color!(78, 154, 6);
pub const LIGHT_BLUE : Color = make_color!(114, 159, 207);
pub const BLUE : Color = make_color!(52, 101, 164);
pub const DARK_BLUE : Color = make_color!(32, 74, 135);
pub const LIGHT_PURPLE : Color = make_color!(173, 127, 168);
pub const PURPLE : Color = make_color!(117, 80, 123);
pub const DARK_PURPLE : Color = make_color!(92, 53, 102);
pub const LIGHT_BROWN : Color = make_color!(233, 185, 110);
pub const BROWN : Color = make_color!(193, 125, 17);
pub const DARK_BROWN : Color = make_color!(143, 89, 2);
pub const BLACK : Color = make_color!(0, 0, 0);
pub const WHITE : Color = make_color!(255, 255, 255);
pub const LIGHT_GRAY : Color = make_color!(238, 238, 236);
pub const GRAY : Color = make_color!(211, 215, 207);
pub const DARK_GRAY : Color = make_color!(186, 189, 182);
pub const LIGHT_GREY : Color = make_color!(238, 238, 236);
pub const GREY : Color = make_color!(211, 215, 207);
pub const DARK_GREY : Color = make_color!(186, 189, 182);
pub const LIGHT_CHARCOAL : Color = make_color!(136, 138, 133);
pub const CHARCOAL : Color = make_color!(85, 87, 83);
pub const DARK_CHARCOAL : Color = make_color!(46, 52, 54);
pub const TRANSPARENT : Color = Color::Rgba(0.0, 0.0, 0.0, 0.0);
pub trait Colorable: Sized {
fn color(self, color: Color) -> Self;
fn rgba(self, r: f32, g: f32, b: f32, a: f32) -> Self {
self.color(rgba(r, g, b, a))
}
fn rgb(self, r: f32, g: f32, b: f32) -> Self {
self.color(rgb(r, g, b))
}
fn hsla(self, h: f32, s: f32, l: f32, a: f32) -> Self {
self.color(hsla(h, s, l, a))
}
fn hsl(self, h: f32, s: f32, l: f32) -> Self {
self.color(hsl(h, s, l))
}
}
#[test]
fn plain_contrast_should_weight_colors() {
let white_contrast = rgb(1.0, 1.0, 1.0).plain_contrast();
let Rgba(r, g, b, _) = white_contrast.to_rgb();
assert_eq!(r, 0.0);
assert_eq!(g, 0.0);
assert_eq!(b, 0.0);
let black_contrast = rgb(0.0, 0.0, 0.0).plain_contrast();
let Rgba(r, g, b, _) = black_contrast.to_rgb();
assert_eq!(r, 1.0);
assert_eq!(g, 1.0);
assert_eq!(b, 1.0);
let greenish = rgb(0.29, 0.90, 0.29).plain_contrast();
let Rgba(r, g, b, _) = greenish.to_rgb();
assert_eq!(r, 0.0);
assert_eq!(g, 0.0);
assert_eq!(b, 0.0);
let purplish = rgb(0.71, 0.10, 0.71).plain_contrast();
let Rgba(r, g, b, _) = purplish.to_rgb();
assert_eq!(r, 1.0);
assert_eq!(g, 1.0);
assert_eq!(b, 1.0);
}