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