branch: externals/ement commit 298a902e07443579db5aefc280d85361614cc832 Author: Adam Porter <a...@alphapapa.net> Commit: Adam Porter <a...@alphapapa.net>
Fix: Color-related compatibility with Emacs 27 Copy (color-luminance-dark-limit) and (color-dark-p) from Emacs 28. Fixes #99. Thanks to Phil Sainty (@phil-s). Reported-by: Phil Sainty <psai...@orcon.net.nz> Suggested-by: Phil Sainty <psai...@orcon.net.nz> --- README.org | 1 + ement-lib.el | 45 ++++++++++++++++++++++++++++++++++++++++++++- ement-notify.el | 2 +- ement-room.el | 2 +- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.org b/README.org index 44f746a70f..a1d4674867 100644 --- a/README.org +++ b/README.org @@ -303,6 +303,7 @@ Note that, while ~matrix-client~ remains usable, and probably will for some time + Command ~ement-describe-room~ for rooms without topics. + Improve insertion of old messages around existing timestamp headers. + Reduce D-Bus notification system check timeout to 2 seconds (from the default of 25). ++ Compatibility with Emacs 27. ** 0.2.1 diff --git a/ement-lib.el b/ement-lib.el index dfcb433114..31d5bb48e8 100644 --- a/ement-lib.el +++ b/ement-lib.el @@ -61,6 +61,49 @@ (defvar ement-room-prism-color-adjustment) (defvar ement-room-prism-minimum-contrast) +;;;; Compatibility + +;; These workarounds should be removed when they aren't needed. + +;;;;; Emacs 28 color features. + +;; Copied from Emacs 28. See <https://github.com/alphapapa/ement.el/issues/99>. + +;; FIXME: Remove this workaround when possible. + +(eval-and-compile + (unless (boundp 'color-luminance-dark-limit) + (defconst ement--color-luminance-dark-limit 0.325 + "The relative luminance below which a color is considered 'dark'. +A 'dark' color in this sense provides better contrast with white +than with black; see `color-dark-p'. +This value was determined experimentally."))) + +(defalias 'ement--color-dark-p + (if (fboundp 'color-dark-p) + 'color-dark-p + (lambda (rgb) + "Whether RGB is more readable against white than black. +RGB is a 3-element list (R G B), each component in the range [0,1]. +This predicate can be used both for determining a suitable (black or white) +contrast colour with RGB as background and as foreground." + (unless (<= 0 (apply #'min rgb) (apply #'max rgb) 1) + (error "RGB components %S not in [0,1]" rgb)) + ;; Compute the relative luminance after gamma-correcting (assuming sRGB), + ;; and compare to a cut-off value determined experimentally. + ;; See https://en.wikipedia.org/wiki/Relative_luminance for details. + (let* ((sr (nth 0 rgb)) + (sg (nth 1 rgb)) + (sb (nth 2 rgb)) + ;; Gamma-correct the RGB components to linear values. + ;; Use the power 2.2 as an approximation to sRGB gamma; + ;; it should be good enough for the purpose of this function. + (r (expt sr 2.2)) + (g (expt sg 2.2)) + (b (expt sb 2.2)) + (y (+ (* r 0.2126) (* g 0.7152) (* b 0.0722)))) + (< y ement--color-luminance-dark-limit))))) + ;;;; Functions ;;;;; Commands @@ -556,7 +599,7 @@ avatars, etc." "white")) (_ ;; The `contrast-with` color is usable: test it. - (if (color-dark-p (color-name-to-rgb contrast-with)) + (if (ement--color-dark-p (color-name-to-rgb contrast-with)) "white" "black"))))))) (apply #'color-rgb-to-hex (append color-rgb (list 2)))))) diff --git a/ement-notify.el b/ement-notify.el index b73eb605b9..d929d0022f 100644 --- a/ement-notify.el +++ b/ement-notify.el @@ -325,7 +325,7 @@ If ROOM has no existing buffer, do nothing." (let ((color (color-desaturate-name (ement--prism-color (ement-room-id room) :contrast-with (face-foreground 'default)) 50))) - (if (color-dark-p (color-name-to-rgb (face-background 'default))) + (if (ement--color-dark-p (color-name-to-rgb (face-background 'default))) (color-darken-name color 25) (color-lighten-name color 25)))))) diff --git a/ement-room.el b/ement-room.el index 3a65731d0f..30f025d6f9 100644 --- a/ement-room.el +++ b/ement-room.el @@ -895,7 +895,7 @@ spec) without requiring all events to use the same margin width." (setf (ement-user-message-color sender) (let ((message-color (color-desaturate-name (ement--user-color sender) ement-room-prism-message-desaturation))) - (if (color-dark-p (color-name-to-rgb (face-background 'default))) + (if (ement--color-dark-p (color-name-to-rgb (face-background 'default))) (color-lighten-name message-color ement-room-prism-message-lightening) (color-darken-name message-color ement-room-prism-message-lightening)))))))) (redacted-face (when (or local-redacted-by unsigned-redacted-by)