From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Joseph Turner Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] New function: color-blend Date: Sat, 02 Nov 2024 11:00:46 -0700 Message-ID: <87a5ehldm9.fsf@breatheoutbreathe.in> References: <87wmhmgq4a.fsf@breatheoutbreathe.in> <86o72yyqxa.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="2054"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Nov 02 19:25:05 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1t7Io4-0000Nt-Kn for ged-emacs-devel@m.gmane-mx.org; Sat, 02 Nov 2024 19:25:05 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t7InS-0004rF-Ee; Sat, 02 Nov 2024 14:24:26 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t7IQj-0001C6-Mb for emacs-devel@gnu.org; Sat, 02 Nov 2024 14:00:57 -0400 Original-Received: from out-179.mta0.migadu.com ([2001:41d0:1004:224b::b3]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t7IQg-0000oy-L2 for emacs-devel@gnu.org; Sat, 02 Nov 2024 14:00:57 -0400 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=breatheoutbreathe.in; s=key1; t=1730570451; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=xU49WYtP+IgOmXi9udOWxgjpJl72j5+VABEN/cowTfE=; b=PLkfTv+GzovyX7kzjLqqCOGUjsiuO8fE7Ed0L0nwFtLh54zgM92OX6MAKZtvHrbSgOmFgs co7kjAI1riOYHvQvgkBFB7eOXnKtd7YU0pm4FGaxmrzv+nSuhx+32nVAWE5fu+P9mPXIlI tHlNAY+07iHw05f3B1D2e4/yZSUjZAQ= In-Reply-To: <86o72yyqxa.fsf@gnu.org> (Eli Zaretskii's message of "Sat, 02 Nov 2024 10:34:57 +0200") X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=2001:41d0:1004:224b::b3; envelope-from=joseph@breatheoutbreathe.in; helo=out-179.mta0.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sat, 02 Nov 2024 14:24:24 -0400 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:325026 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Eli Zaretskii writes: >> From: Joseph Turner >> Date: Fri, 01 Nov 2024 22:29:41 -0700 >>=20 >> Here's `color-blend', a function to blend two colors into one. The >> patchset also replaces `vtable--color-blend' with `color-blend'. >>=20 >> Examples of this function being implemented separately in the wild: >>=20 >> https://github.com/alphapapa/prism.el/blob/2fa8eb5a9ca62a548d33befef4517= e5d0266eb28/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 > Subject: Re: [PATCH] New function: color-blend > To: Joseph Turner > Cc: Emacs Devel Mailing List > 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 = 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=C3=AFve 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=C3=AFve scaling will yield #808080 where= as > middle gray is somewhere around #bebebe in sRGB. Thanks for the feedback! Please see the attached revised patch. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-color-blend-to-blend-two-RGB-lists.patch >From 8c3e96583b6c8248251c026edc730f6ff0b050d4 Mon Sep 17 00:00:00 2001 From: Joseph Turner 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 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0002-Replace-vtable-color-blend-with-color-blend.patch >From abbd1210755f466d40f8167792200bcda306d7d7 Mon Sep 17 00:00:00 2001 From: Joseph Turner 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 --=-=-=--