Move hook format setup earlier and add a hook_client, GitHub issue 2809.
[tmux-openbsd.git] / colour.c
blob7aa7620af18498b98f9383242753a1fb1055c2c1
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
5 * Copyright (c) 2016 Avi Halachmi <avihpit@yahoo.com>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <math.h>
27 #include "tmux.h"
29 static int
30 colour_dist_sq(int R, int G, int B, int r, int g, int b)
32 return ((R - r) * (R - r) + (G - g) * (G - g) + (B - b) * (B - b));
35 static int
36 colour_to_6cube(int v)
38 if (v < 48)
39 return (0);
40 if (v < 114)
41 return (1);
42 return ((v - 35) / 40);
46 * Convert an RGB triplet to the xterm(1) 256 colour palette.
48 * xterm provides a 6x6x6 colour cube (16 - 231) and 24 greys (232 - 255). We
49 * map our RGB colour to the closest in the cube, also work out the closest
50 * grey, and use the nearest of the two.
52 * Note that the xterm has much lower resolution for darker colours (they are
53 * not evenly spread out), so our 6 levels are not evenly spread: 0x0, 0x5f
54 * (95), 0x87 (135), 0xaf (175), 0xd7 (215) and 0xff (255). Greys are more
55 * evenly spread (8, 18, 28 ... 238).
57 int
58 colour_find_rgb(u_char r, u_char g, u_char b)
60 static const int q2c[6] = { 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff };
61 int qr, qg, qb, cr, cg, cb, d, idx;
62 int grey_avg, grey_idx, grey;
64 /* Map RGB to 6x6x6 cube. */
65 qr = colour_to_6cube(r); cr = q2c[qr];
66 qg = colour_to_6cube(g); cg = q2c[qg];
67 qb = colour_to_6cube(b); cb = q2c[qb];
69 /* If we have hit the colour exactly, return early. */
70 if (cr == r && cg == g && cb == b)
71 return ((16 + (36 * qr) + (6 * qg) + qb) | COLOUR_FLAG_256);
73 /* Work out the closest grey (average of RGB). */
74 grey_avg = (r + g + b) / 3;
75 if (grey_avg > 238)
76 grey_idx = 23;
77 else
78 grey_idx = (grey_avg - 3) / 10;
79 grey = 8 + (10 * grey_idx);
81 /* Is grey or 6x6x6 colour closest? */
82 d = colour_dist_sq(cr, cg, cb, r, g, b);
83 if (colour_dist_sq(grey, grey, grey, r, g, b) < d)
84 idx = 232 + grey_idx;
85 else
86 idx = 16 + (36 * qr) + (6 * qg) + qb;
87 return (idx | COLOUR_FLAG_256);
90 /* Join RGB into a colour. */
91 int
92 colour_join_rgb(u_char r, u_char g, u_char b)
94 return ((((int)((r) & 0xff)) << 16) |
95 (((int)((g) & 0xff)) << 8) |
96 (((int)((b) & 0xff))) | COLOUR_FLAG_RGB);
99 /* Split colour into RGB. */
100 void
101 colour_split_rgb(int c, u_char *r, u_char *g, u_char *b)
103 *r = (c >> 16) & 0xff;
104 *g = (c >> 8) & 0xff;
105 *b = c & 0xff;
108 /* Convert colour to a string. */
109 const char *
110 colour_tostring(int c)
112 static char s[32];
113 u_char r, g, b;
115 if (c == -1)
116 return ("invalid");
118 if (c & COLOUR_FLAG_RGB) {
119 colour_split_rgb(c, &r, &g, &b);
120 xsnprintf(s, sizeof s, "#%02x%02x%02x", r, g, b);
121 return (s);
124 if (c & COLOUR_FLAG_256) {
125 xsnprintf(s, sizeof s, "colour%u", c & 0xff);
126 return (s);
129 switch (c) {
130 case 0:
131 return ("black");
132 case 1:
133 return ("red");
134 case 2:
135 return ("green");
136 case 3:
137 return ("yellow");
138 case 4:
139 return ("blue");
140 case 5:
141 return ("magenta");
142 case 6:
143 return ("cyan");
144 case 7:
145 return ("white");
146 case 8:
147 return ("default");
148 case 9:
149 return ("terminal");
150 case 90:
151 return ("brightblack");
152 case 91:
153 return ("brightred");
154 case 92:
155 return ("brightgreen");
156 case 93:
157 return ("brightyellow");
158 case 94:
159 return ("brightblue");
160 case 95:
161 return ("brightmagenta");
162 case 96:
163 return ("brightcyan");
164 case 97:
165 return ("brightwhite");
167 return ("invalid");
170 /* Convert colour from string. */
172 colour_fromstring(const char *s)
174 const char *errstr;
175 const char *cp;
176 int n;
177 u_char r, g, b;
179 if (*s == '#' && strlen(s) == 7) {
180 for (cp = s + 1; isxdigit((u_char) *cp); cp++)
182 if (*cp != '\0')
183 return (-1);
184 n = sscanf(s + 1, "%2hhx%2hhx%2hhx", &r, &g, &b);
185 if (n != 3)
186 return (-1);
187 return (colour_join_rgb(r, g, b));
190 if (strncasecmp(s, "colour", (sizeof "colour") - 1) == 0) {
191 n = strtonum(s + (sizeof "colour") - 1, 0, 255, &errstr);
192 if (errstr != NULL)
193 return (-1);
194 return (n | COLOUR_FLAG_256);
196 if (strncasecmp(s, "color", (sizeof "color") - 1) == 0) {
197 n = strtonum(s + (sizeof "color") - 1, 0, 255, &errstr);
198 if (errstr != NULL)
199 return (-1);
200 return (n | COLOUR_FLAG_256);
203 if (strcasecmp(s, "default") == 0)
204 return (8);
205 if (strcasecmp(s, "terminal") == 0)
206 return (9);
208 if (strcasecmp(s, "black") == 0 || strcmp(s, "0") == 0)
209 return (0);
210 if (strcasecmp(s, "red") == 0 || strcmp(s, "1") == 0)
211 return (1);
212 if (strcasecmp(s, "green") == 0 || strcmp(s, "2") == 0)
213 return (2);
214 if (strcasecmp(s, "yellow") == 0 || strcmp(s, "3") == 0)
215 return (3);
216 if (strcasecmp(s, "blue") == 0 || strcmp(s, "4") == 0)
217 return (4);
218 if (strcasecmp(s, "magenta") == 0 || strcmp(s, "5") == 0)
219 return (5);
220 if (strcasecmp(s, "cyan") == 0 || strcmp(s, "6") == 0)
221 return (6);
222 if (strcasecmp(s, "white") == 0 || strcmp(s, "7") == 0)
223 return (7);
224 if (strcasecmp(s, "brightblack") == 0 || strcmp(s, "90") == 0)
225 return (90);
226 if (strcasecmp(s, "brightred") == 0 || strcmp(s, "91") == 0)
227 return (91);
228 if (strcasecmp(s, "brightgreen") == 0 || strcmp(s, "92") == 0)
229 return (92);
230 if (strcasecmp(s, "brightyellow") == 0 || strcmp(s, "93") == 0)
231 return (93);
232 if (strcasecmp(s, "brightblue") == 0 || strcmp(s, "94") == 0)
233 return (94);
234 if (strcasecmp(s, "brightmagenta") == 0 || strcmp(s, "95") == 0)
235 return (95);
236 if (strcasecmp(s, "brightcyan") == 0 || strcmp(s, "96") == 0)
237 return (96);
238 if (strcasecmp(s, "brightwhite") == 0 || strcmp(s, "97") == 0)
239 return (97);
240 return (colour_byname(s));
243 /* Convert 256 colour to RGB colour. */
245 colour_256toRGB(int c)
247 static const int table[256] = {
248 0x000000, 0x800000, 0x008000, 0x808000,
249 0x000080, 0x800080, 0x008080, 0xc0c0c0,
250 0x808080, 0xff0000, 0x00ff00, 0xffff00,
251 0x0000ff, 0xff00ff, 0x00ffff, 0xffffff,
252 0x000000, 0x00005f, 0x000087, 0x0000af,
253 0x0000d7, 0x0000ff, 0x005f00, 0x005f5f,
254 0x005f87, 0x005faf, 0x005fd7, 0x005fff,
255 0x008700, 0x00875f, 0x008787, 0x0087af,
256 0x0087d7, 0x0087ff, 0x00af00, 0x00af5f,
257 0x00af87, 0x00afaf, 0x00afd7, 0x00afff,
258 0x00d700, 0x00d75f, 0x00d787, 0x00d7af,
259 0x00d7d7, 0x00d7ff, 0x00ff00, 0x00ff5f,
260 0x00ff87, 0x00ffaf, 0x00ffd7, 0x00ffff,
261 0x5f0000, 0x5f005f, 0x5f0087, 0x5f00af,
262 0x5f00d7, 0x5f00ff, 0x5f5f00, 0x5f5f5f,
263 0x5f5f87, 0x5f5faf, 0x5f5fd7, 0x5f5fff,
264 0x5f8700, 0x5f875f, 0x5f8787, 0x5f87af,
265 0x5f87d7, 0x5f87ff, 0x5faf00, 0x5faf5f,
266 0x5faf87, 0x5fafaf, 0x5fafd7, 0x5fafff,
267 0x5fd700, 0x5fd75f, 0x5fd787, 0x5fd7af,
268 0x5fd7d7, 0x5fd7ff, 0x5fff00, 0x5fff5f,
269 0x5fff87, 0x5fffaf, 0x5fffd7, 0x5fffff,
270 0x870000, 0x87005f, 0x870087, 0x8700af,
271 0x8700d7, 0x8700ff, 0x875f00, 0x875f5f,
272 0x875f87, 0x875faf, 0x875fd7, 0x875fff,
273 0x878700, 0x87875f, 0x878787, 0x8787af,
274 0x8787d7, 0x8787ff, 0x87af00, 0x87af5f,
275 0x87af87, 0x87afaf, 0x87afd7, 0x87afff,
276 0x87d700, 0x87d75f, 0x87d787, 0x87d7af,
277 0x87d7d7, 0x87d7ff, 0x87ff00, 0x87ff5f,
278 0x87ff87, 0x87ffaf, 0x87ffd7, 0x87ffff,
279 0xaf0000, 0xaf005f, 0xaf0087, 0xaf00af,
280 0xaf00d7, 0xaf00ff, 0xaf5f00, 0xaf5f5f,
281 0xaf5f87, 0xaf5faf, 0xaf5fd7, 0xaf5fff,
282 0xaf8700, 0xaf875f, 0xaf8787, 0xaf87af,
283 0xaf87d7, 0xaf87ff, 0xafaf00, 0xafaf5f,
284 0xafaf87, 0xafafaf, 0xafafd7, 0xafafff,
285 0xafd700, 0xafd75f, 0xafd787, 0xafd7af,
286 0xafd7d7, 0xafd7ff, 0xafff00, 0xafff5f,
287 0xafff87, 0xafffaf, 0xafffd7, 0xafffff,
288 0xd70000, 0xd7005f, 0xd70087, 0xd700af,
289 0xd700d7, 0xd700ff, 0xd75f00, 0xd75f5f,
290 0xd75f87, 0xd75faf, 0xd75fd7, 0xd75fff,
291 0xd78700, 0xd7875f, 0xd78787, 0xd787af,
292 0xd787d7, 0xd787ff, 0xd7af00, 0xd7af5f,
293 0xd7af87, 0xd7afaf, 0xd7afd7, 0xd7afff,
294 0xd7d700, 0xd7d75f, 0xd7d787, 0xd7d7af,
295 0xd7d7d7, 0xd7d7ff, 0xd7ff00, 0xd7ff5f,
296 0xd7ff87, 0xd7ffaf, 0xd7ffd7, 0xd7ffff,
297 0xff0000, 0xff005f, 0xff0087, 0xff00af,
298 0xff00d7, 0xff00ff, 0xff5f00, 0xff5f5f,
299 0xff5f87, 0xff5faf, 0xff5fd7, 0xff5fff,
300 0xff8700, 0xff875f, 0xff8787, 0xff87af,
301 0xff87d7, 0xff87ff, 0xffaf00, 0xffaf5f,
302 0xffaf87, 0xffafaf, 0xffafd7, 0xffafff,
303 0xffd700, 0xffd75f, 0xffd787, 0xffd7af,
304 0xffd7d7, 0xffd7ff, 0xffff00, 0xffff5f,
305 0xffff87, 0xffffaf, 0xffffd7, 0xffffff,
306 0x080808, 0x121212, 0x1c1c1c, 0x262626,
307 0x303030, 0x3a3a3a, 0x444444, 0x4e4e4e,
308 0x585858, 0x626262, 0x6c6c6c, 0x767676,
309 0x808080, 0x8a8a8a, 0x949494, 0x9e9e9e,
310 0xa8a8a8, 0xb2b2b2, 0xbcbcbc, 0xc6c6c6,
311 0xd0d0d0, 0xdadada, 0xe4e4e4, 0xeeeeee
314 return (table[c & 0xff] | COLOUR_FLAG_RGB);
317 /* Convert 256 colour to 16 colour. */
319 colour_256to16(int c)
321 static const char table[256] = {
322 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
323 0, 4, 4, 4, 12, 12, 2, 6, 4, 4, 12, 12, 2, 2, 6, 4,
324 12, 12, 2, 2, 2, 6, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10,
325 10, 10, 10, 14, 1, 5, 4, 4, 12, 12, 3, 8, 4, 4, 12, 12,
326 2, 2, 6, 4, 12, 12, 2, 2, 2, 6, 12, 12, 10, 10, 10, 10,
327 14, 12, 10, 10, 10, 10, 10, 14, 1, 1, 5, 4, 12, 12, 1, 1,
328 5, 4, 12, 12, 3, 3, 8, 4, 12, 12, 2, 2, 2, 6, 12, 12,
329 10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14, 1, 1, 1, 5,
330 12, 12, 1, 1, 1, 5, 12, 12, 1, 1, 1, 5, 12, 12, 3, 3,
331 3, 7, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14,
332 9, 9, 9, 9, 13, 12, 9, 9, 9, 9, 13, 12, 9, 9, 9, 9,
333 13, 12, 9, 9, 9, 9, 13, 12, 11, 11, 11, 11, 7, 12, 10, 10,
334 10, 10, 10, 14, 9, 9, 9, 9, 9, 13, 9, 9, 9, 9, 9, 13,
335 9, 9, 9, 9, 9, 13, 9, 9, 9, 9, 9, 13, 9, 9, 9, 9,
336 9, 13, 11, 11, 11, 11, 11, 15, 0, 0, 0, 0, 0, 0, 8, 8,
337 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 15, 15, 15, 15, 15, 15
340 return (table[c & 0xff]);
343 /* Get colour by X11 colour name. */
345 colour_byname(const char *name)
347 static const struct {
348 const char *name;
349 int c;
350 } colours[] = {
351 { "AliceBlue", 0xf0f8ff },
352 { "AntiqueWhite", 0xfaebd7 },
353 { "AntiqueWhite1", 0xffefdb },
354 { "AntiqueWhite2", 0xeedfcc },
355 { "AntiqueWhite3", 0xcdc0b0 },
356 { "AntiqueWhite4", 0x8b8378 },
357 { "BlanchedAlmond", 0xffebcd },
358 { "BlueViolet", 0x8a2be2 },
359 { "CadetBlue", 0x5f9ea0 },
360 { "CadetBlue1", 0x98f5ff },
361 { "CadetBlue2", 0x8ee5ee },
362 { "CadetBlue3", 0x7ac5cd },
363 { "CadetBlue4", 0x53868b },
364 { "CornflowerBlue", 0x6495ed },
365 { "DarkBlue", 0x00008b },
366 { "DarkCyan", 0x008b8b },
367 { "DarkGoldenrod", 0xb8860b },
368 { "DarkGoldenrod1", 0xffb90f },
369 { "DarkGoldenrod2", 0xeead0e },
370 { "DarkGoldenrod3", 0xcd950c },
371 { "DarkGoldenrod4", 0x8b6508 },
372 { "DarkGray", 0xa9a9a9 },
373 { "DarkGreen", 0x006400 },
374 { "DarkGrey", 0xa9a9a9 },
375 { "DarkKhaki", 0xbdb76b },
376 { "DarkMagenta", 0x8b008b },
377 { "DarkOliveGreen", 0x556b2f },
378 { "DarkOliveGreen1", 0xcaff70 },
379 { "DarkOliveGreen2", 0xbcee68 },
380 { "DarkOliveGreen3", 0xa2cd5a },
381 { "DarkOliveGreen4", 0x6e8b3d },
382 { "DarkOrange", 0xff8c00 },
383 { "DarkOrange1", 0xff7f00 },
384 { "DarkOrange2", 0xee7600 },
385 { "DarkOrange3", 0xcd6600 },
386 { "DarkOrange4", 0x8b4500 },
387 { "DarkOrchid", 0x9932cc },
388 { "DarkOrchid1", 0xbf3eff },
389 { "DarkOrchid2", 0xb23aee },
390 { "DarkOrchid3", 0x9a32cd },
391 { "DarkOrchid4", 0x68228b },
392 { "DarkRed", 0x8b0000 },
393 { "DarkSalmon", 0xe9967a },
394 { "DarkSeaGreen", 0x8fbc8f },
395 { "DarkSeaGreen1", 0xc1ffc1 },
396 { "DarkSeaGreen2", 0xb4eeb4 },
397 { "DarkSeaGreen3", 0x9bcd9b },
398 { "DarkSeaGreen4", 0x698b69 },
399 { "DarkSlateBlue", 0x483d8b },
400 { "DarkSlateGray", 0x2f4f4f },
401 { "DarkSlateGray1", 0x97ffff },
402 { "DarkSlateGray2", 0x8deeee },
403 { "DarkSlateGray3", 0x79cdcd },
404 { "DarkSlateGray4", 0x528b8b },
405 { "DarkSlateGrey", 0x2f4f4f },
406 { "DarkTurquoise", 0x00ced1 },
407 { "DarkViolet", 0x9400d3 },
408 { "DeepPink", 0xff1493 },
409 { "DeepPink1", 0xff1493 },
410 { "DeepPink2", 0xee1289 },
411 { "DeepPink3", 0xcd1076 },
412 { "DeepPink4", 0x8b0a50 },
413 { "DeepSkyBlue", 0x00bfff },
414 { "DeepSkyBlue1", 0x00bfff },
415 { "DeepSkyBlue2", 0x00b2ee },
416 { "DeepSkyBlue3", 0x009acd },
417 { "DeepSkyBlue4", 0x00688b },
418 { "DimGray", 0x696969 },
419 { "DimGrey", 0x696969 },
420 { "DodgerBlue", 0x1e90ff },
421 { "DodgerBlue1", 0x1e90ff },
422 { "DodgerBlue2", 0x1c86ee },
423 { "DodgerBlue3", 0x1874cd },
424 { "DodgerBlue4", 0x104e8b },
425 { "FloralWhite", 0xfffaf0 },
426 { "ForestGreen", 0x228b22 },
427 { "GhostWhite", 0xf8f8ff },
428 { "GreenYellow", 0xadff2f },
429 { "HotPink", 0xff69b4 },
430 { "HotPink1", 0xff6eb4 },
431 { "HotPink2", 0xee6aa7 },
432 { "HotPink3", 0xcd6090 },
433 { "HotPink4", 0x8b3a62 },
434 { "IndianRed", 0xcd5c5c },
435 { "IndianRed1", 0xff6a6a },
436 { "IndianRed2", 0xee6363 },
437 { "IndianRed3", 0xcd5555 },
438 { "IndianRed4", 0x8b3a3a },
439 { "LavenderBlush", 0xfff0f5 },
440 { "LavenderBlush1", 0xfff0f5 },
441 { "LavenderBlush2", 0xeee0e5 },
442 { "LavenderBlush3", 0xcdc1c5 },
443 { "LavenderBlush4", 0x8b8386 },
444 { "LawnGreen", 0x7cfc00 },
445 { "LemonChiffon", 0xfffacd },
446 { "LemonChiffon1", 0xfffacd },
447 { "LemonChiffon2", 0xeee9bf },
448 { "LemonChiffon3", 0xcdc9a5 },
449 { "LemonChiffon4", 0x8b8970 },
450 { "LightBlue", 0xadd8e6 },
451 { "LightBlue1", 0xbfefff },
452 { "LightBlue2", 0xb2dfee },
453 { "LightBlue3", 0x9ac0cd },
454 { "LightBlue4", 0x68838b },
455 { "LightCoral", 0xf08080 },
456 { "LightCyan", 0xe0ffff },
457 { "LightCyan1", 0xe0ffff },
458 { "LightCyan2", 0xd1eeee },
459 { "LightCyan3", 0xb4cdcd },
460 { "LightCyan4", 0x7a8b8b },
461 { "LightGoldenrod", 0xeedd82 },
462 { "LightGoldenrod1", 0xffec8b },
463 { "LightGoldenrod2", 0xeedc82 },
464 { "LightGoldenrod3", 0xcdbe70 },
465 { "LightGoldenrod4", 0x8b814c },
466 { "LightGoldenrodYellow", 0xfafad2 },
467 { "LightGray", 0xd3d3d3 },
468 { "LightGreen", 0x90ee90 },
469 { "LightGrey", 0xd3d3d3 },
470 { "LightPink", 0xffb6c1 },
471 { "LightPink1", 0xffaeb9 },
472 { "LightPink2", 0xeea2ad },
473 { "LightPink3", 0xcd8c95 },
474 { "LightPink4", 0x8b5f65 },
475 { "LightSalmon", 0xffa07a },
476 { "LightSalmon1", 0xffa07a },
477 { "LightSalmon2", 0xee9572 },
478 { "LightSalmon3", 0xcd8162 },
479 { "LightSalmon4", 0x8b5742 },
480 { "LightSeaGreen", 0x20b2aa },
481 { "LightSkyBlue", 0x87cefa },
482 { "LightSkyBlue1", 0xb0e2ff },
483 { "LightSkyBlue2", 0xa4d3ee },
484 { "LightSkyBlue3", 0x8db6cd },
485 { "LightSkyBlue4", 0x607b8b },
486 { "LightSlateBlue", 0x8470ff },
487 { "LightSlateGray", 0x778899 },
488 { "LightSlateGrey", 0x778899 },
489 { "LightSteelBlue", 0xb0c4de },
490 { "LightSteelBlue1", 0xcae1ff },
491 { "LightSteelBlue2", 0xbcd2ee },
492 { "LightSteelBlue3", 0xa2b5cd },
493 { "LightSteelBlue4", 0x6e7b8b },
494 { "LightYellow", 0xffffe0 },
495 { "LightYellow1", 0xffffe0 },
496 { "LightYellow2", 0xeeeed1 },
497 { "LightYellow3", 0xcdcdb4 },
498 { "LightYellow4", 0x8b8b7a },
499 { "LimeGreen", 0x32cd32 },
500 { "MediumAquamarine", 0x66cdaa },
501 { "MediumBlue", 0x0000cd },
502 { "MediumOrchid", 0xba55d3 },
503 { "MediumOrchid1", 0xe066ff },
504 { "MediumOrchid2", 0xd15fee },
505 { "MediumOrchid3", 0xb452cd },
506 { "MediumOrchid4", 0x7a378b },
507 { "MediumPurple", 0x9370db },
508 { "MediumPurple1", 0xab82ff },
509 { "MediumPurple2", 0x9f79ee },
510 { "MediumPurple3", 0x8968cd },
511 { "MediumPurple4", 0x5d478b },
512 { "MediumSeaGreen", 0x3cb371 },
513 { "MediumSlateBlue", 0x7b68ee },
514 { "MediumSpringGreen", 0x00fa9a },
515 { "MediumTurquoise", 0x48d1cc },
516 { "MediumVioletRed", 0xc71585 },
517 { "MidnightBlue", 0x191970 },
518 { "MintCream", 0xf5fffa },
519 { "MistyRose", 0xffe4e1 },
520 { "MistyRose1", 0xffe4e1 },
521 { "MistyRose2", 0xeed5d2 },
522 { "MistyRose3", 0xcdb7b5 },
523 { "MistyRose4", 0x8b7d7b },
524 { "NavajoWhite", 0xffdead },
525 { "NavajoWhite1", 0xffdead },
526 { "NavajoWhite2", 0xeecfa1 },
527 { "NavajoWhite3", 0xcdb38b },
528 { "NavajoWhite4", 0x8b795e },
529 { "NavyBlue", 0x000080 },
530 { "OldLace", 0xfdf5e6 },
531 { "OliveDrab", 0x6b8e23 },
532 { "OliveDrab1", 0xc0ff3e },
533 { "OliveDrab2", 0xb3ee3a },
534 { "OliveDrab3", 0x9acd32 },
535 { "OliveDrab4", 0x698b22 },
536 { "OrangeRed", 0xff4500 },
537 { "OrangeRed1", 0xff4500 },
538 { "OrangeRed2", 0xee4000 },
539 { "OrangeRed3", 0xcd3700 },
540 { "OrangeRed4", 0x8b2500 },
541 { "PaleGoldenrod", 0xeee8aa },
542 { "PaleGreen", 0x98fb98 },
543 { "PaleGreen1", 0x9aff9a },
544 { "PaleGreen2", 0x90ee90 },
545 { "PaleGreen3", 0x7ccd7c },
546 { "PaleGreen4", 0x548b54 },
547 { "PaleTurquoise", 0xafeeee },
548 { "PaleTurquoise1", 0xbbffff },
549 { "PaleTurquoise2", 0xaeeeee },
550 { "PaleTurquoise3", 0x96cdcd },
551 { "PaleTurquoise4", 0x668b8b },
552 { "PaleVioletRed", 0xdb7093 },
553 { "PaleVioletRed1", 0xff82ab },
554 { "PaleVioletRed2", 0xee799f },
555 { "PaleVioletRed3", 0xcd6889 },
556 { "PaleVioletRed4", 0x8b475d },
557 { "PapayaWhip", 0xffefd5 },
558 { "PeachPuff", 0xffdab9 },
559 { "PeachPuff1", 0xffdab9 },
560 { "PeachPuff2", 0xeecbad },
561 { "PeachPuff3", 0xcdaf95 },
562 { "PeachPuff4", 0x8b7765 },
563 { "PowderBlue", 0xb0e0e6 },
564 { "RebeccaPurple", 0x663399 },
565 { "RosyBrown", 0xbc8f8f },
566 { "RosyBrown1", 0xffc1c1 },
567 { "RosyBrown2", 0xeeb4b4 },
568 { "RosyBrown3", 0xcd9b9b },
569 { "RosyBrown4", 0x8b6969 },
570 { "RoyalBlue", 0x4169e1 },
571 { "RoyalBlue1", 0x4876ff },
572 { "RoyalBlue2", 0x436eee },
573 { "RoyalBlue3", 0x3a5fcd },
574 { "RoyalBlue4", 0x27408b },
575 { "SaddleBrown", 0x8b4513 },
576 { "SandyBrown", 0xf4a460 },
577 { "SeaGreen", 0x2e8b57 },
578 { "SeaGreen1", 0x54ff9f },
579 { "SeaGreen2", 0x4eee94 },
580 { "SeaGreen3", 0x43cd80 },
581 { "SeaGreen4", 0x2e8b57 },
582 { "SkyBlue", 0x87ceeb },
583 { "SkyBlue1", 0x87ceff },
584 { "SkyBlue2", 0x7ec0ee },
585 { "SkyBlue3", 0x6ca6cd },
586 { "SkyBlue4", 0x4a708b },
587 { "SlateBlue", 0x6a5acd },
588 { "SlateBlue1", 0x836fff },
589 { "SlateBlue2", 0x7a67ee },
590 { "SlateBlue3", 0x6959cd },
591 { "SlateBlue4", 0x473c8b },
592 { "SlateGray", 0x708090 },
593 { "SlateGray1", 0xc6e2ff },
594 { "SlateGray2", 0xb9d3ee },
595 { "SlateGray3", 0x9fb6cd },
596 { "SlateGray4", 0x6c7b8b },
597 { "SlateGrey", 0x708090 },
598 { "SpringGreen", 0x00ff7f },
599 { "SpringGreen1", 0x00ff7f },
600 { "SpringGreen2", 0x00ee76 },
601 { "SpringGreen3", 0x00cd66 },
602 { "SpringGreen4", 0x008b45 },
603 { "SteelBlue", 0x4682b4 },
604 { "SteelBlue1", 0x63b8ff },
605 { "SteelBlue2", 0x5cacee },
606 { "SteelBlue3", 0x4f94cd },
607 { "SteelBlue4", 0x36648b },
608 { "VioletRed", 0xd02090 },
609 { "VioletRed1", 0xff3e96 },
610 { "VioletRed2", 0xee3a8c },
611 { "VioletRed3", 0xcd3278 },
612 { "VioletRed4", 0x8b2252 },
613 { "WebGray", 0x808080 },
614 { "WebGreen", 0x008000 },
615 { "WebGrey", 0x808080 },
616 { "WebMaroon", 0x800000 },
617 { "WebPurple", 0x800080 },
618 { "WhiteSmoke", 0xf5f5f5 },
619 { "X11Gray", 0xbebebe },
620 { "X11Green", 0x00ff00 },
621 { "X11Grey", 0xbebebe },
622 { "X11Maroon", 0xb03060 },
623 { "X11Purple", 0xa020f0 },
624 { "YellowGreen", 0x9acd32 },
625 { "alice blue", 0xf0f8ff },
626 { "antique white", 0xfaebd7 },
627 { "aqua", 0x00ffff },
628 { "aquamarine", 0x7fffd4 },
629 { "aquamarine1", 0x7fffd4 },
630 { "aquamarine2", 0x76eec6 },
631 { "aquamarine3", 0x66cdaa },
632 { "aquamarine4", 0x458b74 },
633 { "azure", 0xf0ffff },
634 { "azure1", 0xf0ffff },
635 { "azure2", 0xe0eeee },
636 { "azure3", 0xc1cdcd },
637 { "azure4", 0x838b8b },
638 { "beige", 0xf5f5dc },
639 { "bisque", 0xffe4c4 },
640 { "bisque1", 0xffe4c4 },
641 { "bisque2", 0xeed5b7 },
642 { "bisque3", 0xcdb79e },
643 { "bisque4", 0x8b7d6b },
644 { "black", 0x000000 },
645 { "blanched almond", 0xffebcd },
646 { "blue violet", 0x8a2be2 },
647 { "blue", 0x0000ff },
648 { "blue1", 0x0000ff },
649 { "blue2", 0x0000ee },
650 { "blue3", 0x0000cd },
651 { "blue4", 0x00008b },
652 { "brown", 0xa52a2a },
653 { "brown1", 0xff4040 },
654 { "brown2", 0xee3b3b },
655 { "brown3", 0xcd3333 },
656 { "brown4", 0x8b2323 },
657 { "burlywood", 0xdeb887 },
658 { "burlywood1", 0xffd39b },
659 { "burlywood2", 0xeec591 },
660 { "burlywood3", 0xcdaa7d },
661 { "burlywood4", 0x8b7355 },
662 { "cadet blue", 0x5f9ea0 },
663 { "chartreuse", 0x7fff00 },
664 { "chartreuse1", 0x7fff00 },
665 { "chartreuse2", 0x76ee00 },
666 { "chartreuse3", 0x66cd00 },
667 { "chartreuse4", 0x458b00 },
668 { "chocolate", 0xd2691e },
669 { "chocolate1", 0xff7f24 },
670 { "chocolate2", 0xee7621 },
671 { "chocolate3", 0xcd661d },
672 { "chocolate4", 0x8b4513 },
673 { "coral", 0xff7f50 },
674 { "coral1", 0xff7256 },
675 { "coral2", 0xee6a50 },
676 { "coral3", 0xcd5b45 },
677 { "coral4", 0x8b3e2f },
678 { "cornflower blue", 0x6495ed },
679 { "cornsilk", 0xfff8dc },
680 { "cornsilk1", 0xfff8dc },
681 { "cornsilk2", 0xeee8cd },
682 { "cornsilk3", 0xcdc8b1 },
683 { "cornsilk4", 0x8b8878 },
684 { "crimson", 0xdc143c },
685 { "cyan", 0x00ffff },
686 { "cyan1", 0x00ffff },
687 { "cyan2", 0x00eeee },
688 { "cyan3", 0x00cdcd },
689 { "cyan4", 0x008b8b },
690 { "dark blue", 0x00008b },
691 { "dark cyan", 0x008b8b },
692 { "dark goldenrod", 0xb8860b },
693 { "dark gray", 0xa9a9a9 },
694 { "dark green", 0x006400 },
695 { "dark grey", 0xa9a9a9 },
696 { "dark khaki", 0xbdb76b },
697 { "dark magenta", 0x8b008b },
698 { "dark olive green", 0x556b2f },
699 { "dark orange", 0xff8c00 },
700 { "dark orchid", 0x9932cc },
701 { "dark red", 0x8b0000 },
702 { "dark salmon", 0xe9967a },
703 { "dark sea green", 0x8fbc8f },
704 { "dark slate blue", 0x483d8b },
705 { "dark slate gray", 0x2f4f4f },
706 { "dark slate grey", 0x2f4f4f },
707 { "dark turquoise", 0x00ced1 },
708 { "dark violet", 0x9400d3 },
709 { "deep pink", 0xff1493 },
710 { "deep sky blue", 0x00bfff },
711 { "dim gray", 0x696969 },
712 { "dim grey", 0x696969 },
713 { "dodger blue", 0x1e90ff },
714 { "firebrick", 0xb22222 },
715 { "firebrick1", 0xff3030 },
716 { "firebrick2", 0xee2c2c },
717 { "firebrick3", 0xcd2626 },
718 { "firebrick4", 0x8b1a1a },
719 { "floral white", 0xfffaf0 },
720 { "forest green", 0x228b22 },
721 { "fuchsia", 0xff00ff },
722 { "gainsboro", 0xdcdcdc },
723 { "ghost white", 0xf8f8ff },
724 { "gold", 0xffd700 },
725 { "gold1", 0xffd700 },
726 { "gold2", 0xeec900 },
727 { "gold3", 0xcdad00 },
728 { "gold4", 0x8b7500 },
729 { "goldenrod", 0xdaa520 },
730 { "goldenrod1", 0xffc125 },
731 { "goldenrod2", 0xeeb422 },
732 { "goldenrod3", 0xcd9b1d },
733 { "goldenrod4", 0x8b6914 },
734 { "green yellow", 0xadff2f },
735 { "green", 0x00ff00 },
736 { "green1", 0x00ff00 },
737 { "green2", 0x00ee00 },
738 { "green3", 0x00cd00 },
739 { "green4", 0x008b00 },
740 { "honeydew", 0xf0fff0 },
741 { "honeydew1", 0xf0fff0 },
742 { "honeydew2", 0xe0eee0 },
743 { "honeydew3", 0xc1cdc1 },
744 { "honeydew4", 0x838b83 },
745 { "hot pink", 0xff69b4 },
746 { "indian red", 0xcd5c5c },
747 { "indigo", 0x4b0082 },
748 { "ivory", 0xfffff0 },
749 { "ivory1", 0xfffff0 },
750 { "ivory2", 0xeeeee0 },
751 { "ivory3", 0xcdcdc1 },
752 { "ivory4", 0x8b8b83 },
753 { "khaki", 0xf0e68c },
754 { "khaki1", 0xfff68f },
755 { "khaki2", 0xeee685 },
756 { "khaki3", 0xcdc673 },
757 { "khaki4", 0x8b864e },
758 { "lavender blush", 0xfff0f5 },
759 { "lavender", 0xe6e6fa },
760 { "lawn green", 0x7cfc00 },
761 { "lemon chiffon", 0xfffacd },
762 { "light blue", 0xadd8e6 },
763 { "light coral", 0xf08080 },
764 { "light cyan", 0xe0ffff },
765 { "light goldenrod yellow", 0xfafad2 },
766 { "light goldenrod", 0xeedd82 },
767 { "light gray", 0xd3d3d3 },
768 { "light green", 0x90ee90 },
769 { "light grey", 0xd3d3d3 },
770 { "light pink", 0xffb6c1 },
771 { "light salmon", 0xffa07a },
772 { "light sea green", 0x20b2aa },
773 { "light sky blue", 0x87cefa },
774 { "light slate blue", 0x8470ff },
775 { "light slate gray", 0x778899 },
776 { "light slate grey", 0x778899 },
777 { "light steel blue", 0xb0c4de },
778 { "light yellow", 0xffffe0 },
779 { "lime green", 0x32cd32 },
780 { "lime", 0x00ff00 },
781 { "linen", 0xfaf0e6 },
782 { "magenta", 0xff00ff },
783 { "magenta1", 0xff00ff },
784 { "magenta2", 0xee00ee },
785 { "magenta3", 0xcd00cd },
786 { "magenta4", 0x8b008b },
787 { "maroon", 0xb03060 },
788 { "maroon1", 0xff34b3 },
789 { "maroon2", 0xee30a7 },
790 { "maroon3", 0xcd2990 },
791 { "maroon4", 0x8b1c62 },
792 { "medium aquamarine", 0x66cdaa },
793 { "medium blue", 0x0000cd },
794 { "medium orchid", 0xba55d3 },
795 { "medium purple", 0x9370db },
796 { "medium sea green", 0x3cb371 },
797 { "medium slate blue", 0x7b68ee },
798 { "medium spring green", 0x00fa9a },
799 { "medium turquoise", 0x48d1cc },
800 { "medium violet red", 0xc71585 },
801 { "midnight blue", 0x191970 },
802 { "mint cream", 0xf5fffa },
803 { "misty rose", 0xffe4e1 },
804 { "moccasin", 0xffe4b5 },
805 { "navajo white", 0xffdead },
806 { "navy blue", 0x000080 },
807 { "navy", 0x000080 },
808 { "old lace", 0xfdf5e6 },
809 { "olive drab", 0x6b8e23 },
810 { "olive", 0x808000 },
811 { "orange red", 0xff4500 },
812 { "orange", 0xffa500 },
813 { "orange1", 0xffa500 },
814 { "orange2", 0xee9a00 },
815 { "orange3", 0xcd8500 },
816 { "orange4", 0x8b5a00 },
817 { "orchid", 0xda70d6 },
818 { "orchid1", 0xff83fa },
819 { "orchid2", 0xee7ae9 },
820 { "orchid3", 0xcd69c9 },
821 { "orchid4", 0x8b4789 },
822 { "pale goldenrod", 0xeee8aa },
823 { "pale green", 0x98fb98 },
824 { "pale turquoise", 0xafeeee },
825 { "pale violet red", 0xdb7093 },
826 { "papaya whip", 0xffefd5 },
827 { "peach puff", 0xffdab9 },
828 { "peru", 0xcd853f },
829 { "pink", 0xffc0cb },
830 { "pink1", 0xffb5c5 },
831 { "pink2", 0xeea9b8 },
832 { "pink3", 0xcd919e },
833 { "pink4", 0x8b636c },
834 { "plum", 0xdda0dd },
835 { "plum1", 0xffbbff },
836 { "plum2", 0xeeaeee },
837 { "plum3", 0xcd96cd },
838 { "plum4", 0x8b668b },
839 { "powder blue", 0xb0e0e6 },
840 { "purple", 0xa020f0 },
841 { "purple1", 0x9b30ff },
842 { "purple2", 0x912cee },
843 { "purple3", 0x7d26cd },
844 { "purple4", 0x551a8b },
845 { "rebecca purple", 0x663399 },
846 { "red", 0xff0000 },
847 { "red1", 0xff0000 },
848 { "red2", 0xee0000 },
849 { "red3", 0xcd0000 },
850 { "red4", 0x8b0000 },
851 { "rosy brown", 0xbc8f8f },
852 { "royal blue", 0x4169e1 },
853 { "saddle brown", 0x8b4513 },
854 { "salmon", 0xfa8072 },
855 { "salmon1", 0xff8c69 },
856 { "salmon2", 0xee8262 },
857 { "salmon3", 0xcd7054 },
858 { "salmon4", 0x8b4c39 },
859 { "sandy brown", 0xf4a460 },
860 { "sea green", 0x2e8b57 },
861 { "seashell", 0xfff5ee },
862 { "seashell1", 0xfff5ee },
863 { "seashell2", 0xeee5de },
864 { "seashell3", 0xcdc5bf },
865 { "seashell4", 0x8b8682 },
866 { "sienna", 0xa0522d },
867 { "sienna1", 0xff8247 },
868 { "sienna2", 0xee7942 },
869 { "sienna3", 0xcd6839 },
870 { "sienna4", 0x8b4726 },
871 { "silver", 0xc0c0c0 },
872 { "sky blue", 0x87ceeb },
873 { "slate blue", 0x6a5acd },
874 { "slate gray", 0x708090 },
875 { "slate grey", 0x708090 },
876 { "snow", 0xfffafa },
877 { "snow1", 0xfffafa },
878 { "snow2", 0xeee9e9 },
879 { "snow3", 0xcdc9c9 },
880 { "snow4", 0x8b8989 },
881 { "spring green", 0x00ff7f },
882 { "steel blue", 0x4682b4 },
883 { "tan", 0xd2b48c },
884 { "tan1", 0xffa54f },
885 { "tan2", 0xee9a49 },
886 { "tan3", 0xcd853f },
887 { "tan4", 0x8b5a2b },
888 { "teal", 0x008080 },
889 { "thistle", 0xd8bfd8 },
890 { "thistle1", 0xffe1ff },
891 { "thistle2", 0xeed2ee },
892 { "thistle3", 0xcdb5cd },
893 { "thistle4", 0x8b7b8b },
894 { "tomato", 0xff6347 },
895 { "tomato1", 0xff6347 },
896 { "tomato2", 0xee5c42 },
897 { "tomato3", 0xcd4f39 },
898 { "tomato4", 0x8b3626 },
899 { "turquoise", 0x40e0d0 },
900 { "turquoise1", 0x00f5ff },
901 { "turquoise2", 0x00e5ee },
902 { "turquoise3", 0x00c5cd },
903 { "turquoise4", 0x00868b },
904 { "violet red", 0xd02090 },
905 { "violet", 0xee82ee },
906 { "web gray", 0x808080 },
907 { "web green", 0x008000 },
908 { "web grey", 0x808080 },
909 { "web maroon", 0x800000 },
910 { "web purple", 0x800080 },
911 { "wheat", 0xf5deb3 },
912 { "wheat1", 0xffe7ba },
913 { "wheat2", 0xeed8ae },
914 { "wheat3", 0xcdba96 },
915 { "wheat4", 0x8b7e66 },
916 { "white smoke", 0xf5f5f5 },
917 { "white", 0xffffff },
918 { "x11 gray", 0xbebebe },
919 { "x11 green", 0x00ff00 },
920 { "x11 grey", 0xbebebe },
921 { "x11 maroon", 0xb03060 },
922 { "x11 purple", 0xa020f0 },
923 { "yellow green", 0x9acd32 },
924 { "yellow", 0xffff00 },
925 { "yellow1", 0xffff00 },
926 { "yellow2", 0xeeee00 },
927 { "yellow3", 0xcdcd00 },
928 { "yellow4", 0x8b8b00 }
930 u_int i;
931 int c;
933 if (strncmp(name, "grey", 4) == 0 || strncmp(name, "gray", 4) == 0) {
934 if (!isdigit((u_char)name[4]))
935 return (0xbebebe|COLOUR_FLAG_RGB);
936 c = round(2.55 * atoi(name + 4));
937 if (c < 0 || c > 255)
938 return (-1);
939 return (colour_join_rgb(c, c, c));
941 for (i = 0; i < nitems(colours); i++) {
942 if (strcasecmp(colours[i].name, name) == 0)
943 return (colours[i].c|COLOUR_FLAG_RGB);
945 return (-1);
948 /* Initialize palette. */
949 void
950 colour_palette_init(struct colour_palette *p)
952 p->fg = 8;
953 p->bg = 8;
954 p->palette = NULL;
955 p->default_palette = NULL;
958 /* Clear palette. */
959 void
960 colour_palette_clear(struct colour_palette *p)
962 p->fg = 8;
963 p->bg = 8;
964 if (p != NULL) {
965 free(p->palette);
966 p->palette = NULL;
970 /* Free a palette. */
971 void
972 colour_palette_free(struct colour_palette *p)
974 if (p != NULL) {
975 free(p->palette);
976 p->palette = NULL;
977 free(p->default_palette);
978 p->default_palette = NULL;
982 /* Get a colour from a palette. */
984 colour_palette_get(struct colour_palette *p, int c)
986 if (p == NULL)
987 return (-1);
989 if (c >= 90 && c <= 97)
990 c = 8 + c - 90;
991 else if (c & COLOUR_FLAG_256)
992 c &= ~COLOUR_FLAG_256;
993 else if (c >= 8)
994 return (-1);
996 if (p->palette != NULL && p->palette[c] != -1)
997 return (p->palette[c]);
998 if (p->default_palette != NULL && p->default_palette[c] != -1)
999 return (p->default_palette[c]);
1000 return (-1);
1003 /* Set a colour in a palette. */
1005 colour_palette_set(struct colour_palette *p, int n, int c)
1007 u_int i;
1009 if (p == NULL || n > 255)
1010 return (0);
1012 if (c == -1 && p->palette == NULL)
1013 return (0);
1015 if (c != -1 && p->palette == NULL) {
1016 if (p->palette == NULL)
1017 p->palette = xcalloc(256, sizeof *p->palette);
1018 for (i = 0; i < 256; i++)
1019 p->palette[i] = -1;
1021 p->palette[n] = c;
1022 return (1);
1025 /* Build palette defaults from an option. */
1026 void
1027 colour_palette_from_option(struct colour_palette *p, struct options *oo)
1029 struct options_entry *o;
1030 struct options_array_item *a;
1031 u_int i, n;
1032 int c;
1034 if (p == NULL)
1035 return;
1037 o = options_get(oo, "pane-colours");
1038 if ((a = options_array_first(o)) == NULL) {
1039 if (p->default_palette != NULL) {
1040 free(p->default_palette);
1041 p->default_palette = NULL;
1043 return;
1045 if (p->default_palette == NULL)
1046 p->default_palette = xcalloc(256, sizeof *p->default_palette);
1047 for (i = 0; i < 256; i++)
1048 p->default_palette[i] = -1;
1049 while (a != NULL) {
1050 n = options_array_item_index(a);
1051 if (n < 256) {
1052 c = options_array_item_value(a)->number;
1053 p->default_palette[n] = c;
1055 a = options_array_next(a);