Some fixes to follow coding conventions in files maintained by FSF.
[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
744 '(("white" 7 65535 65535 65535)
745 ("cyan" 6 0 65535 65535)
746 ("magenta" 5 65535 0 65535)
747 ("blue" 4 0 0 65535)
748 ("yellow" 3 65535 65535 0)
749 ("green" 2 0 65535 0)
750 ("red" 1 65535 0 0)
751 ("black" 0 0 0 0))
752 "An alist of 8 standard tty colors, their indices and RGB values.")
753
20de726e
EZ
754(defvar tty-defined-color-alist nil
755 "An alist of defined terminal colors and their RGB values.
756
757See the docstring of `tty-color-alist' for the details.")
758
759(defun tty-color-alist (&optional frame)
760 "Return an alist of colors supported by FRAME's terminal.
761FRAME defaults to the selected frame.
762Each element of the returned alist is of the form:
f795f633
EZ
763 \(NAME INDEX R G B\)
764where NAME is the name of the color, a string;
765INDEX is the index of this color to be sent to the terminal driver
766when the color should be displayed; it is typically a small integer;
767R, G, and B are the intensities of, accordingly, red, green, and blue
768components of the color, represented as numbers between 0 and 65535.
769The file `etc/rgb.txt' in the Emacs distribution lists the standard
770RGB values of the X colors. If RGB is nil, this color will not be
771considered by `tty-color-translate' as an approximation to another
20de726e
EZ
772color."
773 tty-defined-color-alist)
774
775(defun tty-modify-color-alist (elt &optional frame)
776 "Put the association ELT int the alist of terminal colors for FRAME.
777ELT should be of the form \(NAME INDEX R G B\) (see `tty-color-alist'
778for details).
779If FRAME is unspecified or nil, it defaults to the selected frame.
780Value is the modified color alist for FRAME."
781 (let* ((entry (assoc (car elt) (tty-color-alist frame))))
782 (if entry
783 (setcdr entry (cdr elt))
784 (setq tty-defined-color-alist (cons elt tty-defined-color-alist)))
785 tty-defined-color-alist))
f795f633
EZ
786
787(defun tty-color-canonicalize (color)
788 "Return COLOR in canonical form.
789A 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)))
793 color))
794
20de726e 795(defun tty-color-define (name index &optional rgb frame)
f795f633
EZ
796 "Specify a tty color by its NAME, terminal INDEX and RGB values.
797NAME is a string, INDEX is typically a small integer used to send to
e2f599e9
EZ
798the terminal driver a command to switch this color on, and RGB is a
799list of 3 numbers that specify the intensity of red, green, and blue
800components of the color.
f795f633
EZ
801If specified, each one of the RGB components must be a number between
8020 and 65535. If RGB is omitted, the specified color will never be used
20de726e 803by `tty-color-translate' as an approximation to another color.
e2f599e9 804FRAME is the frame where the defined color should be used.
20de726e 805If FRAME is not specified or is nil, it defaults to the selected frame."
f795f633
EZ
806 (if (or (not (stringp name))
807 (not (integerp index))
808 (and rgb (or (not (listp rgb)) (/= (length rgb) 3))))
809 (error "Invalid specification for tty color \"%s\"" name))
20de726e
EZ
810 (tty-modify-color-alist
811 (append (list (tty-color-canonicalize name) index) rgb) frame))
f795f633 812
20de726e
EZ
813(defun tty-color-clear (&optional frame)
814 "Clear the list of supported tty colors for frame FRAME.
815If FRAME is unspecified or nil, it defaults to the selected frame."
816 (setq tty-defined-color-alist nil))
f795f633
EZ
817
818(defun tty-color-off-gray-diag (r g b)
e2f599e9
EZ
819 "Compute the angle between the color given by R,G,B and the gray diagonal.
820The gray diagonal is the diagonal of the 3D cube in RGB space which
821connects the points corresponding to the black and white colors. All the
822colors whose RGB coordinates belong to this diagonal are various shades
823of gray, thus the name."
f795f633
EZ
824 (let ((mag (sqrt (* 3 (+ (* r r) (* g g) (* b b))))))
825 (if (< mag 1) 0 (acos (/ (+ r g b) mag)))))
826
20de726e 827(defun tty-color-approximate (rgb &optional frame)
f795f633
EZ
828 "Given a list of 3 rgb values in RGB, find the color in `tty-color-alist'
829which is the best approximation in the 3-dimensional RGB space,
830and return the index associated with the approximating color.
20de726e
EZ
831Each value of the RGB triplet has to be scaled to the 0..255 range.
832FRAME defaults to the selected frame."
833 (let* ((color-list (tty-color-alist frame))
f795f633
EZ
834 (candidate (car color-list))
835 (best-distance 195076) ;; 3 * 255^2 + 15
836 best-color)
837 (while candidate
838 (let* ((try-rgb (cddr candidate))
839 (r (car rgb))
840 (g (cadr rgb))
841 (b (nth 2 rgb))
842 ;; If the approximated color is not close enough to the
843 ;; gray diagonal of the RGB cube, favor non-gray colors.
844 ;; (The number 0.065 is an empirical ad-hoc'ery.)
845 (favor-non-gray (>= (tty-color-off-gray-diag r g b) 0.065))
846 try-r try-g try-b
847 dif-r dif-g dif-b dist)
848 ;; If the RGB values of the candidate color are unknown, we
849 ;; never consider it for approximating another color.
850 (if try-rgb
851 (progn
852 (setq try-r (lsh (car try-rgb) -8)
853 try-g (lsh (cadr try-rgb) -8)
854 try-b (lsh (nth 2 try-rgb) -8))
855 (setq dif-r (- (car rgb) try-r)
856 dif-g (- (cadr rgb) try-g)
857 dif-b (- (nth 2 rgb) try-b))
858 (setq dist (+ (* dif-r dif-r) (* dif-g dif-g) (* dif-b dif-b)))
859 (if (and (< dist best-distance)
860 ;; The candidate color is on the gray diagonal
861 ;; if its RGB components are all equal.
862 (or (/= try-r try-g) (/= try-g try-b)
863 (not favor-non-gray)))
864 (setq best-distance dist
865 best-color candidate)))))
866 (setq color-list (cdr color-list))
867 (setq candidate (car color-list)))
868 (cadr best-color)))
869
20de726e 870(defun tty-color-translate (color &optional frame)
f795f633 871 "Given a color COLOR, return the index of the corresponding TTY color.
e2f599e9 872
f795f633
EZ
873COLOR must be a string that is either the color's name, or its X-style
874specification like \"#RRGGBB\" or \"RGB:rr/gg/bb\", where each primary.
875color can be given with 1 to 4 hex digits.
e2f599e9 876
f795f633
EZ
877If COLOR is a color name that is found among supported colors in
878`tty-color-alist', the associated index is returned. Otherwise, the
879RGB values of the color, either as given by the argument or from
880looking up the name in `color-name-rgb-alist', are used to find the
881supported color that is the best approximation for COLOR in the RGB
882space.
883If COLOR is neither a valid X RGB specification of the color, nor a
20de726e 884name of a color in `color-name-rgb-alist', the returned value is nil.
e2f599e9 885
20de726e 886If FRAME is unspecified or nil, it defaults to the selected frame."
f795f633
EZ
887 (and (stringp color)
888 (let* ((color (tty-color-canonicalize color))
20de726e 889 (idx (cadr (assoc color (tty-color-alist frame)))))
f795f633
EZ
890 (or idx
891 (let* ((len (length color))
892 (maxval 256)
893 (rgb
894 (cond
895 ((and (>= len 4) ;; X-style "#XXYYZZ" color spec
896 (eq (aref color 0) ?#)
897 (member (aref color 1)
898 '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
899 ?a ?b ?c ?d ?e ?f)))
900 ;; Translate the string "#XXYYZZ" into a list
901 ;; of numbers (XX YY ZZ). If the primary colors
902 ;; are specified with less than 4 hex digits,
903 ;; the used digits represent the most significant
904 ;; bits of the value (e.g. #XYZ = #X000Y000Z000).
905 (let* ((ndig (/ (- len 1) 3))
906 (i1 1)
907 (i2 (+ i1 ndig))
908 (i3 (+ i2 ndig)))
909 (list
910 (lsh
911 (string-to-number (substring color i1 i2) 16)
912 (* 4 (- 2 ndig)))
913 (lsh
914 (string-to-number (substring color i2 i3) 16)
915 (* 4 (- 2 ndig)))
916 (lsh
917 (string-to-number (substring color i3) 16)
918 (* 4 (- 2 ndig))))))
919 ((and (>= len 9) ;; X-style RGB:xx/yy/zz color spec
920 (string= (substring color 0 4) "rgb:"))
921 ;; Translate the string "RGB:XX/YY/ZZ" into a list
922 ;; of numbers (XX YY ZZ). If fewer than 4 hex
923 ;; digits are used, they represent the fraction
924 ;; of the maximum value (RGB:X/Y/Z = #XXXXYYYYZZZZ).
925 (let* ((ndig (/ (- len 3) 3))
926 (maxval (1- (expt 16 (- ndig 1))))
927 (i1 4)
928 (i2 (+ i1 ndig))
929 (i3 (+ i2 ndig)))
930 (list
931 (/ (* (string-to-number
932 (substring color i1 (- i2 1)) 16)
933 255)
934 maxval)
935 (/ (* (string-to-number
936 (substring color i2 (- i3 1)) 16)
937 255)
938 maxval)
939 (/ (* (string-to-number
940 (substring color i3) 16)
941 255)
942 maxval))))
943 (t
944 (cdr (assoc color color-name-rgb-alist))))))
20de726e 945 (and rgb (tty-color-approximate rgb frame)))))))
f795f633 946
20de726e 947(defun tty-color-by-index (idx &optional frame)
f795f633 948 "Given a numeric index of a tty color, return its description.
e2f599e9 949
20de726e 950FRAME, if unspecified or nil, defaults to the selected frame.
f795f633
EZ
951Value is a list of the form \(NAME INDEX R G B\)."
952 (and idx
20de726e 953 (let ((colors (tty-color-alist frame))
f795f633
EZ
954 desc found)
955 (while colors
956 (setq desc (car colors))
957 (if (eq idx (car (cdr desc)))
958 (setq found desc))
959 (setq colors (cdr colors)))
960 found)))
961
962(defun tty-color-values (color &optional frame)
963 "Return RGB values of the color COLOR on a termcap frame FRAME.
e2f599e9 964
f795f633
EZ
965If COLOR is not directly supported by the display, return the RGB
966values for a supported color that is its best approximation.
967The value is a list of integer RGB values--\(RED GREEN BLUE\).
968These values range from 0 to 65535; white is (65535 65535 65535).
969If FRAME is omitted or nil, use the selected frame."
970 (let* ((frame (or frame (selected-frame)))
971 (color (tty-color-canonicalize color))
20de726e 972 (supported (assoc color (tty-color-alist frame))))
f795f633
EZ
973 (or (and supported (cddr supported)) ; full spec in tty-color-alist
974 (and supported ; no RGB values in tty-color-alist: use X RGB values
975 (assoc color color-name-rgb-alist)
976 (cddr
977 (tty-color-by-index
978 (tty-color-approximate
20de726e
EZ
979 (cdr (assoc color color-name-rgb-alist)) frame) frame)))
980 (cddr (tty-color-by-index (tty-color-translate color frame) frame)))))
f795f633 981
20de726e 982(defun tty-color-desc (color &optional frame)
f795f633 983 "Return the description of the color COLOR for a character terminal.
e2f599e9 984
20de726e 985FRAME, if unspecified or nil, defaults to the selected frame.
f795f633
EZ
986Value is a list of the form \(NAME INDEX R G B\). Note that the returned
987NAME is not necessarily the same string as the argument COLOR, because
988the latter might need to be approximated if it is not supported directly."
20de726e
EZ
989 (let ((idx (tty-color-translate color frame)))
990 (tty-color-by-index idx frame)))
c527e600
EZ
991
992(defun tty-color-gray-shades (&optional display)
993 "Return the number of gray colors supported by DISPLAY's terminal.
994A color is considered gray if the 3 components of its RGB value are equal."
995 (let* ((frame (if (framep display) display
996 ;; FIXME: this uses an arbitrary frame from DISPLAY!
997 (car (frames-on-display-list display))))
998 (colors (tty-color-alist frame))
999 (count 0)
1000 desc r g b)
1001 (while colors
1002 (setq desc (cddr (car colors))
1003 r (car desc)
1004 g (cadr desc)
1005 b (car (cddr desc)))
26b09289
EZ
1006 (and (numberp r)
1007 (eq r g) (eq g b)
c527e600
EZ
1008 (setq count (1+ count)))
1009 (setq colors (cdr colors)))
1010 count))
55535639
PJ
1011
1012;;; tty-colors.el ends here