unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] New function: color-blend
@ 2024-11-02  5:29 Joseph Turner
  2024-11-02  8:34 ` Eli Zaretskii
  2024-11-02 11:26 ` Yuri Khan
  0 siblings, 2 replies; 6+ messages in thread
From: Joseph Turner @ 2024-11-02  5:29 UTC (permalink / raw)
  To: Emacs Devel Mailing List

[-- Attachment #1: Type: text/plain, Size: 375 bytes --]

Hello!

Here's `color-blend', a function to blend two colors into one.  The
patchset also replaces `vtable--color-blend' with `color-blend'.

Examples of this function being implemented separately in the wild:

https://github.com/alphapapa/prism.el/blob/2fa8eb5a9ca62a548d33befef4517e5d0266eb28/prism.el#L1044
https://oremacs.com/2015/04/28/blending-faces/

Thanks!

Joseph


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-color-blend-to-blend-two-RGB-lists.patch --]
[-- Type: text/x-diff, Size: 2811 bytes --]

From a3a506d4c3bd5d4a9c561e4428b0b5891d96b2ff Mon Sep 17 00:00:00 2001
From: Joseph Turner <joseph@breatheoutbreathe.in>
Date: Fri, 1 Nov 2024 21:58:07 -0700
Subject: [PATCH 1/2] 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.
---
 etc/NEWS                 |  4 ++++
 lisp/color.el            | 11 +++++++++++
 test/lisp/color-tests.el |  6 ++++++
 3 files changed, 21 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index 4aba4b17055..a77e0525dbb 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -685,6 +685,10 @@ single binding syntax, which we'd kept only for backwards compatibility.
 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 blends two RGB lists into one.
+
 +++
 ** 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 79dced4e3d7..3828e41755a 100644
--- a/lisp/color.el
+++ b/lisp/color.el
@@ -75,6 +75,17 @@ color-complement
           (- 1.0 (nth 1 color))
           (- 1.0 (nth 2 color)))))
 
+(defun color-blend (a b &optional alpha)
+  "Blend the two colors A and B 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."
+  (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 0f53e4332a4..2f874aa5958 100644
--- a/test/lisp/color-tests.el
+++ b/test/lisp/color-tests.el
@@ -62,6 +62,12 @@ color-tests-complement
   (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
-- 
2.46.0


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-Replace-vtable-color-blend-with-color-blend.patch --]
[-- Type: text/x-diff, Size: 1443 bytes --]

From 940d1951548537e07a1fe0ec0acb1858b0af8533 Mon Sep 17 00:00:00 2001
From: Joseph Turner <joseph@breatheoutbreathe.in>
Date: Fri, 1 Nov 2024 22:21:34 -0700
Subject: [PATCH 2/2] Replace vtable--color-blend with color-blend

* lisp/emacs-lisp/vtable.el (vtable--face-color): Use `color-blend'.
(vtable--color-blend): Remove unused function.
---
 lisp/emacs-lisp/vtable.el | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/lisp/emacs-lisp/vtable.el b/lisp/emacs-lisp/vtable.el
index 925961f012c..47eb6e1a7b5 100644
--- a/lisp/emacs-lisp/vtable.el
+++ b/lisp/emacs-lisp/vtable.el
@@ -212,18 +212,12 @@ vtable--face-color
                   (funcall accessor face2)
                 (plist-get face2 slot))))
     (if (and col1 col2)
-        (vtable--color-blend col1 col2)
+        (apply #'color-rgb-to-hex
+               `(,@(color-blend (color-name-to-rgb col1)
+                                (color-name-to-rgb col2))
+                 2))
       (or col1 col2))))
 
-;;; FIXME: This is probably not the right way to blend two colors, is
-;;; it?
-(defun vtable--color-blend (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)))
-
 ;;; Interface utility functions.
 
 (defun vtable-current-table ()
-- 
2.46.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-11-09 18:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-02  5:29 [PATCH] New function: color-blend Joseph Turner
2024-11-02  8:34 ` Eli Zaretskii
2024-11-02 18:00   ` Joseph Turner
2024-11-09 10:46     ` Eli Zaretskii
2024-11-09 18:13       ` Joseph Turner
2024-11-02 11:26 ` Yuri Khan

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).