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