endbasic_std/console/
colors.rs

1// EndBASIC
2// Copyright 2021 Julio Merino
3//
4// Licensed under the Apache License, Version 2.0 (the "License"); you may not
5// use this file except in compliance with the License.  You may obtain a copy
6// of the License at:
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13// License for the specific language governing permissions and limitations
14// under the License.
15
16//! Color constants.
17
18/// Identifiers for the basic ANSI colors.
19#[allow(missing_docs)]
20#[repr(u8)]
21pub enum AnsiColor {
22    Black = 0,
23    Red = 1,
24    Green = 2,
25    Yellow = 3,
26    Blue = 4,
27    Magenta = 5,
28    Cyan = 6,
29    White = 7,
30    BrightBlack = 8,
31    BrightRed = 9,
32    BrightGreen = 10,
33    BrightYellow = 11,
34    BrightBlue = 12,
35    BrightMagenta = 13,
36    BrightCyan = 14,
37    BrightWhite = 15,
38}
39
40/// Represents an RGB color in `[0,255]` quantities.
41#[allow(clippy::upper_case_acronyms)]
42pub type RGB = (u8, u8, u8);
43
44/// Color palette for terminals with precise color support.
45static COLORS: &[RGB] = &[
46    (0, 0, 0),       // Black.
47    (128, 0, 0),     // Maroon.
48    (0, 128, 0),     // Green.
49    (128, 128, 0),   // Olive.
50    (0, 0, 128),     // Navy.
51    (128, 0, 128),   // Purple.
52    (0, 128, 128),   // Teal.
53    (192, 192, 192), // Silver.
54    (128, 128, 128), // Grey.
55    (255, 0, 0),     // Red.
56    (0, 255, 0),     // Lime.
57    (255, 255, 0),   // Yellow.
58    (0, 0, 255),     // Blue.
59    (255, 0, 255),   // Fuchsia.
60    (0, 255, 255),   // Aqua.
61    (255, 255, 255), // White.
62    (0, 0, 0),       // Grey0.
63    (0, 0, 95),      // NavyBlue.
64    (0, 0, 135),     // DarkBlue.
65    (0, 0, 175),     // Blue3.
66    (0, 0, 215),     // Blue3.
67    (0, 0, 255),     // Blue1.
68    (0, 95, 0),      // DarkGreen.
69    (0, 95, 95),     // DeepSkyBlue4.
70    (0, 95, 135),    // DeepSkyBlue4.
71    (0, 95, 175),    // DeepSkyBlue4.
72    (0, 95, 215),    // DodgerBlue3.
73    (0, 95, 255),    // DodgerBlue2.
74    (0, 135, 0),     // Green4.
75    (0, 135, 95),    // SpringGreen4.
76    (0, 135, 135),   // Turquoise4.
77    (0, 135, 175),   // DeepSkyBlue3.
78    (0, 135, 215),   // DeepSkyBlue3.
79    (0, 135, 255),   // DodgerBlue1.
80    (0, 175, 0),     // Green3.
81    (0, 175, 95),    // SpringGreen3.
82    (0, 175, 135),   // DarkCyan.
83    (0, 175, 175),   // LightSeaGreen.
84    (0, 175, 215),   // DeepSkyBlue2.
85    (0, 175, 255),   // DeepSkyBlue1.
86    (0, 215, 0),     // Green3.
87    (0, 215, 95),    // SpringGreen3.
88    (0, 215, 135),   // SpringGreen2.
89    (0, 215, 175),   // Cyan3.
90    (0, 215, 215),   // DarkTurquoise.
91    (0, 215, 255),   // Turquoise2.
92    (0, 255, 0),     // Green1.
93    (0, 255, 95),    // SpringGreen2.
94    (0, 255, 135),   // SpringGreen1.
95    (0, 255, 175),   // MediumSpringGreen.
96    (0, 255, 215),   // Cyan2.
97    (0, 255, 255),   // Cyan1.
98    (95, 0, 0),      // DarkRed.
99    (95, 0, 95),     // DeepPink4.
100    (95, 0, 135),    // Purple4.
101    (95, 0, 175),    // Purple4.
102    (95, 0, 215),    // Purple3.
103    (95, 0, 255),    // BlueViolet.
104    (95, 95, 0),     // Orange4.
105    (95, 95, 95),    // Grey37.
106    (95, 95, 135),   // MediumPurple4.
107    (95, 95, 175),   // SlateBlue3.
108    (95, 95, 215),   // SlateBlue3.
109    (95, 95, 255),   // RoyalBlue1.
110    (95, 135, 0),    // Chartreuse4.
111    (95, 135, 95),   // DarkSeaGreen4.
112    (95, 135, 135),  // PaleTurquoise4.
113    (95, 135, 175),  // SteelBlue.
114    (95, 135, 215),  // SteelBlue3.
115    (95, 135, 255),  // CornflowerBlue.
116    (95, 175, 0),    // Chartreuse3.
117    (95, 175, 95),   // DarkSeaGreen4.
118    (95, 175, 135),  // CadetBlue.
119    (95, 175, 175),  // CadetBlue.
120    (95, 175, 215),  // SkyBlue3.
121    (95, 175, 255),  // SteelBlue1.
122    (95, 215, 0),    // Chartreuse3.
123    (95, 215, 95),   // PaleGreen3.
124    (95, 215, 135),  // SeaGreen3.
125    (95, 215, 175),  // Aquamarine3.
126    (95, 215, 215),  // MediumTurquoise.
127    (95, 215, 255),  // SteelBlue1.
128    (95, 255, 0),    // Chartreuse2.
129    (95, 255, 95),   // SeaGreen2.
130    (95, 255, 135),  // SeaGreen1.
131    (95, 255, 175),  // SeaGreen1.
132    (95, 255, 215),  // Aquamarine1.
133    (95, 255, 255),  // DarkSlateGray2.
134    (135, 0, 0),     // DarkRed.
135    (135, 0, 95),    // DeepPink4.
136    (135, 0, 135),   // DarkMagenta.
137    (135, 0, 175),   // DarkMagenta.
138    (135, 0, 215),   // DarkViolet.
139    (135, 0, 255),   // Purple.
140    (135, 95, 0),    // Orange4.
141    (135, 95, 95),   // LightPink4.
142    (135, 95, 135),  // Plum4.
143    (135, 95, 175),  // MediumPurple3.
144    (135, 95, 215),  // MediumPurple3.
145    (135, 95, 255),  // SlateBlue1.
146    (135, 135, 0),   // Yellow4.
147    (135, 135, 95),  // Wheat4.
148    (135, 135, 135), // Grey53.
149    (135, 135, 175), // LightSlateGrey.
150    (135, 135, 215), // MediumPurple.
151    (135, 135, 255), // LightSlateBlue.
152    (135, 175, 0),   // Yellow4.
153    (135, 175, 95),  // DarkOliveGreen3.
154    (135, 175, 135), // DarkSeaGreen.
155    (135, 175, 175), // LightSkyBlue3.
156    (135, 175, 215), // LightSkyBlue3.
157    (135, 175, 255), // SkyBlue2.
158    (135, 215, 0),   // Chartreuse2.
159    (135, 215, 95),  // DarkOliveGreen3.
160    (135, 215, 135), // PaleGreen3.
161    (135, 215, 175), // DarkSeaGreen3.
162    (135, 215, 215), // DarkSlateGray3.
163    (135, 215, 255), // SkyBlue1.
164    (135, 255, 0),   // Chartreuse1.
165    (135, 255, 95),  // LightGreen.
166    (135, 255, 135), // LightGreen.
167    (135, 255, 175), // PaleGreen1.
168    (135, 255, 215), // Aquamarine1.
169    (135, 255, 255), // DarkSlateGray1.
170    (175, 0, 0),     // Red3.
171    (175, 0, 95),    // DeepPink4.
172    (175, 0, 135),   // MediumVioletRed.
173    (175, 0, 175),   // Magenta3.
174    (175, 0, 215),   // DarkViolet.
175    (175, 0, 255),   // Purple.
176    (175, 95, 0),    // DarkOrange3.
177    (175, 95, 95),   // IndianRed.
178    (175, 95, 135),  // HotPink3.
179    (175, 95, 175),  // MediumOrchid3.
180    (175, 95, 215),  // MediumOrchid.
181    (175, 95, 255),  // MediumPurple2.
182    (175, 135, 0),   // DarkGoldenrod.
183    (175, 135, 95),  // LightSalmon3.
184    (175, 135, 135), // RosyBrown.
185    (175, 135, 175), // Grey63.
186    (175, 135, 215), // MediumPurple2.
187    (175, 135, 255), // MediumPurple1.
188    (175, 175, 0),   // Gold3.
189    (175, 175, 95),  // DarkKhaki.
190    (175, 175, 135), // NavajoWhite3.
191    (175, 175, 175), // Grey69.
192    (175, 175, 215), // LightSteelBlue3.
193    (175, 175, 255), // LightSteelBlue.
194    (175, 215, 0),   // Yellow3.
195    (175, 215, 95),  // DarkOliveGreen3.
196    (175, 215, 135), // DarkSeaGreen3.
197    (175, 215, 175), // DarkSeaGreen2.
198    (175, 215, 215), // LightCyan3.
199    (175, 215, 255), // LightSkyBlue1.
200    (175, 255, 0),   // GreenYellow.
201    (175, 255, 95),  // DarkOliveGreen2.
202    (175, 255, 135), // PaleGreen1.
203    (175, 255, 175), // DarkSeaGreen2.
204    (175, 255, 215), // DarkSeaGreen1.
205    (175, 255, 255), // PaleTurquoise1.
206    (215, 0, 0),     // Red3.
207    (215, 0, 95),    // DeepPink3.
208    (215, 0, 135),   // DeepPink3.
209    (215, 0, 175),   // Magenta3.
210    (215, 0, 215),   // Magenta3.
211    (215, 0, 255),   // Magenta2.
212    (215, 95, 0),    // DarkOrange3.
213    (215, 95, 95),   // IndianRed.
214    (215, 95, 135),  // HotPink3.
215    (215, 95, 175),  // HotPink2.
216    (215, 95, 215),  // Orchid.
217    (215, 95, 255),  // MediumOrchid1.
218    (215, 135, 0),   // Orange3.
219    (215, 135, 95),  // LightSalmon3.
220    (215, 135, 135), // LightPink3.
221    (215, 135, 175), // Pink3.
222    (215, 135, 215), // Plum3.
223    (215, 135, 255), // Violet.
224    (215, 175, 0),   // Gold3.
225    (215, 175, 95),  // LightGoldenrod3.
226    (215, 175, 135), // Tan.
227    (215, 175, 175), // MistyRose3.
228    (215, 175, 215), // Thistle3.
229    (215, 175, 255), // Plum2.
230    (215, 215, 0),   // Yellow3.
231    (215, 215, 95),  // Khaki3.
232    (215, 215, 135), // LightGoldenrod2.
233    (215, 215, 175), // LightYellow3.
234    (215, 215, 215), // Grey84.
235    (215, 215, 255), // LightSteelBlue1.
236    (215, 255, 0),   // Yellow2.
237    (215, 255, 95),  // DarkOliveGreen1.
238    (215, 255, 135), // DarkOliveGreen1.
239    (215, 255, 175), // DarkSeaGreen1.
240    (215, 255, 215), // Honeydew2.
241    (215, 255, 255), // LightCyan1.
242    (255, 0, 0),     // Red1.
243    (255, 0, 95),    // DeepPink2.
244    (255, 0, 135),   // DeepPink1.
245    (255, 0, 175),   // DeepPink1.
246    (255, 0, 215),   // Magenta2.
247    (255, 0, 255),   // Magenta1.
248    (255, 95, 0),    // OrangeRed1.
249    (255, 95, 95),   // IndianRed1.
250    (255, 95, 135),  // IndianRed1.
251    (255, 95, 175),  // HotPink.
252    (255, 95, 215),  // HotPink.
253    (255, 95, 255),  // MediumOrchid1.
254    (255, 135, 0),   // DarkOrange.
255    (255, 135, 95),  // Salmon1.
256    (255, 135, 135), // LightCoral.
257    (255, 135, 175), // PaleVioletRed1.
258    (255, 135, 215), // Orchid2.
259    (255, 135, 255), // Orchid1.
260    (255, 175, 0),   // Orange1.
261    (255, 175, 95),  // SandyBrown.
262    (255, 175, 135), // LightSalmon1.
263    (255, 175, 175), // LightPink1.
264    (255, 175, 215), // Pink1.
265    (255, 175, 255), // Plum1.
266    (255, 215, 0),   // Gold1.
267    (255, 215, 95),  // LightGoldenrod2.
268    (255, 215, 135), // LightGoldenrod2.
269    (255, 215, 175), // NavajoWhite1.
270    (255, 215, 215), // MistyRose1.
271    (255, 215, 255), // Thistle1.
272    (255, 255, 0),   // Yellow1.
273    (255, 255, 95),  // LightGoldenrod1.
274    (255, 255, 135), // Khaki1.
275    (255, 255, 175), // Wheat1.
276    (255, 255, 215), // CornSilk1.
277    (255, 255, 255), // Grey100.
278    (8, 8, 8),       // Grey3.
279    (18, 18, 18),    // Grey7.
280    (28, 28, 28),    // Grey11.
281    (38, 38, 38),    // Grey15.
282    (48, 48, 48),    // Grey19.
283    (58, 58, 58),    // Grey23.
284    (68, 68, 68),    // Grey27.
285    (78, 78, 78),    // Grey30.
286    (88, 88, 88),    // Grey35.
287    (98, 98, 98),    // Grey39.
288    (108, 108, 108), // Grey42.
289    (118, 118, 118), // Grey46.
290    (128, 128, 128), // Grey50.
291    (138, 138, 138), // Grey54.
292    (148, 148, 148), // Grey58.
293    (158, 158, 158), // Grey62.
294    (168, 168, 168), // Grey66.
295    (178, 178, 178), // Grey70.
296    (188, 188, 188), // Grey74.
297    (198, 198, 198), // Grey78.
298    (208, 208, 208), // Grey82.
299    (218, 218, 218), // Grey85.
300    (228, 228, 228), // Grey89.
301    (238, 238, 238), // Grey93.
302];
303
304/// Converts an ANSI color number to an RGB color.
305pub fn ansi_color_to_rgb(color: u8) -> RGB {
306    COLORS[(color as usize) % COLORS.len()]
307}