From: Joseph Turner <joseph@breatheoutbreathe.in>
To: Emacs Devel Mailing List <emacs-devel@gnu.org>
Subject: [PATCH] New function: color-blend
Date: Fri, 01 Nov 2024 22:29:41 -0700 [thread overview]
Message-ID: <87wmhmgq4a.fsf@breatheoutbreathe.in> (raw)
[-- 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
next reply other threads:[~2024-11-02 5:29 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-02 5:29 Joseph Turner [this message]
2024-11-02 8:34 ` [PATCH] New function: color-blend 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87wmhmgq4a.fsf@breatheoutbreathe.in \
--to=joseph@breatheoutbreathe.in \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).