Changes for automatic remapping of X colors on terminal frames:
[bpt/emacs.git] / lisp / term / tty-colors.el
CommitLineData
f795f633
EZ
1;;; tty-color.el --- color support for character terminals
2
3;; Copyright (C) 1999 Free Software Foundation, Inc.
4
5;; Author: Eli Zaretskii <eliz@is.elta.co.il>
6;; Maintainer: FSF
7;; Keywords: terminals, faces
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
25
26;;; Commentary:
27
28;; Emacs support for colors evolved from the X Window System; color
29;; support for character-based terminals came later. Many Lisp
30;; packages use color names defined by X and assume the availability
31;; of certain functions that look up colors, convert them to pixel
32;; values, etc.
33
34;; This file provides a more or less useful emulation of the X color
35;; functionality for character-based terminals, and thus relieves the
36;; rest of Emacs from including special code for this case.
37
38;; Here's how it works. The support for terminal and MSDOS frames
39;; maintains an alist, called `tty-color-alist', which associates
40;; colors supported by the terminal driver with small integers.
41;; (These small integers are passed to the library functions which set
42;; the color, and are effectively indices of the colors in the
43;; supported color palette.) When Emacs needs to send a color command
44;; to the terminal, the color name is first looked up in
45;; `tty-color-alist'. If not found, functions from this file can be
46;; used to map the color to one of the supported colors.
47;; Specifically, the X RGB values of the requested color are extracted
48;; from `color-name-rgb-alist' and then the supported color is found
49;; with the minimal distance in the RGB space from the requested
50;; color.
51
52;; `tty-color-alist' is created at startup by calling the function
53;; `tty-color-define', defined below, passing it each supported color,
54;; its index, and its RGB values. The standard list of colors
55;; supported by many Unix color terminals, including xterm, FreeBSD,
56;; and GNU/Linux, is supplied below in `tty-standard-colors'. If your
57;; terminal supports different or additional colors, call
58;; `tty-color-define' from your `.emacs' or `site-start.el'.
59
60;;; Code:
61
62(defvar color-name-rgb-alist
63 '(("snow" 255 250 250)
64 ("ghostwhite" 248 248 255)
65 ("whitesmoke" 245 245 245)
66 ("gainsboro" 220 220 220)
67 ("floralwhite" 255 250 240)
68 ("oldlace" 253 245 230)
69 ("linen" 250 240 230)
70 ("antiquewhite" 250 235 215)
71 ("papayawhip" 255 239 213)
72 ("blanchedalmond" 255 235 205)
73 ("bisque" 255 228 196)
74 ("peachpuff" 255 218 185)
75 ("navajowhite" 255 222 173)
76 ("moccasin" 255 228 181)
77 ("cornsilk" 255 248 220)
78 ("ivory" 255 255 240)
79 ("lemonchiffon" 255 250 205)
80 ("seashell" 255 245 238)
81 ("honeydew" 240 255 240)
82 ("mintcream" 245 255 250)
83 ("azure" 240 255 255)
84 ("aliceblue" 240 248 255)
85 ("lavender" 230 230 250)
86 ("lavenderblush" 255 240 245)
87 ("mistyrose" 255 228 225)
88 ("white" 255 255 255)
89 ("black" 0 0 0)
90 ("darkslategray" 47 79 79)
91 ("darkslategrey" 47 79 79)
92 ("dimgray" 105 105 105)
93 ("dimgrey" 105 105 105)
94 ("slategray" 112 128 144)
95 ("slategrey" 112 128 144)
96 ("lightslategray" 119 136 153)
97 ("lightslategrey" 119 136 153)
98 ("gray" 190 190 190)
99 ("grey" 190 190 190)
100 ("lightgrey" 211 211 211)
101 ("lightgray" 211 211 211)
102 ("midnightblue" 25 25 112)
103 ("navy" 0 0 128)
104 ("navyblue" 0 0 128)
105 ("cornflowerblue" 100 149 237)
106 ("darkslateblue" 72 61 139)
107 ("slateblue" 106 90 205)
108 ("mediumslateblue" 123 104 238)
109 ("lightslateblue" 132 112 255)
110 ("mediumblue" 0 0 205)
111 ("royalblue" 65 105 225)
112 ("blue" 0 0 255)
113 ("dodgerblue" 30 144 255)
114 ("deepskyblue" 0 191 255)
115 ("skyblue" 135 206 235)
116 ("lightskyblue" 135 206 250)
117 ("steelblue" 70 130 180)
118 ("lightsteelblue" 176 196 222)
119 ("lightblue" 173 216 230)
120 ("powderblue" 176 224 230)
121 ("paleturquoise" 175 238 238)
122 ("darkturquoise" 0 206 209)
123 ("mediumturquoise" 72 209 204)
124 ("turquoise" 64 224 208)
125 ("cyan" 0 255 255)
126 ("lightcyan" 224 255 255)
127 ("cadetblue" 95 158 160)
128 ("mediumaquamarine" 102 205 170)
129 ("aquamarine" 127 255 212)
130 ("darkgreen" 0 100 0)
131 ("darkolivegreen" 85 107 47)
132 ("darkseagreen" 143 188 143)
133 ("seagreen" 46 139 87)
134 ("mediumseagreen" 60 179 113)
135 ("lightseagreen" 32 178 170)
136 ("palegreen" 152 251 152)
137 ("springgreen" 0 255 127)
138 ("lawngreen" 124 252 0)
139 ("green" 0 255 0)
140 ("chartreuse" 127 255 0)
141 ("mediumspringgreen" 0 250 154)
142 ("greenyellow" 173 255 47)
143 ("limegreen" 50 205 50)
144 ("yellowgreen" 154 205 50)
145 ("forestgreen" 34 139 34)
146 ("olivedrab" 107 142 35)
147 ("darkkhaki" 189 183 107)
148 ("khaki" 240 230 140)
149 ("palegoldenrod" 238 232 170)
150 ("lightgoldenrodyellow" 250 250 210)
151 ("lightyellow" 255 255 224)
152 ("yellow" 255 255 0)
153 ("gold" 255 215 0)
154 ("lightgoldenrod" 238 221 130)
155 ("goldenrod" 218 165 32)
156 ("darkgoldenrod" 184 134 11)
157 ("rosybrown" 188 143 143)
158 ("indianred" 205 92 92)
159 ("saddlebrown" 139 69 19)
160 ("sienna" 160 82 45)
161 ("peru" 205 133 63)
162 ("burlywood" 222 184 135)
163 ("beige" 245 245 220)
164 ("wheat" 245 222 179)
165 ("sandybrown" 244 164 96)
166 ("tan" 210 180 140)
167 ("chocolate" 210 105 30)
168 ("firebrick" 178 34 34)
169 ("brown" 165 42 42)
170 ("darksalmon" 233 150 122)
171 ("salmon" 250 128 114)
172 ("lightsalmon" 255 160 122)
173 ("orange" 255 165 0)
174 ("darkorange" 255 140 0)
175 ("coral" 255 127 80)
176 ("lightcoral" 240 128 128)
177 ("tomato" 255 99 71)
178 ("orangered" 255 69 0)
179 ("red" 255 0 0)
180 ("hotpink" 255 105 180)
181 ("deeppink" 255 20 147)
182 ("pink" 255 192 203)
183 ("lightpink" 255 182 193)
184 ("palevioletred" 219 112 147)
185 ("maroon" 176 48 96)
186 ("mediumvioletred" 199 21 133)
187 ("violetred" 208 32 144)
188 ("magenta" 255 0 255)
189 ("violet" 238 130 238)
190 ("plum" 221 160 221)
191 ("orchid" 218 112 214)
192 ("mediumorchid" 186 85 211)
193 ("darkorchid" 153 50 204)
194 ("darkviolet" 148 0 211)
195 ("blueviolet" 138 43 226)
196 ("purple" 160 32 240)
197 ("mediumpurple" 147 112 219)
198 ("thistle" 216 191 216)
199 ("snow1" 255 250 250)
200 ("snow2" 238 233 233)
201 ("snow3" 205 201 201)
202 ("snow4" 139 137 137)
203 ("seashell1" 255 245 238)
204 ("seashell2" 238 229 222)
205 ("seashell3" 205 197 191)
206 ("seashell4" 139 134 130)
207 ("antiquewhite1" 255 239 219)
208 ("antiquewhite2" 238 223 204)
209 ("antiquewhite3" 205 192 176)
210 ("antiquewhite4" 139 131 120)
211 ("bisque1" 255 228 196)
212 ("bisque2" 238 213 183)
213 ("bisque3" 205 183 158)
214 ("bisque4" 139 125 107)
215 ("peachpuff1" 255 218 185)
216 ("peachpuff2" 238 203 173)
217 ("peachpuff3" 205 175 149)
218 ("peachpuff4" 139 119 101)
219 ("navajowhite1" 255 222 173)
220 ("navajowhite2" 238 207 161)
221 ("navajowhite3" 205 179 139)
222 ("navajowhite4" 139 121 94)
223 ("lemonchiffon1" 255 250 205)
224 ("lemonchiffon2" 238 233 191)
225 ("lemonchiffon3" 205 201 165)
226 ("lemonchiffon4" 139 137 112)
227 ("cornsilk1" 255 248 220)
228 ("cornsilk2" 238 232 205)
229 ("cornsilk3" 205 200 177)
230 ("cornsilk4" 139 136 120)
231 ("ivory1" 255 255 240)
232 ("ivory2" 238 238 224)
233 ("ivory3" 205 205 193)
234 ("ivory4" 139 139 131)
235 ("honeydew1" 240 255 240)
236 ("honeydew2" 224 238 224)
237 ("honeydew3" 193 205 193)
238 ("honeydew4" 131 139 131)
239 ("lavenderblush1" 255 240 245)
240 ("lavenderblush2" 238 224 229)
241 ("lavenderblush3" 205 193 197)
242 ("lavenderblush4" 139 131 134)
243 ("mistyrose1" 255 228 225)
244 ("mistyrose2" 238 213 210)
245 ("mistyrose3" 205 183 181)
246 ("mistyrose4" 139 125 123)
247 ("azure1" 240 255 255)
248 ("azure2" 224 238 238)
249 ("azure3" 193 205 205)
250 ("azure4" 131 139 139)
251 ("slateblue1" 131 111 255)
252 ("slateblue2" 122 103 238)
253 ("slateblue3" 105 89 205)
254 ("slateblue4" 71 60 139)
255 ("royalblue1" 72 118 255)
256 ("royalblue2" 67 110 238)
257 ("royalblue3" 58 95 205)
258 ("royalblue4" 39 64 139)
259 ("blue1" 0 0 255)
260 ("blue2" 0 0 238)
261 ("blue3" 0 0 205)
262 ("blue4" 0 0 139)
263 ("dodgerblue1" 30 144 255)
264 ("dodgerblue2" 28 134 238)
265 ("dodgerblue3" 24 116 205)
266 ("dodgerblue4" 16 78 139)
267 ("steelblue1" 99 184 255)
268 ("steelblue2" 92 172 238)
269 ("steelblue3" 79 148 205)
270 ("steelblue4" 54 100 139)
271 ("deepskyblue1" 0 191 255)
272 ("deepskyblue2" 0 178 238)
273 ("deepskyblue3" 0 154 205)
274 ("deepskyblue4" 0 104 139)
275 ("skyblue1" 135 206 255)
276 ("skyblue2" 126 192 238)
277 ("skyblue3" 108 166 205)
278 ("skyblue4" 74 112 139)
279 ("lightskyblue1" 176 226 255)
280 ("lightskyblue2" 164 211 238)
281 ("lightskyblue3" 141 182 205)
282 ("lightskyblue4" 96 123 139)
283 ("slategray1" 198 226 255)
284 ("slategray2" 185 211 238)
285 ("slategray3" 159 182 205)
286 ("slategray4" 108 123 139)
287 ("lightsteelblue1" 202 225 255)
288 ("lightsteelblue2" 188 210 238)
289 ("lightsteelblue3" 162 181 205)
290 ("lightsteelblue4" 110 123 139)
291 ("lightblue1" 191 239 255)
292 ("lightblue2" 178 223 238)
293 ("lightblue3" 154 192 205)
294 ("lightblue4" 104 131 139)
295 ("lightcyan1" 224 255 255)
296 ("lightcyan2" 209 238 238)
297 ("lightcyan3" 180 205 205)
298 ("lightcyan4" 122 139 139)
299 ("paleturquoise1" 187 255 255)
300 ("paleturquoise2" 174 238 238)
301 ("paleturquoise3" 150 205 205)
302 ("paleturquoise4" 102 139 139)
303 ("cadetblue1" 152 245 255)
304 ("cadetblue2" 142 229 238)
305 ("cadetblue3" 122 197 205)
306 ("cadetblue4" 83 134 139)
307 ("turquoise1" 0 245 255)
308 ("turquoise2" 0 229 238)
309 ("turquoise3" 0 197 205)
310 ("turquoise4" 0 134 139)
311 ("cyan1" 0 255 255)
312 ("cyan2" 0 238 238)
313 ("cyan3" 0 205 205)
314 ("cyan4" 0 139 139)
315 ("darkslategray1" 151 255 255)
316 ("darkslategray2" 141 238 238)
317 ("darkslategray3" 121 205 205)
318 ("darkslategray4" 82 139 139)
319 ("aquamarine1" 127 255 212)
320 ("aquamarine2" 118 238 198)
321 ("aquamarine3" 102 205 170)
322 ("aquamarine4" 69 139 116)
323 ("darkseagreen1" 193 255 193)
324 ("darkseagreen2" 180 238 180)
325 ("darkseagreen3" 155 205 155)
326 ("darkseagreen4" 105 139 105)
327 ("seagreen1" 84 255 159)
328 ("seagreen2" 78 238 148)
329 ("seagreen3" 67 205 128)
330 ("seagreen4" 46 139 87)
331 ("palegreen1" 154 255 154)
332 ("palegreen2" 144 238 144)
333 ("palegreen3" 124 205 124)
334 ("palegreen4" 84 139 84)
335 ("springgreen1" 0 255 127)
336 ("springgreen2" 0 238 118)
337 ("springgreen3" 0 205 102)
338 ("springgreen4" 0 139 69)
339 ("green1" 0 255 0)
340 ("green2" 0 238 0)
341 ("green3" 0 205 0)
342 ("green4" 0 139 0)
343 ("chartreuse1" 127 255 0)
344 ("chartreuse2" 118 238 0)
345 ("chartreuse3" 102 205 0)
346 ("chartreuse4" 69 139 0)
347 ("olivedrab1" 192 255 62)
348 ("olivedrab2" 179 238 58)
349 ("olivedrab3" 154 205 50)
350 ("olivedrab4" 105 139 34)
351 ("darkolivegreen1" 202 255 112)
352 ("darkolivegreen2" 188 238 104)
353 ("darkolivegreen3" 162 205 90)
354 ("darkolivegreen4" 110 139 61)
355 ("khaki1" 255 246 143)
356 ("khaki2" 238 230 133)
357 ("khaki3" 205 198 115)
358 ("khaki4" 139 134 78)
359 ("lightgoldenrod1" 255 236 139)
360 ("lightgoldenrod2" 238 220 130)
361 ("lightgoldenrod3" 205 190 112)
362 ("lightgoldenrod4" 139 129 76)
363 ("lightyellow1" 255 255 224)
364 ("lightyellow2" 238 238 209)
365 ("lightyellow3" 205 205 180)
366 ("lightyellow4" 139 139 122)
367 ("yellow1" 255 255 0)
368 ("yellow2" 238 238 0)
369 ("yellow3" 205 205 0)
370 ("yellow4" 139 139 0)
371 ("gold1" 255 215 0)
372 ("gold2" 238 201 0)
373 ("gold3" 205 173 0)
374 ("gold4" 139 117 0)
375 ("goldenrod1" 255 193 37)
376 ("goldenrod2" 238 180 34)
377 ("goldenrod3" 205 155 29)
378 ("goldenrod4" 139 105 20)
379 ("darkgoldenrod1" 255 185 15)
380 ("darkgoldenrod2" 238 173 14)
381 ("darkgoldenrod3" 205 149 12)
382 ("darkgoldenrod4" 139 101 8)
383 ("rosybrown1" 255 193 193)
384 ("rosybrown2" 238 180 180)
385 ("rosybrown3" 205 155 155)
386 ("rosybrown4" 139 105 105)
387 ("indianred1" 255 106 106)
388 ("indianred2" 238 99 99)
389 ("indianred3" 205 85 85)
390 ("indianred4" 139 58 58)
391 ("sienna1" 255 130 71)
392 ("sienna2" 238 121 66)
393 ("sienna3" 205 104 57)
394 ("sienna4" 139 71 38)
395 ("burlywood1" 255 211 155)
396 ("burlywood2" 238 197 145)
397 ("burlywood3" 205 170 125)
398 ("burlywood4" 139 115 85)
399 ("wheat1" 255 231 186)
400 ("wheat2" 238 216 174)
401 ("wheat3" 205 186 150)
402 ("wheat4" 139 126 102)
403 ("tan1" 255 165 79)
404 ("tan2" 238 154 73)
405 ("tan3" 205 133 63)
406 ("tan4" 139 90 43)
407 ("chocolate1" 255 127 36)
408 ("chocolate2" 238 118 33)
409 ("chocolate3" 205 102 29)
410 ("chocolate4" 139 69 19)
411 ("firebrick1" 255 48 48)
412 ("firebrick2" 238 44 44)
413 ("firebrick3" 205 38 38)
414 ("firebrick4" 139 26 26)
415 ("brown1" 255 64 64)
416 ("brown2" 238 59 59)
417 ("brown3" 205 51 51)
418 ("brown4" 139 35 35)
419 ("salmon1" 255 140 105)
420 ("salmon2" 238 130 98)
421 ("salmon3" 205 112 84)
422 ("salmon4" 139 76 57)
423 ("lightsalmon1" 255 160 122)
424 ("lightsalmon2" 238 149 114)
425 ("lightsalmon3" 205 129 98)
426 ("lightsalmon4" 139 87 66)
427 ("orange1" 255 165 0)
428 ("orange2" 238 154 0)
429 ("orange3" 205 133 0)
430 ("orange4" 139 90 0)
431 ("darkorange1" 255 127 0)
432 ("darkorange2" 238 118 0)
433 ("darkorange3" 205 102 0)
434 ("darkorange4" 139 69 0)
435 ("coral1" 255 114 86)
436 ("coral2" 238 106 80)
437 ("coral3" 205 91 69)
438 ("coral4" 139 62 47)
439 ("tomato1" 255 99 71)
440 ("tomato2" 238 92 66)
441 ("tomato3" 205 79 57)
442 ("tomato4" 139 54 38)
443 ("orangered1" 255 69 0)
444 ("orangered2" 238 64 0)
445 ("orangered3" 205 55 0)
446 ("orangered4" 139 37 0)
447 ("red1" 255 0 0)
448 ("red2" 238 0 0)
449 ("red3" 205 0 0)
450 ("red4" 139 0 0)
451 ("deeppink1" 255 20 147)
452 ("deeppink2" 238 18 137)
453 ("deeppink3" 205 16 118)
454 ("deeppink4" 139 10 80)
455 ("hotpink1" 255 110 180)
456 ("hotpink2" 238 106 167)
457 ("hotpink3" 205 96 144)
458 ("hotpink4" 139 58 98)
459 ("pink1" 255 181 197)
460 ("pink2" 238 169 184)
461 ("pink3" 205 145 158)
462 ("pink4" 139 99 108)
463 ("lightpink1" 255 174 185)
464 ("lightpink2" 238 162 173)
465 ("lightpink3" 205 140 149)
466 ("lightpink4" 139 95 101)
467 ("palevioletred1" 255 130 171)
468 ("palevioletred2" 238 121 159)
469 ("palevioletred3" 205 104 137)
470 ("palevioletred4" 139 71 93)
471 ("maroon1" 255 52 179)
472 ("maroon2" 238 48 167)
473 ("maroon3" 205 41 144)
474 ("maroon4" 139 28 98)
475 ("violetred1" 255 62 150)
476 ("violetred2" 238 58 140)
477 ("violetred3" 205 50 120)
478 ("violetred4" 139 34 82)
479 ("magenta1" 255 0 255)
480 ("magenta2" 238 0 238)
481 ("magenta3" 205 0 205)
482 ("magenta4" 139 0 139)
483 ("orchid1" 255 131 250)
484 ("orchid2" 238 122 233)
485 ("orchid3" 205 105 201)
486 ("orchid4" 139 71 137)
487 ("plum1" 255 187 255)
488 ("plum2" 238 174 238)
489 ("plum3" 205 150 205)
490 ("plum4" 139 102 139)
491 ("mediumorchid1" 224 102 255)
492 ("mediumorchid2" 209 95 238)
493 ("mediumorchid3" 180 82 205)
494 ("mediumorchid4" 122 55 139)
495 ("darkorchid1" 191 62 255)
496 ("darkorchid2" 178 58 238)
497 ("darkorchid3" 154 50 205)
498 ("darkorchid4" 104 34 139)
499 ("purple1" 155 48 255)
500 ("purple2" 145 44 238)
501 ("purple3" 125 38 205)
502 ("purple4" 85 26 139)
503 ("mediumpurple1" 171 130 255)
504 ("mediumpurple2" 159 121 238)
505 ("mediumpurple3" 137 104 205)
506 ("mediumpurple4" 93 71 139)
507 ("thistle1" 255 225 255)
508 ("thistle2" 238 210 238)
509 ("thistle3" 205 181 205)
510 ("thistle4" 139 123 139)
511 ("gray0" 0 0 0)
512 ("grey0" 0 0 0)
513 ("gray1" 3 3 3)
514 ("grey1" 3 3 3)
515 ("gray2" 5 5 5)
516 ("grey2" 5 5 5)
517 ("gray3" 8 8 8)
518 ("grey3" 8 8 8)
519 ("gray4" 10 10 10)
520 ("grey4" 10 10 10)
521 ("gray5" 13 13 13)
522 ("grey5" 13 13 13)
523 ("gray6" 15 15 15)
524 ("grey6" 15 15 15)
525 ("gray7" 18 18 18)
526 ("grey7" 18 18 18)
527 ("gray8" 20 20 20)
528 ("grey8" 20 20 20)
529 ("gray9" 23 23 23)
530 ("grey9" 23 23 23)
531 ("gray10" 26 26 26)
532 ("grey10" 26 26 26)
533 ("gray11" 28 28 28)
534 ("grey11" 28 28 28)
535 ("gray12" 31 31 31)
536 ("grey12" 31 31 31)
537 ("gray13" 33 33 33)
538 ("grey13" 33 33 33)
539 ("gray14" 36 36 36)
540 ("grey14" 36 36 36)
541 ("gray15" 38 38 38)
542 ("grey15" 38 38 38)
543 ("gray16" 41 41 41)
544 ("grey16" 41 41 41)
545 ("gray17" 43 43 43)
546 ("grey17" 43 43 43)
547 ("gray18" 46 46 46)
548 ("grey18" 46 46 46)
549 ("gray19" 48 48 48)
550 ("grey19" 48 48 48)
551 ("gray20" 51 51 51)
552 ("grey20" 51 51 51)
553 ("gray21" 54 54 54)
554 ("grey21" 54 54 54)
555 ("gray22" 56 56 56)
556 ("grey22" 56 56 56)
557 ("gray23" 59 59 59)
558 ("grey23" 59 59 59)
559 ("gray24" 61 61 61)
560 ("grey24" 61 61 61)
561 ("gray25" 64 64 64)
562 ("grey25" 64 64 64)
563 ("gray26" 66 66 66)
564 ("grey26" 66 66 66)
565 ("gray27" 69 69 69)
566 ("grey27" 69 69 69)
567 ("gray28" 71 71 71)
568 ("grey28" 71 71 71)
569 ("gray29" 74 74 74)
570 ("grey29" 74 74 74)
571 ("gray30" 77 77 77)
572 ("grey30" 77 77 77)
573 ("gray31" 79 79 79)
574 ("grey31" 79 79 79)
575 ("gray32" 82 82 82)
576 ("grey32" 82 82 82)
577 ("gray33" 84 84 84)
578 ("grey33" 84 84 84)
579 ("gray34" 87 87 87)
580 ("grey34" 87 87 87)
581 ("gray35" 89 89 89)
582 ("grey35" 89 89 89)
583 ("gray36" 92 92 92)
584 ("grey36" 92 92 92)
585 ("gray37" 94 94 94)
586 ("grey37" 94 94 94)
587 ("gray38" 97 97 97)
588 ("grey38" 97 97 97)
589 ("gray39" 99 99 99)
590 ("grey39" 99 99 99)
591 ("gray40" 102 102 102)
592 ("grey40" 102 102 102)
593 ("gray41" 105 105 105)
594 ("grey41" 105 105 105)
595 ("gray42" 107 107 107)
596 ("grey42" 107 107 107)
597 ("gray43" 110 110 110)
598 ("grey43" 110 110 110)
599 ("gray44" 112 112 112)
600 ("grey44" 112 112 112)
601 ("gray45" 115 115 115)
602 ("grey45" 115 115 115)
603 ("gray46" 117 117 117)
604 ("grey46" 117 117 117)
605 ("gray47" 120 120 120)
606 ("grey47" 120 120 120)
607 ("gray48" 122 122 122)
608 ("grey48" 122 122 122)
609 ("gray49" 125 125 125)
610 ("grey49" 125 125 125)
611 ("gray50" 127 127 127)
612 ("grey50" 127 127 127)
613 ("gray51" 130 130 130)
614 ("grey51" 130 130 130)
615 ("gray52" 133 133 133)
616 ("grey52" 133 133 133)
617 ("gray53" 135 135 135)
618 ("grey53" 135 135 135)
619 ("gray54" 138 138 138)
620 ("grey54" 138 138 138)
621 ("gray55" 140 140 140)
622 ("grey55" 140 140 140)
623 ("gray56" 143 143 143)
624 ("grey56" 143 143 143)
625 ("gray57" 145 145 145)
626 ("grey57" 145 145 145)
627 ("gray58" 148 148 148)
628 ("grey58" 148 148 148)
629 ("gray59" 150 150 150)
630 ("grey59" 150 150 150)
631 ("gray60" 153 153 153)
632 ("grey60" 153 153 153)
633 ("gray61" 156 156 156)
634 ("grey61" 156 156 156)
635 ("gray62" 158 158 158)
636 ("grey62" 158 158 158)
637 ("gray63" 161 161 161)
638 ("grey63" 161 161 161)
639 ("gray64" 163 163 163)
640 ("grey64" 163 163 163)
641 ("gray65" 166 166 166)
642 ("grey65" 166 166 166)
643 ("gray66" 168 168 168)
644 ("grey66" 168 168 168)
645 ("gray67" 171 171 171)
646 ("grey67" 171 171 171)
647 ("gray68" 173 173 173)
648 ("grey68" 173 173 173)
649 ("gray69" 176 176 176)
650 ("grey69" 176 176 176)
651 ("gray70" 179 179 179)
652 ("grey70" 179 179 179)
653 ("gray71" 181 181 181)
654 ("grey71" 181 181 181)
655 ("gray72" 184 184 184)
656 ("grey72" 184 184 184)
657 ("gray73" 186 186 186)
658 ("grey73" 186 186 186)
659 ("gray74" 189 189 189)
660 ("grey74" 189 189 189)
661 ("gray75" 191 191 191)
662 ("grey75" 191 191 191)
663 ("gray76" 194 194 194)
664 ("grey76" 194 194 194)
665 ("gray77" 196 196 196)
666 ("grey77" 196 196 196)
667 ("gray78" 199 199 199)
668 ("grey78" 199 199 199)
669 ("gray79" 201 201 201)
670 ("grey79" 201 201 201)
671 ("gray80" 204 204 204)
672 ("grey80" 204 204 204)
673 ("gray81" 207 207 207)
674 ("grey81" 207 207 207)
675 ("gray82" 209 209 209)
676 ("grey82" 209 209 209)
677 ("gray83" 212 212 212)
678 ("grey83" 212 212 212)
679 ("gray84" 214 214 214)
680 ("grey84" 214 214 214)
681 ("gray85" 217 217 217)
682 ("grey85" 217 217 217)
683 ("gray86" 219 219 219)
684 ("grey86" 219 219 219)
685 ("gray87" 222 222 222)
686 ("grey87" 222 222 222)
687 ("gray88" 224 224 224)
688 ("grey88" 224 224 224)
689 ("gray89" 227 227 227)
690 ("grey89" 227 227 227)
691 ("gray90" 229 229 229)
692 ("grey90" 229 229 229)
693 ("gray91" 232 232 232)
694 ("grey91" 232 232 232)
695 ("gray92" 235 235 235)
696 ("grey92" 235 235 235)
697 ("gray93" 237 237 237)
698 ("grey93" 237 237 237)
699 ("gray94" 240 240 240)
700 ("grey94" 240 240 240)
701 ("gray95" 242 242 242)
702 ("grey95" 242 242 242)
703 ("gray96" 245 245 245)
704 ("grey96" 245 245 245)
705 ("gray97" 247 247 247)
706 ("grey97" 247 247 247)
707 ("gray98" 250 250 250)
708 ("grey98" 250 250 250)
709 ("gray99" 252 252 252)
710 ("grey99" 252 252 252)
711 ("gray100" 255 255 255)
712 ("grey100" 255 255 255)
713 ("darkgrey" 169 169 169)
714 ("darkgray" 169 169 169)
715 ("darkblue" 0 0 139)
716 ("darkcyan" 0 139 139)
717 ("darkmagenta" 139 0 139)
718 ("darkred" 139 0 0)
719 ("lightgreen" 144 238 144))
720 "An alist of X color names and associated 8-bit RGB values.")
721
722(defvar tty-standard-colors
723 '(("white" 7 65535 65535 65535)
724 ("cyan" 6 0 65535 65535)
725 ("magenta" 5 65535 0 65535)
726 ("blue" 4 0 0 65535)
727 ("yellow" 3 65535 65535 0)
728 ("green" 2 0 65535 0)
729 ("red" 1 65535 0 0)
730 ("black" 0 0 0 0))
731 "An alist of 8 standard tty colors, their indices and RGB values.")
732
733(defvar tty-color-alist nil
734 "An alist of colors supported by the terminal.
735Each element is of the form:
736 \(NAME INDEX R G B\)
737where NAME is the name of the color, a string;
738INDEX is the index of this color to be sent to the terminal driver
739when the color should be displayed; it is typically a small integer;
740R, G, and B are the intensities of, accordingly, red, green, and blue
741components of the color, represented as numbers between 0 and 65535.
742The file `etc/rgb.txt' in the Emacs distribution lists the standard
743RGB values of the X colors. If RGB is nil, this color will not be
744considered by `tty-color-translate' as an approximation to another
745color.")
746
747(defun tty-color-canonicalize (color)
748 "Return COLOR in canonical form.
749A canonicalized color name is all-lower case, with any blanks removed."
750 (let ((color (downcase color)))
751 (while (string-match " +" color)
752 (setq color (replace-match "" nil nil color)))
753 color))
754
755(defun tty-color-define (name index &optional rgb)
756 "Specify a tty color by its NAME, terminal INDEX and RGB values.
757NAME is a string, INDEX is typically a small integer used to send to
758the terminal driver to switch on this color, and RGB is a list of 3
759numbers that specify the intensity of red, green, and blue components
760of the color.
761If specified, each one of the RGB components must be a number between
7620 and 65535. If RGB is omitted, the specified color will never be used
763by `tty-color-translate' as an approximation to another color."
764 (if (or (not (stringp name))
765 (not (integerp index))
766 (and rgb (or (not (listp rgb)) (/= (length rgb) 3))))
767 (error "Invalid specification for tty color \"%s\"" name))
768 (let* ((name (tty-color-canonicalize name))
769 (entry (assoc name tty-color-alist)))
770 (if entry
771 (setcdr entry (cons index rgb))
772 (setq tty-color-alist
773 (cons (append (list name index) rgb) tty-color-alist)))
774 tty-color-alist))
775
776(defun tty-color-clear ()
777 "Clear the list of supported tty colors."
778 (setq tty-color-alist nil))
779
780(defun tty-color-off-gray-diag (r g b)
781 "Compute the angle between the color given by R,G,B and the gray diagonal."
782 (let ((mag (sqrt (* 3 (+ (* r r) (* g g) (* b b))))))
783 (if (< mag 1) 0 (acos (/ (+ r g b) mag)))))
784
785(defun tty-color-approximate (rgb)
786 "Given a list of 3 rgb values in RGB, find the color in `tty-color-alist'
787which is the best approximation in the 3-dimensional RGB space,
788and return the index associated with the approximating color.
789Each value of the RGB triplet has to be scaled to the 0..255 range."
790 (let* ((color-list tty-color-alist)
791 (candidate (car color-list))
792 (best-distance 195076) ;; 3 * 255^2 + 15
793 best-color)
794 (while candidate
795 (let* ((try-rgb (cddr candidate))
796 (r (car rgb))
797 (g (cadr rgb))
798 (b (nth 2 rgb))
799 ;; If the approximated color is not close enough to the
800 ;; gray diagonal of the RGB cube, favor non-gray colors.
801 ;; (The number 0.065 is an empirical ad-hoc'ery.)
802 (favor-non-gray (>= (tty-color-off-gray-diag r g b) 0.065))
803 try-r try-g try-b
804 dif-r dif-g dif-b dist)
805 ;; If the RGB values of the candidate color are unknown, we
806 ;; never consider it for approximating another color.
807 (if try-rgb
808 (progn
809 (setq try-r (lsh (car try-rgb) -8)
810 try-g (lsh (cadr try-rgb) -8)
811 try-b (lsh (nth 2 try-rgb) -8))
812 (setq dif-r (- (car rgb) try-r)
813 dif-g (- (cadr rgb) try-g)
814 dif-b (- (nth 2 rgb) try-b))
815 (setq dist (+ (* dif-r dif-r) (* dif-g dif-g) (* dif-b dif-b)))
816 (if (and (< dist best-distance)
817 ;; The candidate color is on the gray diagonal
818 ;; if its RGB components are all equal.
819 (or (/= try-r try-g) (/= try-g try-b)
820 (not favor-non-gray)))
821 (setq best-distance dist
822 best-color candidate)))))
823 (setq color-list (cdr color-list))
824 (setq candidate (car color-list)))
825 (cadr best-color)))
826
827(defun tty-color-translate (color)
828 "Given a color COLOR, return the index of the corresponding TTY color.
829COLOR must be a string that is either the color's name, or its X-style
830specification like \"#RRGGBB\" or \"RGB:rr/gg/bb\", where each primary.
831color can be given with 1 to 4 hex digits.
832If COLOR is a color name that is found among supported colors in
833`tty-color-alist', the associated index is returned. Otherwise, the
834RGB values of the color, either as given by the argument or from
835looking up the name in `color-name-rgb-alist', are used to find the
836supported color that is the best approximation for COLOR in the RGB
837space.
838If COLOR is neither a valid X RGB specification of the color, nor a
839name of a color in `color-name-rgb-alist', the returned value is nil."
840 (and (stringp color)
841 (let* ((color (tty-color-canonicalize color))
842 (idx (cadr (assoc color tty-color-alist))))
843 (or idx
844 (let* ((len (length color))
845 (maxval 256)
846 (rgb
847 (cond
848 ((and (>= len 4) ;; X-style "#XXYYZZ" color spec
849 (eq (aref color 0) ?#)
850 (member (aref color 1)
851 '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
852 ?a ?b ?c ?d ?e ?f)))
853 ;; Translate the string "#XXYYZZ" into a list
854 ;; of numbers (XX YY ZZ). If the primary colors
855 ;; are specified with less than 4 hex digits,
856 ;; the used digits represent the most significant
857 ;; bits of the value (e.g. #XYZ = #X000Y000Z000).
858 (let* ((ndig (/ (- len 1) 3))
859 (i1 1)
860 (i2 (+ i1 ndig))
861 (i3 (+ i2 ndig)))
862 (list
863 (lsh
864 (string-to-number (substring color i1 i2) 16)
865 (* 4 (- 2 ndig)))
866 (lsh
867 (string-to-number (substring color i2 i3) 16)
868 (* 4 (- 2 ndig)))
869 (lsh
870 (string-to-number (substring color i3) 16)
871 (* 4 (- 2 ndig))))))
872 ((and (>= len 9) ;; X-style RGB:xx/yy/zz color spec
873 (string= (substring color 0 4) "rgb:"))
874 ;; Translate the string "RGB:XX/YY/ZZ" into a list
875 ;; of numbers (XX YY ZZ). If fewer than 4 hex
876 ;; digits are used, they represent the fraction
877 ;; of the maximum value (RGB:X/Y/Z = #XXXXYYYYZZZZ).
878 (let* ((ndig (/ (- len 3) 3))
879 (maxval (1- (expt 16 (- ndig 1))))
880 (i1 4)
881 (i2 (+ i1 ndig))
882 (i3 (+ i2 ndig)))
883 (list
884 (/ (* (string-to-number
885 (substring color i1 (- i2 1)) 16)
886 255)
887 maxval)
888 (/ (* (string-to-number
889 (substring color i2 (- i3 1)) 16)
890 255)
891 maxval)
892 (/ (* (string-to-number
893 (substring color i3) 16)
894 255)
895 maxval))))
896 (t
897 (cdr (assoc color color-name-rgb-alist))))))
898 (and rgb (tty-color-approximate rgb)))))))
899
900(defun tty-color-by-index (idx)
901 "Given a numeric index of a tty color, return its description.
902Value is a list of the form \(NAME INDEX R G B\)."
903 (and idx
904 (let ((colors tty-color-alist)
905 desc found)
906 (while colors
907 (setq desc (car colors))
908 (if (eq idx (car (cdr desc)))
909 (setq found desc))
910 (setq colors (cdr colors)))
911 found)))
912
913(defun tty-color-values (color &optional frame)
914 "Return RGB values of the color COLOR on a termcap frame FRAME.
915If COLOR is not directly supported by the display, return the RGB
916values for a supported color that is its best approximation.
917The value is a list of integer RGB values--\(RED GREEN BLUE\).
918These values range from 0 to 65535; white is (65535 65535 65535).
919If FRAME is omitted or nil, use the selected frame."
920 (let* ((frame (or frame (selected-frame)))
921 (color (tty-color-canonicalize color))
922 (supported (assoc color tty-color-alist)))
923 (or (and supported (cddr supported)) ; full spec in tty-color-alist
924 (and supported ; no RGB values in tty-color-alist: use X RGB values
925 (assoc color color-name-rgb-alist)
926 (cddr
927 (tty-color-by-index
928 (tty-color-approximate
929 (cdr (assoc color color-name-rgb-alist))))))
930 (cddr (tty-color-by-index (tty-color-translate color))))))
931
932(defun tty-color-desc (color)
933 "Return the description of the color COLOR for a character terminal.
934Value is a list of the form \(NAME INDEX R G B\). Note that the returned
935NAME is not necessarily the same string as the argument COLOR, because
936the latter might need to be approximated if it is not supported directly."
937 (let ((idx (tty-color-translate color)))
938 (tty-color-by-index idx)))