Refine modus-themes-wcag-formula and write a test for it

This commit is contained in:
Protesilaos Stavrou 2026-04-10 14:48:14 +03:00
parent f062c8d99a
commit 953ec93841
No known key found for this signature in database
GPG key ID: 99BD6459CD5CA3EA
2 changed files with 12 additions and 6 deletions

View file

@ -3764,12 +3764,12 @@ ready (per <https://github.com/protesilaos/modus-themes/issues/198>)."
(defun modus-themes-wcag-formula (hex-color)
"Get WCAG value of color value HEX-COLOR.
The value is defined in hexadecimal RGB notation, such #123456."
(let ((channels (modus-themes--hex-to-rgb hex-color))
(weights '(0.2126 0.7152 0.0722))
(contribution nil))
(while channels
(push (modus-themes--wcag-contribution (pop channels) (pop weights)) contribution))
(apply #'+ contribution)))
(when-let* ((channels (modus-themes--hex-to-rgb hex-color)))
(let ((weights '(0.2126 0.7152 0.0722))
(contribution nil))
(while channels
(push (modus-themes--wcag-contribution (pop channels) (pop weights)) contribution))
(apply #'+ contribution))))
;;;###autoload
(defun modus-themes-contrast (hex-color-1 hex-color-2)

View file

@ -63,6 +63,12 @@
(should-not (modus-themes--hex-to-rgb "#gggggg"))
(should-error (modus-themes--hex-to-rgb (list 1.0 1.0 1.0))))
(ert-deftest mtt-modus-themes-wcag-formula ()
"Test that `modus-themes-wcag-formula' does the right thing.
Also see `modus-themes-test--modus-themes--hex-to-rgb'."
(should (= (modus-themes-wcag-formula "#ffffff") 1.0))
(should (= (modus-themes-wcag-formula "#000000") 0.0))
(should-not (modus-themes-wcag-formula "#00000")))
(ert-deftest mtt-inheritance ()
"Ensure all faces inherit from valid faces."
;; Third-party packages, loaded if possible to better test face inheritance.