add chromachron
Getty Ritter
3 years ago
1 | use chrono::Timelike; | |
2 | ||
3 | pub use crate::widgets::widget::{Drawing, Located, Size, Widget}; | |
4 | ||
5 | const COLORS: [(f64, f64, f64); 12] = [ | |
6 | // yellow | |
7 | (0.9882352941176471,0.9450980392156862,0.043137254901960784), | |
8 | // orange | |
9 | (1.0, 0.5843137254901961, 0.011764705882352941), | |
10 | // white | |
11 | (1.0, 1.0, 1.0), | |
12 | // red | |
13 | (0.9607843137254902, 0.00392156862745098, 0.00392156862745098), | |
14 | // pink | |
15 | (0.9725490196078431, 0.011764705882352941, 0.48627450980392156), | |
16 | // purple | |
17 | (0.6784313725490196, 0.00392156862745098, 0.4666666666666667), | |
18 | // blue | |
19 | (0.0, 0.00392156862745098, 0.8470588235294118), | |
20 | // aqua | |
21 | (0.0196078431372549, 0.615686274509804, 0.4666666666666667), | |
22 | // goluboy | |
23 | (0.03529411764705882, 0.7764705882352941, 0.792156862745098), | |
24 | // deep orange | |
25 | (0.8117647058823529, 0.2, 0.00784313725490196), | |
26 | // light orange | |
27 | (0.9450980392156862, 0.4980392156862745, 0.00784313725490196), | |
28 | // peach | |
29 | (0.9921568627450981, 0.7098039215686275, 0.16862745098039217), | |
30 | ]; | |
31 | ||
32 | #[derive(Debug)] | |
33 | pub struct Chromachron; | |
34 | ||
35 | impl Widget for Chromachron { | |
36 | fn draw(&self, d: &Drawing, loc: Located) -> i32 { | |
37 | // get the current time | |
38 | let now = chrono::Local::now(); | |
39 | let hour = (now.hour() % 12) as usize; | |
40 | ||
41 | let progress = now.minute() as f64 / 60.0; | |
42 | ||
43 | let (lr, lg, lb) = if hour == 0 { | |
44 | COLORS[11] | |
45 | } else { | |
46 | COLORS[hour - 1] | |
47 | }; | |
48 | let (nr, ng, nb) = COLORS[hour % 12]; | |
49 | ||
50 | let sz = d.size.ht - (d.buffer as i32 * 2); | |
51 | let offset = sz as f64 * progress; | |
52 | ||
53 | let x = loc.target_x(d, sz); | |
54 | d.ctx.set_source_rgb(nr, ng, nb); | |
55 | d.ctx.rectangle(x, d.buffer, sz as f64 - offset, sz as f64); | |
56 | d.ctx.fill(); | |
57 | ||
58 | d.ctx.set_source_rgb(lr, lg, lb); | |
59 | d.ctx.rectangle(x + (sz as f64 - offset), d.buffer, offset, sz as f64); | |
60 | d.ctx.fill(); | |
61 | sz | |
62 | } | |
63 | } |