Skip to main content

qlog_dancer/plots/
colors.rs

1// Copyright (C) 2024, Cloudflare, Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright notice,
9//       this list of conditions and the following disclaimer.
10//
11//     * Redistributions in binary form must reproduce the above copyright
12//       notice, this list of conditions and the following disclaimer in the
13//       documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27use plotters::style::RGBColor;
28use plotters::style::BLACK;
29use plotters::style::WHITE;
30
31pub const FOREST_GREEN: RGBColor = RGBColor(15, 122, 27);
32pub const PURPLE: RGBColor = RGBColor(54, 2, 89);
33pub const TAUPE: RGBColor = RGBColor(133, 104, 3);
34pub const MID_GREY: RGBColor = RGBColor(172, 172, 172);
35pub const ORANGE: RGBColor = RGBColor(201, 93, 4);
36pub const MUSTARD: RGBColor = RGBColor(158, 150, 2);
37pub const SOFT_PINK: RGBColor = RGBColor(158, 82, 94);
38pub const BROWN: RGBColor = RGBColor(74, 38, 2);
39pub const BLUEY_BLACK: RGBColor = RGBColor(23, 32, 42);
40pub const BLUEY_GREY: RGBColor = RGBColor(128, 139, 150);
41pub const TEAL: RGBColor = RGBColor(0, 128, 128);
42
43/// Cycle through a set of colors
44///
45/// By default, the set is based on a modified version of the set from
46/// https://stackoverflow.com/a/13781114
47pub struct ColorCycle {
48    pub colors: Vec<RGBColor>,
49    pub initial_index: usize,
50    pub tracking_index: usize,
51}
52
53impl ColorCycle {
54    pub fn next_color(&mut self) -> RGBColor {
55        let color = self.colors[self.tracking_index];
56        if self.tracking_index == self.colors.len() - 1 {
57            self.tracking_index = 0
58        } else {
59            self.tracking_index += 1
60        }
61
62        color
63    }
64
65    pub fn reset(&mut self) {
66        self.tracking_index = self.initial_index;
67    }
68}
69
70impl Default for ColorCycle {
71    fn default() -> Self {
72        let colors = vec![
73            RGBColor(204, 81, 81),
74            RGBColor(127, 51, 51),
75            RGBColor(81, 204, 204),
76            RGBColor(51, 127, 127),
77            RGBColor(142, 204, 81),
78            RGBColor(89, 127, 51),
79            RGBColor(142, 81, 204),
80            RGBColor(89, 51, 127),
81            RGBColor(204, 173, 81),
82            RGBColor(127, 108, 51),
83            RGBColor(81, 204, 112),
84            RGBColor(51, 127, 70),
85            RGBColor(81, 112, 204),
86            RGBColor(51, 70, 127),
87            RGBColor(204, 81, 173),
88            RGBColor(127, 51, 108),
89            RGBColor(204, 127, 81),
90            RGBColor(127, 79, 51),
91            RGBColor(188, 204, 81),
92            RGBColor(117, 127, 51),
93            RGBColor(96, 204, 81),
94            RGBColor(60, 127, 51),
95            RGBColor(81, 204, 158),
96            RGBColor(51, 127, 98),
97            RGBColor(81, 158, 204),
98            RGBColor(51, 98, 127),
99            RGBColor(96, 81, 204),
100            RGBColor(60, 51, 127),
101            RGBColor(188, 81, 204),
102            RGBColor(117, 51, 127),
103            RGBColor(204, 81, 127),
104            RGBColor(127, 51, 79),
105            RGBColor(204, 104, 81),
106            RGBColor(127, 65, 51),
107            RGBColor(204, 150, 81),
108            RGBColor(127, 94, 51),
109            RGBColor(204, 196, 81),
110            RGBColor(127, 122, 51),
111            RGBColor(165, 204, 81),
112            RGBColor(103, 127, 51),
113            RGBColor(119, 204, 81),
114            RGBColor(74, 127, 51),
115            RGBColor(81, 204, 89),
116            RGBColor(51, 127, 55),
117            RGBColor(81, 204, 135),
118            RGBColor(51, 127, 84),
119            RGBColor(81, 204, 181),
120            RGBColor(51, 127, 113),
121            RGBColor(81, 181, 204),
122            RGBColor(51, 113, 127),
123            RGBColor(81, 135, 204),
124            RGBColor(51, 84, 127),
125            RGBColor(81, 89, 204),
126            RGBColor(51, 55, 127),
127            RGBColor(119, 81, 204),
128            RGBColor(74, 51, 127),
129            RGBColor(165, 81, 204),
130            RGBColor(103, 51, 127),
131            RGBColor(204, 81, 196),
132            RGBColor(127, 51, 122),
133            RGBColor(204, 81, 150),
134            RGBColor(127, 51, 94),
135            RGBColor(204, 81, 104),
136            RGBColor(127, 51, 65),
137            RGBColor(204, 93, 81),
138            RGBColor(127, 58, 51),
139            RGBColor(204, 116, 81),
140            RGBColor(127, 72, 51),
141            RGBColor(204, 138, 81),
142            RGBColor(127, 86, 51),
143            RGBColor(204, 161, 81),
144            RGBColor(127, 101, 51),
145            RGBColor(204, 184, 81),
146            RGBColor(127, 115, 51),
147            RGBColor(200, 204, 81),
148            RGBColor(125, 127, 51),
149            RGBColor(177, 204, 81),
150            RGBColor(110, 127, 51),
151            RGBColor(154, 204, 81),
152            RGBColor(96, 127, 51),
153            RGBColor(131, 204, 81),
154            RGBColor(82, 127, 51),
155            RGBColor(108, 204, 81),
156            RGBColor(67, 127, 51),
157            RGBColor(85, 204, 81),
158            RGBColor(53, 127, 51),
159            RGBColor(81, 204, 100),
160            RGBColor(51, 127, 62),
161            RGBColor(81, 204, 123),
162            RGBColor(51, 127, 77),
163            RGBColor(81, 204, 146),
164            RGBColor(51, 127, 91),
165            RGBColor(81, 204, 169),
166            RGBColor(51, 127, 105),
167            RGBColor(81, 204, 192),
168            RGBColor(51, 127, 120),
169            RGBColor(81, 192, 204),
170            RGBColor(51, 120, 127),
171            RGBColor(81, 169, 204),
172            RGBColor(51, 105, 127),
173        ];
174
175        Self {
176            colors,
177            initial_index: 0,
178            tracking_index: 0,
179        }
180    }
181}
182
183#[derive(Clone, Copy, Debug)]
184pub struct PlotColors {
185    pub fill: RGBColor,
186    pub axis: RGBColor,
187    pub bold_line: RGBColor,
188    pub light_line: RGBColor,
189    pub caption: RGBColor,
190}
191
192impl Default for PlotColors {
193    fn default() -> Self {
194        LIGHT_MODE
195    }
196}
197
198pub const LIGHT_MODE: PlotColors = PlotColors {
199    fill: WHITE,
200    axis: BLUEY_GREY,
201    bold_line: BLUEY_GREY,
202    light_line: BLUEY_GREY,
203    caption: BLACK,
204};
205
206pub const DARK_MODE: PlotColors = PlotColors {
207    fill: BLUEY_BLACK,
208    axis: BLUEY_GREY,
209    bold_line: BLUEY_GREY,
210    light_line: BLUEY_GREY,
211    caption: WHITE,
212};