diff options
| author | Eli Zaretskii | 2000-01-02 14:11:02 +0000 |
|---|---|---|
| committer | Eli Zaretskii | 2000-01-02 14:11:02 +0000 |
| commit | 20de726edeca2c3158a5e0e5588a16af9cdede11 (patch) | |
| tree | 7f7c68991a602ae2f850f668f0694a7cf2244239 | |
| parent | a61c12d547c5262d9441e664d56108f7d08e41a6 (diff) | |
| download | emacs-20de726edeca2c3158a5e0e5588a16af9cdede11.tar.gz emacs-20de726edeca2c3158a5e0e5588a16af9cdede11.zip | |
(tty-defined-color-alist): Renamed from tty-color-alist.
(tty-color-alist, tty-modify-color-alist): New functions.
(tty-color-define, tty-color-clear, tty-color-approximate)
(tty-color-translate, tty-color-by-index, tty-color-desc): Accept
an optional parameter FRAME.
| -rw-r--r-- | lisp/term/tty-colors.el | 82 |
1 files changed, 51 insertions, 31 deletions
diff --git a/lisp/term/tty-colors.el b/lisp/term/tty-colors.el index f4836fe8efa..8291ac111be 100644 --- a/lisp/term/tty-colors.el +++ b/lisp/term/tty-colors.el | |||
| @@ -730,9 +730,15 @@ | |||
| 730 | ("black" 0 0 0 0)) | 730 | ("black" 0 0 0 0)) |
| 731 | "An alist of 8 standard tty colors, their indices and RGB values.") | 731 | "An alist of 8 standard tty colors, their indices and RGB values.") |
| 732 | 732 | ||
| 733 | (defvar tty-color-alist nil | 733 | (defvar tty-defined-color-alist nil |
| 734 | "An alist of colors supported by the terminal. | 734 | "An alist of defined terminal colors and their RGB values. |
| 735 | Each element is of the form: | 735 | |
| 736 | See the docstring of `tty-color-alist' for the details.") | ||
| 737 | |||
| 738 | (defun tty-color-alist (&optional frame) | ||
| 739 | "Return an alist of colors supported by FRAME's terminal. | ||
| 740 | FRAME defaults to the selected frame. | ||
| 741 | Each element of the returned alist is of the form: | ||
| 736 | \(NAME INDEX R G B\) | 742 | \(NAME INDEX R G B\) |
| 737 | where NAME is the name of the color, a string; | 743 | where NAME is the name of the color, a string; |
| 738 | INDEX is the index of this color to be sent to the terminal driver | 744 | INDEX is the index of this color to be sent to the terminal driver |
| @@ -742,7 +748,20 @@ components of the color, represented as numbers between 0 and 65535. | |||
| 742 | The file `etc/rgb.txt' in the Emacs distribution lists the standard | 748 | The file `etc/rgb.txt' in the Emacs distribution lists the standard |
| 743 | RGB values of the X colors. If RGB is nil, this color will not be | 749 | RGB values of the X colors. If RGB is nil, this color will not be |
| 744 | considered by `tty-color-translate' as an approximation to another | 750 | considered by `tty-color-translate' as an approximation to another |
| 745 | color.") | 751 | color." |
| 752 | tty-defined-color-alist) | ||
| 753 | |||
| 754 | (defun tty-modify-color-alist (elt &optional frame) | ||
| 755 | "Put the association ELT int the alist of terminal colors for FRAME. | ||
| 756 | ELT should be of the form \(NAME INDEX R G B\) (see `tty-color-alist' | ||
| 757 | for details). | ||
| 758 | If FRAME is unspecified or nil, it defaults to the selected frame. | ||
| 759 | Value is the modified color alist for FRAME." | ||
| 760 | (let* ((entry (assoc (car elt) (tty-color-alist frame)))) | ||
| 761 | (if entry | ||
| 762 | (setcdr entry (cdr elt)) | ||
| 763 | (setq tty-defined-color-alist (cons elt tty-defined-color-alist))) | ||
| 764 | tty-defined-color-alist)) | ||
| 746 | 765 | ||
| 747 | (defun tty-color-canonicalize (color) | 766 | (defun tty-color-canonicalize (color) |
| 748 | "Return COLOR in canonical form. | 767 | "Return COLOR in canonical form. |
| @@ -752,7 +771,7 @@ A canonicalized color name is all-lower case, with any blanks removed." | |||
| 752 | (setq color (replace-match "" nil nil color))) | 771 | (setq color (replace-match "" nil nil color))) |
| 753 | color)) | 772 | color)) |
| 754 | 773 | ||
| 755 | (defun tty-color-define (name index &optional rgb) | 774 | (defun tty-color-define (name index &optional rgb frame) |
| 756 | "Specify a tty color by its NAME, terminal INDEX and RGB values. | 775 | "Specify a tty color by its NAME, terminal INDEX and RGB values. |
| 757 | NAME is a string, INDEX is typically a small integer used to send to | 776 | NAME is a string, INDEX is typically a small integer used to send to |
| 758 | the terminal driver to switch on this color, and RGB is a list of 3 | 777 | the terminal driver to switch on this color, and RGB is a list of 3 |
| @@ -760,34 +779,32 @@ numbers that specify the intensity of red, green, and blue components | |||
| 760 | of the color. | 779 | of the color. |
| 761 | If specified, each one of the RGB components must be a number between | 780 | If specified, each one of the RGB components must be a number between |
| 762 | 0 and 65535. If RGB is omitted, the specified color will never be used | 781 | 0 and 65535. If RGB is omitted, the specified color will never be used |
| 763 | by `tty-color-translate' as an approximation to another color." | 782 | by `tty-color-translate' as an approximation to another color. |
| 783 | If FRAME is not specified or is nil, it defaults to the selected frame." | ||
| 764 | (if (or (not (stringp name)) | 784 | (if (or (not (stringp name)) |
| 765 | (not (integerp index)) | 785 | (not (integerp index)) |
| 766 | (and rgb (or (not (listp rgb)) (/= (length rgb) 3)))) | 786 | (and rgb (or (not (listp rgb)) (/= (length rgb) 3)))) |
| 767 | (error "Invalid specification for tty color \"%s\"" name)) | 787 | (error "Invalid specification for tty color \"%s\"" name)) |
| 768 | (let* ((name (tty-color-canonicalize name)) | 788 | (tty-modify-color-alist |
| 769 | (entry (assoc name tty-color-alist))) | 789 | (append (list (tty-color-canonicalize name) index) rgb) frame)) |
| 770 | (if entry | ||
| 771 | (setcdr entry (cons index rgb)) | ||
| 772 | (setq tty-color-alist | ||
| 773 | (cons (append (list name index) rgb) tty-color-alist))) | ||
| 774 | tty-color-alist)) | ||
| 775 | 790 | ||
| 776 | (defun tty-color-clear () | 791 | (defun tty-color-clear (&optional frame) |
| 777 | "Clear the list of supported tty colors." | 792 | "Clear the list of supported tty colors for frame FRAME. |
| 778 | (setq tty-color-alist nil)) | 793 | If FRAME is unspecified or nil, it defaults to the selected frame." |
| 794 | (setq tty-defined-color-alist nil)) | ||
| 779 | 795 | ||
| 780 | (defun tty-color-off-gray-diag (r g b) | 796 | (defun tty-color-off-gray-diag (r g b) |
| 781 | "Compute the angle between the color given by R,G,B and the gray diagonal." | 797 | "Compute the angle between the color given by R,G,B and the gray diagonal." |
| 782 | (let ((mag (sqrt (* 3 (+ (* r r) (* g g) (* b b)))))) | 798 | (let ((mag (sqrt (* 3 (+ (* r r) (* g g) (* b b)))))) |
| 783 | (if (< mag 1) 0 (acos (/ (+ r g b) mag))))) | 799 | (if (< mag 1) 0 (acos (/ (+ r g b) mag))))) |
| 784 | 800 | ||
| 785 | (defun tty-color-approximate (rgb) | 801 | (defun tty-color-approximate (rgb &optional frame) |
| 786 | "Given a list of 3 rgb values in RGB, find the color in `tty-color-alist' | 802 | "Given a list of 3 rgb values in RGB, find the color in `tty-color-alist' |
| 787 | which is the best approximation in the 3-dimensional RGB space, | 803 | which is the best approximation in the 3-dimensional RGB space, |
| 788 | and return the index associated with the approximating color. | 804 | and return the index associated with the approximating color. |
| 789 | Each value of the RGB triplet has to be scaled to the 0..255 range." | 805 | Each value of the RGB triplet has to be scaled to the 0..255 range. |
| 790 | (let* ((color-list tty-color-alist) | 806 | FRAME defaults to the selected frame." |
| 807 | (let* ((color-list (tty-color-alist frame)) | ||
| 791 | (candidate (car color-list)) | 808 | (candidate (car color-list)) |
| 792 | (best-distance 195076) ;; 3 * 255^2 + 15 | 809 | (best-distance 195076) ;; 3 * 255^2 + 15 |
| 793 | best-color) | 810 | best-color) |
| @@ -824,7 +841,7 @@ Each value of the RGB triplet has to be scaled to the 0..255 range." | |||
| 824 | (setq candidate (car color-list))) | 841 | (setq candidate (car color-list))) |
| 825 | (cadr best-color))) | 842 | (cadr best-color))) |
| 826 | 843 | ||
| 827 | (defun tty-color-translate (color) | 844 | (defun tty-color-translate (color &optional frame) |
| 828 | "Given a color COLOR, return the index of the corresponding TTY color. | 845 | "Given a color COLOR, return the index of the corresponding TTY color. |
| 829 | COLOR must be a string that is either the color's name, or its X-style | 846 | COLOR must be a string that is either the color's name, or its X-style |
| 830 | specification like \"#RRGGBB\" or \"RGB:rr/gg/bb\", where each primary. | 847 | specification like \"#RRGGBB\" or \"RGB:rr/gg/bb\", where each primary. |
| @@ -836,10 +853,11 @@ looking up the name in `color-name-rgb-alist', are used to find the | |||
| 836 | supported color that is the best approximation for COLOR in the RGB | 853 | supported color that is the best approximation for COLOR in the RGB |
| 837 | space. | 854 | space. |
| 838 | If COLOR is neither a valid X RGB specification of the color, nor a | 855 | If COLOR is neither a valid X RGB specification of the color, nor a |
| 839 | name of a color in `color-name-rgb-alist', the returned value is nil." | 856 | name of a color in `color-name-rgb-alist', the returned value is nil. |
| 857 | If FRAME is unspecified or nil, it defaults to the selected frame." | ||
| 840 | (and (stringp color) | 858 | (and (stringp color) |
| 841 | (let* ((color (tty-color-canonicalize color)) | 859 | (let* ((color (tty-color-canonicalize color)) |
| 842 | (idx (cadr (assoc color tty-color-alist)))) | 860 | (idx (cadr (assoc color (tty-color-alist frame))))) |
| 843 | (or idx | 861 | (or idx |
| 844 | (let* ((len (length color)) | 862 | (let* ((len (length color)) |
| 845 | (maxval 256) | 863 | (maxval 256) |
| @@ -895,13 +913,14 @@ name of a color in `color-name-rgb-alist', the returned value is nil." | |||
| 895 | maxval)))) | 913 | maxval)))) |
| 896 | (t | 914 | (t |
| 897 | (cdr (assoc color color-name-rgb-alist)))))) | 915 | (cdr (assoc color color-name-rgb-alist)))))) |
| 898 | (and rgb (tty-color-approximate rgb))))))) | 916 | (and rgb (tty-color-approximate rgb frame))))))) |
| 899 | 917 | ||
| 900 | (defun tty-color-by-index (idx) | 918 | (defun tty-color-by-index (idx &optional frame) |
| 901 | "Given a numeric index of a tty color, return its description. | 919 | "Given a numeric index of a tty color, return its description. |
| 920 | FRAME, if unspecified or nil, defaults to the selected frame. | ||
| 902 | Value is a list of the form \(NAME INDEX R G B\)." | 921 | Value is a list of the form \(NAME INDEX R G B\)." |
| 903 | (and idx | 922 | (and idx |
| 904 | (let ((colors tty-color-alist) | 923 | (let ((colors (tty-color-alist frame)) |
| 905 | desc found) | 924 | desc found) |
| 906 | (while colors | 925 | (while colors |
| 907 | (setq desc (car colors)) | 926 | (setq desc (car colors)) |
| @@ -919,20 +938,21 @@ These values range from 0 to 65535; white is (65535 65535 65535). | |||
| 919 | If FRAME is omitted or nil, use the selected frame." | 938 | If FRAME is omitted or nil, use the selected frame." |
| 920 | (let* ((frame (or frame (selected-frame))) | 939 | (let* ((frame (or frame (selected-frame))) |
| 921 | (color (tty-color-canonicalize color)) | 940 | (color (tty-color-canonicalize color)) |
| 922 | (supported (assoc color tty-color-alist))) | 941 | (supported (assoc color (tty-color-alist frame)))) |
| 923 | (or (and supported (cddr supported)) ; full spec in tty-color-alist | 942 | (or (and supported (cddr supported)) ; full spec in tty-color-alist |
| 924 | (and supported ; no RGB values in tty-color-alist: use X RGB values | 943 | (and supported ; no RGB values in tty-color-alist: use X RGB values |
| 925 | (assoc color color-name-rgb-alist) | 944 | (assoc color color-name-rgb-alist) |
| 926 | (cddr | 945 | (cddr |
| 927 | (tty-color-by-index | 946 | (tty-color-by-index |
| 928 | (tty-color-approximate | 947 | (tty-color-approximate |
| 929 | (cdr (assoc color color-name-rgb-alist)))))) | 948 | (cdr (assoc color color-name-rgb-alist)) frame) frame))) |
| 930 | (cddr (tty-color-by-index (tty-color-translate color)))))) | 949 | (cddr (tty-color-by-index (tty-color-translate color frame) frame))))) |
| 931 | 950 | ||
| 932 | (defun tty-color-desc (color) | 951 | (defun tty-color-desc (color &optional frame) |
| 933 | "Return the description of the color COLOR for a character terminal. | 952 | "Return the description of the color COLOR for a character terminal. |
| 953 | FRAME, if unspecified or nil, defaults to the selected frame. | ||
| 934 | Value is a list of the form \(NAME INDEX R G B\). Note that the returned | 954 | Value is a list of the form \(NAME INDEX R G B\). Note that the returned |
| 935 | NAME is not necessarily the same string as the argument COLOR, because | 955 | NAME is not necessarily the same string as the argument COLOR, because |
| 936 | the latter might need to be approximated if it is not supported directly." | 956 | the latter might need to be approximated if it is not supported directly." |
| 937 | (let ((idx (tty-color-translate color))) | 957 | (let ((idx (tty-color-translate color frame))) |
| 938 | (tty-color-by-index idx))) | 958 | (tty-color-by-index idx frame))) |