unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Jan Moringen <jan.moringen@uni-bielefeld.de>
To: bug-gnu-emacs@gnu.org
Subject: bug#5305: [patch] add defined-colors-with-face-attributes to faces.el
Date: Sun, 03 Jan 2010 22:02:31 +0100	[thread overview]
Message-ID: <27784_1262552552_ZZg0O2405RUGf.00_1262552551.3761.1645.camel@localhost.localdomain> (raw)

[-- Attachment #1: Type: text/plain, Size: 797 bytes --]

Hi,

in an application I wrote, the user is prompted for a color using
`read-color'. The completion table used there presents color names.
However, based on color names alone, I often found it hard to choose a
color. Therefore I wrote `defined-colors-with-face-attributes' which
is
like `defined-colors' but adds text properties to the color names that
make them render with the color they name as background. I also
changed
`read-color' to call the new function when a color display is
available.

I have tested the new behavior on X displays and TTYs and it seems to
work. However, I could not test windows or other terminal types and/or
other color depths.

If this change is considered useful, I would like to contribute it. I
have done the copyright assignment for Emacs.

Kind regards,
Jan


[-- Attachment #2: read-color-99203-merge-directive.txt --]
[-- Type: text/plain, Size: 5242 bytes --]

# Bazaar merge directive format 2 (Bazaar 0.90)
# revision_id: jmoringe@techfak.uni-bielefeld.de-20100101171626-\
#   xqgoilbu7snk983t
# target_branch: ../../trunk/
# testament_sha1: 03e05b71c38db0d0fe6939f26f5602047497d84b
# timestamp: 2010-01-01 19:08:52 +0100
# base_revision_id: handa@m17n.org-20091228074829-kp7iv7vpr3xvibbn
# 
# Begin patch
=== modified file 'lisp/ChangeLog'
--- lisp/ChangeLog	2009-12-28 07:29:24 +0000
+++ lisp/ChangeLog	2010-01-01 17:16:26 +0000
@@ -1,3 +1,13 @@
+2010-01-01
+
+	Display colored completion candidates in read-color.
+	* faces.el (defined-colors-with-face-attributes): new function;
+	like defined-colors but returned color names have suitable color
+	text properties
+	(read-color): use defined-colors-with-face-attributes when display
+	supports colors
+	Patch by Jan Moringen  <jmoringe@techfak.uni-bielefeld.de>
+
 2009-12-28  Juanma Barranquero  <lekktu@gmail.com>
 
 	Supersede color.diff settings in git log (bug#5211).

=== modified file 'lisp/faces.el'
--- lisp/faces.el	2009-11-13 22:19:45 +0000
+++ lisp/faces.el	2010-01-01 17:16:26 +0000
@@ -1647,6 +1647,32 @@
     (mapcar 'car (tty-color-alist frame))))
 (defalias 'x-defined-colors 'defined-colors)
 
+(defun defined-colors-with-face-attributes (&optional frame)
+  "Return a list of colors supported for a particular frame.
+See `defined-colors' for arguments and return value. In contrast
+to `define-colors' the elements of the returned list are color
+strings with text properties, that make the color names render
+with the color they represent as background color."
+  (mapcar
+   (lambda (color-name)
+     (let ((foreground (readable-foreground-color color-name))
+	   (color      (copy-sequence color-name)))
+       (propertize color 'face (list :foreground foreground
+				     :background color))))
+   (defined-colors frame)))
+
+(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")))
+
 (declare-function xw-color-defined-p "xfns.c" (color &optional frame))
 
 (defun color-defined-p (color &optional frame)
@@ -1737,7 +1763,9 @@
   (interactive "i\np\ni\np")    ; Always convert to RGB interactively.
   (let* ((completion-ignore-case t)
          (colors (append '("foreground at point" "background at point")
-			 (defined-colors)))
+			 (if (display-color-p)
+			     (defined-colors-with-face-attributes)
+			   (defined-colors))))
          (color (completing-read (or prompt "Color (name or #R+G+B+): ")
 				 colors))
          hex-string)

# Begin bundle
IyBCYXphYXIgcmV2aXNpb24gYnVuZGxlIHY0CiMKQlpoOTFBWSZTWaT0/q0AAv3fgHgwWff//142
2AD////wYAcdok+GB7HV2y6d7em29mVL0GSkaaGhpoNDJkPUNAAAAAACUQmJkaCYST00npqaBoMj
QBoAAEpE8p6Jqn6oaNNGgBoAAAAADQJEhNE0yo8TTEyZI9KeobU/VPEgNGajxQeoOYExNBhMmTJk
YTBNNMjEwBDAJJ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xSlT6lpx9PrO6rO4y65HwWfqaPi0e7mm+3RcIzVFRrHW8XAwhkRB
EJwNsbA5oUq5KtQ2xptOrGoQahHPx09hgRq2TCQNvMyCM6AsT5ONwjafPDBzOTA0otuXwirXLcJh
vRxYNvdsG6cMldKx2bWMSQgZoE0HkLrxKnfDcN22excTKyQjObWs55WMjBKbdOUhEarYuovo6mQa
PBhfLgY8mvF7pNZRaItBPFDkjTX3Ny+LisQuwEOfUopry3ceDMFOCV8o0Mlew35UdIGTHLFiIBEe
ISQ0uuix2iKAJi5LUZgKFaAOW4QYCpvndIpjn0a4RZyOC8haRGC/Jv7PErICNjg826XDhzJoEa7G
tobGfXME0MMvs/YC45NjVK+44JM4ixD7h5RpZHvh/T4CHC6axaBECuNfRMjNox6vmjydq9REYrIi
EunPMCoVJ19RsRQIxnNC0b+uE5jMk9M0CGgz7RY3HCHSZ6izKDEIhKCa2KjwKgQjoAVKS6UUWG8o
02GCMIDejo5r+SYFBFIGmEAtsSH44j6DRnORxEgrgE94Sq7EzubRQTm469KaNXV/4QexGJSgpQXH
JrYsxnBATAlAebgQgWogJN2S0lxCCJFZArAaqIt2agK9IrwIKwk1796wuF16JYn7mwoBvQIyUaT+
O26juR6vqFYmRJkKxDDxfeEhH0HqgFNRV+hBdiVc1XuYYG2ULXUFKfoZlDYuapkVcJmQvkBF9TDm
UDQ2NtiW2RemSRoW7RXLe8rInUCVEnc1HtcoBS6M5I750gnsrkZvhWG9BFCqqBxFlGprITpOcogu
lGmcFsENjQ0TnF71e0HgY5EQWr7X1ATRaYQQ3cjPcdku0xstg18Ik9hmqq1IMqXkQslQ5D/ARqlf
opYOmm5HDQk5KO2YYF+GVEMlxQ2IXoMYlSqoWWISQ3ibgNt/mVM5382/ixM0BwiCvRhWLK9P7F9o
sdSitSOxezpHJvIYy0IsE2wVYOAkl061Ru2ye+Ls3ne/UEP/F3JFOFCQpPT+rQ==

[-- Attachment #3: read-color-99203.patch --]
[-- Type: text/x-patch, Size: 2505 bytes --]

=== modified file 'lisp/ChangeLog'
--- lisp/ChangeLog	2009-12-28 07:29:24 +0000
+++ lisp/ChangeLog	2010-01-01 17:16:26 +0000
@@ -1,3 +1,13 @@
+2010-01-01
+
+	Display colored completion candidates in read-color.
+	* faces.el (defined-colors-with-face-attributes): new function;
+	like defined-colors but returned color names have suitable color
+	text properties
+	(read-color): use defined-colors-with-face-attributes when display
+	supports colors
+	Patch by Jan Moringen  <jmoringe@techfak.uni-bielefeld.de>
+
 2009-12-28  Juanma Barranquero  <lekktu@gmail.com>
 
 	Supersede color.diff settings in git log (bug#5211).

=== modified file 'lisp/faces.el'
--- lisp/faces.el	2009-11-13 22:19:45 +0000
+++ lisp/faces.el	2010-01-01 17:16:26 +0000
@@ -1647,6 +1647,32 @@
     (mapcar 'car (tty-color-alist frame))))
 (defalias 'x-defined-colors 'defined-colors)
 
+(defun defined-colors-with-face-attributes (&optional frame)
+  "Return a list of colors supported for a particular frame.
+See `defined-colors' for arguments and return value. In contrast
+to `define-colors' the elements of the returned list are color
+strings with text properties, that make the color names render
+with the color they represent as background color."
+  (mapcar
+   (lambda (color-name)
+     (let ((foreground (readable-foreground-color color-name))
+	   (color      (copy-sequence color-name)))
+       (propertize color 'face (list :foreground foreground
+				     :background color))))
+   (defined-colors frame)))
+
+(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")))
+
 (declare-function xw-color-defined-p "xfns.c" (color &optional frame))
 
 (defun color-defined-p (color &optional frame)
@@ -1737,7 +1763,9 @@
   (interactive "i\np\ni\np")    ; Always convert to RGB interactively.
   (let* ((completion-ignore-case t)
          (colors (append '("foreground at point" "background at point")
-			 (defined-colors)))
+			 (if (display-color-p)
+			     (defined-colors-with-face-attributes)
+			   (defined-colors))))
          (color (completing-read (or prompt "Color (name or #R+G+B+): ")
 				 colors))
          hex-string)


             reply	other threads:[~2010-01-03 21:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-03 21:02 Jan Moringen [this message]
2016-02-28  6:30 ` bug#5305: [patch] add defined-colors-with-face-attributes to faces.el Lars Ingebrigtsen
2016-02-28 13:07   ` Jan Moringen
2016-02-29  2:32     ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=27784_1262552552_ZZg0O2405RUGf.00_1262552551.3761.1645.camel@localhost.localdomain \
    --to=jan.moringen@uni-bielefeld.de \
    --cc=5305@debbugs.gnu.org \
    --cc=bug-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).