Function: erc-nicks--adjust-contrast

erc-nicks--adjust-contrast is a byte-compiled function defined in erc-nicks.el.gz.

Signature

(erc-nicks--adjust-contrast COLOR TARGET &optional DECREASE)

Source Code

;; Defined in /usr/src/emacs/lisp/erc/erc-nicks.el.gz
;; https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html
(defun erc-nicks--adjust-contrast (color target &optional decrease)
  (cl-assert erc-nicks--fg-rgb)
  (let* ((lum-bg (or erc-nicks--bg-luminance
                     (setq erc-nicks--bg-luminance
                           (erc-nicks--get-luminance erc-nicks-bg-color))))
         (stop (if decrease
                   (color-name-to-rgb erc-nicks-bg-color)
                 erc-nicks--fg-rgb))
         ;; From `color-gradient' in color.el
         (r (nth 0 color))
         (g (nth 1 color))
         (b (nth 2 color))
         (interval (float (1+ (expt 2 erc-nicks--grad-steps))))
         (r-step (/ (- (nth 0 stop) r) interval))
         (g-step (/ (- (nth 1 stop) g) interval))
         (b-step (/ (- (nth 2 stop) b) interval))
         (maxtries erc-nicks--grad-steps)
         started)
    ;; FIXME stop when sufficiently close instead of exhausting.
    (while (let* ((lum-fg (erc-nicks--get-luminance (list r g b)))
                  (darker (if (< lum-bg lum-fg) lum-bg lum-fg))
                  (lighter (if (= darker lum-bg) lum-fg lum-bg))
                  (cur (/ (+ 0.05 lighter) (+ 0.05 darker)))
                  (scale (expt 2 maxtries)))
             (cond ((if decrease (> cur target) (< cur target))
                    (setq r (+ r (* r-step scale))
                          g (+ g (* g-step scale))
                          b (+ b (* b-step scale))))
                   (started
                    (setq r (- r (* r-step scale))
                          g (- g (* g-step scale))
                          b (- b (* b-step scale))))
                   (t (setq maxtries 1)))
             (unless started
               (setq started t))
             (setq r (min 1.0 (max 0 r))
                   g (min 1.0 (max 0 g))
                   b (min 1.0 (max 0 b)))
             (not (zerop (cl-decf maxtries)))))
    (list r g b)))