1 ;;; tty-color.el --- color support for character terminals
3 ;; Copyright (C) 1999 Free Software Foundation, Inc.
5 ;; Author: Eli Zaretskii <eliz@is.elta.co.il>
7 ;; Keywords: terminals, faces
9 ;; This file is part of GNU Emacs.
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)
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.
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.
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
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.
38 ;; Here's how it works. The support for terminal and MSDOS frames
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.
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
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.
64 ;; The following list is taken from rgb.txt distributed with X.
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.
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.
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.
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)
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)
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)
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)
121 ("lightgrey" 211 211 211)
122 ("lightgray" 211 211 211)
123 ("midnightblue" 25 25 112)
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)
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)
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)
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)
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)
183 ("burlywood" 222 184 135)
184 ("beige" 245 245 220)
185 ("wheat" 245 222 179)
186 ("sandybrown" 244 164 96)
188 ("chocolate" 210 105 30)
189 ("firebrick" 178 34 34)
191 ("darksalmon" 233 150 122)
192 ("salmon" 250 128 114)
193 ("lightsalmon" 255 160 122)
195 ("darkorange" 255 140 0)
197 ("lightcoral" 240 128 128)
199 ("orangered" 255 69 0)
201 ("hotpink" 255 105 180)
202 ("deeppink" 255 20 147)
204 ("lightpink" 255 182 193)
205 ("palevioletred" 219 112 147)
207 ("mediumvioletred" 199 21 133)
208 ("violetred" 208 32 144)
209 ("magenta" 255 0 255)
210 ("violet" 238 130 238)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
737 ("darkcyan" 0 139 139) ; no "lightmagenta", see the comment above
738 ("darkmagenta" 139 0 139)
739 ("darkred" 139 0 0) ; but no "lightred", see the comment above
740 ("lightgreen" 144 238 144))
741 "An alist of X color names and associated 8-bit RGB values.")
743 (defvar tty-standard-colors
744 '(("white" 7 65535 65535 65535)
745 ("cyan" 6 0 65535 65535)
746 ("magenta" 5 65535 0 65535)
748 ("yellow" 3 65535 65535 0)
749 ("green" 2 0 65535 0)
752 "An alist of 8 standard tty colors, their indices and RGB values.")
754 (defvar tty-defined-color-alist nil
755 "An alist of defined terminal colors and their RGB values.
757 See the docstring of `tty-color-alist' for the details.")
759 (defun tty-color-alist (&optional frame
)
760 "Return an alist of colors supported by FRAME's terminal.
761 FRAME defaults to the selected frame.
762 Each element of the returned alist is of the form:
764 where NAME is the name of the color, a string;
765 INDEX is the index of this color to be sent to the terminal driver
766 when the color should be displayed; it is typically a small integer;
767 R, G, and B are the intensities of, accordingly, red, green, and blue
768 components of the color, represented as numbers between 0 and 65535.
769 The file `etc/rgb.txt' in the Emacs distribution lists the standard
770 RGB values of the X colors. If RGB is nil, this color will not be
771 considered by `tty-color-translate' as an approximation to another
773 tty-defined-color-alist
)
775 (defun tty-modify-color-alist (elt &optional frame
)
776 "Put the association ELT int the alist of terminal colors for FRAME.
777 ELT should be of the form \(NAME INDEX R G B\) (see `tty-color-alist'
779 If FRAME is unspecified or nil, it defaults to the selected frame.
780 Value is the modified color alist for FRAME."
781 (let* ((entry (assoc (car elt
) (tty-color-alist frame
))))
783 (setcdr entry
(cdr elt
))
784 (setq tty-defined-color-alist
(cons elt tty-defined-color-alist
)))
785 tty-defined-color-alist
))
787 (defun tty-color-canonicalize (color)
788 "Return COLOR in canonical form.
789 A canonicalized color name is all-lower case, with any blanks removed."
790 (let ((color (downcase color
)))
791 (while (string-match " +" color
)
792 (setq color
(replace-match "" nil nil color
)))
795 (defun tty-color-define (name index
&optional rgb frame
)
796 "Specify a tty color by its NAME, terminal INDEX and RGB values.
797 NAME is a string, INDEX is typically a small integer used to send to
798 the terminal driver to switch on this color, and RGB is a list of 3
799 numbers that specify the intensity of red, green, and blue components
801 If specified, each one of the RGB components must be a number between
802 0 and 65535. If RGB is omitted, the specified color will never be used
803 by `tty-color-translate' as an approximation to another color.
804 If FRAME is not specified or is nil, it defaults to the selected frame."
805 (if (or (not (stringp name
))
806 (not (integerp index
))
807 (and rgb
(or (not (listp rgb
)) (/= (length rgb
) 3))))
808 (error "Invalid specification for tty color \"%s\"" name
))
809 (tty-modify-color-alist
810 (append (list (tty-color-canonicalize name
) index
) rgb
) frame
))
812 (defun tty-color-clear (&optional frame
)
813 "Clear the list of supported tty colors for frame FRAME.
814 If FRAME is unspecified or nil, it defaults to the selected frame."
815 (setq tty-defined-color-alist nil
))
817 (defun tty-color-off-gray-diag (r g b
)
818 "Compute the angle between the color given by R,G,B and the gray diagonal."
819 (let ((mag (sqrt (* 3 (+ (* r r
) (* g g
) (* b b
))))))
820 (if (< mag
1) 0 (acos (/ (+ r g b
) mag
)))))
822 (defun tty-color-approximate (rgb &optional frame
)
823 "Given a list of 3 rgb values in RGB, find the color in `tty-color-alist'
824 which is the best approximation in the 3-dimensional RGB space,
825 and return the index associated with the approximating color.
826 Each value of the RGB triplet has to be scaled to the 0..255 range.
827 FRAME defaults to the selected frame."
828 (let* ((color-list (tty-color-alist frame
))
829 (candidate (car color-list
))
830 (best-distance 195076) ;; 3 * 255^2 + 15
833 (let* ((try-rgb (cddr candidate
))
837 ;; If the approximated color is not close enough to the
838 ;; gray diagonal of the RGB cube, favor non-gray colors.
839 ;; (The number 0.065 is an empirical ad-hoc'ery.)
840 (favor-non-gray (>= (tty-color-off-gray-diag r g b
) 0.065))
842 dif-r dif-g dif-b dist
)
843 ;; If the RGB values of the candidate color are unknown, we
844 ;; never consider it for approximating another color.
847 (setq try-r
(lsh (car try-rgb
) -
8)
848 try-g
(lsh (cadr try-rgb
) -
8)
849 try-b
(lsh (nth 2 try-rgb
) -
8))
850 (setq dif-r
(- (car rgb
) try-r
)
851 dif-g
(- (cadr rgb
) try-g
)
852 dif-b
(- (nth 2 rgb
) try-b
))
853 (setq dist
(+ (* dif-r dif-r
) (* dif-g dif-g
) (* dif-b dif-b
)))
854 (if (and (< dist best-distance
)
855 ;; The candidate color is on the gray diagonal
856 ;; if its RGB components are all equal.
857 (or (/= try-r try-g
) (/= try-g try-b
)
858 (not favor-non-gray
)))
859 (setq best-distance dist
860 best-color candidate
)))))
861 (setq color-list
(cdr color-list
))
862 (setq candidate
(car color-list
)))
865 (defun tty-color-translate (color &optional frame
)
866 "Given a color COLOR, return the index of the corresponding TTY color.
867 COLOR must be a string that is either the color's name, or its X-style
868 specification like \"#RRGGBB\" or \"RGB:rr/gg/bb\", where each primary.
869 color can be given with 1 to 4 hex digits.
870 If COLOR is a color name that is found among supported colors in
871 `tty-color-alist', the associated index is returned. Otherwise, the
872 RGB values of the color, either as given by the argument or from
873 looking up the name in `color-name-rgb-alist', are used to find the
874 supported color that is the best approximation for COLOR in the RGB
876 If COLOR is neither a valid X RGB specification of the color, nor a
877 name of a color in `color-name-rgb-alist', the returned value is nil.
878 If FRAME is unspecified or nil, it defaults to the selected frame."
880 (let* ((color (tty-color-canonicalize color
))
881 (idx (cadr (assoc color
(tty-color-alist frame
)))))
883 (let* ((len (length color
))
887 ((and (>= len
4) ;; X-style "#XXYYZZ" color spec
888 (eq (aref color
0) ?
#)
889 (member (aref color
1)
890 '(?
0 ?
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 ?
8 ?
9
892 ;; Translate the string "#XXYYZZ" into a list
893 ;; of numbers (XX YY ZZ). If the primary colors
894 ;; are specified with less than 4 hex digits,
895 ;; the used digits represent the most significant
896 ;; bits of the value (e.g. #XYZ = #X000Y000Z000).
897 (let* ((ndig (/ (- len
1) 3))
903 (string-to-number (substring color i1 i2
) 16)
906 (string-to-number (substring color i2 i3
) 16)
909 (string-to-number (substring color i3
) 16)
911 ((and (>= len
9) ;; X-style RGB:xx/yy/zz color spec
912 (string= (substring color
0 4) "rgb:"))
913 ;; Translate the string "RGB:XX/YY/ZZ" into a list
914 ;; of numbers (XX YY ZZ). If fewer than 4 hex
915 ;; digits are used, they represent the fraction
916 ;; of the maximum value (RGB:X/Y/Z = #XXXXYYYYZZZZ).
917 (let* ((ndig (/ (- len
3) 3))
918 (maxval (1- (expt 16 (- ndig
1))))
923 (/ (* (string-to-number
924 (substring color i1
(- i2
1)) 16)
927 (/ (* (string-to-number
928 (substring color i2
(- i3
1)) 16)
931 (/ (* (string-to-number
932 (substring color i3
) 16)
936 (cdr (assoc color color-name-rgb-alist
))))))
937 (and rgb
(tty-color-approximate rgb frame
)))))))
939 (defun tty-color-by-index (idx &optional frame
)
940 "Given a numeric index of a tty color, return its description.
941 FRAME, if unspecified or nil, defaults to the selected frame.
942 Value is a list of the form \(NAME INDEX R G B\)."
944 (let ((colors (tty-color-alist frame
))
947 (setq desc
(car colors
))
948 (if (eq idx
(car (cdr desc
)))
950 (setq colors
(cdr colors
)))
953 (defun tty-color-values (color &optional frame
)
954 "Return RGB values of the color COLOR on a termcap frame FRAME.
955 If COLOR is not directly supported by the display, return the RGB
956 values for a supported color that is its best approximation.
957 The value is a list of integer RGB values--\(RED GREEN BLUE\).
958 These values range from 0 to 65535; white is (65535 65535 65535).
959 If FRAME is omitted or nil, use the selected frame."
960 (let* ((frame (or frame
(selected-frame)))
961 (color (tty-color-canonicalize color
))
962 (supported (assoc color
(tty-color-alist frame
))))
963 (or (and supported
(cddr supported
)) ; full spec in tty-color-alist
964 (and supported
; no RGB values in tty-color-alist: use X RGB values
965 (assoc color color-name-rgb-alist
)
968 (tty-color-approximate
969 (cdr (assoc color color-name-rgb-alist
)) frame
) frame
)))
970 (cddr (tty-color-by-index (tty-color-translate color frame
) frame
)))))
972 (defun tty-color-desc (color &optional frame
)
973 "Return the description of the color COLOR for a character terminal.
974 FRAME, if unspecified or nil, defaults to the selected frame.
975 Value is a list of the form \(NAME INDEX R G B\). Note that the returned
976 NAME is not necessarily the same string as the argument COLOR, because
977 the latter might need to be approximated if it is not supported directly."
978 (let ((idx (tty-color-translate color frame
)))
979 (tty-color-by-index idx frame
)))
981 (defun tty-color-gray-shades (&optional display
)
982 "Return the number of gray colors supported by DISPLAY's terminal.
983 A color is considered gray if the 3 components of its RGB value are equal."
984 (let* ((frame (if (framep display
) display
985 ;; FIXME: this uses an arbitrary frame from DISPLAY!
986 (car (frames-on-display-list display
))))
987 (colors (tty-color-alist frame
))
991 (setq desc
(cddr (car colors
))
997 (setq count
(1+ count
)))
998 (setq colors
(cdr colors
)))