diff options
| author | Protesilaos Stavrou <info@protesilaos.com> | 2026-04-10 14:48:51 +0300 |
|---|---|---|
| committer | Protesilaos Stavrou <info@protesilaos.com> | 2026-04-10 14:48:51 +0300 |
| commit | c63be80d3be8fc7d62845b5060c6bdbbf70f3b48 (patch) | |
| tree | 5d6dabde36accba547c6c77e6b7956ebbb764cec | |
| parent | 953ec93841d53764217713faf9164c7c7338f1ab (diff) | |
Refine modus-themes-contrast and write a test for it
| -rw-r--r-- | modus-themes.el | 11 | ||||
| -rw-r--r-- | tests/modus-themes-test.el | 16 |
2 files changed, 21 insertions, 6 deletions
diff --git a/modus-themes.el b/modus-themes.el index ed902cd..f22aa05 100644 --- a/modus-themes.el +++ b/modus-themes.el @@ -3775,12 +3775,11 @@ The value is defined in hexadecimal RGB notation, such #123456." (defun modus-themes-contrast (hex-color-1 hex-color-2) "Measure WCAG contrast ratio between HEX-COLOR-1 and HEX-COLOR-2. HEX-COLOR-1 and HEX-COLOR-2 are color values written in hexadecimal RGB." - (let ((ct (/ (+ (modus-themes-wcag-formula hex-color-1) 0.05) - (+ (modus-themes-wcag-formula hex-color-2) 0.05)))) - (max ct (/ ct)))) - -(defun modus-themes--color-six-digits (hex-color) - "Reduce representation of hexadecimal RGB HEX-COLOR to six digits." + (if-let* ((hex1-weight (modus-themes-wcag-formula hex-color-1)) + (hex2-weight (modus-themes-wcag-formula hex-color-2))) + (let ((contrast (/ (+ hex1-weight 0.05) (+ hex2-weight 0.05)))) + (max contrast (/ contrast))) + (error "Both `%s' and `%s' must be valid hexadecimal RGB colors" hex-color-1 hex-color-2))) (let ((color-no-hash (substring hex-color 1))) (if (= (length color-no-hash) 6) hex-color diff --git a/tests/modus-themes-test.el b/tests/modus-themes-test.el index 388b7a2..a6a585b 100644 --- a/tests/modus-themes-test.el +++ b/tests/modus-themes-test.el @@ -69,6 +69,22 @@ 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-modus-themes-contrast () + "Test that `modus-themes-contrast' works as intended. +Also see `modus-themes-test--modus-themes--hex-to-rgb'." + (should (= (modus-themes-contrast "#ffffff" "#000000") 21.0)) + (should (= (modus-themes-contrast "#000000" "#ffffff") 21.0)) + (let ((float-2-fn (lambda (hex1 hex2) + (string-to-number (format "%.2f" (modus-themes-contrast hex1 hex2)))))) + (should (= (funcall float-2-fn "#ff0000" "#ffffff") 4.0)) + (should (= (funcall float-2-fn "#00ff00" "#ffffff") 1.37)) + (should (= (funcall float-2-fn "#0000ff" "#ffffff") 8.59)) + (should (= (funcall float-2-fn "#ffff00" "#ffffff") 1.07)) + (should (= (funcall float-2-fn "#00ffff" "#ffffff") 1.25)) + (should (= (funcall float-2-fn "#ff00ff" "#ffffff") 3.14))) + (should-error (modus-themes-contrast "#ffffff" "#00000")) + (should-error (modus-themes-contrast "#fffff" "#00000"))) (ert-deftest mtt-inheritance () "Ensure all faces inherit from valid faces." ;; Third-party packages, loaded if possible to better test face inheritance. |
