bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#25890: `color-values` gives wrong value


From: Eli Zaretskii
Subject: bug#25890: `color-values` gives wrong value
Date: Tue, 28 Feb 2017 17:50:30 +0200

> From: Rasmus <address@hidden>
> Cc: address@hidden, address@hidden
> Date: Tue, 28 Feb 2017 10:44:22 +0100
> 
> Michael Heerdegen <address@hidden> writes:
> 
> > Eli Zaretskii <address@hidden> writes:
> >
> >> >   (color-values "#ffffff") => (65280 65280 65280) ; The max values.
> >>
> >> Are you sure?
> >
> > FWIW, I see the same here; and we already have a bug report like this
> > one:
> >
> >   https://debbugs.gnu.org/cgi/bugreport.cgi?bug=24273
> 
> Oh thanks.  (I didn't search as gmane search is still down).
> 
> I guess this bug can be closed.

I'd rather we fixed it.

I think there's a bug in color.el: it assumes that the #RGB notation
uses 24 bits, i.e. each component maxes out at 255.  But Emacs does
its color calculations based on 16-bit components, which max out at
65535, which is why #ffffff does NOT produce (65535 65535 65535), the
white color.  (We decided long ago to use 16-bit components because X
APIs such as XParseColor on some systems, including yours, work with
such components.)  IOW, #ffffff is parsed as #ff00ff00ff00, and that
is not "white".

So I think the patch below is what is needed to fix this problem.  Can
you try it?  (There's one user of the functions the patch changes,
shr-color.el, which will need to be adapted to the change, if we
decide to install it.)


--- lisp/color.el~0     2017-01-02 06:25:09.000000000 +0200
+++ lisp/color.el       2017-02-28 17:35:43.570586100 +0200
@@ -52,14 +52,14 @@
 If FRAME cannot display COLOR, return nil."
   ;; `colors-values' maximum value is either 65535 or 65280 depending on the
   ;; display system.  So we use a white conversion to get the max value.
-  (let ((valmax (float (car (color-values "#ffffff")))))
+  (let ((valmax (float (car (color-values "#ffffffffffff")))))
     (mapcar (lambda (x) (/ x valmax)) (color-values color frame))))
 
 (defun color-rgb-to-hex  (red green blue)
   "Return hexadecimal notation for the color RED GREEN BLUE.
 RED, GREEN, and BLUE should be numbers between 0.0 and 1.0, inclusive."
-  (format "#%02x%02x%02x"
-          (* red 255) (* green 255) (* blue 255)))
+  (format "#%04x%04x%04x"
+          (* red 65535) (* green 65535) (* blue 65535)))
 
 (defun color-complement (color-name)
   "Return the color that is the complement of COLOR-NAME.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]