From: Joseph Turner <joseph@breatheoutbreathe.in>
To: Eli Zaretskii <eliz@gnu.org>
Cc: emacs-devel@gnu.org
Subject: Re: [PATCH] New function: color-blend
Date: Sat, 02 Nov 2024 11:00:46 -0700 [thread overview]
Message-ID: <87a5ehldm9.fsf@breatheoutbreathe.in> (raw)
In-Reply-To: <86o72yyqxa.fsf@gnu.org> (Eli Zaretskii's message of "Sat, 02 Nov 2024 10:34:57 +0200")
[-- Attachment #1: Type: text/plain, Size: 2250 bytes --]
Eli Zaretskii <eliz@gnu.org> writes:
>> From: Joseph Turner <joseph@breatheoutbreathe.in>
>> Date: Fri, 01 Nov 2024 22:29:41 -0700
>>
>> 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.
>
>> +---
>> +** New function 'color-blend'.
>> +This function blends two RGB lists into one.
>
> This should explain what the function does, in terms that are clear
> even to those who are not experts in this area. AFAIU, "color
> blending" has at least two different meanings.
>
>> +(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."
>
> The doc string should document the return value. Perhaps an example
> of blending will also be a good addition to the doc string.
> From: Yuri Khan <yuri.v.khan@gmail.com>
> Subject: Re: [PATCH] New function: color-blend
> To: Joseph Turner <joseph@breatheoutbreathe.in>
> Cc: Emacs Devel Mailing List <emacs-devel@gnu.org>
> Date: Sat, 2 Nov 2024 18:26:57 +0700 (6 hours, 32 minutes, 20 seconds ago)
> Flags: seen, personal
> Maildir: /joseph-breatheoutbreathein/INBOX
> On Sat, 2 Nov 2024 at 14:03, Joseph Turner <joseph@breatheoutbreathe.in> wrote:
> > Here's `color-blend', a function to blend two colors into one.
> It would be nice to explicitly mention linear RGB space. Most
> users[citation needed] these days are accustomed to colors specified
> in sRGB space and naïve conversion dividing by 255 will not give the
> desired blends. As an example, linear mixing black (0 0 0) with white
> (1 1 1) yields (0.5 0.5 0.5). Naïve scaling will yield #808080 whereas
> middle gray is somewhere around #bebebe in sRGB.
Thanks for the feedback! Please see the attached revised patch.
[-- 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: 3022 bytes --]
From 8c3e96583b6c8248251c026edc730f6ff0b050d4 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 | 5 +++++
lisp/color.el | 16 ++++++++++++++++
test/lisp/color-tests.el | 6 ++++++
3 files changed, 27 insertions(+)
diff --git a/etc/NEWS b/etc/NEWS
index 4aba4b17055..434732dfccf 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -685,6 +685,11 @@ 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 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 79dced4e3d7..155b0dd54ca 100644
--- a/lisp/color.el
+++ b/lisp/color.el
@@ -75,6 +75,22 @@ 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 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 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 abbd1210755f466d40f8167792200bcda306d7d7 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 prev parent reply other threads:[~2024-11-02 18:00 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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=87a5ehldm9.fsf@breatheoutbreathe.in \
--to=joseph@breatheoutbreathe.in \
--cc=eliz@gnu.org \
--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).