colour.c revision 1.5 1 1.5 christos /* $OpenBSD$ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.1 jmmv * Copyright (c) 2008 Nicholas Marriott <nicm (at) users.sourceforge.net>
5 1.1 jmmv *
6 1.1 jmmv * Permission to use, copy, modify, and distribute this software for any
7 1.1 jmmv * purpose with or without fee is hereby granted, provided that the above
8 1.1 jmmv * copyright notice and this permission notice appear in all copies.
9 1.1 jmmv *
10 1.1 jmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 jmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 jmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 jmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 jmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 1.1 jmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 1.1 jmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 jmmv */
18 1.1 jmmv
19 1.1 jmmv #include <sys/types.h>
20 1.1 jmmv
21 1.2 he #include <ctype.h>
22 1.1 jmmv #include <stdlib.h>
23 1.1 jmmv #include <string.h>
24 1.1 jmmv
25 1.1 jmmv #include "tmux.h"
26 1.1 jmmv
27 1.1 jmmv /*
28 1.1 jmmv * Colour to string conversion functions. Bit 8 of the colour means it is one
29 1.1 jmmv * of the 256 colour palette.
30 1.1 jmmv */
31 1.1 jmmv
32 1.2 he struct colour_rgb {
33 1.5 christos u_char i;
34 1.2 he u_char r;
35 1.2 he u_char g;
36 1.2 he u_char b;
37 1.2 he };
38 1.2 he
39 1.5 christos const struct colour_rgb colour_from_256[] = {
40 1.5 christos { 0, 0x00, 0x00, 0x00 }, { 1, 0x00, 0x00, 0x5f },
41 1.5 christos { 2, 0x00, 0x00, 0x87 }, { 3, 0x00, 0x00, 0xaf },
42 1.5 christos { 4, 0x00, 0x00, 0xd7 }, { 5, 0x00, 0x00, 0xff },
43 1.5 christos { 6, 0x00, 0x5f, 0x00 }, { 7, 0x00, 0x5f, 0x5f },
44 1.5 christos { 8, 0x00, 0x5f, 0x87 }, { 9, 0x00, 0x5f, 0xaf },
45 1.5 christos { 10, 0x00, 0x5f, 0xd7 }, { 11, 0x00, 0x5f, 0xff },
46 1.5 christos { 12, 0x00, 0x87, 0x00 }, { 13, 0x00, 0x87, 0x5f },
47 1.5 christos { 14, 0x00, 0x87, 0x87 }, { 15, 0x00, 0x87, 0xaf },
48 1.5 christos { 16, 0x00, 0x87, 0xd7 }, { 17, 0x00, 0x87, 0xff },
49 1.5 christos { 18, 0x00, 0xaf, 0x00 }, { 19, 0x00, 0xaf, 0x5f },
50 1.5 christos { 20, 0x00, 0xaf, 0x87 }, { 21, 0x00, 0xaf, 0xaf },
51 1.5 christos { 22, 0x00, 0xaf, 0xd7 }, { 23, 0x00, 0xaf, 0xff },
52 1.5 christos { 24, 0x00, 0xd7, 0x00 }, { 25, 0x00, 0xd7, 0x5f },
53 1.5 christos { 26, 0x00, 0xd7, 0x87 }, { 27, 0x00, 0xd7, 0xaf },
54 1.5 christos { 28, 0x00, 0xd7, 0xd7 }, { 29, 0x00, 0xd7, 0xff },
55 1.5 christos { 30, 0x00, 0xff, 0x00 }, { 31, 0x00, 0xff, 0x5f },
56 1.5 christos { 32, 0x00, 0xff, 0x87 }, { 33, 0x00, 0xff, 0xaf },
57 1.5 christos { 34, 0x00, 0xff, 0xd7 }, { 35, 0x00, 0xff, 0xff },
58 1.5 christos { 36, 0x5f, 0x00, 0x00 }, { 37, 0x5f, 0x00, 0x5f },
59 1.5 christos { 38, 0x5f, 0x00, 0x87 }, { 39, 0x5f, 0x00, 0xaf },
60 1.5 christos { 40, 0x5f, 0x00, 0xd7 }, { 41, 0x5f, 0x00, 0xff },
61 1.5 christos { 42, 0x5f, 0x5f, 0x00 }, { 43, 0x5f, 0x5f, 0x5f },
62 1.5 christos { 44, 0x5f, 0x5f, 0x87 }, { 45, 0x5f, 0x5f, 0xaf },
63 1.5 christos { 46, 0x5f, 0x5f, 0xd7 }, { 47, 0x5f, 0x5f, 0xff },
64 1.5 christos { 48, 0x5f, 0x87, 0x00 }, { 49, 0x5f, 0x87, 0x5f },
65 1.5 christos { 50, 0x5f, 0x87, 0x87 }, { 51, 0x5f, 0x87, 0xaf },
66 1.5 christos { 52, 0x5f, 0x87, 0xd7 }, { 53, 0x5f, 0x87, 0xff },
67 1.5 christos { 54, 0x5f, 0xaf, 0x00 }, { 55, 0x5f, 0xaf, 0x5f },
68 1.5 christos { 56, 0x5f, 0xaf, 0x87 }, { 57, 0x5f, 0xaf, 0xaf },
69 1.5 christos { 58, 0x5f, 0xaf, 0xd7 }, { 59, 0x5f, 0xaf, 0xff },
70 1.5 christos { 60, 0x5f, 0xd7, 0x00 }, { 61, 0x5f, 0xd7, 0x5f },
71 1.5 christos { 62, 0x5f, 0xd7, 0x87 }, { 63, 0x5f, 0xd7, 0xaf },
72 1.5 christos { 64, 0x5f, 0xd7, 0xd7 }, { 65, 0x5f, 0xd7, 0xff },
73 1.5 christos { 66, 0x5f, 0xff, 0x00 }, { 67, 0x5f, 0xff, 0x5f },
74 1.5 christos { 68, 0x5f, 0xff, 0x87 }, { 69, 0x5f, 0xff, 0xaf },
75 1.5 christos { 70, 0x5f, 0xff, 0xd7 }, { 71, 0x5f, 0xff, 0xff },
76 1.5 christos { 72, 0x87, 0x00, 0x00 }, { 73, 0x87, 0x00, 0x5f },
77 1.5 christos { 74, 0x87, 0x00, 0x87 }, { 75, 0x87, 0x00, 0xaf },
78 1.5 christos { 76, 0x87, 0x00, 0xd7 }, { 77, 0x87, 0x00, 0xff },
79 1.5 christos { 78, 0x87, 0x5f, 0x00 }, { 79, 0x87, 0x5f, 0x5f },
80 1.5 christos { 80, 0x87, 0x5f, 0x87 }, { 81, 0x87, 0x5f, 0xaf },
81 1.5 christos { 82, 0x87, 0x5f, 0xd7 }, { 83, 0x87, 0x5f, 0xff },
82 1.5 christos { 84, 0x87, 0x87, 0x00 }, { 85, 0x87, 0x87, 0x5f },
83 1.5 christos { 86, 0x87, 0x87, 0x87 }, { 87, 0x87, 0x87, 0xaf },
84 1.5 christos { 88, 0x87, 0x87, 0xd7 }, { 89, 0x87, 0x87, 0xff },
85 1.5 christos { 90, 0x87, 0xaf, 0x00 }, { 91, 0x87, 0xaf, 0x5f },
86 1.5 christos { 92, 0x87, 0xaf, 0x87 }, { 93, 0x87, 0xaf, 0xaf },
87 1.5 christos { 94, 0x87, 0xaf, 0xd7 }, { 95, 0x87, 0xaf, 0xff },
88 1.5 christos { 96, 0x87, 0xd7, 0x00 }, { 97, 0x87, 0xd7, 0x5f },
89 1.5 christos { 98, 0x87, 0xd7, 0x87 }, { 99, 0x87, 0xd7, 0xaf },
90 1.5 christos { 100, 0x87, 0xd7, 0xd7 }, { 101, 0x87, 0xd7, 0xff },
91 1.5 christos { 102, 0x87, 0xff, 0x00 }, { 103, 0x87, 0xff, 0x5f },
92 1.5 christos { 104, 0x87, 0xff, 0x87 }, { 105, 0x87, 0xff, 0xaf },
93 1.5 christos { 106, 0x87, 0xff, 0xd7 }, { 107, 0x87, 0xff, 0xff },
94 1.5 christos { 108, 0xaf, 0x00, 0x00 }, { 109, 0xaf, 0x00, 0x5f },
95 1.5 christos { 110, 0xaf, 0x00, 0x87 }, { 111, 0xaf, 0x00, 0xaf },
96 1.5 christos { 112, 0xaf, 0x00, 0xd7 }, { 113, 0xaf, 0x00, 0xff },
97 1.5 christos { 114, 0xaf, 0x5f, 0x00 }, { 115, 0xaf, 0x5f, 0x5f },
98 1.5 christos { 116, 0xaf, 0x5f, 0x87 }, { 117, 0xaf, 0x5f, 0xaf },
99 1.5 christos { 118, 0xaf, 0x5f, 0xd7 }, { 119, 0xaf, 0x5f, 0xff },
100 1.5 christos { 120, 0xaf, 0x87, 0x00 }, { 121, 0xaf, 0x87, 0x5f },
101 1.5 christos { 122, 0xaf, 0x87, 0x87 }, { 123, 0xaf, 0x87, 0xaf },
102 1.5 christos { 124, 0xaf, 0x87, 0xd7 }, { 125, 0xaf, 0x87, 0xff },
103 1.5 christos { 126, 0xaf, 0xaf, 0x00 }, { 127, 0xaf, 0xaf, 0x5f },
104 1.5 christos { 128, 0xaf, 0xaf, 0x87 }, { 129, 0xaf, 0xaf, 0xaf },
105 1.5 christos { 130, 0xaf, 0xaf, 0xd7 }, { 131, 0xaf, 0xaf, 0xff },
106 1.5 christos { 132, 0xaf, 0xd7, 0x00 }, { 133, 0xaf, 0xd7, 0x5f },
107 1.5 christos { 134, 0xaf, 0xd7, 0x87 }, { 135, 0xaf, 0xd7, 0xaf },
108 1.5 christos { 136, 0xaf, 0xd7, 0xd7 }, { 137, 0xaf, 0xd7, 0xff },
109 1.5 christos { 138, 0xaf, 0xff, 0x00 }, { 139, 0xaf, 0xff, 0x5f },
110 1.5 christos { 140, 0xaf, 0xff, 0x87 }, { 141, 0xaf, 0xff, 0xaf },
111 1.5 christos { 142, 0xaf, 0xff, 0xd7 }, { 143, 0xaf, 0xff, 0xff },
112 1.5 christos { 144, 0xd7, 0x00, 0x00 }, { 145, 0xd7, 0x00, 0x5f },
113 1.5 christos { 146, 0xd7, 0x00, 0x87 }, { 147, 0xd7, 0x00, 0xaf },
114 1.5 christos { 148, 0xd7, 0x00, 0xd7 }, { 149, 0xd7, 0x00, 0xff },
115 1.5 christos { 150, 0xd7, 0x5f, 0x00 }, { 151, 0xd7, 0x5f, 0x5f },
116 1.5 christos { 152, 0xd7, 0x5f, 0x87 }, { 153, 0xd7, 0x5f, 0xaf },
117 1.5 christos { 154, 0xd7, 0x5f, 0xd7 }, { 155, 0xd7, 0x5f, 0xff },
118 1.5 christos { 156, 0xd7, 0x87, 0x00 }, { 157, 0xd7, 0x87, 0x5f },
119 1.5 christos { 158, 0xd7, 0x87, 0x87 }, { 159, 0xd7, 0x87, 0xaf },
120 1.5 christos { 160, 0xd7, 0x87, 0xd7 }, { 161, 0xd7, 0x87, 0xff },
121 1.5 christos { 162, 0xd7, 0xaf, 0x00 }, { 163, 0xd7, 0xaf, 0x5f },
122 1.5 christos { 164, 0xd7, 0xaf, 0x87 }, { 165, 0xd7, 0xaf, 0xaf },
123 1.5 christos { 166, 0xd7, 0xaf, 0xd7 }, { 167, 0xd7, 0xaf, 0xff },
124 1.5 christos { 168, 0xd7, 0xd7, 0x00 }, { 169, 0xd7, 0xd7, 0x5f },
125 1.5 christos { 170, 0xd7, 0xd7, 0x87 }, { 171, 0xd7, 0xd7, 0xaf },
126 1.5 christos { 172, 0xd7, 0xd7, 0xd7 }, { 173, 0xd7, 0xd7, 0xff },
127 1.5 christos { 174, 0xd7, 0xff, 0x00 }, { 175, 0xd7, 0xff, 0x5f },
128 1.5 christos { 176, 0xd7, 0xff, 0x87 }, { 177, 0xd7, 0xff, 0xaf },
129 1.5 christos { 178, 0xd7, 0xff, 0xd7 }, { 179, 0xd7, 0xff, 0xff },
130 1.5 christos { 180, 0xff, 0x00, 0x00 }, { 181, 0xff, 0x00, 0x5f },
131 1.5 christos { 182, 0xff, 0x00, 0x87 }, { 183, 0xff, 0x00, 0xaf },
132 1.5 christos { 184, 0xff, 0x00, 0xd7 }, { 185, 0xff, 0x00, 0xff },
133 1.5 christos { 186, 0xff, 0x5f, 0x00 }, { 187, 0xff, 0x5f, 0x5f },
134 1.5 christos { 188, 0xff, 0x5f, 0x87 }, { 189, 0xff, 0x5f, 0xaf },
135 1.5 christos { 190, 0xff, 0x5f, 0xd7 }, { 191, 0xff, 0x5f, 0xff },
136 1.5 christos { 192, 0xff, 0x87, 0x00 }, { 193, 0xff, 0x87, 0x5f },
137 1.5 christos { 194, 0xff, 0x87, 0x87 }, { 195, 0xff, 0x87, 0xaf },
138 1.5 christos { 196, 0xff, 0x87, 0xd7 }, { 197, 0xff, 0x87, 0xff },
139 1.5 christos { 198, 0xff, 0xaf, 0x00 }, { 199, 0xff, 0xaf, 0x5f },
140 1.5 christos { 200, 0xff, 0xaf, 0x87 }, { 201, 0xff, 0xaf, 0xaf },
141 1.5 christos { 202, 0xff, 0xaf, 0xd7 }, { 203, 0xff, 0xaf, 0xff },
142 1.5 christos { 204, 0xff, 0xd7, 0x00 }, { 205, 0xff, 0xd7, 0x5f },
143 1.5 christos { 206, 0xff, 0xd7, 0x87 }, { 207, 0xff, 0xd7, 0xaf },
144 1.5 christos { 208, 0xff, 0xd7, 0xd7 }, { 209, 0xff, 0xd7, 0xff },
145 1.5 christos { 210, 0xff, 0xff, 0x00 }, { 211, 0xff, 0xff, 0x5f },
146 1.5 christos { 212, 0xff, 0xff, 0x87 }, { 213, 0xff, 0xff, 0xaf },
147 1.5 christos { 214, 0xff, 0xff, 0xd7 }, { 215, 0xff, 0xff, 0xff },
148 1.5 christos { 216, 0x08, 0x08, 0x08 }, { 217, 0x12, 0x12, 0x12 },
149 1.5 christos { 218, 0x1c, 0x1c, 0x1c }, { 219, 0x26, 0x26, 0x26 },
150 1.5 christos { 220, 0x30, 0x30, 0x30 }, { 221, 0x3a, 0x3a, 0x3a },
151 1.5 christos { 222, 0x44, 0x44, 0x44 }, { 223, 0x4e, 0x4e, 0x4e },
152 1.5 christos { 224, 0x58, 0x58, 0x58 }, { 225, 0x62, 0x62, 0x62 },
153 1.5 christos { 226, 0x6c, 0x6c, 0x6c }, { 227, 0x76, 0x76, 0x76 },
154 1.5 christos { 228, 0x80, 0x80, 0x80 }, { 229, 0x8a, 0x8a, 0x8a },
155 1.5 christos { 230, 0x94, 0x94, 0x94 }, { 231, 0x9e, 0x9e, 0x9e },
156 1.5 christos { 232, 0xa8, 0xa8, 0xa8 }, { 233, 0xb2, 0xb2, 0xb2 },
157 1.5 christos { 234, 0xbc, 0xbc, 0xbc }, { 235, 0xc6, 0xc6, 0xc6 },
158 1.5 christos { 236, 0xd0, 0xd0, 0xd0 }, { 237, 0xda, 0xda, 0xda },
159 1.5 christos { 238, 0xe4, 0xe4, 0xe4 }, { 239, 0xee, 0xee, 0xee },
160 1.5 christos };
161 1.5 christos const struct colour_rgb colour_to_256[] = {
162 1.5 christos { 0, 0x00, 0x00, 0x00 }, { 1, 0x00, 0x00, 0x5f },
163 1.5 christos { 2, 0x00, 0x00, 0x87 }, { 3, 0x00, 0x00, 0xaf },
164 1.5 christos { 4, 0x00, 0x00, 0xd7 }, { 5, 0x00, 0x00, 0xff },
165 1.5 christos { 6, 0x00, 0x5f, 0x00 }, { 7, 0x00, 0x5f, 0x5f },
166 1.5 christos { 8, 0x00, 0x5f, 0x87 }, { 9, 0x00, 0x5f, 0xaf },
167 1.5 christos { 10, 0x00, 0x5f, 0xd7 }, { 11, 0x00, 0x5f, 0xff },
168 1.5 christos { 12, 0x00, 0x87, 0x00 }, { 13, 0x00, 0x87, 0x5f },
169 1.5 christos { 14, 0x00, 0x87, 0x87 }, { 15, 0x00, 0x87, 0xaf },
170 1.5 christos { 16, 0x00, 0x87, 0xd7 }, { 17, 0x00, 0x87, 0xff },
171 1.5 christos { 18, 0x00, 0xaf, 0x00 }, { 19, 0x00, 0xaf, 0x5f },
172 1.5 christos { 20, 0x00, 0xaf, 0x87 }, { 21, 0x00, 0xaf, 0xaf },
173 1.5 christos { 22, 0x00, 0xaf, 0xd7 }, { 23, 0x00, 0xaf, 0xff },
174 1.5 christos { 24, 0x00, 0xd7, 0x00 }, { 25, 0x00, 0xd7, 0x5f },
175 1.5 christos { 26, 0x00, 0xd7, 0x87 }, { 27, 0x00, 0xd7, 0xaf },
176 1.5 christos { 28, 0x00, 0xd7, 0xd7 }, { 29, 0x00, 0xd7, 0xff },
177 1.5 christos { 30, 0x00, 0xff, 0x00 }, { 31, 0x00, 0xff, 0x5f },
178 1.5 christos { 32, 0x00, 0xff, 0x87 }, { 33, 0x00, 0xff, 0xaf },
179 1.5 christos { 34, 0x00, 0xff, 0xd7 }, { 35, 0x00, 0xff, 0xff },
180 1.5 christos { 216, 0x08, 0x08, 0x08 }, { 217, 0x12, 0x12, 0x12 },
181 1.5 christos { 218, 0x1c, 0x1c, 0x1c }, { 219, 0x26, 0x26, 0x26 },
182 1.5 christos { 220, 0x30, 0x30, 0x30 }, { 221, 0x3a, 0x3a, 0x3a },
183 1.5 christos { 222, 0x44, 0x44, 0x44 }, { 223, 0x4e, 0x4e, 0x4e },
184 1.5 christos { 224, 0x58, 0x58, 0x58 }, { 36, 0x5f, 0x00, 0x00 },
185 1.5 christos { 37, 0x5f, 0x00, 0x5f }, { 38, 0x5f, 0x00, 0x87 },
186 1.5 christos { 39, 0x5f, 0x00, 0xaf }, { 40, 0x5f, 0x00, 0xd7 },
187 1.5 christos { 41, 0x5f, 0x00, 0xff }, { 42, 0x5f, 0x5f, 0x00 },
188 1.5 christos { 43, 0x5f, 0x5f, 0x5f }, { 44, 0x5f, 0x5f, 0x87 },
189 1.5 christos { 45, 0x5f, 0x5f, 0xaf }, { 46, 0x5f, 0x5f, 0xd7 },
190 1.5 christos { 47, 0x5f, 0x5f, 0xff }, { 48, 0x5f, 0x87, 0x00 },
191 1.5 christos { 49, 0x5f, 0x87, 0x5f }, { 50, 0x5f, 0x87, 0x87 },
192 1.5 christos { 51, 0x5f, 0x87, 0xaf }, { 52, 0x5f, 0x87, 0xd7 },
193 1.5 christos { 53, 0x5f, 0x87, 0xff }, { 54, 0x5f, 0xaf, 0x00 },
194 1.5 christos { 55, 0x5f, 0xaf, 0x5f }, { 56, 0x5f, 0xaf, 0x87 },
195 1.5 christos { 57, 0x5f, 0xaf, 0xaf }, { 58, 0x5f, 0xaf, 0xd7 },
196 1.5 christos { 59, 0x5f, 0xaf, 0xff }, { 60, 0x5f, 0xd7, 0x00 },
197 1.5 christos { 61, 0x5f, 0xd7, 0x5f }, { 62, 0x5f, 0xd7, 0x87 },
198 1.5 christos { 63, 0x5f, 0xd7, 0xaf }, { 64, 0x5f, 0xd7, 0xd7 },
199 1.5 christos { 65, 0x5f, 0xd7, 0xff }, { 66, 0x5f, 0xff, 0x00 },
200 1.5 christos { 67, 0x5f, 0xff, 0x5f }, { 68, 0x5f, 0xff, 0x87 },
201 1.5 christos { 69, 0x5f, 0xff, 0xaf }, { 70, 0x5f, 0xff, 0xd7 },
202 1.5 christos { 71, 0x5f, 0xff, 0xff }, { 225, 0x62, 0x62, 0x62 },
203 1.5 christos { 226, 0x6c, 0x6c, 0x6c }, { 227, 0x76, 0x76, 0x76 },
204 1.5 christos { 228, 0x80, 0x80, 0x80 }, { 72, 0x87, 0x00, 0x00 },
205 1.5 christos { 73, 0x87, 0x00, 0x5f }, { 74, 0x87, 0x00, 0x87 },
206 1.5 christos { 75, 0x87, 0x00, 0xaf }, { 76, 0x87, 0x00, 0xd7 },
207 1.5 christos { 77, 0x87, 0x00, 0xff }, { 78, 0x87, 0x5f, 0x00 },
208 1.5 christos { 79, 0x87, 0x5f, 0x5f }, { 80, 0x87, 0x5f, 0x87 },
209 1.5 christos { 81, 0x87, 0x5f, 0xaf }, { 82, 0x87, 0x5f, 0xd7 },
210 1.5 christos { 83, 0x87, 0x5f, 0xff }, { 84, 0x87, 0x87, 0x00 },
211 1.5 christos { 85, 0x87, 0x87, 0x5f }, { 86, 0x87, 0x87, 0x87 },
212 1.5 christos { 87, 0x87, 0x87, 0xaf }, { 88, 0x87, 0x87, 0xd7 },
213 1.5 christos { 89, 0x87, 0x87, 0xff }, { 90, 0x87, 0xaf, 0x00 },
214 1.5 christos { 91, 0x87, 0xaf, 0x5f }, { 92, 0x87, 0xaf, 0x87 },
215 1.5 christos { 93, 0x87, 0xaf, 0xaf }, { 94, 0x87, 0xaf, 0xd7 },
216 1.5 christos { 95, 0x87, 0xaf, 0xff }, { 96, 0x87, 0xd7, 0x00 },
217 1.5 christos { 97, 0x87, 0xd7, 0x5f }, { 98, 0x87, 0xd7, 0x87 },
218 1.5 christos { 99, 0x87, 0xd7, 0xaf }, { 100, 0x87, 0xd7, 0xd7 },
219 1.5 christos { 101, 0x87, 0xd7, 0xff }, { 102, 0x87, 0xff, 0x00 },
220 1.5 christos { 103, 0x87, 0xff, 0x5f }, { 104, 0x87, 0xff, 0x87 },
221 1.5 christos { 105, 0x87, 0xff, 0xaf }, { 106, 0x87, 0xff, 0xd7 },
222 1.5 christos { 107, 0x87, 0xff, 0xff }, { 229, 0x8a, 0x8a, 0x8a },
223 1.5 christos { 230, 0x94, 0x94, 0x94 }, { 231, 0x9e, 0x9e, 0x9e },
224 1.5 christos { 232, 0xa8, 0xa8, 0xa8 }, { 108, 0xaf, 0x00, 0x00 },
225 1.5 christos { 109, 0xaf, 0x00, 0x5f }, { 110, 0xaf, 0x00, 0x87 },
226 1.5 christos { 111, 0xaf, 0x00, 0xaf }, { 112, 0xaf, 0x00, 0xd7 },
227 1.5 christos { 113, 0xaf, 0x00, 0xff }, { 114, 0xaf, 0x5f, 0x00 },
228 1.5 christos { 115, 0xaf, 0x5f, 0x5f }, { 116, 0xaf, 0x5f, 0x87 },
229 1.5 christos { 117, 0xaf, 0x5f, 0xaf }, { 118, 0xaf, 0x5f, 0xd7 },
230 1.5 christos { 119, 0xaf, 0x5f, 0xff }, { 120, 0xaf, 0x87, 0x00 },
231 1.5 christos { 121, 0xaf, 0x87, 0x5f }, { 122, 0xaf, 0x87, 0x87 },
232 1.5 christos { 123, 0xaf, 0x87, 0xaf }, { 124, 0xaf, 0x87, 0xd7 },
233 1.5 christos { 125, 0xaf, 0x87, 0xff }, { 126, 0xaf, 0xaf, 0x00 },
234 1.5 christos { 127, 0xaf, 0xaf, 0x5f }, { 128, 0xaf, 0xaf, 0x87 },
235 1.5 christos { 129, 0xaf, 0xaf, 0xaf }, { 130, 0xaf, 0xaf, 0xd7 },
236 1.5 christos { 131, 0xaf, 0xaf, 0xff }, { 132, 0xaf, 0xd7, 0x00 },
237 1.5 christos { 133, 0xaf, 0xd7, 0x5f }, { 134, 0xaf, 0xd7, 0x87 },
238 1.5 christos { 135, 0xaf, 0xd7, 0xaf }, { 136, 0xaf, 0xd7, 0xd7 },
239 1.5 christos { 137, 0xaf, 0xd7, 0xff }, { 138, 0xaf, 0xff, 0x00 },
240 1.5 christos { 139, 0xaf, 0xff, 0x5f }, { 140, 0xaf, 0xff, 0x87 },
241 1.5 christos { 141, 0xaf, 0xff, 0xaf }, { 142, 0xaf, 0xff, 0xd7 },
242 1.5 christos { 143, 0xaf, 0xff, 0xff }, { 233, 0xb2, 0xb2, 0xb2 },
243 1.5 christos { 234, 0xbc, 0xbc, 0xbc }, { 235, 0xc6, 0xc6, 0xc6 },
244 1.5 christos { 236, 0xd0, 0xd0, 0xd0 }, { 144, 0xd7, 0x00, 0x00 },
245 1.5 christos { 145, 0xd7, 0x00, 0x5f }, { 146, 0xd7, 0x00, 0x87 },
246 1.5 christos { 147, 0xd7, 0x00, 0xaf }, { 148, 0xd7, 0x00, 0xd7 },
247 1.5 christos { 149, 0xd7, 0x00, 0xff }, { 150, 0xd7, 0x5f, 0x00 },
248 1.5 christos { 151, 0xd7, 0x5f, 0x5f }, { 152, 0xd7, 0x5f, 0x87 },
249 1.5 christos { 153, 0xd7, 0x5f, 0xaf }, { 154, 0xd7, 0x5f, 0xd7 },
250 1.5 christos { 155, 0xd7, 0x5f, 0xff }, { 156, 0xd7, 0x87, 0x00 },
251 1.5 christos { 157, 0xd7, 0x87, 0x5f }, { 158, 0xd7, 0x87, 0x87 },
252 1.5 christos { 159, 0xd7, 0x87, 0xaf }, { 160, 0xd7, 0x87, 0xd7 },
253 1.5 christos { 161, 0xd7, 0x87, 0xff }, { 162, 0xd7, 0xaf, 0x00 },
254 1.5 christos { 163, 0xd7, 0xaf, 0x5f }, { 164, 0xd7, 0xaf, 0x87 },
255 1.5 christos { 165, 0xd7, 0xaf, 0xaf }, { 166, 0xd7, 0xaf, 0xd7 },
256 1.5 christos { 167, 0xd7, 0xaf, 0xff }, { 168, 0xd7, 0xd7, 0x00 },
257 1.5 christos { 169, 0xd7, 0xd7, 0x5f }, { 170, 0xd7, 0xd7, 0x87 },
258 1.5 christos { 171, 0xd7, 0xd7, 0xaf }, { 172, 0xd7, 0xd7, 0xd7 },
259 1.5 christos { 173, 0xd7, 0xd7, 0xff }, { 174, 0xd7, 0xff, 0x00 },
260 1.5 christos { 175, 0xd7, 0xff, 0x5f }, { 176, 0xd7, 0xff, 0x87 },
261 1.5 christos { 177, 0xd7, 0xff, 0xaf }, { 178, 0xd7, 0xff, 0xd7 },
262 1.5 christos { 179, 0xd7, 0xff, 0xff }, { 237, 0xda, 0xda, 0xda },
263 1.5 christos { 238, 0xe4, 0xe4, 0xe4 }, { 239, 0xee, 0xee, 0xee },
264 1.5 christos { 180, 0xff, 0x00, 0x00 }, { 181, 0xff, 0x00, 0x5f },
265 1.5 christos { 182, 0xff, 0x00, 0x87 }, { 183, 0xff, 0x00, 0xaf },
266 1.5 christos { 184, 0xff, 0x00, 0xd7 }, { 185, 0xff, 0x00, 0xff },
267 1.5 christos { 186, 0xff, 0x5f, 0x00 }, { 187, 0xff, 0x5f, 0x5f },
268 1.5 christos { 188, 0xff, 0x5f, 0x87 }, { 189, 0xff, 0x5f, 0xaf },
269 1.5 christos { 190, 0xff, 0x5f, 0xd7 }, { 191, 0xff, 0x5f, 0xff },
270 1.5 christos { 192, 0xff, 0x87, 0x00 }, { 193, 0xff, 0x87, 0x5f },
271 1.5 christos { 194, 0xff, 0x87, 0x87 }, { 195, 0xff, 0x87, 0xaf },
272 1.5 christos { 196, 0xff, 0x87, 0xd7 }, { 197, 0xff, 0x87, 0xff },
273 1.5 christos { 198, 0xff, 0xaf, 0x00 }, { 199, 0xff, 0xaf, 0x5f },
274 1.5 christos { 200, 0xff, 0xaf, 0x87 }, { 201, 0xff, 0xaf, 0xaf },
275 1.5 christos { 202, 0xff, 0xaf, 0xd7 }, { 203, 0xff, 0xaf, 0xff },
276 1.5 christos { 204, 0xff, 0xd7, 0x00 }, { 205, 0xff, 0xd7, 0x5f },
277 1.5 christos { 206, 0xff, 0xd7, 0x87 }, { 207, 0xff, 0xd7, 0xaf },
278 1.5 christos { 208, 0xff, 0xd7, 0xd7 }, { 209, 0xff, 0xd7, 0xff },
279 1.5 christos { 210, 0xff, 0xff, 0x00 }, { 211, 0xff, 0xff, 0x5f },
280 1.5 christos { 212, 0xff, 0xff, 0x87 }, { 213, 0xff, 0xff, 0xaf },
281 1.5 christos { 214, 0xff, 0xff, 0xd7 }, { 215, 0xff, 0xff, 0xff },
282 1.5 christos };
283 1.2 he
284 1.5 christos int colour_cmp_rgb(const void *, const void *);
285 1.2 he
286 1.5 christos /* Compare function for bsearch(). */
287 1.5 christos int
288 1.5 christos colour_cmp_rgb(const void *lhs0, const void *rhs0)
289 1.2 he {
290 1.5 christos const struct colour_rgb *lhs = lhs0, *rhs = rhs0;
291 1.2 he
292 1.5 christos if (lhs->r < rhs->r)
293 1.5 christos return (-1);
294 1.5 christos if (lhs->r > rhs->r)
295 1.5 christos return (1);
296 1.2 he
297 1.5 christos if (lhs->g < rhs->g)
298 1.5 christos return (-1);
299 1.5 christos if (lhs->g > rhs->g)
300 1.5 christos return (1);
301 1.2 he
302 1.5 christos if (lhs->b < rhs->b)
303 1.5 christos return (-1);
304 1.5 christos if (lhs->b > rhs->b)
305 1.5 christos return (1);
306 1.2 he
307 1.5 christos return (0);
308 1.2 he }
309 1.2 he
310 1.2 he /* Work out the nearest colour from the 256 colour set. */
311 1.2 he int
312 1.5 christos colour_find_rgb(u_char r, u_char g, u_char b)
313 1.2 he {
314 1.5 christos struct colour_rgb rgb = { .r = r, .g = g, .b = b }, *found;
315 1.5 christos u_int distance, lowest, colour, i;
316 1.5 christos int dr, dg, db;
317 1.5 christos
318 1.5 christos found = bsearch(&rgb, colour_to_256, nitems(colour_to_256),
319 1.5 christos sizeof colour_to_256[0], colour_cmp_rgb);
320 1.5 christos if (found != NULL)
321 1.5 christos return (16 + found->i);
322 1.2 he
323 1.2 he colour = 16;
324 1.2 he lowest = UINT_MAX;
325 1.3 he for (i = 0; i < 240; i++) {
326 1.5 christos dr = (int)colour_from_256[i].r - r;
327 1.5 christos dg = (int)colour_from_256[i].g - g;
328 1.5 christos db = (int)colour_from_256[i].b - b;
329 1.5 christos
330 1.5 christos distance = dr * dr + dg * dg + db * db;
331 1.2 he if (distance < lowest) {
332 1.2 he lowest = distance;
333 1.2 he colour = 16 + i;
334 1.2 he }
335 1.2 he }
336 1.2 he return (colour);
337 1.2 he }
338 1.2 he
339 1.2 he /* Set grid cell foreground colour. */
340 1.1 jmmv void
341 1.1 jmmv colour_set_fg(struct grid_cell *gc, int c)
342 1.1 jmmv {
343 1.1 jmmv if (c & 0x100)
344 1.1 jmmv gc->flags |= GRID_FLAG_FG256;
345 1.1 jmmv gc->fg = c;
346 1.1 jmmv }
347 1.1 jmmv
348 1.2 he /* Set grid cell background colour. */
349 1.1 jmmv void
350 1.1 jmmv colour_set_bg(struct grid_cell *gc, int c)
351 1.1 jmmv {
352 1.1 jmmv if (c & 0x100)
353 1.1 jmmv gc->flags |= GRID_FLAG_BG256;
354 1.1 jmmv gc->bg = c;
355 1.1 jmmv }
356 1.1 jmmv
357 1.2 he /* Convert colour to a string. */
358 1.1 jmmv const char *
359 1.1 jmmv colour_tostring(int c)
360 1.1 jmmv {
361 1.1 jmmv static char s[32];
362 1.1 jmmv
363 1.1 jmmv if (c & 0x100) {
364 1.5 christos xsnprintf(s, sizeof s, "colour%d", c & ~0x100);
365 1.1 jmmv return (s);
366 1.1 jmmv }
367 1.1 jmmv
368 1.1 jmmv switch (c) {
369 1.1 jmmv case 0:
370 1.1 jmmv return ("black");
371 1.1 jmmv case 1:
372 1.1 jmmv return ("red");
373 1.1 jmmv case 2:
374 1.1 jmmv return ("green");
375 1.1 jmmv case 3:
376 1.1 jmmv return ("yellow");
377 1.1 jmmv case 4:
378 1.1 jmmv return ("blue");
379 1.1 jmmv case 5:
380 1.1 jmmv return ("magenta");
381 1.1 jmmv case 6:
382 1.1 jmmv return ("cyan");
383 1.1 jmmv case 7:
384 1.1 jmmv return ("white");
385 1.1 jmmv case 8:
386 1.1 jmmv return ("default");
387 1.4 christos case 90:
388 1.4 christos return ("brightblack");
389 1.4 christos case 91:
390 1.4 christos return ("brightred");
391 1.4 christos case 92:
392 1.4 christos return ("brightgreen");
393 1.4 christos case 93:
394 1.4 christos return ("brightyellow");
395 1.4 christos case 94:
396 1.4 christos return ("brightblue");
397 1.4 christos case 95:
398 1.4 christos return ("brightmagenta");
399 1.4 christos case 96:
400 1.4 christos return ("brightcyan");
401 1.4 christos case 97:
402 1.4 christos return ("brightwhite");
403 1.1 jmmv }
404 1.1 jmmv return (NULL);
405 1.1 jmmv }
406 1.1 jmmv
407 1.2 he /* Convert colour from string. */
408 1.1 jmmv int
409 1.1 jmmv colour_fromstring(const char *s)
410 1.1 jmmv {
411 1.5 christos const char *errstr;
412 1.5 christos const char *cp;
413 1.5 christos int n;
414 1.5 christos u_char r, g, b;
415 1.2 he
416 1.2 he if (*s == '#' && strlen(s) == 7) {
417 1.2 he for (cp = s + 1; isxdigit((u_char) *cp); cp++)
418 1.2 he ;
419 1.2 he if (*cp != '\0')
420 1.2 he return (-1);
421 1.5 christos n = sscanf(s + 1, "%2hhx%2hhx%2hhx", &r, &g, &b);
422 1.2 he if (n != 3)
423 1.2 he return (-1);
424 1.5 christos return (colour_find_rgb(r, g, b) | 0x100);
425 1.2 he }
426 1.1 jmmv
427 1.1 jmmv if (strncasecmp(s, "colour", (sizeof "colour") - 1) == 0) {
428 1.1 jmmv n = strtonum(s + (sizeof "colour") - 1, 0, 255, &errstr);
429 1.1 jmmv if (errstr != NULL)
430 1.1 jmmv return (-1);
431 1.1 jmmv return (n | 0x100);
432 1.1 jmmv }
433 1.1 jmmv
434 1.5 christos if (strcasecmp(s, "black") == 0 || strcmp(s, "0") == 0)
435 1.1 jmmv return (0);
436 1.5 christos if (strcasecmp(s, "red") == 0 || strcmp(s, "1") == 0)
437 1.1 jmmv return (1);
438 1.5 christos if (strcasecmp(s, "green") == 0 || strcmp(s, "2") == 0)
439 1.1 jmmv return (2);
440 1.5 christos if (strcasecmp(s, "yellow") == 0 || strcmp(s, "3") == 0)
441 1.1 jmmv return (3);
442 1.5 christos if (strcasecmp(s, "blue") == 0 || strcmp(s, "4") == 0)
443 1.1 jmmv return (4);
444 1.5 christos if (strcasecmp(s, "magenta") == 0 || strcmp(s, "5") == 0)
445 1.1 jmmv return (5);
446 1.5 christos if (strcasecmp(s, "cyan") == 0 || strcmp(s, "6") == 0)
447 1.1 jmmv return (6);
448 1.5 christos if (strcasecmp(s, "white") == 0 || strcmp(s, "7") == 0)
449 1.1 jmmv return (7);
450 1.5 christos if (strcasecmp(s, "default") == 0 || strcmp(s, "8") == 0)
451 1.1 jmmv return (8);
452 1.5 christos if (strcasecmp(s, "brightblack") == 0 || strcmp(s, "90") == 0)
453 1.4 christos return (90);
454 1.5 christos if (strcasecmp(s, "brightred") == 0 || strcmp(s, "91") == 0)
455 1.4 christos return (91);
456 1.5 christos if (strcasecmp(s, "brightgreen") == 0 || strcmp(s, "92") == 0)
457 1.4 christos return (92);
458 1.5 christos if (strcasecmp(s, "brightyellow") == 0 || strcmp(s, "93") == 0)
459 1.4 christos return (93);
460 1.5 christos if (strcasecmp(s, "brightblue") == 0 || strcmp(s, "94") == 0)
461 1.4 christos return (94);
462 1.5 christos if (strcasecmp(s, "brightmagenta") == 0 || strcmp(s, "95") == 0)
463 1.4 christos return (95);
464 1.5 christos if (strcasecmp(s, "brightcyan") == 0 || strcmp(s, "96") == 0)
465 1.4 christos return (96);
466 1.5 christos if (strcasecmp(s, "brightwhite") == 0 || strcmp(s, "97") == 0)
467 1.4 christos return (97);
468 1.1 jmmv return (-1);
469 1.1 jmmv }
470 1.1 jmmv
471 1.2 he /* Convert 256 colour palette to 16. */
472 1.1 jmmv u_char
473 1.1 jmmv colour_256to16(u_char c)
474 1.1 jmmv {
475 1.1 jmmv static const u_char table[256] = {
476 1.1 jmmv 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
477 1.1 jmmv 0, 4, 4, 4, 12, 12, 2, 6, 4, 4, 12, 12, 2, 2, 6, 4,
478 1.1 jmmv 12, 12, 2, 2, 2, 6, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10,
479 1.1 jmmv 10, 10, 10, 14, 1, 5, 4, 4, 12, 12, 3, 8, 4, 4, 12, 12,
480 1.1 jmmv 2, 2, 6, 4, 12, 12, 2, 2, 2, 6, 12, 12, 10, 10, 10, 10,
481 1.1 jmmv 14, 12, 10, 10, 10, 10, 10, 14, 1, 1, 5, 4, 12, 12, 1, 1,
482 1.1 jmmv 5, 4, 12, 12, 3, 3, 8, 4, 12, 12, 2, 2, 2, 6, 12, 12,
483 1.1 jmmv 10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14, 1, 1, 1, 5,
484 1.1 jmmv 12, 12, 1, 1, 1, 5, 12, 12, 1, 1, 1, 5, 12, 12, 3, 3,
485 1.1 jmmv 3, 7, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14,
486 1.1 jmmv 9, 9, 9, 9, 13, 12, 9, 9, 9, 9, 13, 12, 9, 9, 9, 9,
487 1.1 jmmv 13, 12, 9, 9, 9, 9, 13, 12, 11, 11, 11, 11, 7, 12, 10, 10,
488 1.1 jmmv 10, 10, 10, 14, 9, 9, 9, 9, 9, 13, 9, 9, 9, 9, 9, 13,
489 1.1 jmmv 9, 9, 9, 9, 9, 13, 9, 9, 9, 9, 9, 13, 9, 9, 9, 9,
490 1.1 jmmv 9, 13, 11, 11, 11, 11, 11, 15, 0, 0, 0, 0, 0, 0, 8, 8,
491 1.1 jmmv 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 15, 15, 15, 15, 15, 15
492 1.1 jmmv };
493 1.1 jmmv
494 1.1 jmmv return (table[c]);
495 1.1 jmmv }
496