diff options
| author | Joseph Turner | 2024-11-01 21:58:07 -0700 |
|---|---|---|
| committer | Eli Zaretskii | 2024-11-09 12:40:18 +0200 |
| commit | bf312529def48bc6fdf72d43d5be158d125f52f6 (patch) | |
| tree | 3883711fbc57445c6dae5417399e23b5a1077194 | |
| parent | e1e8da5e4f1951744d26b99b000c7f746a9ca8c6 (diff) | |
| download | emacs-bf312529def48bc6fdf72d43d5be158d125f52f6.tar.gz emacs-bf312529def48bc6fdf72d43d5be158d125f52f6.zip | |
Add color-blend to blend two RGB lists
* lisp/color.el (color-blend): Blend two RGB lists.
* test/lisp/color-tests.el (color-tests-blend): Test color-blend.
* etc/NEWS: Announce color-blend.
| -rw-r--r-- | etc/NEWS | 5 | ||||
| -rw-r--r-- | lisp/color.el | 16 | ||||
| -rw-r--r-- | test/lisp/color-tests.el | 6 |
3 files changed, 27 insertions, 0 deletions
| @@ -732,6 +732,11 @@ restore the old behavior, you can set 'eshell-pwd-convert-function' to | |||
| 732 | This function natively-compiles all Lisp files in a directory and in its | 732 | This function natively-compiles all Lisp files in a directory and in its |
| 733 | sub-directories, recursively, which were not already natively-compiled. | 733 | sub-directories, recursively, which were not already natively-compiled. |
| 734 | 734 | ||
| 735 | --- | ||
| 736 | ** New function 'color-blend'. | ||
| 737 | This function takes two RGB lists and optional ALPHA and returns an RGB | ||
| 738 | list whose elements are blended in linear space proportional to ALPHA. | ||
| 739 | |||
| 735 | +++ | 740 | +++ |
| 736 | ** The 'defcustom' ':local' keyword can now be 'permanent-only'. | 741 | ** The 'defcustom' ':local' keyword can now be 'permanent-only'. |
| 737 | This means that the variable's 'permanent-local' property is set to t, | 742 | 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\")." | |||
| 75 | (- 1.0 (nth 1 color)) | 75 | (- 1.0 (nth 1 color)) |
| 76 | (- 1.0 (nth 2 color))))) | 76 | (- 1.0 (nth 2 color))))) |
| 77 | 77 | ||
| 78 | (defun color-blend (a b &optional alpha) | ||
| 79 | "Blend the two colors A and B in linear space with ALPHA. | ||
| 80 | A and B should be lists (RED GREEN BLUE), where each element is | ||
| 81 | between 0.0 and 1.0, inclusive. ALPHA controls the influence A | ||
| 82 | has on the result and should be between 0.0 and 1.0, inclusive. | ||
| 83 | |||
| 84 | For instance: | ||
| 85 | |||
| 86 | (color-blend '(1 0.5 1) '(0 0 0) 0.75) | ||
| 87 | => (0.75 0.375 0.75)" | ||
| 88 | (setq alpha (or alpha 0.5)) | ||
| 89 | (let (blend) | ||
| 90 | (dotimes (i 3) | ||
| 91 | (push (+ (* (nth i a) alpha) (* (nth i b) (- 1 alpha))) blend)) | ||
| 92 | (nreverse blend))) | ||
| 93 | |||
| 78 | (defun color-gradient (start stop step-number) | 94 | (defun color-gradient (start stop step-number) |
| 79 | "Return a list with STEP-NUMBER colors from START to STOP. | 95 | "Return a list with STEP-NUMBER colors from START to STOP. |
| 80 | The color list builds a color gradient starting at color START to | 96 | 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 @@ | |||
| 62 | (should (equal (color-complement "#ffffffffffff") '(0.0 0.0 0.0))) | 62 | (should (equal (color-complement "#ffffffffffff") '(0.0 0.0 0.0))) |
| 63 | (should (equal (color-complement "red") '(0.0 1.0 1.0)))) | 63 | (should (equal (color-complement "red") '(0.0 1.0 1.0)))) |
| 64 | 64 | ||
| 65 | (ert-deftest color-tests-blend () | ||
| 66 | (should (equal (color-blend '(1.0 0.0 0.0) '(0.0 1.0 0.0)) '(0.5 0.5 0.0))) | ||
| 67 | (should (equal (color-blend '(1.0 1.0 1.0) '(0.0 1.0 0.0)) '(0.5 1.0 0.5))) | ||
| 68 | (should (equal (color-blend '(0.0 0.39215686274509803 0.0) '(0.9607843137254902 0.8705882352941177 0.7019607843137254)) | ||
| 69 | '(0.4803921568627451 0.6313725490196078 0.3509803921568627)))) | ||
| 70 | |||
| 65 | (ert-deftest color-tests-gradient () | 71 | (ert-deftest color-tests-gradient () |
| 66 | (should-not (color-gradient '(0 0 0) '(255 255 255) 0)) | 72 | (should-not (color-gradient '(0 0 0) '(255 255 255) 0)) |
| 67 | (should | 73 | (should |