aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorEli Zaretskii2017-03-03 16:05:02 +0200
committerEli Zaretskii2017-03-03 16:05:02 +0200
commit7b00e956b485d8ade03c870cbdd0ae086348737b (patch)
tree52c360b2ff1ea3bd394ecbed8a208fff9edc2069 /lisp
parent244de7b0ed3bb23e700c9edef51e413602d8720a (diff)
downloademacs-7b00e956b485d8ade03c870cbdd0ae086348737b.tar.gz
emacs-7b00e956b485d8ade03c870cbdd0ae086348737b.zip
Fix color component calculations in color.el
* lisp/color.el (color-name-to-rgb): Use 16 bits per color component. (color-rgb-to-hex): Accept an optional argument DIGITS-PER-COMPONENT, defaulting to 4, and format the hexadecimal notation either for 8 or 16 bits per component. (Bug#25890) * lisp/net/shr-color.el (shr-color->hexadecimal): Call color-rgb-to-hex with the optional argument of 2, to match color processing on the Web.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/color.el16
-rw-r--r--lisp/net/shr-color.el2
2 files changed, 11 insertions, 7 deletions
diff --git a/lisp/color.el b/lisp/color.el
index 32c8127e316..6dbf3d55cbc 100644
--- a/lisp/color.el
+++ b/lisp/color.el
@@ -52,14 +52,18 @@ displayed. If FRAME is omitted or nil, use the selected frame.
52If FRAME cannot display COLOR, return nil." 52If FRAME cannot display COLOR, return nil."
53 ;; `colors-values' maximum value is either 65535 or 65280 depending on the 53 ;; `colors-values' maximum value is either 65535 or 65280 depending on the
54 ;; display system. So we use a white conversion to get the max value. 54 ;; display system. So we use a white conversion to get the max value.
55 (let ((valmax (float (car (color-values "#ffffff"))))) 55 (let ((valmax (float (car (color-values "#ffffffffffff")))))
56 (mapcar (lambda (x) (/ x valmax)) (color-values color frame)))) 56 (mapcar (lambda (x) (/ x valmax)) (color-values color frame))))
57 57
58(defun color-rgb-to-hex (red green blue) 58(defun color-rgb-to-hex (red green blue &optional digits-per-component)
59 "Return hexadecimal notation for the color RED GREEN BLUE. 59 "Return hexadecimal #RGB notation for the color specified by RED GREEN BLUE.
60RED, GREEN, and BLUE should be numbers between 0.0 and 1.0, inclusive." 60RED, GREEN, and BLUE should be numbers between 0.0 and 1.0, inclusive.
61 (format "#%02x%02x%02x" 61Optional argument DIGITS-PER-COMPONENT can be either 4 (the default)
62 (* red 255) (* green 255) (* blue 255))) 62or 2; use the latter if you need a 24-bit specification of a color."
63 (or digits-per-component (setq digits-per-component 4))
64 (let* ((maxval (if (= digits-per-component 2) 255 65535))
65 (fmt (if (= digits-per-component 2) "#%02x%02x%02x" "#%04x%04x%04x")))
66 (format fmt (* red maxval) (* green maxval) (* blue maxval))))
63 67
64(defun color-complement (color-name) 68(defun color-complement (color-name)
65 "Return the color that is the complement of COLOR-NAME. 69 "Return the color that is the complement of COLOR-NAME.
diff --git a/lisp/net/shr-color.el b/lisp/net/shr-color.el
index cb081cbbb10..b0c706eb5da 100644
--- a/lisp/net/shr-color.el
+++ b/lisp/net/shr-color.el
@@ -260,7 +260,7 @@ Like rgb() or hsl()."
260 (l (/ (string-to-number (match-string-no-properties 3 color)) 100.0))) 260 (l (/ (string-to-number (match-string-no-properties 3 color)) 100.0)))
261 (destructuring-bind (r g b) 261 (destructuring-bind (r g b)
262 (shr-color-hsl-to-rgb-fractions h s l) 262 (shr-color-hsl-to-rgb-fractions h s l)
263 (color-rgb-to-hex r g b)))) 263 (color-rgb-to-hex r g b 2))))
264 ;; Color names 264 ;; Color names
265 ((cdr (assoc-string color shr-color-html-colors-alist t))) 265 ((cdr (assoc-string color shr-color-html-colors-alist t)))
266 ;; Unrecognized color :( 266 ;; Unrecognized color :(