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: [PATCH] New function: color-blend Date: Fri, 01 Nov 2024 22:29:41 -0700 Message-ID: <87wmhmgq4a.fsf@breatheoutbreathe.in> 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="14821"; mail-complaints-to="usenet@ciao.gmane.io" To: Emacs Devel Mailing List Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Nov 02 08:04:00 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 1t78Ax-0003hb-82 for ged-emacs-devel@m.gmane-mx.org; Sat, 02 Nov 2024 08:03:59 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t78A7-0008Mn-8i; Sat, 02 Nov 2024 03:03:07 -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 1t76hv-0006T1-Nn for emacs-devel@gnu.org; Sat, 02 Nov 2024 01:29:55 -0400 Original-Received: from out-185.mta1.migadu.com ([95.215.58.185]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t76ht-0001LV-2H for emacs-devel@gnu.org; Sat, 02 Nov 2024 01:29:55 -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=1730525387; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=5khvB54Z8wTa7gYZuVTBjHiOmbnFonRGu+Op9q0J+Fs=; b=Ez4JcwbZiYtf5o2VTxVOVxstyrYbSOvURAygWA6i/49fwPOARaIKEPgZNlG3g2+g+WGnKh 8Ztb0HEkiDFhBffhEIzN8TV/I0tZ1C7l09cYREVB7BcGKbLucpQ+H+z1cvbGAZ1hd0zsiC 0+Em7vhemwmIzYq4P7FQ0qMAxtqavvE= X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=95.215.58.185; envelope-from=joseph@breatheoutbreathe.in; helo=out-185.mta1.migadu.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sat, 02 Nov 2024 03:03:03 -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:325002 Archived-At: --=-=-= Content-Type: text/plain 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 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-color-blend-to-blend-two-RGB-lists.patch >From a3a506d4c3bd5d4a9c561e4428b0b5891d96b2ff 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 | 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 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0002-Replace-vtable-color-blend-with-color-blend.patch >From 940d1951548537e07a1fe0ec0acb1858b0af8533 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 --=-=-=--