Find nearest colors in Emacs 24

As I mentioned in my What’s New In Emacs 24 series (part one, part two) Emacs 24 can now sort the colors in M-x list-colors-display based on the RGB or HSV distance to another color. This is perhaps of most interest to web designers or color theme creators, but it’s still a fun little addition.

Unfortunately, this functionality is only available through the customize interface, and that means most people will never bother with it at all. Hence, I’ve written two helper commands below to make this neat addition to Emacs 24 easier to use.

If you’re looking up a named color (like red ) then it must exist in M-x list-colors-display ; if it’s an HTML color then as long as it is valid it will work.

To use it, you must invoke M-x find-nearest-color and enter the name of a color. The other command, find-nearest-color-at-point does just that: it looks at what your point is on and tries to look it up.

( defun find-nearest-color (color &optional use-hsv) "Finds the nearest color by RGB distance to `color'. If called with a universal argument (or if `use-hsv' is set) use HSV instead of RGB. Runs \\ [list-colors-display] after setting `list-colors-sort'" (interactive "sColor:

P" ) ( let ((list-colors-sort `(,( if ( or use-hsv current-prefix-arg) 'hsv-dist 'rgb-dist) . ,color))) ( if (color-defined-p color) (list-colors-display) ( error "The color \" %s \" does not exist." color)))) ( defun find-nearest-color-at-point (pt) "Finds the nearest color at point `pt'. If called interactively, `pt' is the value immediately under `point'." (interactive "d" ) (find-nearest-color (with-syntax-table (copy-syntax-table (syntax-table)) ;; turn `#' into a word constituent to help ;; `thing-at-point' find HTML color codes. (modify-syntax-entry ?# "w" ) (thing-at-point 'word))))