diff --git a/etc/NEWS b/etc/NEWS index f6fe068b830..e63132efeda 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -732,6 +732,11 @@ restore the old behavior, you can set 'eshell-pwd-convert-function' to This function natively-compiles all Lisp files in a directory and in its sub-directories, recursively, which were not already natively-compiled. +--- +** New function 'color-blend'. +This function takes two RGB lists and optional ALPHA and returns an RGB +list whose elements are blended in linear space proportional to ALPHA. + +++ ** The 'defcustom' ':local' keyword can now be 'permanent-only'. This means that the variable's 'permanent-local' property is set to t, diff --git a/lisp/color.el b/lisp/color.el index 007504043cc..30e041c60a7 100644 --- a/lisp/color.el +++ b/lisp/color.el @@ -75,6 +75,22 @@ components (e.g. \"#ffff1212ecec\")." (- 1.0 (nth 1 color)) (- 1.0 (nth 2 color))))) +(defun color-blend (a b &optional alpha) + "Blend the two colors A and B in linear space with ALPHA. +A and B should be lists (RED GREEN BLUE), where each element is +between 0.0 and 1.0, inclusive. ALPHA controls the influence A +has on the result and should be between 0.0 and 1.0, inclusive. + +For instance: + + (color-blend '(1 0.5 1) '(0 0 0) 0.75) + => (0.75 0.375 0.75)" + (setq alpha (or alpha 0.5)) + (let (blend) + (dotimes (i 3) + (push (+ (* (nth i a) alpha) (* (nth i b) (- 1 alpha))) blend)) + (nreverse blend))) + (defun color-gradient (start stop step-number) "Return a list with STEP-NUMBER colors from START to STOP. The color list builds a color gradient starting at color START to diff --git a/test/lisp/color-tests.el b/test/lisp/color-tests.el index 63cb024bb8d..3f7483a97c6 100644 --- a/test/lisp/color-tests.el +++ b/test/lisp/color-tests.el @@ -62,6 +62,12 @@ (should (equal (color-complement "#ffffffffffff") '(0.0 0.0 0.0))) (should (equal (color-complement "red") '(0.0 1.0 1.0)))) +(ert-deftest color-tests-blend () + (should (equal (color-blend '(1.0 0.0 0.0) '(0.0 1.0 0.0)) '(0.5 0.5 0.0))) + (should (equal (color-blend '(1.0 1.0 1.0) '(0.0 1.0 0.0)) '(0.5 1.0 0.5))) + (should (equal (color-blend '(0.0 0.39215686274509803 0.0) '(0.9607843137254902 0.8705882352941177 0.7019607843137254)) + '(0.4803921568627451 0.6313725490196078 0.3509803921568627)))) + (ert-deftest color-tests-gradient () (should-not (color-gradient '(0 0 0) '(255 255 255) 0)) (should