199e242abSchristos /* $OpenBSD$ */ 2698d5317Sjmmv 3698d5317Sjmmv /* 4f26e8bc9Schristos * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com> 5e9a2d6faSchristos * Copyright (c) 2016 Avi Halachmi <avihpit@yahoo.com> 6698d5317Sjmmv * 7698d5317Sjmmv * Permission to use, copy, modify, and distribute this software for any 8698d5317Sjmmv * purpose with or without fee is hereby granted, provided that the above 9698d5317Sjmmv * copyright notice and this permission notice appear in all copies. 10698d5317Sjmmv * 11698d5317Sjmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12698d5317Sjmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13698d5317Sjmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14698d5317Sjmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15698d5317Sjmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 16698d5317Sjmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 17698d5317Sjmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18698d5317Sjmmv */ 19698d5317Sjmmv 20698d5317Sjmmv #include <sys/types.h> 21698d5317Sjmmv 22d530c4d0Sjmmv #include <ctype.h> 23698d5317Sjmmv #include <stdlib.h> 24698d5317Sjmmv #include <string.h> 25e271dbb8Schristos #include <math.h> 26698d5317Sjmmv 27698d5317Sjmmv #include "tmux.h" 28698d5317Sjmmv 29e9a2d6faSchristos static int 30e9a2d6faSchristos colour_dist_sq(int R, int G, int B, int r, int g, int b) 31d530c4d0Sjmmv { 32e9a2d6faSchristos return ((R - r) * (R - r) + (G - g) * (G - g) + (B - b) * (B - b)); 33d530c4d0Sjmmv } 34d530c4d0Sjmmv 35e9a2d6faSchristos static int 36e9a2d6faSchristos colour_to_6cube(int v) 37e9a2d6faSchristos { 38e9a2d6faSchristos if (v < 48) 39e9a2d6faSchristos return (0); 40e9a2d6faSchristos if (v < 114) 41e9a2d6faSchristos return (1); 42e9a2d6faSchristos return ((v - 35) / 40); 43e9a2d6faSchristos } 44e9a2d6faSchristos 45e9a2d6faSchristos /* 46e9a2d6faSchristos * Convert an RGB triplet to the xterm(1) 256 colour palette. 47e9a2d6faSchristos * 48e9a2d6faSchristos * xterm provides a 6x6x6 colour cube (16 - 231) and 24 greys (232 - 255). We 49e9a2d6faSchristos * map our RGB colour to the closest in the cube, also work out the closest 50e9a2d6faSchristos * grey, and use the nearest of the two. 51e9a2d6faSchristos * 52e9a2d6faSchristos * Note that the xterm has much lower resolution for darker colours (they are 53e9a2d6faSchristos * not evenly spread out), so our 6 levels are not evenly spread: 0x0, 0x5f 54e9a2d6faSchristos * (95), 0x87 (135), 0xaf (175), 0xd7 (215) and 0xff (255). Greys are more 55e9a2d6faSchristos * evenly spread (8, 18, 28 ... 238). 56e9a2d6faSchristos */ 57d530c4d0Sjmmv int 5899e242abSchristos colour_find_rgb(u_char r, u_char g, u_char b) 59d530c4d0Sjmmv { 60e9a2d6faSchristos static const int q2c[6] = { 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff }; 61e9a2d6faSchristos int qr, qg, qb, cr, cg, cb, d, idx; 62e9a2d6faSchristos int grey_avg, grey_idx, grey; 63d530c4d0Sjmmv 64e9a2d6faSchristos /* Map RGB to 6x6x6 cube. */ 65e9a2d6faSchristos qr = colour_to_6cube(r); cr = q2c[qr]; 66e9a2d6faSchristos qg = colour_to_6cube(g); cg = q2c[qg]; 67e9a2d6faSchristos qb = colour_to_6cube(b); cb = q2c[qb]; 68d530c4d0Sjmmv 69e9a2d6faSchristos /* If we have hit the colour exactly, return early. */ 70e9a2d6faSchristos if (cr == r && cg == g && cb == b) 71e9a2d6faSchristos return ((16 + (36 * qr) + (6 * qg) + qb) | COLOUR_FLAG_256); 7299e242abSchristos 73e9a2d6faSchristos /* Work out the closest grey (average of RGB). */ 74e9a2d6faSchristos grey_avg = (r + g + b) / 3; 75e9a2d6faSchristos if (grey_avg > 238) 76e9a2d6faSchristos grey_idx = 23; 77e9a2d6faSchristos else 78e9a2d6faSchristos grey_idx = (grey_avg - 3) / 10; 79e9a2d6faSchristos grey = 8 + (10 * grey_idx); 80e9a2d6faSchristos 81e9a2d6faSchristos /* Is grey or 6x6x6 colour closest? */ 82e9a2d6faSchristos d = colour_dist_sq(cr, cg, cb, r, g, b); 83e9a2d6faSchristos if (colour_dist_sq(grey, grey, grey, r, g, b) < d) 84e9a2d6faSchristos idx = 232 + grey_idx; 85e9a2d6faSchristos else 86e9a2d6faSchristos idx = 16 + (36 * qr) + (6 * qg) + qb; 87e9a2d6faSchristos return (idx | COLOUR_FLAG_256); 88d530c4d0Sjmmv } 89d530c4d0Sjmmv 90e9a2d6faSchristos /* Join RGB into a colour. */ 91e9a2d6faSchristos int 92e9a2d6faSchristos colour_join_rgb(u_char r, u_char g, u_char b) 93698d5317Sjmmv { 94e9a2d6faSchristos return ((((int)((r) & 0xff)) << 16) | 95e9a2d6faSchristos (((int)((g) & 0xff)) << 8) | 96e9a2d6faSchristos (((int)((b) & 0xff))) | COLOUR_FLAG_RGB); 97698d5317Sjmmv } 98698d5317Sjmmv 99e9a2d6faSchristos /* Split colour into RGB. */ 100698d5317Sjmmv void 101e9a2d6faSchristos colour_split_rgb(int c, u_char *r, u_char *g, u_char *b) 102698d5317Sjmmv { 103e9a2d6faSchristos *r = (c >> 16) & 0xff; 104e9a2d6faSchristos *g = (c >> 8) & 0xff; 105e9a2d6faSchristos *b = c & 0xff; 106698d5317Sjmmv } 107698d5317Sjmmv 10846548964Swiz /* Force colour to RGB if not already. */ 10946548964Swiz int 11046548964Swiz colour_force_rgb(int c) 11146548964Swiz { 11246548964Swiz if (c & COLOUR_FLAG_RGB) 11346548964Swiz return (c); 11446548964Swiz if (c & COLOUR_FLAG_256) 11546548964Swiz return (colour_256toRGB(c)); 11646548964Swiz if (c >= 0 && c <= 7) 11746548964Swiz return (colour_256toRGB(c)); 11846548964Swiz if (c >= 90 && c <= 97) 11946548964Swiz return (colour_256toRGB(8 + c - 90)); 12046548964Swiz return (-1); 12146548964Swiz } 12246548964Swiz 123d530c4d0Sjmmv /* Convert colour to a string. */ 124698d5317Sjmmv const char * 125698d5317Sjmmv colour_tostring(int c) 126698d5317Sjmmv { 127698d5317Sjmmv static char s[32]; 128e9a2d6faSchristos u_char r, g, b; 129698d5317Sjmmv 130e271dbb8Schristos if (c == -1) 13146548964Swiz return ("none"); 132e271dbb8Schristos 133e9a2d6faSchristos if (c & COLOUR_FLAG_RGB) { 134e9a2d6faSchristos colour_split_rgb(c, &r, &g, &b); 135e9a2d6faSchristos xsnprintf(s, sizeof s, "#%02x%02x%02x", r, g, b); 136e9a2d6faSchristos return (s); 137e9a2d6faSchristos } 138e9a2d6faSchristos 139e9a2d6faSchristos if (c & COLOUR_FLAG_256) { 140e9a2d6faSchristos xsnprintf(s, sizeof s, "colour%u", c & 0xff); 141698d5317Sjmmv return (s); 142698d5317Sjmmv } 143698d5317Sjmmv 144698d5317Sjmmv switch (c) { 145698d5317Sjmmv case 0: 146698d5317Sjmmv return ("black"); 147698d5317Sjmmv case 1: 148698d5317Sjmmv return ("red"); 149698d5317Sjmmv case 2: 150698d5317Sjmmv return ("green"); 151698d5317Sjmmv case 3: 152698d5317Sjmmv return ("yellow"); 153698d5317Sjmmv case 4: 154698d5317Sjmmv return ("blue"); 155698d5317Sjmmv case 5: 156698d5317Sjmmv return ("magenta"); 157698d5317Sjmmv case 6: 158698d5317Sjmmv return ("cyan"); 159698d5317Sjmmv case 7: 160698d5317Sjmmv return ("white"); 161698d5317Sjmmv case 8: 162698d5317Sjmmv return ("default"); 1630a274e86Schristos case 9: 1640a274e86Schristos return ("terminal"); 16561fba46bSchristos case 90: 16661fba46bSchristos return ("brightblack"); 16761fba46bSchristos case 91: 16861fba46bSchristos return ("brightred"); 16961fba46bSchristos case 92: 17061fba46bSchristos return ("brightgreen"); 17161fba46bSchristos case 93: 17261fba46bSchristos return ("brightyellow"); 17361fba46bSchristos case 94: 17461fba46bSchristos return ("brightblue"); 17561fba46bSchristos case 95: 17661fba46bSchristos return ("brightmagenta"); 17761fba46bSchristos case 96: 17861fba46bSchristos return ("brightcyan"); 17961fba46bSchristos case 97: 18061fba46bSchristos return ("brightwhite"); 181698d5317Sjmmv } 1820a274e86Schristos return ("invalid"); 183698d5317Sjmmv } 184698d5317Sjmmv 185d530c4d0Sjmmv /* Convert colour from string. */ 186698d5317Sjmmv int 187698d5317Sjmmv colour_fromstring(const char *s) 188698d5317Sjmmv { 189698d5317Sjmmv const char *errstr; 190d530c4d0Sjmmv const char *cp; 191698d5317Sjmmv int n; 19299e242abSchristos u_char r, g, b; 193698d5317Sjmmv 194d530c4d0Sjmmv if (*s == '#' && strlen(s) == 7) { 195d530c4d0Sjmmv for (cp = s + 1; isxdigit((u_char) *cp); cp++) 196d530c4d0Sjmmv ; 197d530c4d0Sjmmv if (*cp != '\0') 198d530c4d0Sjmmv return (-1); 19999e242abSchristos n = sscanf(s + 1, "%2hhx%2hhx%2hhx", &r, &g, &b); 200d530c4d0Sjmmv if (n != 3) 201d530c4d0Sjmmv return (-1); 202e9a2d6faSchristos return (colour_join_rgb(r, g, b)); 203d530c4d0Sjmmv } 204d530c4d0Sjmmv 205698d5317Sjmmv if (strncasecmp(s, "colour", (sizeof "colour") - 1) == 0) { 206698d5317Sjmmv n = strtonum(s + (sizeof "colour") - 1, 0, 255, &errstr); 207698d5317Sjmmv if (errstr != NULL) 208698d5317Sjmmv return (-1); 209e9a2d6faSchristos return (n | COLOUR_FLAG_256); 210698d5317Sjmmv } 211e271dbb8Schristos if (strncasecmp(s, "color", (sizeof "color") - 1) == 0) { 212e271dbb8Schristos n = strtonum(s + (sizeof "color") - 1, 0, 255, &errstr); 213e271dbb8Schristos if (errstr != NULL) 214e271dbb8Schristos return (-1); 215e271dbb8Schristos return (n | COLOUR_FLAG_256); 216e271dbb8Schristos } 217698d5317Sjmmv 2180a274e86Schristos if (strcasecmp(s, "default") == 0) 2190a274e86Schristos return (8); 2200a274e86Schristos if (strcasecmp(s, "terminal") == 0) 2210a274e86Schristos return (9); 2220a274e86Schristos 22399e242abSchristos if (strcasecmp(s, "black") == 0 || strcmp(s, "0") == 0) 224698d5317Sjmmv return (0); 22599e242abSchristos if (strcasecmp(s, "red") == 0 || strcmp(s, "1") == 0) 226698d5317Sjmmv return (1); 22799e242abSchristos if (strcasecmp(s, "green") == 0 || strcmp(s, "2") == 0) 228698d5317Sjmmv return (2); 22999e242abSchristos if (strcasecmp(s, "yellow") == 0 || strcmp(s, "3") == 0) 230698d5317Sjmmv return (3); 23199e242abSchristos if (strcasecmp(s, "blue") == 0 || strcmp(s, "4") == 0) 232698d5317Sjmmv return (4); 23399e242abSchristos if (strcasecmp(s, "magenta") == 0 || strcmp(s, "5") == 0) 234698d5317Sjmmv return (5); 23599e242abSchristos if (strcasecmp(s, "cyan") == 0 || strcmp(s, "6") == 0) 236698d5317Sjmmv return (6); 23799e242abSchristos if (strcasecmp(s, "white") == 0 || strcmp(s, "7") == 0) 238698d5317Sjmmv return (7); 23999e242abSchristos if (strcasecmp(s, "brightblack") == 0 || strcmp(s, "90") == 0) 24061fba46bSchristos return (90); 24199e242abSchristos if (strcasecmp(s, "brightred") == 0 || strcmp(s, "91") == 0) 24261fba46bSchristos return (91); 24399e242abSchristos if (strcasecmp(s, "brightgreen") == 0 || strcmp(s, "92") == 0) 24461fba46bSchristos return (92); 24599e242abSchristos if (strcasecmp(s, "brightyellow") == 0 || strcmp(s, "93") == 0) 24661fba46bSchristos return (93); 24799e242abSchristos if (strcasecmp(s, "brightblue") == 0 || strcmp(s, "94") == 0) 24861fba46bSchristos return (94); 24999e242abSchristos if (strcasecmp(s, "brightmagenta") == 0 || strcmp(s, "95") == 0) 25061fba46bSchristos return (95); 25199e242abSchristos if (strcasecmp(s, "brightcyan") == 0 || strcmp(s, "96") == 0) 25261fba46bSchristos return (96); 25399e242abSchristos if (strcasecmp(s, "brightwhite") == 0 || strcmp(s, "97") == 0) 25461fba46bSchristos return (97); 255e271dbb8Schristos return (colour_byname(s)); 256698d5317Sjmmv } 257698d5317Sjmmv 25830744affSchristos /* Convert 256 colour to RGB colour. */ 25930744affSchristos int 26030744affSchristos colour_256toRGB(int c) 261698d5317Sjmmv { 26230744affSchristos static const int table[256] = { 26330744affSchristos 0x000000, 0x800000, 0x008000, 0x808000, 26430744affSchristos 0x000080, 0x800080, 0x008080, 0xc0c0c0, 26530744affSchristos 0x808080, 0xff0000, 0x00ff00, 0xffff00, 26630744affSchristos 0x0000ff, 0xff00ff, 0x00ffff, 0xffffff, 26730744affSchristos 0x000000, 0x00005f, 0x000087, 0x0000af, 26830744affSchristos 0x0000d7, 0x0000ff, 0x005f00, 0x005f5f, 26930744affSchristos 0x005f87, 0x005faf, 0x005fd7, 0x005fff, 27030744affSchristos 0x008700, 0x00875f, 0x008787, 0x0087af, 27130744affSchristos 0x0087d7, 0x0087ff, 0x00af00, 0x00af5f, 27230744affSchristos 0x00af87, 0x00afaf, 0x00afd7, 0x00afff, 27330744affSchristos 0x00d700, 0x00d75f, 0x00d787, 0x00d7af, 27430744affSchristos 0x00d7d7, 0x00d7ff, 0x00ff00, 0x00ff5f, 27530744affSchristos 0x00ff87, 0x00ffaf, 0x00ffd7, 0x00ffff, 27630744affSchristos 0x5f0000, 0x5f005f, 0x5f0087, 0x5f00af, 27730744affSchristos 0x5f00d7, 0x5f00ff, 0x5f5f00, 0x5f5f5f, 27830744affSchristos 0x5f5f87, 0x5f5faf, 0x5f5fd7, 0x5f5fff, 27930744affSchristos 0x5f8700, 0x5f875f, 0x5f8787, 0x5f87af, 28030744affSchristos 0x5f87d7, 0x5f87ff, 0x5faf00, 0x5faf5f, 28130744affSchristos 0x5faf87, 0x5fafaf, 0x5fafd7, 0x5fafff, 28230744affSchristos 0x5fd700, 0x5fd75f, 0x5fd787, 0x5fd7af, 28330744affSchristos 0x5fd7d7, 0x5fd7ff, 0x5fff00, 0x5fff5f, 28430744affSchristos 0x5fff87, 0x5fffaf, 0x5fffd7, 0x5fffff, 28530744affSchristos 0x870000, 0x87005f, 0x870087, 0x8700af, 28630744affSchristos 0x8700d7, 0x8700ff, 0x875f00, 0x875f5f, 28730744affSchristos 0x875f87, 0x875faf, 0x875fd7, 0x875fff, 28830744affSchristos 0x878700, 0x87875f, 0x878787, 0x8787af, 28930744affSchristos 0x8787d7, 0x8787ff, 0x87af00, 0x87af5f, 29030744affSchristos 0x87af87, 0x87afaf, 0x87afd7, 0x87afff, 29130744affSchristos 0x87d700, 0x87d75f, 0x87d787, 0x87d7af, 29230744affSchristos 0x87d7d7, 0x87d7ff, 0x87ff00, 0x87ff5f, 29330744affSchristos 0x87ff87, 0x87ffaf, 0x87ffd7, 0x87ffff, 29430744affSchristos 0xaf0000, 0xaf005f, 0xaf0087, 0xaf00af, 29530744affSchristos 0xaf00d7, 0xaf00ff, 0xaf5f00, 0xaf5f5f, 29630744affSchristos 0xaf5f87, 0xaf5faf, 0xaf5fd7, 0xaf5fff, 29730744affSchristos 0xaf8700, 0xaf875f, 0xaf8787, 0xaf87af, 29830744affSchristos 0xaf87d7, 0xaf87ff, 0xafaf00, 0xafaf5f, 29930744affSchristos 0xafaf87, 0xafafaf, 0xafafd7, 0xafafff, 30030744affSchristos 0xafd700, 0xafd75f, 0xafd787, 0xafd7af, 30130744affSchristos 0xafd7d7, 0xafd7ff, 0xafff00, 0xafff5f, 30230744affSchristos 0xafff87, 0xafffaf, 0xafffd7, 0xafffff, 30330744affSchristos 0xd70000, 0xd7005f, 0xd70087, 0xd700af, 30430744affSchristos 0xd700d7, 0xd700ff, 0xd75f00, 0xd75f5f, 30530744affSchristos 0xd75f87, 0xd75faf, 0xd75fd7, 0xd75fff, 30630744affSchristos 0xd78700, 0xd7875f, 0xd78787, 0xd787af, 30730744affSchristos 0xd787d7, 0xd787ff, 0xd7af00, 0xd7af5f, 30830744affSchristos 0xd7af87, 0xd7afaf, 0xd7afd7, 0xd7afff, 30930744affSchristos 0xd7d700, 0xd7d75f, 0xd7d787, 0xd7d7af, 31030744affSchristos 0xd7d7d7, 0xd7d7ff, 0xd7ff00, 0xd7ff5f, 31130744affSchristos 0xd7ff87, 0xd7ffaf, 0xd7ffd7, 0xd7ffff, 31230744affSchristos 0xff0000, 0xff005f, 0xff0087, 0xff00af, 31330744affSchristos 0xff00d7, 0xff00ff, 0xff5f00, 0xff5f5f, 31430744affSchristos 0xff5f87, 0xff5faf, 0xff5fd7, 0xff5fff, 31530744affSchristos 0xff8700, 0xff875f, 0xff8787, 0xff87af, 31630744affSchristos 0xff87d7, 0xff87ff, 0xffaf00, 0xffaf5f, 31730744affSchristos 0xffaf87, 0xffafaf, 0xffafd7, 0xffafff, 31830744affSchristos 0xffd700, 0xffd75f, 0xffd787, 0xffd7af, 31930744affSchristos 0xffd7d7, 0xffd7ff, 0xffff00, 0xffff5f, 32030744affSchristos 0xffff87, 0xffffaf, 0xffffd7, 0xffffff, 32130744affSchristos 0x080808, 0x121212, 0x1c1c1c, 0x262626, 32230744affSchristos 0x303030, 0x3a3a3a, 0x444444, 0x4e4e4e, 32330744affSchristos 0x585858, 0x626262, 0x6c6c6c, 0x767676, 32430744affSchristos 0x808080, 0x8a8a8a, 0x949494, 0x9e9e9e, 32530744affSchristos 0xa8a8a8, 0xb2b2b2, 0xbcbcbc, 0xc6c6c6, 32630744affSchristos 0xd0d0d0, 0xdadada, 0xe4e4e4, 0xeeeeee 32730744affSchristos }; 32830744affSchristos 32930744affSchristos return (table[c & 0xff] | COLOUR_FLAG_RGB); 33030744affSchristos } 33130744affSchristos 33230744affSchristos /* Convert 256 colour to 16 colour. */ 33330744affSchristos int 33430744affSchristos colour_256to16(int c) 33530744affSchristos { 33630744affSchristos static const char table[256] = { 337698d5317Sjmmv 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 338698d5317Sjmmv 0, 4, 4, 4, 12, 12, 2, 6, 4, 4, 12, 12, 2, 2, 6, 4, 339698d5317Sjmmv 12, 12, 2, 2, 2, 6, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10, 340698d5317Sjmmv 10, 10, 10, 14, 1, 5, 4, 4, 12, 12, 3, 8, 4, 4, 12, 12, 341698d5317Sjmmv 2, 2, 6, 4, 12, 12, 2, 2, 2, 6, 12, 12, 10, 10, 10, 10, 342698d5317Sjmmv 14, 12, 10, 10, 10, 10, 10, 14, 1, 1, 5, 4, 12, 12, 1, 1, 343698d5317Sjmmv 5, 4, 12, 12, 3, 3, 8, 4, 12, 12, 2, 2, 2, 6, 12, 12, 344698d5317Sjmmv 10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14, 1, 1, 1, 5, 345698d5317Sjmmv 12, 12, 1, 1, 1, 5, 12, 12, 1, 1, 1, 5, 12, 12, 3, 3, 346698d5317Sjmmv 3, 7, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14, 347698d5317Sjmmv 9, 9, 9, 9, 13, 12, 9, 9, 9, 9, 13, 12, 9, 9, 9, 9, 348698d5317Sjmmv 13, 12, 9, 9, 9, 9, 13, 12, 11, 11, 11, 11, 7, 12, 10, 10, 349698d5317Sjmmv 10, 10, 10, 14, 9, 9, 9, 9, 9, 13, 9, 9, 9, 9, 9, 13, 350698d5317Sjmmv 9, 9, 9, 9, 9, 13, 9, 9, 9, 9, 9, 13, 9, 9, 9, 9, 351698d5317Sjmmv 9, 13, 11, 11, 11, 11, 11, 15, 0, 0, 0, 0, 0, 0, 8, 8, 352698d5317Sjmmv 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 15, 15, 15, 15, 15, 15 353698d5317Sjmmv }; 354698d5317Sjmmv 35530744affSchristos return (table[c & 0xff]); 356698d5317Sjmmv } 357e271dbb8Schristos 358e271dbb8Schristos /* Get colour by X11 colour name. */ 359e271dbb8Schristos int 360e271dbb8Schristos colour_byname(const char *name) 361e271dbb8Schristos { 362e271dbb8Schristos static const struct { 363e271dbb8Schristos const char *name; 364e271dbb8Schristos int c; 365e271dbb8Schristos } colours[] = { 366e271dbb8Schristos { "AliceBlue", 0xf0f8ff }, 367e271dbb8Schristos { "AntiqueWhite", 0xfaebd7 }, 368e271dbb8Schristos { "AntiqueWhite1", 0xffefdb }, 369e271dbb8Schristos { "AntiqueWhite2", 0xeedfcc }, 370e271dbb8Schristos { "AntiqueWhite3", 0xcdc0b0 }, 371e271dbb8Schristos { "AntiqueWhite4", 0x8b8378 }, 372e271dbb8Schristos { "BlanchedAlmond", 0xffebcd }, 373e271dbb8Schristos { "BlueViolet", 0x8a2be2 }, 374e271dbb8Schristos { "CadetBlue", 0x5f9ea0 }, 375e271dbb8Schristos { "CadetBlue1", 0x98f5ff }, 376e271dbb8Schristos { "CadetBlue2", 0x8ee5ee }, 377e271dbb8Schristos { "CadetBlue3", 0x7ac5cd }, 378e271dbb8Schristos { "CadetBlue4", 0x53868b }, 379e271dbb8Schristos { "CornflowerBlue", 0x6495ed }, 380e271dbb8Schristos { "DarkBlue", 0x00008b }, 381e271dbb8Schristos { "DarkCyan", 0x008b8b }, 382e271dbb8Schristos { "DarkGoldenrod", 0xb8860b }, 383e271dbb8Schristos { "DarkGoldenrod1", 0xffb90f }, 384e271dbb8Schristos { "DarkGoldenrod2", 0xeead0e }, 385e271dbb8Schristos { "DarkGoldenrod3", 0xcd950c }, 386e271dbb8Schristos { "DarkGoldenrod4", 0x8b6508 }, 387e271dbb8Schristos { "DarkGray", 0xa9a9a9 }, 388e271dbb8Schristos { "DarkGreen", 0x006400 }, 389e271dbb8Schristos { "DarkGrey", 0xa9a9a9 }, 390e271dbb8Schristos { "DarkKhaki", 0xbdb76b }, 391e271dbb8Schristos { "DarkMagenta", 0x8b008b }, 392e271dbb8Schristos { "DarkOliveGreen", 0x556b2f }, 393e271dbb8Schristos { "DarkOliveGreen1", 0xcaff70 }, 394e271dbb8Schristos { "DarkOliveGreen2", 0xbcee68 }, 395e271dbb8Schristos { "DarkOliveGreen3", 0xa2cd5a }, 396e271dbb8Schristos { "DarkOliveGreen4", 0x6e8b3d }, 397e271dbb8Schristos { "DarkOrange", 0xff8c00 }, 398e271dbb8Schristos { "DarkOrange1", 0xff7f00 }, 399e271dbb8Schristos { "DarkOrange2", 0xee7600 }, 400e271dbb8Schristos { "DarkOrange3", 0xcd6600 }, 401e271dbb8Schristos { "DarkOrange4", 0x8b4500 }, 402e271dbb8Schristos { "DarkOrchid", 0x9932cc }, 403e271dbb8Schristos { "DarkOrchid1", 0xbf3eff }, 404e271dbb8Schristos { "DarkOrchid2", 0xb23aee }, 405e271dbb8Schristos { "DarkOrchid3", 0x9a32cd }, 406e271dbb8Schristos { "DarkOrchid4", 0x68228b }, 407e271dbb8Schristos { "DarkRed", 0x8b0000 }, 408e271dbb8Schristos { "DarkSalmon", 0xe9967a }, 409e271dbb8Schristos { "DarkSeaGreen", 0x8fbc8f }, 410e271dbb8Schristos { "DarkSeaGreen1", 0xc1ffc1 }, 411e271dbb8Schristos { "DarkSeaGreen2", 0xb4eeb4 }, 412e271dbb8Schristos { "DarkSeaGreen3", 0x9bcd9b }, 413e271dbb8Schristos { "DarkSeaGreen4", 0x698b69 }, 414e271dbb8Schristos { "DarkSlateBlue", 0x483d8b }, 415e271dbb8Schristos { "DarkSlateGray", 0x2f4f4f }, 416e271dbb8Schristos { "DarkSlateGray1", 0x97ffff }, 417e271dbb8Schristos { "DarkSlateGray2", 0x8deeee }, 418e271dbb8Schristos { "DarkSlateGray3", 0x79cdcd }, 419e271dbb8Schristos { "DarkSlateGray4", 0x528b8b }, 420e271dbb8Schristos { "DarkSlateGrey", 0x2f4f4f }, 421e271dbb8Schristos { "DarkTurquoise", 0x00ced1 }, 422e271dbb8Schristos { "DarkViolet", 0x9400d3 }, 423e271dbb8Schristos { "DeepPink", 0xff1493 }, 424e271dbb8Schristos { "DeepPink1", 0xff1493 }, 425e271dbb8Schristos { "DeepPink2", 0xee1289 }, 426e271dbb8Schristos { "DeepPink3", 0xcd1076 }, 427e271dbb8Schristos { "DeepPink4", 0x8b0a50 }, 428e271dbb8Schristos { "DeepSkyBlue", 0x00bfff }, 429e271dbb8Schristos { "DeepSkyBlue1", 0x00bfff }, 430e271dbb8Schristos { "DeepSkyBlue2", 0x00b2ee }, 431e271dbb8Schristos { "DeepSkyBlue3", 0x009acd }, 432e271dbb8Schristos { "DeepSkyBlue4", 0x00688b }, 433e271dbb8Schristos { "DimGray", 0x696969 }, 434e271dbb8Schristos { "DimGrey", 0x696969 }, 435e271dbb8Schristos { "DodgerBlue", 0x1e90ff }, 436e271dbb8Schristos { "DodgerBlue1", 0x1e90ff }, 437e271dbb8Schristos { "DodgerBlue2", 0x1c86ee }, 438e271dbb8Schristos { "DodgerBlue3", 0x1874cd }, 439e271dbb8Schristos { "DodgerBlue4", 0x104e8b }, 440e271dbb8Schristos { "FloralWhite", 0xfffaf0 }, 441e271dbb8Schristos { "ForestGreen", 0x228b22 }, 442e271dbb8Schristos { "GhostWhite", 0xf8f8ff }, 443e271dbb8Schristos { "GreenYellow", 0xadff2f }, 444e271dbb8Schristos { "HotPink", 0xff69b4 }, 445e271dbb8Schristos { "HotPink1", 0xff6eb4 }, 446e271dbb8Schristos { "HotPink2", 0xee6aa7 }, 447e271dbb8Schristos { "HotPink3", 0xcd6090 }, 448e271dbb8Schristos { "HotPink4", 0x8b3a62 }, 449e271dbb8Schristos { "IndianRed", 0xcd5c5c }, 450e271dbb8Schristos { "IndianRed1", 0xff6a6a }, 451e271dbb8Schristos { "IndianRed2", 0xee6363 }, 452e271dbb8Schristos { "IndianRed3", 0xcd5555 }, 453e271dbb8Schristos { "IndianRed4", 0x8b3a3a }, 454e271dbb8Schristos { "LavenderBlush", 0xfff0f5 }, 455e271dbb8Schristos { "LavenderBlush1", 0xfff0f5 }, 456e271dbb8Schristos { "LavenderBlush2", 0xeee0e5 }, 457e271dbb8Schristos { "LavenderBlush3", 0xcdc1c5 }, 458e271dbb8Schristos { "LavenderBlush4", 0x8b8386 }, 459e271dbb8Schristos { "LawnGreen", 0x7cfc00 }, 460e271dbb8Schristos { "LemonChiffon", 0xfffacd }, 461e271dbb8Schristos { "LemonChiffon1", 0xfffacd }, 462e271dbb8Schristos { "LemonChiffon2", 0xeee9bf }, 463e271dbb8Schristos { "LemonChiffon3", 0xcdc9a5 }, 464e271dbb8Schristos { "LemonChiffon4", 0x8b8970 }, 465e271dbb8Schristos { "LightBlue", 0xadd8e6 }, 466e271dbb8Schristos { "LightBlue1", 0xbfefff }, 467e271dbb8Schristos { "LightBlue2", 0xb2dfee }, 468e271dbb8Schristos { "LightBlue3", 0x9ac0cd }, 469e271dbb8Schristos { "LightBlue4", 0x68838b }, 470e271dbb8Schristos { "LightCoral", 0xf08080 }, 471e271dbb8Schristos { "LightCyan", 0xe0ffff }, 472e271dbb8Schristos { "LightCyan1", 0xe0ffff }, 473e271dbb8Schristos { "LightCyan2", 0xd1eeee }, 474e271dbb8Schristos { "LightCyan3", 0xb4cdcd }, 475e271dbb8Schristos { "LightCyan4", 0x7a8b8b }, 476e271dbb8Schristos { "LightGoldenrod", 0xeedd82 }, 477e271dbb8Schristos { "LightGoldenrod1", 0xffec8b }, 478e271dbb8Schristos { "LightGoldenrod2", 0xeedc82 }, 479e271dbb8Schristos { "LightGoldenrod3", 0xcdbe70 }, 480e271dbb8Schristos { "LightGoldenrod4", 0x8b814c }, 481e271dbb8Schristos { "LightGoldenrodYellow", 0xfafad2 }, 482e271dbb8Schristos { "LightGray", 0xd3d3d3 }, 483e271dbb8Schristos { "LightGreen", 0x90ee90 }, 484e271dbb8Schristos { "LightGrey", 0xd3d3d3 }, 485e271dbb8Schristos { "LightPink", 0xffb6c1 }, 486e271dbb8Schristos { "LightPink1", 0xffaeb9 }, 487e271dbb8Schristos { "LightPink2", 0xeea2ad }, 488e271dbb8Schristos { "LightPink3", 0xcd8c95 }, 489e271dbb8Schristos { "LightPink4", 0x8b5f65 }, 490e271dbb8Schristos { "LightSalmon", 0xffa07a }, 491e271dbb8Schristos { "LightSalmon1", 0xffa07a }, 492e271dbb8Schristos { "LightSalmon2", 0xee9572 }, 493e271dbb8Schristos { "LightSalmon3", 0xcd8162 }, 494e271dbb8Schristos { "LightSalmon4", 0x8b5742 }, 495e271dbb8Schristos { "LightSeaGreen", 0x20b2aa }, 496e271dbb8Schristos { "LightSkyBlue", 0x87cefa }, 497e271dbb8Schristos { "LightSkyBlue1", 0xb0e2ff }, 498e271dbb8Schristos { "LightSkyBlue2", 0xa4d3ee }, 499e271dbb8Schristos { "LightSkyBlue3", 0x8db6cd }, 500e271dbb8Schristos { "LightSkyBlue4", 0x607b8b }, 501e271dbb8Schristos { "LightSlateBlue", 0x8470ff }, 502e271dbb8Schristos { "LightSlateGray", 0x778899 }, 503e271dbb8Schristos { "LightSlateGrey", 0x778899 }, 504e271dbb8Schristos { "LightSteelBlue", 0xb0c4de }, 505e271dbb8Schristos { "LightSteelBlue1", 0xcae1ff }, 506e271dbb8Schristos { "LightSteelBlue2", 0xbcd2ee }, 507e271dbb8Schristos { "LightSteelBlue3", 0xa2b5cd }, 508e271dbb8Schristos { "LightSteelBlue4", 0x6e7b8b }, 509e271dbb8Schristos { "LightYellow", 0xffffe0 }, 510e271dbb8Schristos { "LightYellow1", 0xffffe0 }, 511e271dbb8Schristos { "LightYellow2", 0xeeeed1 }, 512e271dbb8Schristos { "LightYellow3", 0xcdcdb4 }, 513e271dbb8Schristos { "LightYellow4", 0x8b8b7a }, 514e271dbb8Schristos { "LimeGreen", 0x32cd32 }, 515e271dbb8Schristos { "MediumAquamarine", 0x66cdaa }, 516e271dbb8Schristos { "MediumBlue", 0x0000cd }, 517e271dbb8Schristos { "MediumOrchid", 0xba55d3 }, 518e271dbb8Schristos { "MediumOrchid1", 0xe066ff }, 519e271dbb8Schristos { "MediumOrchid2", 0xd15fee }, 520e271dbb8Schristos { "MediumOrchid3", 0xb452cd }, 521e271dbb8Schristos { "MediumOrchid4", 0x7a378b }, 522e271dbb8Schristos { "MediumPurple", 0x9370db }, 523e271dbb8Schristos { "MediumPurple1", 0xab82ff }, 524e271dbb8Schristos { "MediumPurple2", 0x9f79ee }, 525e271dbb8Schristos { "MediumPurple3", 0x8968cd }, 526e271dbb8Schristos { "MediumPurple4", 0x5d478b }, 527e271dbb8Schristos { "MediumSeaGreen", 0x3cb371 }, 528e271dbb8Schristos { "MediumSlateBlue", 0x7b68ee }, 529e271dbb8Schristos { "MediumSpringGreen", 0x00fa9a }, 530e271dbb8Schristos { "MediumTurquoise", 0x48d1cc }, 531e271dbb8Schristos { "MediumVioletRed", 0xc71585 }, 532e271dbb8Schristos { "MidnightBlue", 0x191970 }, 533e271dbb8Schristos { "MintCream", 0xf5fffa }, 534e271dbb8Schristos { "MistyRose", 0xffe4e1 }, 535e271dbb8Schristos { "MistyRose1", 0xffe4e1 }, 536e271dbb8Schristos { "MistyRose2", 0xeed5d2 }, 537e271dbb8Schristos { "MistyRose3", 0xcdb7b5 }, 538e271dbb8Schristos { "MistyRose4", 0x8b7d7b }, 539e271dbb8Schristos { "NavajoWhite", 0xffdead }, 540e271dbb8Schristos { "NavajoWhite1", 0xffdead }, 541e271dbb8Schristos { "NavajoWhite2", 0xeecfa1 }, 542e271dbb8Schristos { "NavajoWhite3", 0xcdb38b }, 543e271dbb8Schristos { "NavajoWhite4", 0x8b795e }, 544e271dbb8Schristos { "NavyBlue", 0x000080 }, 545e271dbb8Schristos { "OldLace", 0xfdf5e6 }, 546e271dbb8Schristos { "OliveDrab", 0x6b8e23 }, 547e271dbb8Schristos { "OliveDrab1", 0xc0ff3e }, 548e271dbb8Schristos { "OliveDrab2", 0xb3ee3a }, 549e271dbb8Schristos { "OliveDrab3", 0x9acd32 }, 550e271dbb8Schristos { "OliveDrab4", 0x698b22 }, 551e271dbb8Schristos { "OrangeRed", 0xff4500 }, 552e271dbb8Schristos { "OrangeRed1", 0xff4500 }, 553e271dbb8Schristos { "OrangeRed2", 0xee4000 }, 554e271dbb8Schristos { "OrangeRed3", 0xcd3700 }, 555e271dbb8Schristos { "OrangeRed4", 0x8b2500 }, 556e271dbb8Schristos { "PaleGoldenrod", 0xeee8aa }, 557e271dbb8Schristos { "PaleGreen", 0x98fb98 }, 558e271dbb8Schristos { "PaleGreen1", 0x9aff9a }, 559e271dbb8Schristos { "PaleGreen2", 0x90ee90 }, 560e271dbb8Schristos { "PaleGreen3", 0x7ccd7c }, 561e271dbb8Schristos { "PaleGreen4", 0x548b54 }, 562e271dbb8Schristos { "PaleTurquoise", 0xafeeee }, 563e271dbb8Schristos { "PaleTurquoise1", 0xbbffff }, 564e271dbb8Schristos { "PaleTurquoise2", 0xaeeeee }, 565e271dbb8Schristos { "PaleTurquoise3", 0x96cdcd }, 566e271dbb8Schristos { "PaleTurquoise4", 0x668b8b }, 567e271dbb8Schristos { "PaleVioletRed", 0xdb7093 }, 568e271dbb8Schristos { "PaleVioletRed1", 0xff82ab }, 569e271dbb8Schristos { "PaleVioletRed2", 0xee799f }, 570e271dbb8Schristos { "PaleVioletRed3", 0xcd6889 }, 571e271dbb8Schristos { "PaleVioletRed4", 0x8b475d }, 572e271dbb8Schristos { "PapayaWhip", 0xffefd5 }, 573e271dbb8Schristos { "PeachPuff", 0xffdab9 }, 574e271dbb8Schristos { "PeachPuff1", 0xffdab9 }, 575e271dbb8Schristos { "PeachPuff2", 0xeecbad }, 576e271dbb8Schristos { "PeachPuff3", 0xcdaf95 }, 577e271dbb8Schristos { "PeachPuff4", 0x8b7765 }, 578e271dbb8Schristos { "PowderBlue", 0xb0e0e6 }, 579e271dbb8Schristos { "RebeccaPurple", 0x663399 }, 580e271dbb8Schristos { "RosyBrown", 0xbc8f8f }, 581e271dbb8Schristos { "RosyBrown1", 0xffc1c1 }, 582e271dbb8Schristos { "RosyBrown2", 0xeeb4b4 }, 583e271dbb8Schristos { "RosyBrown3", 0xcd9b9b }, 584e271dbb8Schristos { "RosyBrown4", 0x8b6969 }, 585e271dbb8Schristos { "RoyalBlue", 0x4169e1 }, 586e271dbb8Schristos { "RoyalBlue1", 0x4876ff }, 587e271dbb8Schristos { "RoyalBlue2", 0x436eee }, 588e271dbb8Schristos { "RoyalBlue3", 0x3a5fcd }, 589e271dbb8Schristos { "RoyalBlue4", 0x27408b }, 590e271dbb8Schristos { "SaddleBrown", 0x8b4513 }, 591e271dbb8Schristos { "SandyBrown", 0xf4a460 }, 592e271dbb8Schristos { "SeaGreen", 0x2e8b57 }, 593e271dbb8Schristos { "SeaGreen1", 0x54ff9f }, 594e271dbb8Schristos { "SeaGreen2", 0x4eee94 }, 595e271dbb8Schristos { "SeaGreen3", 0x43cd80 }, 596e271dbb8Schristos { "SeaGreen4", 0x2e8b57 }, 597e271dbb8Schristos { "SkyBlue", 0x87ceeb }, 598e271dbb8Schristos { "SkyBlue1", 0x87ceff }, 599e271dbb8Schristos { "SkyBlue2", 0x7ec0ee }, 600e271dbb8Schristos { "SkyBlue3", 0x6ca6cd }, 601e271dbb8Schristos { "SkyBlue4", 0x4a708b }, 602e271dbb8Schristos { "SlateBlue", 0x6a5acd }, 603e271dbb8Schristos { "SlateBlue1", 0x836fff }, 604e271dbb8Schristos { "SlateBlue2", 0x7a67ee }, 605e271dbb8Schristos { "SlateBlue3", 0x6959cd }, 606e271dbb8Schristos { "SlateBlue4", 0x473c8b }, 607e271dbb8Schristos { "SlateGray", 0x708090 }, 608e271dbb8Schristos { "SlateGray1", 0xc6e2ff }, 609e271dbb8Schristos { "SlateGray2", 0xb9d3ee }, 610e271dbb8Schristos { "SlateGray3", 0x9fb6cd }, 611e271dbb8Schristos { "SlateGray4", 0x6c7b8b }, 612e271dbb8Schristos { "SlateGrey", 0x708090 }, 613e271dbb8Schristos { "SpringGreen", 0x00ff7f }, 614e271dbb8Schristos { "SpringGreen1", 0x00ff7f }, 615e271dbb8Schristos { "SpringGreen2", 0x00ee76 }, 616e271dbb8Schristos { "SpringGreen3", 0x00cd66 }, 617e271dbb8Schristos { "SpringGreen4", 0x008b45 }, 618e271dbb8Schristos { "SteelBlue", 0x4682b4 }, 619e271dbb8Schristos { "SteelBlue1", 0x63b8ff }, 620e271dbb8Schristos { "SteelBlue2", 0x5cacee }, 621e271dbb8Schristos { "SteelBlue3", 0x4f94cd }, 622e271dbb8Schristos { "SteelBlue4", 0x36648b }, 623e271dbb8Schristos { "VioletRed", 0xd02090 }, 624e271dbb8Schristos { "VioletRed1", 0xff3e96 }, 625e271dbb8Schristos { "VioletRed2", 0xee3a8c }, 626e271dbb8Schristos { "VioletRed3", 0xcd3278 }, 627e271dbb8Schristos { "VioletRed4", 0x8b2252 }, 628e271dbb8Schristos { "WebGray", 0x808080 }, 629e271dbb8Schristos { "WebGreen", 0x008000 }, 630e271dbb8Schristos { "WebGrey", 0x808080 }, 631e271dbb8Schristos { "WebMaroon", 0x800000 }, 632e271dbb8Schristos { "WebPurple", 0x800080 }, 633e271dbb8Schristos { "WhiteSmoke", 0xf5f5f5 }, 634e271dbb8Schristos { "X11Gray", 0xbebebe }, 635e271dbb8Schristos { "X11Green", 0x00ff00 }, 636e271dbb8Schristos { "X11Grey", 0xbebebe }, 637e271dbb8Schristos { "X11Maroon", 0xb03060 }, 638e271dbb8Schristos { "X11Purple", 0xa020f0 }, 639e271dbb8Schristos { "YellowGreen", 0x9acd32 }, 640e271dbb8Schristos { "alice blue", 0xf0f8ff }, 641e271dbb8Schristos { "antique white", 0xfaebd7 }, 642e271dbb8Schristos { "aqua", 0x00ffff }, 643e271dbb8Schristos { "aquamarine", 0x7fffd4 }, 644e271dbb8Schristos { "aquamarine1", 0x7fffd4 }, 645e271dbb8Schristos { "aquamarine2", 0x76eec6 }, 646e271dbb8Schristos { "aquamarine3", 0x66cdaa }, 647e271dbb8Schristos { "aquamarine4", 0x458b74 }, 648e271dbb8Schristos { "azure", 0xf0ffff }, 649e271dbb8Schristos { "azure1", 0xf0ffff }, 650e271dbb8Schristos { "azure2", 0xe0eeee }, 651e271dbb8Schristos { "azure3", 0xc1cdcd }, 652e271dbb8Schristos { "azure4", 0x838b8b }, 653e271dbb8Schristos { "beige", 0xf5f5dc }, 654e271dbb8Schristos { "bisque", 0xffe4c4 }, 655e271dbb8Schristos { "bisque1", 0xffe4c4 }, 656e271dbb8Schristos { "bisque2", 0xeed5b7 }, 657e271dbb8Schristos { "bisque3", 0xcdb79e }, 658e271dbb8Schristos { "bisque4", 0x8b7d6b }, 659e271dbb8Schristos { "black", 0x000000 }, 660e271dbb8Schristos { "blanched almond", 0xffebcd }, 661e271dbb8Schristos { "blue violet", 0x8a2be2 }, 662e271dbb8Schristos { "blue", 0x0000ff }, 663e271dbb8Schristos { "blue1", 0x0000ff }, 664e271dbb8Schristos { "blue2", 0x0000ee }, 665e271dbb8Schristos { "blue3", 0x0000cd }, 666e271dbb8Schristos { "blue4", 0x00008b }, 667e271dbb8Schristos { "brown", 0xa52a2a }, 668e271dbb8Schristos { "brown1", 0xff4040 }, 669e271dbb8Schristos { "brown2", 0xee3b3b }, 670e271dbb8Schristos { "brown3", 0xcd3333 }, 671e271dbb8Schristos { "brown4", 0x8b2323 }, 672e271dbb8Schristos { "burlywood", 0xdeb887 }, 673e271dbb8Schristos { "burlywood1", 0xffd39b }, 674e271dbb8Schristos { "burlywood2", 0xeec591 }, 675e271dbb8Schristos { "burlywood3", 0xcdaa7d }, 676e271dbb8Schristos { "burlywood4", 0x8b7355 }, 677e271dbb8Schristos { "cadet blue", 0x5f9ea0 }, 678e271dbb8Schristos { "chartreuse", 0x7fff00 }, 679e271dbb8Schristos { "chartreuse1", 0x7fff00 }, 680e271dbb8Schristos { "chartreuse2", 0x76ee00 }, 681e271dbb8Schristos { "chartreuse3", 0x66cd00 }, 682e271dbb8Schristos { "chartreuse4", 0x458b00 }, 683e271dbb8Schristos { "chocolate", 0xd2691e }, 684e271dbb8Schristos { "chocolate1", 0xff7f24 }, 685e271dbb8Schristos { "chocolate2", 0xee7621 }, 686e271dbb8Schristos { "chocolate3", 0xcd661d }, 687e271dbb8Schristos { "chocolate4", 0x8b4513 }, 688e271dbb8Schristos { "coral", 0xff7f50 }, 689e271dbb8Schristos { "coral1", 0xff7256 }, 690e271dbb8Schristos { "coral2", 0xee6a50 }, 691e271dbb8Schristos { "coral3", 0xcd5b45 }, 692e271dbb8Schristos { "coral4", 0x8b3e2f }, 693e271dbb8Schristos { "cornflower blue", 0x6495ed }, 694e271dbb8Schristos { "cornsilk", 0xfff8dc }, 695e271dbb8Schristos { "cornsilk1", 0xfff8dc }, 696e271dbb8Schristos { "cornsilk2", 0xeee8cd }, 697e271dbb8Schristos { "cornsilk3", 0xcdc8b1 }, 698e271dbb8Schristos { "cornsilk4", 0x8b8878 }, 699e271dbb8Schristos { "crimson", 0xdc143c }, 700e271dbb8Schristos { "cyan", 0x00ffff }, 701e271dbb8Schristos { "cyan1", 0x00ffff }, 702e271dbb8Schristos { "cyan2", 0x00eeee }, 703e271dbb8Schristos { "cyan3", 0x00cdcd }, 704e271dbb8Schristos { "cyan4", 0x008b8b }, 705e271dbb8Schristos { "dark blue", 0x00008b }, 706e271dbb8Schristos { "dark cyan", 0x008b8b }, 707e271dbb8Schristos { "dark goldenrod", 0xb8860b }, 708e271dbb8Schristos { "dark gray", 0xa9a9a9 }, 709e271dbb8Schristos { "dark green", 0x006400 }, 710e271dbb8Schristos { "dark grey", 0xa9a9a9 }, 711e271dbb8Schristos { "dark khaki", 0xbdb76b }, 712e271dbb8Schristos { "dark magenta", 0x8b008b }, 713e271dbb8Schristos { "dark olive green", 0x556b2f }, 714e271dbb8Schristos { "dark orange", 0xff8c00 }, 715e271dbb8Schristos { "dark orchid", 0x9932cc }, 716e271dbb8Schristos { "dark red", 0x8b0000 }, 717e271dbb8Schristos { "dark salmon", 0xe9967a }, 718e271dbb8Schristos { "dark sea green", 0x8fbc8f }, 719e271dbb8Schristos { "dark slate blue", 0x483d8b }, 720e271dbb8Schristos { "dark slate gray", 0x2f4f4f }, 721e271dbb8Schristos { "dark slate grey", 0x2f4f4f }, 722e271dbb8Schristos { "dark turquoise", 0x00ced1 }, 723e271dbb8Schristos { "dark violet", 0x9400d3 }, 724e271dbb8Schristos { "deep pink", 0xff1493 }, 725e271dbb8Schristos { "deep sky blue", 0x00bfff }, 726e271dbb8Schristos { "dim gray", 0x696969 }, 727e271dbb8Schristos { "dim grey", 0x696969 }, 728e271dbb8Schristos { "dodger blue", 0x1e90ff }, 729e271dbb8Schristos { "firebrick", 0xb22222 }, 730e271dbb8Schristos { "firebrick1", 0xff3030 }, 731e271dbb8Schristos { "firebrick2", 0xee2c2c }, 732e271dbb8Schristos { "firebrick3", 0xcd2626 }, 733e271dbb8Schristos { "firebrick4", 0x8b1a1a }, 734e271dbb8Schristos { "floral white", 0xfffaf0 }, 735e271dbb8Schristos { "forest green", 0x228b22 }, 736e271dbb8Schristos { "fuchsia", 0xff00ff }, 737e271dbb8Schristos { "gainsboro", 0xdcdcdc }, 738e271dbb8Schristos { "ghost white", 0xf8f8ff }, 739e271dbb8Schristos { "gold", 0xffd700 }, 740e271dbb8Schristos { "gold1", 0xffd700 }, 741e271dbb8Schristos { "gold2", 0xeec900 }, 742e271dbb8Schristos { "gold3", 0xcdad00 }, 743e271dbb8Schristos { "gold4", 0x8b7500 }, 744e271dbb8Schristos { "goldenrod", 0xdaa520 }, 745e271dbb8Schristos { "goldenrod1", 0xffc125 }, 746e271dbb8Schristos { "goldenrod2", 0xeeb422 }, 747e271dbb8Schristos { "goldenrod3", 0xcd9b1d }, 748e271dbb8Schristos { "goldenrod4", 0x8b6914 }, 749e271dbb8Schristos { "green yellow", 0xadff2f }, 750e271dbb8Schristos { "green", 0x00ff00 }, 751e271dbb8Schristos { "green1", 0x00ff00 }, 752e271dbb8Schristos { "green2", 0x00ee00 }, 753e271dbb8Schristos { "green3", 0x00cd00 }, 754e271dbb8Schristos { "green4", 0x008b00 }, 755e271dbb8Schristos { "honeydew", 0xf0fff0 }, 756e271dbb8Schristos { "honeydew1", 0xf0fff0 }, 757e271dbb8Schristos { "honeydew2", 0xe0eee0 }, 758e271dbb8Schristos { "honeydew3", 0xc1cdc1 }, 759e271dbb8Schristos { "honeydew4", 0x838b83 }, 760e271dbb8Schristos { "hot pink", 0xff69b4 }, 761e271dbb8Schristos { "indian red", 0xcd5c5c }, 762e271dbb8Schristos { "indigo", 0x4b0082 }, 763e271dbb8Schristos { "ivory", 0xfffff0 }, 764e271dbb8Schristos { "ivory1", 0xfffff0 }, 765e271dbb8Schristos { "ivory2", 0xeeeee0 }, 766e271dbb8Schristos { "ivory3", 0xcdcdc1 }, 767e271dbb8Schristos { "ivory4", 0x8b8b83 }, 768e271dbb8Schristos { "khaki", 0xf0e68c }, 769e271dbb8Schristos { "khaki1", 0xfff68f }, 770e271dbb8Schristos { "khaki2", 0xeee685 }, 771e271dbb8Schristos { "khaki3", 0xcdc673 }, 772e271dbb8Schristos { "khaki4", 0x8b864e }, 773e271dbb8Schristos { "lavender blush", 0xfff0f5 }, 774e271dbb8Schristos { "lavender", 0xe6e6fa }, 775e271dbb8Schristos { "lawn green", 0x7cfc00 }, 776e271dbb8Schristos { "lemon chiffon", 0xfffacd }, 777e271dbb8Schristos { "light blue", 0xadd8e6 }, 778e271dbb8Schristos { "light coral", 0xf08080 }, 779e271dbb8Schristos { "light cyan", 0xe0ffff }, 780e271dbb8Schristos { "light goldenrod yellow", 0xfafad2 }, 781e271dbb8Schristos { "light goldenrod", 0xeedd82 }, 782e271dbb8Schristos { "light gray", 0xd3d3d3 }, 783e271dbb8Schristos { "light green", 0x90ee90 }, 784e271dbb8Schristos { "light grey", 0xd3d3d3 }, 785e271dbb8Schristos { "light pink", 0xffb6c1 }, 786e271dbb8Schristos { "light salmon", 0xffa07a }, 787e271dbb8Schristos { "light sea green", 0x20b2aa }, 788e271dbb8Schristos { "light sky blue", 0x87cefa }, 789e271dbb8Schristos { "light slate blue", 0x8470ff }, 790e271dbb8Schristos { "light slate gray", 0x778899 }, 791e271dbb8Schristos { "light slate grey", 0x778899 }, 792e271dbb8Schristos { "light steel blue", 0xb0c4de }, 793e271dbb8Schristos { "light yellow", 0xffffe0 }, 794e271dbb8Schristos { "lime green", 0x32cd32 }, 795e271dbb8Schristos { "lime", 0x00ff00 }, 796e271dbb8Schristos { "linen", 0xfaf0e6 }, 797e271dbb8Schristos { "magenta", 0xff00ff }, 798e271dbb8Schristos { "magenta1", 0xff00ff }, 799e271dbb8Schristos { "magenta2", 0xee00ee }, 800e271dbb8Schristos { "magenta3", 0xcd00cd }, 801e271dbb8Schristos { "magenta4", 0x8b008b }, 802e271dbb8Schristos { "maroon", 0xb03060 }, 803e271dbb8Schristos { "maroon1", 0xff34b3 }, 804e271dbb8Schristos { "maroon2", 0xee30a7 }, 805e271dbb8Schristos { "maroon3", 0xcd2990 }, 806e271dbb8Schristos { "maroon4", 0x8b1c62 }, 807e271dbb8Schristos { "medium aquamarine", 0x66cdaa }, 808e271dbb8Schristos { "medium blue", 0x0000cd }, 809e271dbb8Schristos { "medium orchid", 0xba55d3 }, 810e271dbb8Schristos { "medium purple", 0x9370db }, 811e271dbb8Schristos { "medium sea green", 0x3cb371 }, 812e271dbb8Schristos { "medium slate blue", 0x7b68ee }, 813e271dbb8Schristos { "medium spring green", 0x00fa9a }, 814e271dbb8Schristos { "medium turquoise", 0x48d1cc }, 815e271dbb8Schristos { "medium violet red", 0xc71585 }, 816e271dbb8Schristos { "midnight blue", 0x191970 }, 817e271dbb8Schristos { "mint cream", 0xf5fffa }, 818e271dbb8Schristos { "misty rose", 0xffe4e1 }, 819e271dbb8Schristos { "moccasin", 0xffe4b5 }, 820e271dbb8Schristos { "navajo white", 0xffdead }, 821e271dbb8Schristos { "navy blue", 0x000080 }, 822e271dbb8Schristos { "navy", 0x000080 }, 823e271dbb8Schristos { "old lace", 0xfdf5e6 }, 824e271dbb8Schristos { "olive drab", 0x6b8e23 }, 825e271dbb8Schristos { "olive", 0x808000 }, 826e271dbb8Schristos { "orange red", 0xff4500 }, 827e271dbb8Schristos { "orange", 0xffa500 }, 828e271dbb8Schristos { "orange1", 0xffa500 }, 829e271dbb8Schristos { "orange2", 0xee9a00 }, 830e271dbb8Schristos { "orange3", 0xcd8500 }, 831e271dbb8Schristos { "orange4", 0x8b5a00 }, 832e271dbb8Schristos { "orchid", 0xda70d6 }, 833e271dbb8Schristos { "orchid1", 0xff83fa }, 834e271dbb8Schristos { "orchid2", 0xee7ae9 }, 835e271dbb8Schristos { "orchid3", 0xcd69c9 }, 836e271dbb8Schristos { "orchid4", 0x8b4789 }, 837e271dbb8Schristos { "pale goldenrod", 0xeee8aa }, 838e271dbb8Schristos { "pale green", 0x98fb98 }, 839e271dbb8Schristos { "pale turquoise", 0xafeeee }, 840e271dbb8Schristos { "pale violet red", 0xdb7093 }, 841e271dbb8Schristos { "papaya whip", 0xffefd5 }, 842e271dbb8Schristos { "peach puff", 0xffdab9 }, 843e271dbb8Schristos { "peru", 0xcd853f }, 844e271dbb8Schristos { "pink", 0xffc0cb }, 845e271dbb8Schristos { "pink1", 0xffb5c5 }, 846e271dbb8Schristos { "pink2", 0xeea9b8 }, 847e271dbb8Schristos { "pink3", 0xcd919e }, 848e271dbb8Schristos { "pink4", 0x8b636c }, 849e271dbb8Schristos { "plum", 0xdda0dd }, 850e271dbb8Schristos { "plum1", 0xffbbff }, 851e271dbb8Schristos { "plum2", 0xeeaeee }, 852e271dbb8Schristos { "plum3", 0xcd96cd }, 853e271dbb8Schristos { "plum4", 0x8b668b }, 854e271dbb8Schristos { "powder blue", 0xb0e0e6 }, 855e271dbb8Schristos { "purple", 0xa020f0 }, 856e271dbb8Schristos { "purple1", 0x9b30ff }, 857e271dbb8Schristos { "purple2", 0x912cee }, 858e271dbb8Schristos { "purple3", 0x7d26cd }, 859e271dbb8Schristos { "purple4", 0x551a8b }, 860e271dbb8Schristos { "rebecca purple", 0x663399 }, 861e271dbb8Schristos { "red", 0xff0000 }, 862e271dbb8Schristos { "red1", 0xff0000 }, 863e271dbb8Schristos { "red2", 0xee0000 }, 864e271dbb8Schristos { "red3", 0xcd0000 }, 865e271dbb8Schristos { "red4", 0x8b0000 }, 866e271dbb8Schristos { "rosy brown", 0xbc8f8f }, 867e271dbb8Schristos { "royal blue", 0x4169e1 }, 868e271dbb8Schristos { "saddle brown", 0x8b4513 }, 869e271dbb8Schristos { "salmon", 0xfa8072 }, 870e271dbb8Schristos { "salmon1", 0xff8c69 }, 871e271dbb8Schristos { "salmon2", 0xee8262 }, 872e271dbb8Schristos { "salmon3", 0xcd7054 }, 873e271dbb8Schristos { "salmon4", 0x8b4c39 }, 874e271dbb8Schristos { "sandy brown", 0xf4a460 }, 875e271dbb8Schristos { "sea green", 0x2e8b57 }, 876e271dbb8Schristos { "seashell", 0xfff5ee }, 877e271dbb8Schristos { "seashell1", 0xfff5ee }, 878e271dbb8Schristos { "seashell2", 0xeee5de }, 879e271dbb8Schristos { "seashell3", 0xcdc5bf }, 880e271dbb8Schristos { "seashell4", 0x8b8682 }, 881e271dbb8Schristos { "sienna", 0xa0522d }, 882e271dbb8Schristos { "sienna1", 0xff8247 }, 883e271dbb8Schristos { "sienna2", 0xee7942 }, 884e271dbb8Schristos { "sienna3", 0xcd6839 }, 885e271dbb8Schristos { "sienna4", 0x8b4726 }, 886e271dbb8Schristos { "silver", 0xc0c0c0 }, 887e271dbb8Schristos { "sky blue", 0x87ceeb }, 888e271dbb8Schristos { "slate blue", 0x6a5acd }, 889e271dbb8Schristos { "slate gray", 0x708090 }, 890e271dbb8Schristos { "slate grey", 0x708090 }, 891e271dbb8Schristos { "snow", 0xfffafa }, 892e271dbb8Schristos { "snow1", 0xfffafa }, 893e271dbb8Schristos { "snow2", 0xeee9e9 }, 894e271dbb8Schristos { "snow3", 0xcdc9c9 }, 895e271dbb8Schristos { "snow4", 0x8b8989 }, 896e271dbb8Schristos { "spring green", 0x00ff7f }, 897e271dbb8Schristos { "steel blue", 0x4682b4 }, 898e271dbb8Schristos { "tan", 0xd2b48c }, 899e271dbb8Schristos { "tan1", 0xffa54f }, 900e271dbb8Schristos { "tan2", 0xee9a49 }, 901e271dbb8Schristos { "tan3", 0xcd853f }, 902e271dbb8Schristos { "tan4", 0x8b5a2b }, 903e271dbb8Schristos { "teal", 0x008080 }, 904e271dbb8Schristos { "thistle", 0xd8bfd8 }, 905e271dbb8Schristos { "thistle1", 0xffe1ff }, 906e271dbb8Schristos { "thistle2", 0xeed2ee }, 907e271dbb8Schristos { "thistle3", 0xcdb5cd }, 908e271dbb8Schristos { "thistle4", 0x8b7b8b }, 909e271dbb8Schristos { "tomato", 0xff6347 }, 910e271dbb8Schristos { "tomato1", 0xff6347 }, 911e271dbb8Schristos { "tomato2", 0xee5c42 }, 912e271dbb8Schristos { "tomato3", 0xcd4f39 }, 913e271dbb8Schristos { "tomato4", 0x8b3626 }, 914e271dbb8Schristos { "turquoise", 0x40e0d0 }, 915e271dbb8Schristos { "turquoise1", 0x00f5ff }, 916e271dbb8Schristos { "turquoise2", 0x00e5ee }, 917e271dbb8Schristos { "turquoise3", 0x00c5cd }, 918e271dbb8Schristos { "turquoise4", 0x00868b }, 919e271dbb8Schristos { "violet red", 0xd02090 }, 920e271dbb8Schristos { "violet", 0xee82ee }, 921e271dbb8Schristos { "web gray", 0x808080 }, 922e271dbb8Schristos { "web green", 0x008000 }, 923e271dbb8Schristos { "web grey", 0x808080 }, 924e271dbb8Schristos { "web maroon", 0x800000 }, 925e271dbb8Schristos { "web purple", 0x800080 }, 926e271dbb8Schristos { "wheat", 0xf5deb3 }, 927e271dbb8Schristos { "wheat1", 0xffe7ba }, 928e271dbb8Schristos { "wheat2", 0xeed8ae }, 929e271dbb8Schristos { "wheat3", 0xcdba96 }, 930e271dbb8Schristos { "wheat4", 0x8b7e66 }, 931e271dbb8Schristos { "white smoke", 0xf5f5f5 }, 932e271dbb8Schristos { "white", 0xffffff }, 933e271dbb8Schristos { "x11 gray", 0xbebebe }, 934e271dbb8Schristos { "x11 green", 0x00ff00 }, 935e271dbb8Schristos { "x11 grey", 0xbebebe }, 936e271dbb8Schristos { "x11 maroon", 0xb03060 }, 937e271dbb8Schristos { "x11 purple", 0xa020f0 }, 938e271dbb8Schristos { "yellow green", 0x9acd32 }, 939e271dbb8Schristos { "yellow", 0xffff00 }, 940e271dbb8Schristos { "yellow1", 0xffff00 }, 941e271dbb8Schristos { "yellow2", 0xeeee00 }, 942e271dbb8Schristos { "yellow3", 0xcdcd00 }, 943e271dbb8Schristos { "yellow4", 0x8b8b00 } 944e271dbb8Schristos }; 945e271dbb8Schristos u_int i; 946e271dbb8Schristos int c; 947*890b6d91Swiz const char *errstr; 948e271dbb8Schristos 949e271dbb8Schristos if (strncmp(name, "grey", 4) == 0 || strncmp(name, "gray", 4) == 0) { 950*890b6d91Swiz if (name[4] == '\0') 951e271dbb8Schristos return (0xbebebe|COLOUR_FLAG_RGB); 952*890b6d91Swiz c = strtonum(name + 4, 0, 100, &errstr); 953*890b6d91Swiz if (errstr != NULL) 954*890b6d91Swiz return (-1); 955*890b6d91Swiz c = round(2.55 * c); 956e271dbb8Schristos if (c < 0 || c > 255) 957e271dbb8Schristos return (-1); 958e271dbb8Schristos return (colour_join_rgb(c, c, c)); 959e271dbb8Schristos } 960e271dbb8Schristos for (i = 0; i < nitems(colours); i++) { 961e271dbb8Schristos if (strcasecmp(colours[i].name, name) == 0) 962e271dbb8Schristos return (colours[i].c|COLOUR_FLAG_RGB); 963e271dbb8Schristos } 964e271dbb8Schristos return (-1); 965e271dbb8Schristos } 96646548964Swiz 967f844e94eSwiz /* Parse colour from an X11 string. */ 968f844e94eSwiz int 969f844e94eSwiz colour_parseX11(const char *p) 970f844e94eSwiz { 971f844e94eSwiz double c, m, y, k = 0; 972f844e94eSwiz u_int r, g, b; 973f844e94eSwiz size_t len = strlen(p); 974f844e94eSwiz int colour = -1; 975f844e94eSwiz char *copy; 976f844e94eSwiz 977f844e94eSwiz if ((len == 12 && sscanf(p, "rgb:%02x/%02x/%02x", &r, &g, &b) == 3) || 978f844e94eSwiz (len == 7 && sscanf(p, "#%02x%02x%02x", &r, &g, &b) == 3) || 979f844e94eSwiz sscanf(p, "%d,%d,%d", &r, &g, &b) == 3) 980f844e94eSwiz colour = colour_join_rgb(r, g, b); 981f844e94eSwiz else if ((len == 18 && 982f844e94eSwiz sscanf(p, "rgb:%04x/%04x/%04x", &r, &g, &b) == 3) || 983f844e94eSwiz (len == 13 && sscanf(p, "#%04x%04x%04x", &r, &g, &b) == 3)) 984f844e94eSwiz colour = colour_join_rgb(r >> 8, g >> 8, b >> 8); 985f844e94eSwiz else if ((sscanf(p, "cmyk:%lf/%lf/%lf/%lf", &c, &m, &y, &k) == 4 || 986f844e94eSwiz sscanf(p, "cmy:%lf/%lf/%lf", &c, &m, &y) == 3) && 987f844e94eSwiz c >= 0 && c <= 1 && m >= 0 && m <= 1 && 988f844e94eSwiz y >= 0 && y <= 1 && k >= 0 && k <= 1) { 989f844e94eSwiz colour = colour_join_rgb( 990f844e94eSwiz (1 - c) * (1 - k) * 255, 991f844e94eSwiz (1 - m) * (1 - k) * 255, 992f844e94eSwiz (1 - y) * (1 - k) * 255); 993f844e94eSwiz } else { 994f844e94eSwiz while (len != 0 && *p == ' ') { 995f844e94eSwiz p++; 996f844e94eSwiz len--; 997f844e94eSwiz } 998f844e94eSwiz while (len != 0 && p[len - 1] == ' ') 999f844e94eSwiz len--; 1000f844e94eSwiz copy = xstrndup(p, len); 1001f844e94eSwiz colour = colour_byname(copy); 1002f844e94eSwiz free(copy); 1003f844e94eSwiz } 1004f844e94eSwiz log_debug("%s: %s = %s", __func__, p, colour_tostring(colour)); 1005f844e94eSwiz return (colour); 1006f844e94eSwiz } 1007f844e94eSwiz 100846548964Swiz /* Initialize palette. */ 100946548964Swiz void 101046548964Swiz colour_palette_init(struct colour_palette *p) 101146548964Swiz { 101246548964Swiz p->fg = 8; 101346548964Swiz p->bg = 8; 101446548964Swiz p->palette = NULL; 101546548964Swiz p->default_palette = NULL; 101646548964Swiz } 101746548964Swiz 101846548964Swiz /* Clear palette. */ 101946548964Swiz void 102046548964Swiz colour_palette_clear(struct colour_palette *p) 102146548964Swiz { 102246548964Swiz if (p != NULL) { 102346548964Swiz p->fg = 8; 102446548964Swiz p->bg = 8; 102546548964Swiz free(p->palette); 102646548964Swiz p->palette = NULL; 102746548964Swiz } 102846548964Swiz } 102946548964Swiz 103046548964Swiz /* Free a palette. */ 103146548964Swiz void 103246548964Swiz colour_palette_free(struct colour_palette *p) 103346548964Swiz { 103446548964Swiz if (p != NULL) { 103546548964Swiz free(p->palette); 103646548964Swiz p->palette = NULL; 103746548964Swiz free(p->default_palette); 103846548964Swiz p->default_palette = NULL; 103946548964Swiz } 104046548964Swiz } 104146548964Swiz 104246548964Swiz /* Get a colour from a palette. */ 104346548964Swiz int 104446548964Swiz colour_palette_get(struct colour_palette *p, int c) 104546548964Swiz { 104646548964Swiz if (p == NULL) 104746548964Swiz return (-1); 104846548964Swiz 104946548964Swiz if (c >= 90 && c <= 97) 105046548964Swiz c = 8 + c - 90; 105146548964Swiz else if (c & COLOUR_FLAG_256) 105246548964Swiz c &= ~COLOUR_FLAG_256; 105346548964Swiz else if (c >= 8) 105446548964Swiz return (-1); 105546548964Swiz 105646548964Swiz if (p->palette != NULL && p->palette[c] != -1) 105746548964Swiz return (p->palette[c]); 105846548964Swiz if (p->default_palette != NULL && p->default_palette[c] != -1) 105946548964Swiz return (p->default_palette[c]); 106046548964Swiz return (-1); 106146548964Swiz } 106246548964Swiz 106346548964Swiz /* Set a colour in a palette. */ 106446548964Swiz int 106546548964Swiz colour_palette_set(struct colour_palette *p, int n, int c) 106646548964Swiz { 106746548964Swiz u_int i; 106846548964Swiz 106946548964Swiz if (p == NULL || n > 255) 107046548964Swiz return (0); 107146548964Swiz 107246548964Swiz if (c == -1 && p->palette == NULL) 107346548964Swiz return (0); 107446548964Swiz 107546548964Swiz if (c != -1 && p->palette == NULL) { 107646548964Swiz if (p->palette == NULL) 107746548964Swiz p->palette = xcalloc(256, sizeof *p->palette); 107846548964Swiz for (i = 0; i < 256; i++) 107946548964Swiz p->palette[i] = -1; 108046548964Swiz } 108146548964Swiz p->palette[n] = c; 108246548964Swiz return (1); 108346548964Swiz } 108446548964Swiz 108546548964Swiz /* Build palette defaults from an option. */ 108646548964Swiz void 108746548964Swiz colour_palette_from_option(struct colour_palette *p, struct options *oo) 108846548964Swiz { 108946548964Swiz struct options_entry *o; 109046548964Swiz struct options_array_item *a; 109146548964Swiz u_int i, n; 109246548964Swiz int c; 109346548964Swiz 109446548964Swiz if (p == NULL) 109546548964Swiz return; 109646548964Swiz 109746548964Swiz o = options_get(oo, "pane-colours"); 109846548964Swiz if ((a = options_array_first(o)) == NULL) { 109946548964Swiz if (p->default_palette != NULL) { 110046548964Swiz free(p->default_palette); 110146548964Swiz p->default_palette = NULL; 110246548964Swiz } 110346548964Swiz return; 110446548964Swiz } 110546548964Swiz if (p->default_palette == NULL) 110646548964Swiz p->default_palette = xcalloc(256, sizeof *p->default_palette); 110746548964Swiz for (i = 0; i < 256; i++) 110846548964Swiz p->default_palette[i] = -1; 110946548964Swiz while (a != NULL) { 111046548964Swiz n = options_array_item_index(a); 111146548964Swiz if (n < 256) { 111246548964Swiz c = options_array_item_value(a)->number; 111346548964Swiz p->default_palette[n] = c; 111446548964Swiz } 111546548964Swiz a = options_array_next(a); 111646548964Swiz } 111746548964Swiz } 1118