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