Function: tty-color-approximate
tty-color-approximate is a byte-compiled function defined in
tty-colors.el.gz.
Signature
(tty-color-approximate RGB &optional FRAME)
Documentation
Find the color in tty-color-alist that best approximates RGB.
Value is a list of the form (NAME INDEX R G B). The argument RGB should be an rgb value, that is, a list of three integers in the 0..65535 range. FRAME defaults to the selected frame.
Source Code
;; Defined in /usr/src/emacs/lisp/term/tty-colors.el.gz
(defun tty-color-approximate (rgb &optional frame)
"Find the color in `tty-color-alist' that best approximates RGB.
Value is a list of the form (NAME INDEX R G B).
The argument RGB should be an rgb value, that is, a list of three
integers in the 0..65535 range.
FRAME defaults to the selected frame."
(let* ((color-list (tty-color-alist frame))
(candidate (car color-list))
(best-distance 195076) ;; 3 * 255^2 + 15
(r (ash (car rgb) -8))
(g (ash (cadr rgb) -8))
(b (ash (nth 2 rgb) -8))
best-color)
(while candidate
(let ((try-rgb (cddr candidate))
;; If the approximated color is not close enough to the
;; gray diagonal of the RGB cube, favor non-gray colors.
;; (The number 0.065 is an empirical ad-hoc'ery.)
(favor-non-gray (>= (tty-color-off-gray-diag r g b) 0.065))
try-r try-g try-b
dif-r dif-g dif-b dist)
;; If the RGB values of the candidate color are unknown, we
;; never consider it for approximating another color.
(if try-rgb
(progn
(setq try-r (ash (car try-rgb) -8)
try-g (ash (cadr try-rgb) -8)
try-b (ash (nth 2 try-rgb) -8))
(setq dif-r (- r try-r)
dif-g (- g try-g)
dif-b (- b try-b))
(setq dist (+ (* dif-r dif-r) (* dif-g dif-g) (* dif-b dif-b)))
(if (and (< dist best-distance)
;; The candidate color is on the gray diagonal
;; if its RGB components are all equal.
(or (/= try-r try-g) (/= try-g try-b)
(not favor-non-gray)))
(setq best-distance dist
best-color candidate)))))
(setq color-list (cdr color-list))
(setq candidate (car color-list)))
best-color))