From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#41544: 26.3; Possible incorrect results from color-distance Date: Sat, 06 Jun 2020 21:27:42 +0300 Message-ID: <83bllw82xt.fsf@gnu.org> References: <5C4A633D-8222-4439-BE37-9B8674F1DA6D@acm.org> <87r1v2aat3.fsf@tromey.com> <9902865C-01B4-4E50-A433-DBC8B8311234@acm.org> <83tuzueogo.fsf@gnu.org> <6272275C-560C-4437-90F1-2A8294D27019@acm.org> <83o8q2elja.fsf@gnu.org> <83mu5mel4o.fsf@gnu.org> <77F1DDD3-A69F-40ED-902D-74986D5E6596@acm.org> <83y2p5cumz.fsf@gnu.org> <83blm0cjlz.fsf@gnu.org> <83367ccf8w.fsf@gnu.org> <624D7FB8-A836-4A7E-8895-47E867214504@acm.org> <83o8pyc4bq.fsf@gnu.org> <55D73CA5-1EFB-4B0A-8F8B-FDA1D39F51BF@acm.org> <835zc5bsut.fsf@gnu.org> <3BBCFDD4-C14D-4628-91CB-2A0456A96FC7@acm.org> <838sh0abzz.fsf@gnu.org> <83r1us8kw6.fsf@gnu.org> <020DE875-14A8-457A-9AE4-AA0925DB8997@acm.org> <83img48ffx.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="42544"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 41544@debbugs.gnu.org To: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Jun 06 20:29:08 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jhdZ6-000Awo-0M for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 06 Jun 2020 20:29:08 +0200 Original-Received: from localhost ([::1]:48682 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jhdZ4-0008D5-Vb for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 06 Jun 2020 14:29:06 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59852) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jhdZ0-0008Cx-BJ for bug-gnu-emacs@gnu.org; Sat, 06 Jun 2020 14:29:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:40907) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jhdZ0-0002xd-2C for bug-gnu-emacs@gnu.org; Sat, 06 Jun 2020 14:29:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jhdYz-0007Ft-Vh for bug-gnu-emacs@gnu.org; Sat, 06 Jun 2020 14:29:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 06 Jun 2020 18:29:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 41544 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 41544-submit@debbugs.gnu.org id=B41544.159146808327820 (code B ref 41544); Sat, 06 Jun 2020 18:29:01 +0000 Original-Received: (at 41544) by debbugs.gnu.org; 6 Jun 2020 18:28:03 +0000 Original-Received: from localhost ([127.0.0.1]:52451 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jhdY3-0007EV-7F for submit@debbugs.gnu.org; Sat, 06 Jun 2020 14:28:03 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:51438) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jhdY1-0007Dp-4t for 41544@debbugs.gnu.org; Sat, 06 Jun 2020 14:28:01 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:36188) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jhdXv-0002o3-TR; Sat, 06 Jun 2020 14:27:55 -0400 Original-Received: from [176.228.60.248] (port=1972 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1jhdXr-0001xZ-W0; Sat, 06 Jun 2020 14:27:53 -0400 In-Reply-To: (message from Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= on Sat, 6 Jun 2020 18:54:41 +0200) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:181627 Archived-At: > From: Mattias EngdegÄrd > Date: Sat, 6 Jun 2020 18:54:41 +0200 > Cc: 41544@debbugs.gnu.org > > Since we are making little progress, let's leave color-name-to-rgb unchanged for the moment. We can both change our minds later. It's not strictly required for the introduction and use of color-dark-p; patch updated. What practical problem is being solved here? (Please don't say "this was done in different ways", that's not a practical problem from my POV.) > * lisp/facemenu.el (list-colors-print): Use readable-foreground-color. I don't mind installing this part, but have the rationale should be spelled out in the log message. > --- a/lisp/faces.el > +++ b/lisp/faces.el > @@ -1786,15 +1786,24 @@ defined-colors-with-face-attributes > > (defun readable-foreground-color (color) > "Return a readable foreground color for background COLOR." > - (let* ((rgb (color-values color)) > - (max (apply #'max rgb)) > - (black (car (color-values "black"))) > - (white (car (color-values "white")))) > - ;; Select black or white depending on which one is less similar to > - ;; the brightest component. > - (if (> (abs (- max black)) (abs (- max white))) > - "black" > - "white"))) What was wrong with the original code? If it produced sub-optimal results, please show an example of that. > +(defun color-dark-p (rgb) > + "Whether RGB is more readable against white than black. > +RGB is a 3-element list (R G B), each component in the range [0,1]." > + (let* ((sr (nth 0 rgb)) > + (sg (nth 1 rgb)) > + (sb (nth 2 rgb)) > + ;; Use the power 2.2 as an approximation to sRGB gamma; > + ;; it should be good enough for the purpose of this function. > + (r (expt sr 2.2)) > + (g (expt sg 2.2)) > + (b (expt sb 2.2))) > + (unless (<= 0 (min r g b) (max r g b) 1) > + (error "RGB components %S not in [0,1]" rgb)) > + ;; The cut-off value was determined experimentally; see bug#41544. > + (< (+ (* r 0.299) (* g 0.587) (* b 0.114)) > + (eval-when-compile (expt 0.6 2.2))))) This code's algorithm and rationale should be explained in the comments before we can discuss whether it's an improvement and why. > diff --git a/lisp/frame.el b/lisp/frame.el > index 6c2f774709..253528da75 100644 > --- a/lisp/frame.el > +++ b/lisp/frame.el > @@ -1156,6 +1156,13 @@ frame-background-mode This is a non-starter, sorry. I'm not interested in changing what is considered dark and light background of a frame. > diff --git a/lisp/term/rxvt.el b/lisp/term/rxvt.el > index 31e3d6ede4..5dc754c8e0 100644 > --- a/lisp/term/rxvt.el > +++ b/lisp/term/rxvt.el > @@ -206,13 +206,11 @@ rxvt-set-background-mode Likewise here. > diff --git a/lisp/term/w32console.el b/lisp/term/w32console.el > index 36e9d896c7..0e9d7c8b5c 100644 > --- a/lisp/term/w32console.el > +++ b/lisp/term/w32console.el > @@ -86,9 +86,9 @@ terminal-init-w32console > (setq r (nth 2 descr) > g (nth 3 descr) > b (nth 4 descr)) > - (if (< (+ r g b) (* .6 (+ 65535 65535 65535))) > - (setq bg-mode 'dark) > - (setq bg-mode 'light)) > + (setq bg-mode (if (color-dark-p > + (list (/ r 65535.0) (/ g 65535.0) (/ b 65535.0))) > + 'dark 'light)) > (set-terminal-parameter nil 'background-mode bg-mode)) And here. > diff --git a/lisp/term/xterm.el b/lisp/term/xterm.el > index 1a727e3933..bf9bcae526 100644 > --- a/lisp/term/xterm.el > +++ b/lisp/term/xterm.el > @@ -1120,9 +1120,8 @@ xterm-register-default-colors > (clear-face-cache))) > > (defun xterm-maybe-set-dark-background-mode (redc greenc bluec) > - ;; Use the heuristic in `frame-set-background-mode' to decide if a > - ;; frame is dark. > - (when (< (+ redc greenc bluec) (* .6 (+ 65535 65535 65535))) > + (when (color-dark-p (mapcar (lambda (c) (/ c 65535.0)) > + (list redc greenc bluec))) > (set-terminal-parameter nil 'background-mode 'dark) And here. > -(defun css--contrasty-color (name) > - "Return a color that contrasts with NAME. > -NAME is of any form accepted by `color-distance'. > -The returned color will be usable by Emacs and will contrast > -with NAME; in particular so that if NAME is used as a background > -color, the returned color can be used as the foreground and still > -be readable." > - ;; See bug#25525 for a discussion of this. > - (if (> (color-distance name "black") 292485) > - "black" "white")) > - > (defcustom css-fontify-colors t > "Whether CSS colors should be fontified using the color as the background. > When non-`nil', a text representing CSS color will be fontified > @@ -1199,7 +1188,8 @@ css--fontify-region > (add-text-properties > start (point) > (list 'face (list :background color > - :foreground (css--contrasty-color color) > + :foreground (readable-foreground-color > + color) > :box '(:line-width -1)))))))))))) > extended-region)) Here, once again I will ask what practical problem is being fixed. Thanks.