Source:NetHack 5.0.0/src/coloratt.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to coloratt.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/coloratt.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 5.0	coloratt.c	$NHDT-Date: 1737286550 2025/01/19 03:35:50 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.14 $ */
2. /* Copyright (c) Pasi Kallinen, 2024 */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. #include "hack.h"
6. 
7. struct color_names {
8.     const char *name;
9.     int color;
10. };
11. 
12. static const struct color_names colornames[] = {
13.     { "black", CLR_BLACK },
14.     { "red", CLR_RED },
15.     { "green", CLR_GREEN },
16.     { "brown", CLR_BROWN },
17.     { "blue", CLR_BLUE },
18.     { "magenta", CLR_MAGENTA },
19.     { "cyan", CLR_CYAN },
20.     { "gray", CLR_GRAY },
21.     { "orange", CLR_ORANGE },
22.     { "light green", CLR_BRIGHT_GREEN },
23.     { "yellow", CLR_YELLOW },
24.     { "light blue", CLR_BRIGHT_BLUE },
25.     { "light magenta", CLR_BRIGHT_MAGENTA },
26.     { "light cyan", CLR_BRIGHT_CYAN },
27.     { "white", CLR_WHITE },
28.     { "no color", NO_COLOR },
29.     { (const char *) 0, CLR_BLACK }, /* everything after this is an alias */
30.     { "transparent", NO_COLOR },
31.     { "purple", CLR_MAGENTA },
32.     { "light purple", CLR_BRIGHT_MAGENTA },
33.     { "bright purple", CLR_BRIGHT_MAGENTA },
34.     { "grey", CLR_GRAY },
35.     { "bright red", CLR_ORANGE },
36.     { "bright green", CLR_BRIGHT_GREEN },
37.     { "bright blue", CLR_BRIGHT_BLUE },
38.     { "bright magenta", CLR_BRIGHT_MAGENTA },
39.     { "bright cyan", CLR_BRIGHT_CYAN }
40. };
41. 
42. struct attr_names {
43.     const char *name;
44.     int attr;
45. };
46. 
47. static const struct attr_names attrnames[] = {
48.     { "none", ATR_NONE },
49.     { "bold", ATR_BOLD },
50.     { "dim", ATR_DIM },
51.     { "italic", ATR_ITALIC },
52.     { "underline", ATR_ULINE },
53.     { "blink", ATR_BLINK },
54.     { "inverse", ATR_INVERSE },
55.     { (const char *) 0, ATR_NONE }, /* everything after this is an alias */
56.     { "normal", ATR_NONE },
57.     { "uline", ATR_ULINE },
58.     { "reverse", ATR_INVERSE },
59. };
60. 
61. /* { colortyp, tableindex, rgbindx, name, r, g, b }, */
62. 
63. #define NHC nh_color
64. #define NOC no_color
65. #define RGBC rgb_color
66. 
67. const struct nethack_color colortable[] = {
68.     { NHC,    0,   0, "black", 0x00, 0x00, 0x00 },      /*        CLR_BLACK */
69.     { NHC,    1,   0, "red", 0xFF, 0x00, 0x00 },        /*          CLR_RED */
70.     { NHC,    2,   0, "green", 0x22, 0x8B, 0x22 },      /*        CLR_GREEN */
71.     { NHC,    3,   0, "brown", 0xA5, 0x2A, 0x2A },      /*        CLR_BROWN */
72.     { NHC,    4,   0, "blue", 0x00, 0x00, 0xFF },       /*         CLR_BLUE */
73.     { NHC,    5,   0, "magenta", 0xFF, 0x00, 0xFF },    /*      CLR_MAGENTA */
74.     { NHC,    6,   0, "cyan", 0x00, 0xFF, 0xFF },       /*         CLR_CYAN */
75.     { NHC,    7,   0, "gray", 0x80, 0x80, 0x80 },       /*         CLR_GRAY */
76.     { NOC,    8,   0, "nocolor", 0x00, 0x00, 0x00 },    /*         NO_COLOR */
77.     { NHC,    9,   0, "orange", 0xFF, 0xA5, 0x00 },     /*       CLR_ORANGE */
78.     { NHC,   10,   0, "bright-green",
79.                       0x00, 0x80, 0x00 },               /* CLR_BRIGHT_GREEN */
80.     { NHC,   11,   0, "yellow", 0xFF, 0xFF, 0x00 },     /*       CLR_YELLOW */
81.     { NHC,   12,   0, "bright-blue", 0xAD, 0xD8, 0xE6 }, /* CLR_BRIGHT_BLUE */
82.     { NHC,   13,   0, "bright-magenta",
83.                       0x93, 0x70, 0xDB },             /* CLR_BRIGHT_MAGENTA */
84.     { NHC,   14,   0, "light-cyan", 0xE0, 0xFF, 0xFF }, /*  CLR_BRIGHT_CYAN */
85.     { NHC,   15,   0, "white", 0xFF, 0xFF, 0xFF },      /*        CLR_WHITE */
86.     { RGBC,  16,   0, "maroon", 0x80, 0x00, 0x00 },              /* #800000 */
87.     { RGBC,  17,   1, "dark-red", 0x8B, 0x00, 0x00 },            /* #8B0000 */
88.     { RGBC,  18,   2, "brown", 0xA5, 0x2A, 0x2A },               /* #A52A2A */
89.     { RGBC,  19,   3, "firebrick", 0xB2, 0x22, 0x22 },           /* #B22222 */
90.     { RGBC,  20,   4, "crimson", 0xDC, 0x14, 0x3C },             /* #DC143C */
91.     { RGBC,  21,   5, "red", 0xFF, 0x00, 0x00 },                 /* #FF0000 */
92.     { RGBC,  22,   6, "tomato", 0xFF, 0x63, 0x47 },              /* #FF6347 */
93.     { RGBC,  23,   7, "coral", 0xFF, 0x7F, 0x50 },               /* #FF7F50 */
94.     { RGBC,  24,   8, "indian-red", 0xCD, 0x5C, 0x5C },          /* #CD5C5C */
95.     { RGBC,  25,   9, "light-coral", 0xF0, 0x80, 0x80 },         /* #F08080 */
96.     { RGBC,  26,  10, "dark-salmon", 0xE9, 0x96, 0x7A },         /* #E9967A */
97.     { RGBC,  27,  11, "salmon", 0xFA, 0x80, 0x72 },              /* #FA8072 */
98.     { RGBC,  28,  12, "light-salmon", 0xFF, 0xA0, 0x7A },        /* #FFA07A */
99.     { RGBC,  29,  13, "orange-red", 0xFF, 0x45, 0x00 },          /* #FF4500 */
100.     { RGBC,  30,  14, "dark-orange", 0xFF, 0x8C, 0x00 },         /* #FF8C00 */
101.     { RGBC,  31,  15, "orange", 0xFF, 0xA5, 0x00 },              /* #FFA500 */
102.     { RGBC,  32,  16, "gold", 0xFF, 0xD7, 0x00 },                /* #FFD700 */
103.     { RGBC,  33,  17, "dark-golden-rod", 0xB8, 0x86, 0x0B },     /* #B8860B */
104.     { RGBC,  34,  18, "golden-rod", 0xDA, 0xA5, 0x20 },          /* #DAA520 */
105.     { RGBC,  35,  19, "pale-golden-rod", 0xEE, 0xE8, 0xAA },     /* #EEE8AA */
106.     { RGBC,  36,  20, "dark-khaki", 0xBD, 0xB7, 0x6B },          /* #BDB76B */
107.     { RGBC,  37,  21, "khaki", 0xF0, 0xE6, 0x8C },               /* #F0E68C */
108.     { RGBC,  38,  22, "olive", 0x80, 0x80, 0x00 },               /* #808000 */
109.     { RGBC,  39,  23, "yellow", 0xFF, 0xFF, 0x00 },              /* #FFFF00 */
110.     { RGBC,  40,  24, "yellow-green", 0x9A, 0xCD, 0x32 },        /* #9ACD32 */
111.     { RGBC,  41,  25, "dark-olive-green", 0x55, 0x6B, 0x2F },    /* #556B2F */
112.     { RGBC,  42,  26, "olive-drab", 0x6B, 0x8E, 0x23 },          /* #6B8E23 */
113.     { RGBC,  43,  27, "lawn-green", 0x7C, 0xFC, 0x00 },          /* #7CFC00 */
114.     { RGBC,  44,  28, "chart-reuse", 0x7F, 0xFF, 0x00 },         /* #7FFF00 */
115.     { RGBC,  45,  29, "green-yellow", 0xAD, 0xFF, 0x2F },        /* #ADFF2F */
116.     { RGBC,  46,  30, "dark-green", 0x00, 0x64, 0x00 },          /* #006400 */
117.     { RGBC,  47,  31, "green", 0x00, 0x80, 0x00 },               /* #008000 */
118.     { RGBC,  48,  32, "forest-green", 0x22, 0x8B, 0x22 },        /* #228B22 */
119.     { RGBC,  49,  33, "lime", 0x00, 0xFF, 0x00 },                /* #00FF00 */
120.     { RGBC,  50,  34, "lime-green", 0x32, 0xCD, 0x32 },          /* #32CD32 */
121.     { RGBC,  51,  35, "light-green", 0x90, 0xEE, 0x90 },         /* #90EE90 */
122.     { RGBC,  52,  36, "pale-green", 0x98, 0xFB, 0x98 },          /* #98FB98 */
123.     { RGBC,  53,  37, "dark-sea-green", 0x8F, 0xBC, 0x8F },      /* #8FBC8F */
124.     { RGBC,  54,  38, "medium-spring-green", 0x00, 0xFA, 0x9A }, /* #00FA9A */
125.     { RGBC,  55,  39, "spring-green", 0x00, 0xFF, 0x7F },        /* #00FF7F */
126.     { RGBC,  56,  40, "sea-green", 0x2E, 0x8B, 0x57 },           /* #2E8B57 */
127.     { RGBC,  57,  41, "medium-aqua-marine", 0x66, 0xCD, 0xAA },  /* #66CDAA */
128.     { RGBC,  58,  42, "medium-sea-green", 0x3C, 0xB3, 0x71 },    /* #3CB371 */
129.     { RGBC,  59,  43, "light-sea-green", 0x20, 0xB2, 0xAA },     /* #20B2AA */
130.     { RGBC,  60,  44, "dark-slate-gray", 0x2F, 0x4F, 0x4F },     /* #2F4F4F */
131.     { RGBC,  61,  45, "teal", 0x00, 0x80, 0x80 },                /* #008080 */
132.     { RGBC,  62,  46, "dark-cyan", 0x00, 0x8B, 0x8B },           /* #008B8B */
133.     { RGBC,  63,  47, "aqua", 0x00, 0xFF, 0xFF },                /* #00FFFF */
134.     { RGBC,  64,  48, "cyan", 0x00, 0xFF, 0xFF },                /* #00FFFF */
135.     { RGBC,  65,  49, "light-cyan", 0xE0, 0xFF, 0xFF },          /* #E0FFFF */
136.     { RGBC,  66,  50, "dark-turquoise", 0x00, 0xCE, 0xD1 },      /* #00CED1 */
137.     { RGBC,  67,  51, "turquoise", 0x40, 0xE0, 0xD0 },           /* #40E0D0 */
138.     { RGBC,  68,  52, "medium-turquoise", 0x48, 0xD1, 0xCC },    /* #48D1CC */
139.     { RGBC,  69,  53, "pale-turquoise", 0xAF, 0xEE, 0xEE },      /* #AFEEEE */
140.     { RGBC,  70,  54, "aqua-marine", 0x7F, 0xFF, 0xD4 },         /* #7FFFD4 */
141.     { RGBC,  71,  55, "powder-blue", 0xB0, 0xE0, 0xE6 },         /* #B0E0E6 */
142.     { RGBC,  72,  56, "cadet-blue", 0x5F, 0x9E, 0xA0 },          /* #5F9EA0 */
143.     { RGBC,  73,  57, "steel-blue", 0x46, 0x82, 0xB4 },          /* #4682B4 */
144.     { RGBC,  74,  58, "corn-flower-blue", 0x64, 0x95, 0xED },    /* #6495ED */
145.     { RGBC,  75,  59, "deep-sky-blue", 0x00, 0xBF, 0xFF },       /* #00BFFF */
146.     { RGBC,  76,  60, "dodger-blue", 0x1E, 0x90, 0xFF },         /* #1E90FF */
147.     { RGBC,  77,  61, "light-blue", 0xAD, 0xD8, 0xE6 },          /* #ADD8E6 */
148.     { RGBC,  78,  62, "sky-blue", 0x87, 0xCE, 0xEB },            /* #87CEEB */
149.     { RGBC,  79,  63, "light-sky-blue", 0x87, 0xCE, 0xFA },      /* #87CEFA */
150.     { RGBC,  80,  64, "midnight-blue", 0x19, 0x19, 0x70 },       /* #191970 */
151.     { RGBC,  81,  65, "navy", 0x00, 0x00, 0x80 },                /* #000080 */
152.     { RGBC,  82,  66, "dark-blue", 0x00, 0x00, 0x8B },           /* #00008B */
153.     { RGBC,  83,  67, "medium-blue", 0x00, 0x00, 0xCD },         /* #0000CD */
154.     { RGBC,  84,  68, "blue", 0x00, 0x00, 0xFF },                /* #0000FF */
155.     { RGBC,  85,  69, "royal-blue", 0x41, 0x69, 0xE1 },          /* #4169E1 */
156.     { RGBC,  86,  70, "blue-violet", 0x8A, 0x2B, 0xE2 },         /* #8A2BE2 */
157.     { RGBC,  87,  71, "indigo", 0x4B, 0x00, 0x82 },              /* #4B0082 */
158.     { RGBC,  88,  72, "dark-slate-blue", 0x48, 0x3D, 0x8B },     /* #483D8B */
159.     { RGBC,  89,  73, "slate-blue", 0x6A, 0x5A, 0xCD },          /* #6A5ACD */
160.     { RGBC,  90,  74, "medium-slate-blue", 0x7B, 0x68, 0xEE },   /* #7B68EE */
161.     { RGBC,  91,  75, "medium-purple", 0x93, 0x70, 0xDB },       /* #9370DB */
162.     { RGBC,  92,  76, "dark-magenta", 0x8B, 0x00, 0x8B },        /* #8B008B */
163.     { RGBC,  93,  77, "dark-violet", 0x94, 0x00, 0xD3 },         /* #9400D3 */
164.     { RGBC,  94,  78, "dark-orchid", 0x99, 0x32, 0xCC },         /* #9932CC */
165.     { RGBC,  95,  79, "medium-orchid", 0xBA, 0x55, 0xD3 },       /* #BA55D3 */
166.     { RGBC,  96,  80, "purple", 0x80, 0x00, 0x80 },              /* #800080 */
167.     { RGBC,  97,  81, "thistle", 0xD8, 0xBF, 0xD8 },             /* #D8BFD8 */
168.     { RGBC,  98,  82, "plum", 0xDD, 0xA0, 0xDD },                /* #DDA0DD */
169.     { RGBC,  99,  83, "violet", 0xEE, 0x82, 0xEE },              /* #EE82EE */
170.     { RGBC, 100,  84, "magenta", 0xFF, 0x00, 0xFF },             /* #FF00FF */
171.     { RGBC, 101,  85, "orchid", 0xDA, 0x70, 0xD6 },              /* #DA70D6 */
172.     { RGBC, 102,  86, "medium-violet-red", 0xC7, 0x15, 0x85 },   /* #C71585 */
173.     { RGBC, 103,  87, "pale-violet-red", 0xDB, 0x70, 0x93 },     /* #DB7093 */
174.     { RGBC, 104,  88, "deep-pink", 0xFF, 0x14, 0x93 },           /* #FF1493 */
175.     { RGBC, 105,  89, "hot-pink", 0xFF, 0x69, 0xB4 },            /* #FF69B4 */
176.     { RGBC, 106,  90, "light-pink", 0xFF, 0xB6, 0xC1 },          /* #FFB6C1 */
177.     { RGBC, 107,  91, "pink", 0xFF, 0xC0, 0xCB },                /* #FFC0CB */
178.     { RGBC, 108,  92, "antique-white", 0xFA, 0xEB, 0xD7 },       /* #FAEBD7 */
179.     { RGBC, 109,  93, "beige", 0xF5, 0xF5, 0xDC },               /* #F5F5DC */
180.     { RGBC, 110,  94, "bisque", 0xFF, 0xE4, 0xC4 },              /* #FFE4C4 */
181.     { RGBC, 111,  95, "blanched-almond", 0xFF, 0xEB, 0xCD },     /* #FFEBCD */
182.     { RGBC, 112,  96, "wheat", 0xF5, 0xDE, 0xB3 },               /* #F5DEB3 */
183.     { RGBC, 113,  97, "corn-silk", 0xFF, 0xF8, 0xDC },           /* #FFF8DC */
184.     { RGBC, 114,  98, "lemon-chiffon", 0xFF, 0xFA, 0xCD },       /* #FFFACD */
185.     { RGBC, 115,  99, "light-golden-rod-yellow",
186.                       0xFA, 0xFA, 0xD2 },                        /* #FAFAD2 */
187.     { RGBC, 116, 100, "light-yellow", 0xFF, 0xFF, 0xE0 },        /* #FFFFE0 */
188.     { RGBC, 117, 101, "saddle-brown", 0x8B, 0x45, 0x13 },        /* #8B4513 */
189.     { RGBC, 118, 102, "sienna", 0xA0, 0x52, 0x2D },              /* #A0522D */
190.     { RGBC, 119, 103, "chocolate", 0xD2, 0x69, 0x1E },           /* #D2691E */
191.     { RGBC, 120, 104, "peru", 0xCD, 0x85, 0x3F },                /* #CD853F */
192.     { RGBC, 121, 105, "sandy-brown", 0xF4, 0xA4, 0x60 },         /* #F4A460 */
193.     { RGBC, 122, 106, "burly-wood", 0xDE, 0xB8, 0x87 },          /* #DEB887 */
194.     { RGBC, 123, 107, "tan", 0xD2, 0xB4, 0x8C },                 /* #D2B48C */
195.     { RGBC, 124, 108, "rosy-brown", 0xBC, 0x8F, 0x8F },          /* #BC8F8F */
196.     { RGBC, 125, 109, "moccasin", 0xFF, 0xE4, 0xB5 },            /* #FFE4B5 */
197.     { RGBC, 126, 110, "navajo-white", 0xFF, 0xDE, 0xAD },        /* #FFDEAD */
198.     { RGBC, 127, 111, "peach-puff", 0xFF, 0xDA, 0xB9 },          /* #FFDAB9 */
199.     { RGBC, 128, 112, "misty-rose", 0xFF, 0xE4, 0xE1 },          /* #FFE4E1 */
200.     { RGBC, 129, 113, "lavender-blush", 0xFF, 0xF0, 0xF5 },      /* #FFF0F5 */
201.     { RGBC, 130, 114, "linen", 0xFA, 0xF0, 0xE6 },               /* #FAF0E6 */
202.     { RGBC, 131, 115, "old-lace", 0xFD, 0xF5, 0xE6 },            /* #FDF5E6 */
203.     { RGBC, 132, 116, "papaya-whip", 0xFF, 0xEF, 0xD5 },         /* #FFEFD5 */
204.     { RGBC, 133, 117, "sea-shell", 0xFF, 0xF5, 0xEE },           /* #FFF5EE */
205.     { RGBC, 134, 118, "mint-cream", 0xF5, 0xFF, 0xFA },          /* #F5FFFA */
206.     { RGBC, 135, 119, "slate-gray", 0x70, 0x80, 0x90 },          /* #708090 */
207.     { RGBC, 136, 120, "light-slate-gray", 0x77, 0x88, 0x99 },    /* #778899 */
208.     { RGBC, 137, 121, "light-steel-blue", 0xB0, 0xC4, 0xDE },    /* #B0C4DE */
209.     { RGBC, 138, 122, "lavender", 0xE6, 0xE6, 0xFA },            /* #E6E6FA */
210.     { RGBC, 139, 123, "floral-white", 0xFF, 0xFA, 0xF0 },        /* #FFFAF0 */
211.     { RGBC, 140, 124, "alice-blue", 0xF0, 0xF8, 0xFF },          /* #F0F8FF */
212.     { RGBC, 141, 125, "ghost-white", 0xF8, 0xF8, 0xFF },         /* #F8F8FF */
213.     { RGBC, 142, 126, "honeydew", 0xF0, 0xFF, 0xF0 },            /* #F0FFF0 */
214.     { RGBC, 143, 127, "ivory", 0xFF, 0xFF, 0xF0 },               /* #FFFFF0 */
215.     { RGBC, 144, 128, "azure", 0xF0, 0xFF, 0xFF },               /* #F0FFFF */
216.     { RGBC, 145, 129, "snow", 0xFF, 0xFA, 0xFA },                /* #FFFAFA */
217.     { RGBC, 146, 130, "black", 0x00, 0x00, 0x00 },               /* #000000 */
218.     { RGBC, 147, 131, "dim-gray", 0x69, 0x69, 0x69 },            /* #696969 */
219.     { RGBC, 148, 132, "gray", 0x80, 0x80, 0x80 },                /* #808080 */
220.     { RGBC, 149, 133, "dark-gray", 0xA9, 0xA9, 0xA9 },           /* #A9A9A9 */
221.     { RGBC, 150, 134, "silver", 0xC0, 0xC0, 0xC0 },              /* #C0C0C0 */
222.     { RGBC, 151, 135, "light-gray", 0xD3, 0xD3, 0xD3 },          /* #D3D3D3 */
223.     { RGBC, 152, 136, "gainsboro", 0xDC, 0xDC, 0xDC },           /* #DCDCDC */
224.     { RGBC, 153, 137, "white-smoke", 0xF5, 0xF5, 0xF5 },         /* #F5F5F5 */
225.     { RGBC, 154, 138, "white", 0xFF, 0xFF, 0xFF },               /* #FFFFFF */
226. };
227. 
228. #undef NHC
229. #undef NOC
230. #undef RGBC
231. 
232. #ifdef CHANGE_COLOR
233. staticfn int32 alt_color_spec(const char *cp);
234. #endif
235. 
236. int32
237. colortable_to_int32(const struct nethack_color *cte)
238. {
239.     int32 clr = NO_COLOR | NH_BASIC_COLOR;
240. 
241.     if (cte->colortyp == rgb_color)
242.         clr = (cte->r << 16) | (cte->g << 8) | cte->b;
243.     else if (cte->colortyp == nh_color)
244.         clr = cte->tableindex | NH_BASIC_COLOR;
245.     return clr;
246. }
247. 
248. char *
249. color_attr_to_str(color_attr *ca)
250. {
251.     static char buf[BUFSZ];
252. 
253.     Sprintf(buf, "%s&%s",
254.             clr2colorname(ca->color),
255.             attr2attrname(ca->attr));
256.     return buf;
257. }
258. 
259. /* parse string like "color&attr" into color_attr */
260. boolean
261. color_attr_parse_str(color_attr *ca, char *str)
262. {
263.     char buf[BUFSZ];
264.     char *amp = NULL;
265.     int tmp, c = NO_COLOR, a = ATR_NONE;
266. 
267.     (void) strncpy(buf, str, sizeof buf - 1);
268.     buf[sizeof buf - 1] = '\0';
269. 
270.     if ((amp = strchr(buf, '&')) != 0)
271.         *amp = '\0';
272. 
273.     if (amp) {
274.         amp++;
275.         c = match_str2clr(buf, FALSE);
276.         a = match_str2attr(amp, TRUE);
277.         /* FIXME: match_str2clr & match_str2attr give config_error_add(),
278.            so this is useless */
279.         if (c >= CLR_MAX && a == -1) {
280.             /* try other way around */
281.             c = match_str2clr(amp, FALSE);
282.             a = match_str2attr(buf, TRUE);
283.         }
284.         if (c >= CLR_MAX || a == -1)
285.             return FALSE;
286.     } else {
287.         /* one param only */
288.         tmp = match_str2attr(buf, FALSE);
289.         if (tmp == -1) {
290.             tmp = match_str2clr(buf, FALSE);
291.             if (tmp >= CLR_MAX)
292.                 return FALSE;
293.             c = tmp;
294.         } else {
295.             a = tmp;
296.         }
297.     }
298.     ca->attr = a;
299.     ca->color = c;
300.     return TRUE;
301. }
302. 
303. boolean
304. query_color_attr(color_attr *ca, const char *prompt)
305. {
306.     int c, a;
307. 
308.     c = query_color(prompt, ca->color);
309.     if (c == -1)
310.         return FALSE;
311.     a = query_attr(prompt, ca->attr);
312.     if (a == -1)
313.         return FALSE;
314.     ca->color = c;
315.     ca->attr = a;
316.     return TRUE;
317. }
318. 
319. const char *
320. attr2attrname(int attr)
321. {
322.     int i;
323. 
324.     for (i = 0; i < SIZE(attrnames); i++)
325.         if (attrnames[i].attr == attr)
326.             return attrnames[i].name;
327.     return (char *) 0;
328. }
329. 
330. /*
331.  * Color support functions and data for "color"
332.  *
333.  * Used by: optfn_()
334.  *
335.  */
336. 
337. const char *
338. clr2colorname(int clr)
339. {
340.     int i;
341. 
342.     for (i = 0; i < SIZE(colornames); i++)
343.         if (colornames[i].name && colornames[i].color == clr)
344.             return colornames[i].name;
345.     return (char *) 0;
346. }
347. 
348. int
349. match_str2clr(char *str, boolean suppress_msg)
350. {
351.     int i, c = CLR_MAX;
352. 
353.     /* allow "lightblue", "light blue", and "light-blue" to match "light blue"
354.        (also junk like "_l i-gh_t---b l u e" but we won't worry about that);
355.        also copes with trailing space; caller has removed any leading space */
356.     for (i = 0; i < SIZE(colornames); i++)
357.         if (colornames[i].name
358.             && fuzzymatch(str, colornames[i].name, " -_", TRUE)) {
359.             c = colornames[i].color;
360.             break;
361.         }
362.     if (i == SIZE(colornames) && digit(*str))
363.         c = atoi(str);
364. 
365.     if (c < 0 || c >= CLR_MAX) {
366.         if (!suppress_msg)
367.             config_error_add("Unknown color '%.60s'", str);
368.         c = CLR_MAX; /* "none of the above" */
369.     }
370.     return c;
371. }
372. 
373. int
374. match_str2attr(const char *str, boolean complain)
375. {
376.     int i, a = -1;
377. 
378.     for (i = 0; i < SIZE(attrnames); i++)
379.         if (attrnames[i].name
380.             && fuzzymatch(str, attrnames[i].name, " -_", TRUE)) {
381.             a = attrnames[i].attr;
382.             break;
383.         }
384. 
385.     if (a == -1 && complain)
386.         config_error_add("Unknown text attribute '%.50s'", str);
387. 
388.     return a;
389. }
390. 
391. /* ask about highlighting attribute; for menu headers and menu
392.    coloring patterns, only one attribute at a time is allowed;
393.    for status highlighting, multiple attributes are allowed [overkill;
394.    life would be much simpler if that were restricted to one also...] */
395. int
396. query_attr(const char *prompt, int dflt_attr)
397. {
398.     winid tmpwin;
399.     anything any;
400.     int i, pick_cnt;
401.     menu_item *picks = (menu_item *) 0;
402.     boolean allow_many = (prompt && !strncmpi(prompt, "Choose", 6));
403.     int clr = NO_COLOR;
404. 
405.     tmpwin = create_nhwindow(NHW_MENU);
406.     start_menu(tmpwin, MENU_BEHAVE_STANDARD);
407.     any = cg.zeroany;
408.     for (i = 0; i < SIZE(attrnames); i++) {
409.         if (!attrnames[i].name)
410.             break;
411.         any.a_int = i + 1;
412.         add_menu(tmpwin, &nul_glyphinfo, &any, 0, 0,
413.                  attrnames[i].attr, clr, attrnames[i].name,
414.                  (attrnames[i].attr == dflt_attr) ? MENU_ITEMFLAGS_SELECTED
415.                                                   : MENU_ITEMFLAGS_NONE);
416.     }
417.     end_menu(tmpwin, (prompt && *prompt) ? prompt : "Pick an attribute");
418.     pick_cnt = select_menu(tmpwin, allow_many ? PICK_ANY : PICK_ONE, &picks);
419.     destroy_nhwindow(tmpwin);
420.     if (pick_cnt > 0) {
421.         int j, k = 0;
422. 
423.         if (allow_many) {
424.             /* PICK_ANY, with one preselected entry (ATR_NONE) which
425.                should be excluded if any other choices were picked */
426.             for (i = 0; i < pick_cnt; ++i) {
427.                 j = picks[i].item.a_int - 1;
428.                 if (attrnames[j].attr != ATR_NONE || pick_cnt == 1) {
429.                     switch (attrnames[j].attr) {
430.                     case ATR_NONE:
431.                         k = HL_NONE;
432.                         break;
433.                     case ATR_BOLD:
434.                         k |= HL_BOLD;
435.                         break;
436.                     case ATR_DIM:
437.                         k |= HL_DIM;
438.                         break;
439.                     case ATR_ITALIC:
440.                         k |= HL_ITALIC;
441.                         break;
442.                     case ATR_ULINE:
443.                         k |= HL_ULINE;
444.                         break;
445.                     case ATR_BLINK:
446.                         k |= HL_BLINK;
447.                         break;
448.                     case ATR_INVERSE:
449.                         k |= HL_INVERSE;
450.                         break;
451.                     }
452.                 }
453.             }
454.         } else {
455.             /* PICK_ONE, but might get 0 or 2 due to preselected entry */
456.             j = picks[0].item.a_int - 1;
457.             /* pick_cnt==2: explicitly picked something other than the
458.                preselected entry */
459.             if (pick_cnt == 2 && attrnames[j].attr == dflt_attr)
460.                 j = picks[1].item.a_int - 1;
461.             k = attrnames[j].attr;
462.         }
463.         free((genericptr_t) picks);
464.         return k;
465.     } else if (pick_cnt == 0 && !allow_many) {
466.         /* PICK_ONE, preselected entry explicitly chosen */
467.         return dflt_attr;
468.     }
469.     /* either ESC to explicitly cancel (pick_cnt==-1) or
470.        PICK_ANY with preselected entry toggled off and nothing chosen */
471.     return -1;
472. }
473. 
474. int
475. query_color(const char *prompt, int dflt_color)
476. {
477.     winid tmpwin;
478.     anything any;
479.     int i, pick_cnt;
480.     menu_item *picks = (menu_item *) 0;
481. 
482.     /* replace user patterns with color name ones and force 'menucolors' On */
483.     basic_menu_colors(TRUE);
484. 
485.     tmpwin = create_nhwindow(NHW_MENU);
486.     start_menu(tmpwin, MENU_BEHAVE_STANDARD);
487.     any = cg.zeroany;
488.     for (i = 0; i < SIZE(colornames); i++) {
489.         if (!colornames[i].name)
490.             break;
491.         any.a_int = i + 1;
492.         add_menu(tmpwin, &nul_glyphinfo, &any, 0, 0,
493.                  ATR_NONE, NO_COLOR, colornames[i].name,
494.                  (colornames[i].color == dflt_color) ? MENU_ITEMFLAGS_SELECTED
495.                                                      : MENU_ITEMFLAGS_NONE);
496.     }
497.     end_menu(tmpwin, (prompt && *prompt) ? prompt : "Pick a color");
498.     pick_cnt = select_menu(tmpwin, PICK_ONE, &picks);
499.     destroy_nhwindow(tmpwin);
500. 
501.     /* remove temporary color name patterns and restore user-specified ones;
502.        reset 'menucolors' option to its previous value */
503.     basic_menu_colors(FALSE);
504. 
505.     if (pick_cnt > 0) {
506.         i = colornames[picks[0].item.a_int - 1].color;
507.         /* pick_cnt==2: explicitly picked something other than the
508.            preselected entry */
509.         if (pick_cnt == 2 && i == NO_COLOR)
510.             i = colornames[picks[1].item.a_int - 1].color;
511.         free((genericptr_t) picks);
512.         return i;
513.     } else if (pick_cnt == 0) {
514.         /* pick_cnt==0: explicitly picking preselected entry toggled it off */
515.         return dflt_color;
516.     }
517.     return -1;
518. }
519. 
520. DISABLE_WARNING_FORMAT_NONLITERAL
521. 
522. extern const char regex_id[]; /* from sys/share/<various>regex.{c,cpp} */
523. 
524. /* set up a menu for picking a color, one that shows each name in its color;
525.    overrides player's MENUCOLORS with a set of "blue"=blue, "red"=red, and
526.    so forth; suppresses color for black and white because one of those will
527.    likely be invisible due to matching the background; the alternate set of
528.    MENUCOLORS is kept around for potential re-use */
529. void
530. basic_menu_colors(
531.     boolean load_colors) /* True: temporarily replace menu color entries with
532.                           * a fake set of menu colors which match their names;
533.                           * False: restore user-specified colorings */
534. {
535.     if (load_colors) {
536.         /* replace normal menu colors with a set specifically for colors */
537.         gs.save_menucolors = iflags.use_menu_color;
538.         gs.save_colorings = gm.menu_colorings;
539. 
540.         iflags.use_menu_color = TRUE;
541.         if (gc.color_colorings) {
542.             /* use the alternate colorings which were set up previously */
543.             gm.menu_colorings = gc.color_colorings;
544.         } else {
545.             /* create the alternate colorings once */
546.             char cnm[QBUFSZ];
547.             int i, c;
548.             boolean pmatchregex = !strcmpi(regex_id, "pmatchregex");
549.             const char *patternfmt = pmatchregex ? "*%s" : "%s";
550. 
551.             /* menu_colorings pointer has been saved; clear it in order
552.                to add the alternate entries as if from scratch */
553.             gm.menu_colorings = (struct menucoloring *) 0;
554. 
555.             /* this orders the patterns last-in/first-out; that means
556.                that the "light <foo>" variations come before the basic
557.                "<foo>" ones, which is exactly what we want (so that the
558.                shorter basic names won't get false matches as substrings
559.                of the longer ones) */
560.             for (i = 0; i < SIZE(colornames); ++i) {
561.                 if (!colornames[i].name) /* first alias entry has no name */
562.                     break;
563.                 c = colornames[i].color;
564.                 if (c == CLR_BLACK || c == CLR_WHITE || c == NO_COLOR)
565.                     continue; /* skip these */
566.                 Sprintf(cnm, patternfmt, colornames[i].name);
567.                 add_menu_coloring_parsed(cnm, c, ATR_NONE);
568.             }
569. 
570.             /* right now, menu_colorings contains the alternate color list;
571.                remember that list for future pick-a-color instances and
572.                also keep it as is for this instance */
573.             gc.color_colorings = gm.menu_colorings;
574.         }
575.     } else {
576.         /* restore normal user-specified menu colors */
577.         iflags.use_menu_color = gs.save_menucolors;
578.         gm.menu_colorings = gs.save_colorings;
579.     }
580. }
581. 
582. RESTORE_WARNING_FORMAT_NONLITERAL
583. 
584. boolean
585. add_menu_coloring_parsed(const char *str, int c, int a)
586. {
587.     static const char re_error[] = "Menucolor regex error";
588.     struct menucoloring *tmp;
589. 
590.     if (!str)
591.         return FALSE;
592.     tmp = (struct menucoloring *) alloc(sizeof *tmp);
593.     tmp->match = regex_init();
594.     /* test_regex_pattern() has already validated this regexp but parsing
595.        it again could conceivably run out of memory */
596.     if (!regex_compile(str, tmp->match)) {
597.         char errbuf[BUFSZ];
598.         char *re_error_desc = regex_error_desc(tmp->match, errbuf);
599. 
600.         /* free first in case reason for regcomp failure was out-of-memory */
601.         regex_free(tmp->match);
602.         free((genericptr_t) tmp);
603.         config_error_add("%s: %s", re_error, re_error_desc);
604.         return FALSE;
605.     }
606.     tmp->next = gm.menu_colorings;
607.     tmp->origstr = dupstr(str);
608.     tmp->color = c;
609.     tmp->attr = a;
610.     gm.menu_colorings = tmp;
611.     iflags.use_menu_color = TRUE;
612.     return TRUE;
613. }
614. 
615. /* parse '"regex_string"=color&attr' and add it to menucoloring */
616. boolean
617. add_menu_coloring(char *tmpstr) /* never Null but could be empty */
618. {
619.     int c = NO_COLOR, a = ATR_NONE;
620.     char *tmps, *cs, *amp;
621.     char str[BUFSZ];
622. 
623.     (void) strncpy(str, tmpstr, sizeof str - 1);
624.     str[sizeof str - 1] = '\0';
625. 
626.     if ((cs = strchr(str, '=')) == 0) {
627.         config_error_add("Malformed MENUCOLOR");
628.         return FALSE;
629.     }
630. 
631.     tmps = cs + 1; /* advance past '=' */
632.     mungspaces(tmps);
633.     if ((amp = strchr(tmps, '&')) != 0)
634.         *amp = '\0';
635. 
636.     c = match_str2clr(tmps, FALSE);
637.     if (c >= CLR_MAX)
638.         return FALSE;
639. 
640.     if (amp) {
641.         tmps = amp + 1; /* advance past '&' */
642.         a = match_str2attr(tmps, TRUE);
643.         if (a == -1)
644.             return FALSE;
645.     }
646. 
647.     /* the regexp portion here has not been condensed by mungspaces() */
648.     *cs = '\0';
649.     tmps = str;
650.     if (*tmps == '"' || *tmps == '\) {
651.         cs--;
652.         while (isspace((uchar) *cs))
653.             cs--;
654.         if (*cs == *tmps) {
655.             *cs = '\0';
656.             tmps++;
657.         }
658.     }
659.     return add_menu_coloring_parsed(tmps, c, a);
660. }
661. 
662. /* release all menu color patterns */
663. void
664. free_menu_coloring(void)
665. {
666.     /* either menu_colorings or color_colorings or both might need to
667.        be freed or already be Null; do-loop will iterate at most twice */
668.     do {
669.         struct menucoloring *tmp, *tmp2;
670. 
671.         for (tmp = gm.menu_colorings; tmp; tmp = tmp2) {
672.             tmp2 = tmp->next;
673.             regex_free(tmp->match);
674.             free((genericptr_t) tmp->origstr);
675.             free((genericptr_t) tmp);
676.         }
677.         gm.menu_colorings = gc.color_colorings;
678.         gc.color_colorings = (struct menucoloring *) 0;
679.     } while (gm.menu_colorings);
680. }
681. 
682. /* release a specific menu color pattern; not used for color_colorings */
683. void
684. free_one_menu_coloring(int idx) /* 0 .. */
685. {
686.     struct menucoloring *tmp = gm.menu_colorings;
687.     struct menucoloring *prev = NULL;
688. 
689.     while (tmp) {
690.         if (idx == 0) {
691.             struct menucoloring *next = tmp->next;
692. 
693.             regex_free(tmp->match);
694.             free((genericptr_t) tmp->origstr);
695.             free((genericptr_t) tmp);
696.             if (prev)
697.                 prev->next = next;
698.             else
699.                 gm.menu_colorings = next;
700.             return;
701.         }
702.         idx--;
703.         prev = tmp;
704.         tmp = tmp->next;
705.     }
706. }
707. 
708. int
709. count_menucolors(void)
710. {
711.     struct menucoloring *tmp;
712.     int count = 0;
713. 
714.     for (tmp = gm.menu_colorings; tmp; tmp = tmp->next)
715.         count++;
716.     return count;
717. }
718. 
719. /* returns -1 on no-match.
720.  * buf is NONNULLARG1
721.  */
722. int32
723. check_enhanced_colors(char *buf)
724. {
725.     char xtra = '\0'; /* used to catch trailing junk after "#rrggbb" */
726.     unsigned r, g, b;
727.     int32 retcolor = -1, color;
728. 
729.     if ((color = match_str2clr(buf, TRUE)) != CLR_MAX)  {
730.         retcolor = color | NH_BASIC_COLOR;
731.     } else if (sscanf(buf, "#%02x%02x%02x%c", &r, &g, &b, &xtra) >= 3) {
732.         retcolor = !xtra ? (int32) ((r << 16) | (g << 8) | b) : -1;
733.     } else {
734.         /* altbuf: allow user's "grey" to match colortable[]'s "gray";
735.          * fuzzymatch(): ignore spaces, hyphens, and underscores so that
736.          * space or underscore in user-supplied name will match hyphen
737.          * [note: caller splits text at spaces so we won't see any here]
738.          */
739.         char *altbuf = NULL, *grey = strstri(buf, "grey");
740.         ptrdiff_t greyoffset = grey ? (grey - buf) : -1;
741. 
742.         if (greyoffset >= 0) {
743.             altbuf = dupstr(buf);
744.             /* use direct copy because strsubst() is case-sensitive */
745.             /*(void) strncpy(&altbuf[greyoffset], "gray", 4);*/
746.             (void) memcpy(altbuf + greyoffset, "gray", 4);
747.         }
748.         for (color = 0; color < SIZE(colortable); ++color) {
749.             if (fuzzymatch(buf, colortable[color].name, " -_", TRUE)
750.                 || (altbuf && fuzzymatch(altbuf, colortable[color].name,
751.                                          " -_", TRUE))) {
752.                 retcolor = colortable_to_int32(&colortable[color]);
753.                 break;
754.             }
755.         }
756.         if (altbuf)
757.             free(altbuf);
758.     }
759.     return retcolor;
760. }
761. 
762. /* return the canonical name of a particular color */
763. const char *
764. wc_color_name(int32 colorindx)
765. {
766.     static char hexcolor[sizeof "#rrggbb"]; /* includes room for '\0' */
767.     const char *result = "no-color";
768. 
769.     if (colorindx >= 0) {
770.         int32 basicindx = colorindx & ~NH_BASIC_COLOR;
771. 
772.         /* if colorindx has NH_BASIC_COLOR bit set, basicindx won't,
773.            so differing implies a basic color */
774.         if (basicindx != colorindx) {
775.             assert(basicindx < 16);
776.             result = colortable[basicindx].name;
777.         } else {
778.             int indx;
779.             long r = (colorindx >> 16) & 0x0000ff, /* shift rrXXXX to rr */
780.                  g = (colorindx >> 8) & 0x0000ff,  /* shift XXggXX to gg */
781.                  b = colorindx & 0x0000ff;         /* mask  XXXXbb to bb */
782. 
783.             Snprintf(hexcolor, sizeof hexcolor, "#%02x%02x%02x",
784.                      (uint8) r, (uint8) g, (uint8) b);
785.             result = hexcolor;
786.             /* override hex value if this is a named color */
787.             for (indx = 16; indx < SIZE(colortable); ++indx)
788.                 if (colortable[indx].r == r
789.                     && colortable[indx].g == g
790.                     && colortable[indx].b == b) {
791.                     result = colortable[indx].name;
792.                     break;
793.                 }
794.         }
795.     }
796.     return result;
797. }
798. 
799. /* hexdd[] is defined in decl.c */
800. boolean
801. onlyhexdigits(const char *buf)
802. {
803.     const char *dp = buf;
804. 
805.     for (dp = buf; *dp; ++dp) {
806.         if (!(strchr(hexdd, *dp) || *dp == '-'))
807.             return FALSE;
808.     }
809.     return TRUE;
810. }
811. 
812. int32_t
813. rgbstr_to_int32(const char *rgbstr)
814. {
815.     int r, g, b, milestone = 0;
816.     char *cp, *c_r, *c_g, *c_b;
817.     int32_t rgb = 0;
818.     char buf[BUFSZ];
819.     boolean dash = FALSE;
820. 
821. 
822.     Snprintf(buf, sizeof buf, "%s",
823.              rgbstr ? rgbstr : "");
824. 
825.     if (*buf && onlyhexdigits(buf)) {
826.         c_g = c_b = (char *) 0;
827.         c_r = cp = buf;
828.         while (*cp) {
829.             if (digit(*cp) || *cp == '-') {
830.                 if (*cp == '-') {
831.                     *cp = '\0';
832.                     milestone++;
833.                     dash = TRUE;
834.                 }
835.                 cp++;
836.                 if (dash) {
837.                     if (milestone < 2)
838.                         c_g = cp;
839.                     else
840.                         c_b = cp;
841.                     dash = FALSE;
842.                 }
843.             } else {
844.                 return -1L;
845.             }
846.         }
847.         /* sanity checks */
848.         if (c_r && c_g && c_b
849.             && (strlen(c_r) > 0 && strlen(c_r) < 4)
850.             && (strlen(c_g) > 0 && strlen(c_g) < 4)
851.             && (strlen(c_b) > 0 && strlen(c_b) < 4)) {
852.             r = atoi(c_r);
853.             g = atoi(c_g);
854.             b = atoi(c_b);
855.             rgb = (r << 16) | (g << 8) | (b << 0);
856.             return rgb;
857.         }
858.     } else if (*buf) {
859.         /* perhaps an enhanced color name was used instead of rgb value? */
860.         if ((rgb = check_enhanced_colors(buf)) != -1) {
861.             return rgb;
862.         }
863.     }
864.     return -1;
865. }
866. 
867. int
868. set_map_customcolor(glyph_map *gmap, uint32 nhcolor)
869. {
870.     glyph_map *tmpgm = gmap;
871.     uint32 closecolor = 0;
872.     uint16 clridx = 0;
873. 
874.     if (!tmpgm)
875.         return 0;
876. 
877.     gmap->customcolor = nhcolor;
878.     if (closest_color(nhcolor, &closecolor, &clridx))
879.         gmap->color256idx = clridx;
880.     else
881.         gmap->color256idx = 0;
882.     return 1;
883. }
884. 
885. static struct {
886.     int index;
887.     uint32 value;
888. } color_256_definitions[] = {
889.     /* color values are from unnethack */
890.     {  16, 0x000000 }, {  17, 0x00005f }, {  18, 0x000087 },
891.     {  19, 0x0000af }, {  20, 0x0000d7 }, {  21, 0x0000ff },
892.     {  22, 0x005f00 }, {  23, 0x005f5f }, {  24, 0x005f87 },
893.     {  25, 0x005faf }, {  26, 0x005fd7 }, {  27, 0x005fff },
894.     {  28, 0x008700 }, {  29, 0x00875f }, {  30, 0x008787 },
895.     {  31, 0x0087af }, {  32, 0x0087d7 }, {  33, 0x0087ff },
896.     {  34, 0x00af00 }, {  35, 0x00af5f }, {  36, 0x00af87 },
897.     {  37, 0x00afaf }, {  38, 0x00afd7 }, {  39, 0x00afff },
898.     {  40, 0x00d700 }, {  41, 0x00d75f }, {  42, 0x00d787 },
899.     {  43, 0x00d7af }, {  44, 0x00d7d7 }, {  45, 0x00d7ff },
900.     {  46, 0x00ff00 }, {  47, 0x00ff5f }, {  48, 0x00ff87 },
901.     {  49, 0x00ffaf }, {  50, 0x00ffd7 }, {  51, 0x00ffff },
902.     {  52, 0x5f0000 }, {  53, 0x5f005f }, {  54, 0x5f0087 },
903.     {  55, 0x5f00af }, {  56, 0x5f00d7 }, {  57, 0x5f00ff },
904.     {  58, 0x5f5f00 }, {  59, 0x5f5f5f }, {  60, 0x5f5f87 },
905.     {  61, 0x5f5faf }, {  62, 0x5f5fd7 }, {  63, 0x5f5fff },
906.     {  64, 0x5f8700 }, {  65, 0x5f875f }, {  66, 0x5f8787 },
907.     {  67, 0x5f87af }, {  68, 0x5f87d7 }, {  69, 0x5f87ff },
908.     {  70, 0x5faf00 }, {  71, 0x5faf5f }, {  72, 0x5faf87 },
909.     {  73, 0x5fafaf }, {  74, 0x5fafd7 }, {  75, 0x5fafff },
910.     {  76, 0x5fd700 }, {  77, 0x5fd75f }, {  78, 0x5fd787 },
911.     {  79, 0x5fd7af }, {  80, 0x5fd7d7 }, {  81, 0x5fd7ff },
912.     {  82, 0x5fff00 }, {  83, 0x5fff5f }, {  84, 0x5fff87 },
913.     {  85, 0x5fffaf }, {  86, 0x5fffd7 }, {  87, 0x5fffff },
914.     {  88, 0x870000 }, {  89, 0x87005f }, {  90, 0x870087 },
915.     {  91, 0x8700af }, {  92, 0x8700d7 }, {  93, 0x8700ff },
916.     {  94, 0x875f00 }, {  95, 0x875f5f }, {  96, 0x875f87 },
917.     {  97, 0x875faf }, {  98, 0x875fd7 }, {  99, 0x875fff },
918.     { 100, 0x878700 }, { 101, 0x87875f }, { 102, 0x878787 },
919.     { 103, 0x8787af }, { 104, 0x8787d7 }, { 105, 0x8787ff },
920.     { 106, 0x87af00 }, { 107, 0x87af5f }, { 108, 0x87af87 },
921.     { 109, 0x87afaf }, { 110, 0x87afd7 }, { 111, 0x87afff },
922.     { 112, 0x87d700 }, { 113, 0x87d75f }, { 114, 0x87d787 },
923.     { 115, 0x87d7af }, { 116, 0x87d7d7 }, { 117, 0x87d7ff },
924.     { 118, 0x87ff00 }, { 119, 0x87ff5f }, { 120, 0x87ff87 },
925.     { 121, 0x87ffaf }, { 122, 0x87ffd7 }, { 123, 0x87ffff },
926.     { 124, 0xaf0000 }, { 125, 0xaf005f }, { 126, 0xaf0087 },
927.     { 127, 0xaf00af }, { 128, 0xaf00d7 }, { 129, 0xaf00ff },
928.     { 130, 0xaf5f00 }, { 131, 0xaf5f5f }, { 132, 0xaf5f87 },
929.     { 133, 0xaf5faf }, { 134, 0xaf5fd7 }, { 135, 0xaf5fff },
930.     { 136, 0xaf8700 }, { 137, 0xaf875f }, { 138, 0xaf8787 },
931.     { 139, 0xaf87af }, { 140, 0xaf87d7 }, { 141, 0xaf87ff },
932.     { 142, 0xafaf00 }, { 143, 0xafaf5f }, { 144, 0xafaf87 },
933.     { 145, 0xafafaf }, { 146, 0xafafd7 }, { 147, 0xafafff },
934.     { 148, 0xafd700 }, { 149, 0xafd75f }, { 150, 0xafd787 },
935.     { 151, 0xafd7af }, { 152, 0xafd7d7 }, { 153, 0xafd7ff },
936.     { 154, 0xafff00 }, { 155, 0xafff5f }, { 156, 0xafff87 },
937.     { 157, 0xafffaf }, { 158, 0xafffd7 }, { 159, 0xafffff },
938.     { 160, 0xd70000 }, { 161, 0xd7005f }, { 162, 0xd70087 },
939.     { 163, 0xd700af }, { 164, 0xd700d7 }, { 165, 0xd700ff },
940.     { 166, 0xd75f00 }, { 167, 0xd75f5f }, { 168, 0xd75f87 },
941.     { 169, 0xd75faf }, { 170, 0xd75fd7 }, { 171, 0xd75fff },
942.     { 172, 0xd78700 }, { 173, 0xd7875f }, { 174, 0xd78787 },
943.     { 175, 0xd787af }, { 176, 0xd787d7 }, { 177, 0xd787ff },
944.     { 178, 0xd7af00 }, { 179, 0xd7af5f }, { 180, 0xd7af87 },
945.     { 181, 0xd7afaf }, { 182, 0xd7afd7 }, { 183, 0xd7afff },
946.     { 184, 0xd7d700 }, { 185, 0xd7d75f }, { 186, 0xd7d787 },
947.     { 187, 0xd7d7af }, { 188, 0xd7d7d7 }, { 189, 0xd7d7ff },
948.     { 190, 0xd7ff00 }, { 191, 0xd7ff5f }, { 192, 0xd7ff87 },
949.     { 193, 0xd7ffaf }, { 194, 0xd7ffd7 }, { 195, 0xd7ffff },
950.     { 196, 0xff0000 }, { 197, 0xff005f }, { 198, 0xff0087 },
951.     { 199, 0xff00af }, { 200, 0xff00d7 }, { 201, 0xff00ff },
952.     { 202, 0xff5f00 }, { 203, 0xff5f5f }, { 204, 0xff5f87 },
953.     { 205, 0xff5faf }, { 206, 0xff5fd7 }, { 207, 0xff5fff },
954.     { 208, 0xff8700 }, { 209, 0xff875f }, { 210, 0xff8787 },
955.     { 211, 0xff87af }, { 212, 0xff87d7 }, { 213, 0xff87ff },
956.     { 214, 0xffaf00 }, { 215, 0xffaf5f }, { 216, 0xffaf87 },
957.     { 217, 0xffafaf }, { 218, 0xffafd7 }, { 219, 0xffafff },
958.     { 220, 0xffd700 }, { 221, 0xffd75f }, { 222, 0xffd787 },
959.     { 223, 0xffd7af }, { 224, 0xffd7d7 }, { 225, 0xffd7ff },
960.     { 226, 0xffff00 }, { 227, 0xffff5f }, { 228, 0xffff87 },
961.     { 229, 0xffffaf }, { 230, 0xffffd7 }, { 231, 0xffffff },
962.     { 232, 0x080808 }, { 233, 0x121212 }, { 234, 0x1c1c1c },
963.     { 235, 0x262626 }, { 236, 0x303030 }, { 237, 0x3a3a3a },
964.     { 238, 0x444444 }, { 239, 0x4e4e4e }, { 240, 0x585858 },
965.     { 241, 0x626262 }, { 242, 0x6c6c6c }, { 243, 0x767676 },
966.     { 244, 0x808080 }, { 245, 0x8a8a8a }, { 246, 0x949494 },
967.     { 247, 0x9e9e9e }, { 248, 0xa8a8a8 }, { 249, 0xb2b2b2 },
968.     { 250, 0xbcbcbc }, { 251, 0xc6c6c6 }, { 252, 0xd0d0d0 },
969.     { 253, 0xdadada }, { 254, 0xe4e4e4 }, { 255, 0xeeeeee },
970. };
971. 
972. /** Calculate the color distance between two colors.
973.  *
974.  * Algorithm taken from UnNetHack which took it from
975.  * https://www.compuphase.com/cmetric.htm
976.  **/
977. 
978. int
979. color_distance(uint32_t rgb1, uint32_t rgb2)
980. {
981.     int r1 = (rgb1 >> 16) & 0xFF;
982.     int g1 = (rgb1 >> 8) & 0xFF;
983.     int b1 = (rgb1) & 0xFF;
984.     int r2 = (rgb2 >> 16) & 0xFF;
985.     int g2 = (rgb2 >> 8) & 0xFF;
986.     int b2 = (rgb2) & 0xFF;
987. 
988.     int rmean = (r1 + r2) / 2;
989.     int r = r1 - r2;
990.     int g = g1 - g2;
991.     int b = b1 - b2;
992.     return ((((512 + rmean) * r * r) >> 8) + 4 * g * g
993.             + (((767 - rmean) * b * b) >> 8));
994. }
995. 
996. boolean
997. closest_color(uint32 lcolor, uint32 *closecolor, uint16 *clridx)
998. {
999.     int i, color_index = -1, similar = INT_MAX, current;
1000.     boolean retbool = FALSE;
1001. 
1002.     for (i = 0; i < SIZE(color_256_definitions); i++) {
1003.         /* look for an exact match */
1004.         if (lcolor == color_256_definitions[i].value) {
1005.             color_index = i;
1006.             break;
1007.         }
1008.         /* find a close color match */
1009.         current = color_distance(lcolor, color_256_definitions[i].value);
1010.         if (current < similar) {
1011.             color_index = i;
1012.             similar = current;
1013.         }
1014.     }
1015.     if (closecolor && clridx && color_index >= 0) {
1016.         *closecolor = color_256_definitions[color_index].value;
1017.         *clridx = color_256_definitions[color_index].index;
1018.         retbool = TRUE;
1019.     }
1020.     return retbool;
1021. }
1022. 
1023. uint32
1024. get_nhcolor_from_256_index(int idx)
1025. {
1026.     uint32 retcolor = NO_COLOR | NH_BASIC_COLOR;
1027. 
1028.     if (IndexOk(idx, color_256_definitions))
1029.         retcolor = color_256_definitions[idx].value;
1030.     return retcolor;
1031. }
1032. 
1033. #ifdef CHANGE_COLOR
1034. 
1035. int
1036. count_alt_palette(void)
1037. {
1038.     int clr, clrcount = 0;
1039. 
1040.     for (clr = 0; clr < CLR_MAX; ++clr) {
1041.         if (ga.altpalette[clr] != 0U)
1042.             clrcount++;
1043.     }
1044.     return clrcount;
1045. }
1046. 
1047. int
1048. alternative_palette(char *op)
1049. {
1050.     char buf[BUFSZ], *c_colorid, *c_colorval, *cp;
1051.     int reslt = 0, coloridx = CLR_MAX;
1052.     long rgb = 0L;
1053.     boolean slash = FALSE;
1054. 
1055.     if (!op)
1056.         return 0;
1057. 
1058.     Snprintf(buf, sizeof buf, "%s", op);
1059.     c_colorval = (char *) 0;
1060.     c_colorid = cp = buf;
1061.     while (*cp) {
1062.         if (*cp == ':' || *cp == '/') {
1063.             if (*cp == '/') {
1064.                 slash = TRUE;
1065.                 *cp = '\0';
1066.             }
1067.         }
1068.         cp++;
1069.         if (slash) {
1070.             c_colorval = cp;
1071.             slash = FALSE;
1072.         }
1073.     }
1074.     /* some sanity checks */
1075.     if (c_colorid && *c_colorid == ' ')
1076.         c_colorid++;
1077.     if (c_colorval && *c_colorval == ' ')
1078.         c_colorval++;
1079.     if (c_colorid)
1080.         coloridx = match_str2clr(c_colorid, TRUE);
1081. 
1082.     if (c_colorval && coloridx >= 0 && coloridx < CLR_MAX) {
1083.         rgb = rgbstr_to_int32(c_colorval);
1084.         if (rgb == -1) {
1085.             rgb = alt_color_spec(c_colorval);
1086.         }
1087.         if (rgb != -1) {
1088.             ga.altpalette[coloridx] = (uint32) rgb | NH_ALTPALETTE;
1089.             /* use COLORVAL(ga.altpalette[coloridx]) to get
1090.                the actual rgb value out of ga.altpalette[] */
1091.             reslt = 1;
1092.         }
1093.     }
1094.     return reslt;
1095. }
1096. 
1097. void
1098. change_palette(void)
1099. {
1100.     int clridx;
1101. 
1102.     for (clridx = 0; clridx < CLR_MAX; ++clridx) {
1103.         if (ga.altpalette[clridx] != 0) {
1104.             long rgb = (long) COLORVAL(ga.altpalette[clridx]);
1105.             (*windowprocs.win_change_color)(clridx, rgb, 0);
1106.         }
1107.     }
1108. }
1109. 
1110. staticfn int32
1111. alt_color_spec(const char *str)
1112. {
1113.     static NEARDATA const char oct[] = "01234567", dec[] = "0123456789";
1114.     /* hexdd[] is defined in decl.c */
1115. 
1116.     const char *dp, *cp = str;
1117.     int32 cval = -1;
1118.     int dcount, dlimit = 6;
1119.     boolean hexescape = FALSE, octescape = FALSE;
1120. 
1121.     dcount = 0; /* for decimal, octal, hexadecimal cases */
1122.     hexescape =
1123.         (*cp == '\\' && cp[1] && (cp[1] == 'x' || cp[1] == 'X') && cp[2]);
1124.     if (!hexescape) {
1125.         octescape =
1126.             (*cp == '\\' && cp[1] && (cp[1] == 'o' || cp[1] == 'O') && cp[2]);
1127.     }
1128. 
1129.     if (hexescape || octescape) {
1130.         cval = 0;
1131.         cp += 2;
1132.         if (octescape)
1133.             dlimit = 8;
1134.     } else if (*cp == '#' && cp[1]) {
1135.         hexescape = TRUE;
1136.         cval = 0;
1137.         cp += 1;
1138.     } else if (cp[1]) {
1139.         cval = 0;
1140.         dlimit = 8;
1141.     } else if (!cp[1]) {
1142.         if (strchr(dec, *cp) != 0) {
1143.             /* simple val, or nothing left for \ to escape */
1144.             cval = (*cp - '0');
1145.         }
1146.         dlimit = 1;
1147.         cp++;
1148.     }
1149. 
1150.     while (*cp) {
1151.         if (!hexescape && !octescape && strchr(dec, *cp)) {
1152.             cval = (cval * 10) + (*cp - '0');
1153.         } else if (octescape && strchr(oct, *cp)) {
1154.             cval = (cval * 8) + (*cp - '0');
1155.         } else if (hexescape && (dp = strchr(hexdd, *cp)) != 0) {
1156.             cval = (cval * 16) + ((int) (dp - hexdd) / 2);
1157.         }
1158.         ++cp;
1159.         if (++dcount > dlimit) {
1160.             cval = -1;
1161.             break;
1162.         }
1163.     }
1164.     return cval;
1165. }
1166. #endif /* CHANGE_COLOR */
1167. 
1168. /*coloratt.c*/