5.11 Override colors through blending

This is yet another method of overriding color values.

Override colors.

Override color saturation.

Building on ideas and concepts from the previous sections, this method blends the entire palette at once with the chosen colors. The function my-modus-themes-interpolate blends two colors, taking a value from the themes and mixing it with a user-defined color to arrive at a midpoint. This scales to all background and foreground colors with the help of the my-modus-themes-tint-palette function.

(setq my-modus-operandi-bg-blend "#fbf1c7"
      my-modus-operandi-fg-blend "#3a6084"
      my-modus-vivendi-bg-blend "#3a4042"
      my-modus-vivendi-fg-blend "#d7b765")

;; Adapted from the `kurecolor-interpolate' function of kurecolor.el
(defun my-modus-themes-interpolate (color1 color2)
  (cl-destructuring-bind (r g b)
      (mapcar #'(lambda (n) (* (/ n 2) 255.0))
              (cl-mapcar '+ (color-name-to-rgb color1) (color-name-to-rgb color2)))
    (format "#%02X%02X%02X" r g b)))

(defun my-modus-themes-tint-palette (palette bg-blend fg-blend)
  "Modify Modus PALETTE programmatically and return a new palette.
Blend background colors with BG-BLEND and foreground colors with FG-BLEND."
  (let (name cons colors)
    (dolist (cons palette)
      (let ((blend (if (string-match "bg" (symbol-name (car cons)))
                       bg-blend
                     fg-blend)))
        (setq name (my-modus-themes-interpolate (cdr cons) blend)))
      (setq name (format "%s" name))
      (setq cons `(,(car cons) . ,name))
      (push cons colors))
    colors))

(define-minor-mode modus-themes-tinted-mode
  "Tweak some Modus themes colors."
  :init-value nil
  :global t
  (if modus-themes-tinted-mode
      (setq modus-themes-operandi-color-overrides
            (my-modus-themes-tint-palette modus-themes-operandi-colors
                                          my-modus-operandi-bg-blend
                                          my-modus-operandi-fg-blend)
            modus-themes-vivendi-color-overrides
            (my-modus-themes-tint-palette modus-themes-vivendi-colors
                                          my-modus-vivendi-bg-blend
                                          my-modus-vivendi-fg-blend))
    (setq modus-themes-operandi-color-overrides nil
          modus-themes-vivendi-color-overrides nil)))

(modus-themes-tinted-mode 1)