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