Subject: bug#36304: 27.0.50; request: switch to the superior HTML #RGB convention for colors

From: Pip Cet <[email-masked]>
Date: Sat, 22 Jun 2019 15:33:14 +0000

On Sat, Jun 22, 2019 at 3:05 PM Eli Zaretskii <[email-masked]> wrote:
> > From: Pip Cet <[email-masked]>
> > Date: Sat, 22 Jun 2019 14:41:30 +0000
> > Cc: [email-masked], [email-masked]
> > > > and it's only used for text terminals, right?
> > >
> > > Yes, but we nowadays support text terminals that can display 24-bit
> > > colors, and having their colors display differently from the same
> > > color on X is just asking for bug reports.
> > Okay, let's change it, then.
> Thanks.

While I was there, I've also made it accept upper-case hex digits
(previously, #0F0 was accepted but #F00 wasn't), and fixed the ranges
so rgb:f/f/f translates to (65535 65535 65535) rather than (255 255
255).
From 6973cd4bc54aee7352b99afc774ee82af3c9482f Mon Sep 17 00:00:00 2001
From: Pip Cet <[email-masked]>
Date: Sat, 22 Jun 2019 15:31:00 +0000
Subject: [PATCH] Use CSS convention for interpreting colors.

* lisp/term/tty-colors.el (tty-color-standard-values): Change
  interpretation of #RGB strings to match CSS rather than X
  conventions. Allow upper-case digits. Fix rgb:R/G/B interpretation.
---
 lisp/term/tty-colors.el | 50 ++++++++++++++++++++++-------------------
 1 file changed, 27 insertions(+), 23 deletions(-)

diff --git a/lisp/term/tty-colors.el b/lisp/term/tty-colors.el
index 307586f221..07bf47c042 100644
--- a/lisp/term/tty-colors.el
+++ b/lisp/term/tty-colors.el
@@ -919,40 +919,44 @@ tty-color-standard-values
 The result is a list of integer RGB values--(RED GREEN BLUE).
 These values range from 0 to 65535; white is (65535 65535 65535).
 
-The returned value reflects the standard X definition of COLOR,
+The returned value reflects the standard Emacs definition of COLOR,
 regardless of whether the terminal can display it, so the return value
 should be the same regardless of what display is being used."
   (let ((len (length color)))
-    (cond ((and (>= len 4) ;; X-style "#XXYYZZ" color spec
+    (cond ((and (>= len 4) ;; "#XXYYZZ" color spec
 		(eq (aref color 0) ?#)
 		(member (aref color 1)
 			'(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
-			     ?a ?b ?c ?d ?e ?f)))
+			     ?a ?b ?c ?d ?e ?f
+                             ?A ?B ?C ?D ?E ?F)))
 	   ;; Translate the string "#XXYYZZ" into a list
-	   ;; of numbers (XX YY ZZ).  If the primary colors
-	   ;; are specified with less than 4 hex digits,
-	   ;; the used digits represent the most significant
-	   ;; bits of the value (e.g. #XYZ = #X000Y000Z000).
+	   ;; of numbers (XX YY ZZ).  If fewer than 4 hex
+	   ;; digits are used, they represent the fraction
+	   ;; of the maximum value (#XYZ = #XXXXYYYYZZZZ).
 	   (let* ((ndig (/ (- len 1) 3))
+		  (maxval (1- (ash 1 (* 4 ndig))))
 		  (i1 1)
 		  (i2 (+ i1 ndig))
 		  (i3 (+ i2 ndig)))
 	     (list
-	      (ash
-	       (string-to-number (substring color i1 i2) 16)
-	       (* 4 (- 4 ndig)))
-	      (ash
-	       (string-to-number (substring color i2 i3) 16)
-	       (* 4 (- 4 ndig)))
-	      (ash
-	       (string-to-number (substring color i3) 16)
-	       (* 4 (- 4 ndig))))))
-	  ((and (>= len 9) ;; X-style RGB:xx/yy/zz color spec
+	      (/ (* (string-to-number
+		     (substring color i1 i2) 16)
+		    65535)
+		 maxval)
+	      (/ (* (string-to-number
+		     (substring color i2 i3) 16)
+		    65535)
+		 maxval)
+	      (/ (* (string-to-number
+		     (substring color i3) 16)
+		    65535)
+		 maxval))))
+	  ((and (>= len 9) ;; X-style rgb:xx/yy/zz color spec
 		(string= (substring color 0 4) "rgb:"))
-	   ;; Translate the string "RGB:XX/YY/ZZ" into a list
+	   ;; Translate the string "rgb:XX/YY/ZZ" into a list
 	   ;; of numbers (XX YY ZZ).  If fewer than 4 hex
 	   ;; digits are used, they represent the fraction
-	   ;; of the maximum value (RGB:X/Y/Z = #XXXXYYYYZZZZ).
+	   ;; of the maximum value (rgb:X/Y/Z = #XXXXYYYYZZZZ).
 	   (let* ((ndig (/ (- len 3) 3))
 		  (maxval (1- (ash 1 (* 4 (- ndig 1)))))
 		  (i1 4)
@@ -961,15 +965,15 @@ tty-color-standard-values
 	     (list
 	      (/ (* (string-to-number
 		     (substring color i1 (- i2 1)) 16)
-		    255)
+		    65535)
 		 maxval)
 	      (/ (* (string-to-number
 		     (substring color i2 (- i3 1)) 16)
-		    255)
+		    65535)
 		 maxval)
 	      (/ (* (string-to-number
 		     (substring color i3) 16)
-		    255)
+		    65535)
 		 maxval))))
 	  (t
 	   (cdr (assoc color color-name-rgb-alist))))))
@@ -978,7 +982,7 @@ tty-color-translate
   "Given a color COLOR, return the index of the corresponding TTY color.
 
 COLOR must be a string that is either the color's name, or its X-style
-specification like \"#RRGGBB\" or \"RGB:rr/gg/bb\", where each primary.
+specification like \"#RRGGBB\" or \"rgb:RR/GG/BB\", where each primary.
 color can be given with 1 to 4 hex digits.
 
 If COLOR is a color name that is found among supported colors in
-- 
2.20.1