diff options
| author | Mattias EngdegÄrd | 2020-06-12 18:12:37 +0200 |
|---|---|---|
| committer | Mattias EngdegÄrd | 2020-06-21 21:22:26 +0200 |
| commit | 9fe2bdb88a4ebd4b2286c1c2a2a2ba7411af01b6 (patch) | |
| tree | 0979ec4f38172e25a0420eca5b22650c249a80f4 /lisp | |
| parent | 0792f8e4f0de2328c57d552a5845bdf77265a971 (diff) | |
| download | emacs-9fe2bdb88a4ebd4b2286c1c2a2a2ba7411af01b6.tar.gz emacs-9fe2bdb88a4ebd4b2286c1c2a2a2ba7411af01b6.zip | |
Consolidate #RGB string parsers
Use a single parser of color strings in the #RGB, rgb:R/G/B and
rgbi:R/G/B formats, replacing four existing ones. Previously,
error-checking was spotty, handling of the rgbi: format not always
present, and normalization of the result was sometimes incorrect.
* src/dispextern.h: New prototype.
* src/xfaces.c (parse_hex_color_comp, parse_float_color_comp)
(parse_color_spec, Finternal-color_values_from_color_spec): New functions.
* test/src/xfaces-tests.el (xfaces-internal-color-values-from-color-spec):
New test.
* lisp/term/tty-colors.el (tty-color-standard-values):
Use internal-color-values-from-color-spec, replacing old parser.
* src/nsterm.m (ns_get_color):
* src/w32fns.c (x_to_w32_color):
* src/xterm.c (x_parse_color): Use parse_color_spec, replacing old
parsers.
(HEX_COLOR_NAME_LENGTH): Remove #define.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/term/tty-colors.el | 58 |
1 files changed, 2 insertions, 56 deletions
diff --git a/lisp/term/tty-colors.el b/lisp/term/tty-colors.el index 39ca2d36276..73e2431822e 100644 --- a/lisp/term/tty-colors.el +++ b/lisp/term/tty-colors.el | |||
| @@ -923,62 +923,8 @@ The returned value reflects the standard Emacs definition of | |||
| 923 | COLOR (see the info node `(emacs) Colors'), regardless of whether | 923 | COLOR (see the info node `(emacs) Colors'), regardless of whether |
| 924 | the terminal can display it, so the return value should be the | 924 | the terminal can display it, so the return value should be the |
| 925 | same regardless of what display is being used." | 925 | same regardless of what display is being used." |
| 926 | (let ((len (length color))) | 926 | (or (internal-color-values-from-color-spec color) |
| 927 | (cond ((and (>= len 4) ;; HTML/CSS/SVG-style "#XXYYZZ" color spec | 927 | (cdr (assoc color color-name-rgb-alist)))) |
| 928 | (eq (aref color 0) ?#) | ||
| 929 | (member (aref color 1) | ||
| 930 | '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 | ||
| 931 | ?a ?b ?c ?d ?e ?f | ||
| 932 | ?A ?B ?C ?D ?E ?F))) | ||
| 933 | ;; Translate the string "#XXYYZZ" into a list of numbers | ||
| 934 | ;; (XX YY ZZ), scaling each to the {0..65535} range. This | ||
| 935 | ;; follows the HTML color convention, where both "#fff" and | ||
| 936 | ;; "#ffffff" represent the same color, white. | ||
| 937 | (let* ((ndig (/ (- len 1) 3)) | ||
| 938 | (maxval (1- (ash 1 (* 4 ndig)))) | ||
| 939 | (i1 1) | ||
| 940 | (i2 (+ i1 ndig)) | ||
| 941 | (i3 (+ i2 ndig)) | ||
| 942 | (i4 (+ i3 ndig))) | ||
| 943 | (list | ||
| 944 | (/ (* (string-to-number | ||
| 945 | (substring color i1 i2) 16) | ||
| 946 | 65535) | ||
| 947 | maxval) | ||
| 948 | (/ (* (string-to-number | ||
| 949 | (substring color i2 i3) 16) | ||
| 950 | 65535) | ||
| 951 | maxval) | ||
| 952 | (/ (* (string-to-number | ||
| 953 | (substring color i3 i4) 16) | ||
| 954 | 65535) | ||
| 955 | maxval)))) | ||
| 956 | ((and (>= len 9) ;; X-style rgb:xx/yy/zz color spec | ||
| 957 | (string= (substring color 0 4) "rgb:")) | ||
| 958 | ;; Translate the string "rgb:XX/YY/ZZ" into a list of | ||
| 959 | ;; numbers (XX YY ZZ), scaling each to the {0..65535} | ||
| 960 | ;; range. "rgb:F/F/F" is white. | ||
| 961 | (let* ((ndig (/ (- len 3) 3)) | ||
| 962 | (maxval (1- (ash 1 (* 4 (- ndig 1))))) | ||
| 963 | (i1 4) | ||
| 964 | (i2 (+ i1 ndig)) | ||
| 965 | (i3 (+ i2 ndig)) | ||
| 966 | (i4 (+ i3 ndig))) | ||
| 967 | (list | ||
| 968 | (/ (* (string-to-number | ||
| 969 | (substring color i1 (- i2 1)) 16) | ||
| 970 | 65535) | ||
| 971 | maxval) | ||
| 972 | (/ (* (string-to-number | ||
| 973 | (substring color i2 (- i3 1)) 16) | ||
| 974 | 65535) | ||
| 975 | maxval) | ||
| 976 | (/ (* (string-to-number | ||
| 977 | (substring color i3 (1- i4)) 16) | ||
| 978 | 65535) | ||
| 979 | maxval)))) | ||
| 980 | (t | ||
| 981 | (cdr (assoc color color-name-rgb-alist)))))) | ||
| 982 | 928 | ||
| 983 | (defun tty-color-translate (color &optional frame) | 929 | (defun tty-color-translate (color &optional frame) |
| 984 | "Given a color COLOR, return the index of the corresponding TTY color. | 930 | "Given a color COLOR, return the index of the corresponding TTY color. |