Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Title: Elisp To Compute Contrast Ratio? #2

Closed
tvraman opened this issue Oct 15, 2020 · 9 comments
Closed

Title: Elisp To Compute Contrast Ratio? #2

tvraman opened this issue Oct 15, 2020 · 9 comments

Comments

@tvraman
Copy link

tvraman commented Oct 15, 2020

Description: Emacs has some elisp libraries for color conversion among
various color coordinates, and there is the excellent name-this-color
package on melpa. However, none of these can compute contrast ratio
best I can tell. So how are you doing this for the modus themes? If
it's there and I'm missing it, I'd like a pointer; otherwise it would
be nice to have an elisp contrast compute function so that over time,
other theme authors can at least verify if their color contrasts are
good for accessibility.

@protesilaos
Copy link
Owner

When I started the project ~1 year ago, I relied on my own bash script that implements the WCAG formula.

Now I prefer this Elisp solution:

;; Code is courtesy of Omar Antolín Camarena:
;; https://github.com/oantolin/emacs-config
(defun wcag (hex)
  (apply #'+
         (cl-mapcar
          (lambda (k x)
            (* k (if (<= x 0.03928)
                     (/ x 12.92)
                   (expt (/ (+ x 0.055) 1.055) 2.4))))
          '(0.2126 0.7152 0.0722)
          (color-name-to-rgb hex))))

(defun clr (c1 c2)
  (let ((ct (/ (+ (wcag c1) 0.05)
               (+ (wcag c2) 0.05))))
    (max ct (/ ct))))

You can then combine that with Org's tables. Such as what I have on this blog post: https://protesilaos.com/codelog/2020-07-04-modus-themes-faint-colours/ (I am not particularly good with Org tables, but those work).


Note that Emacs also has the color-contrast utility. It is useful in some cases, though one needs to make value judgements as well. Refer, for example, to this ongoing thread in the Modus themes' main git repo: https://gitlab.com/protesilaos/modus-themes/-/issues/105

@tvraman
Copy link
Author

tvraman commented Oct 15, 2020 via email

@protesilaos
Copy link
Owner

Please let me know how I can check it in and I shall do it.

@tvraman
Copy link
Author

tvraman commented Oct 15, 2020 via email

@protesilaos
Copy link
Owner

The code is not mine and I would prefer not to include extras in the themes' repo.

Can I send it to you via email? Is your address the one you have in your resume?

@tvraman
Copy link
Author

tvraman commented Oct 18, 2020 via email

@neeasade
Copy link

neeasade commented Jan 5, 2021

Hello there @tvraman -- I have a function for computing contrast levels in my recently created emacs package color-tools.el. It will (hopefully) make it to melpa soon.

@protesilaos
Copy link
Owner

Just to add that the latest version of the themes does include the functions that implement the WCAG formula for colour contrast:

;; This is the WCAG formula: https://www.w3.org/TR/WCAG20-TECHS/G18.html
(defun modus-themes-wcag-formula (hex)
  "Get WCAG value of color value HEX.
The value is defined in hexadecimal RGB notation, such as those in
`modus-themes-operandi-colors' and `modus-themes-vivendi-colors'."
  (cl-loop for k in '(0.2126 0.7152 0.0722)
           for x in (color-name-to-rgb hex)
           sum (* k (if (<= x 0.03928)
                        (/ x 12.92)
                      (expt (/ (+ x 0.055) 1.055) 2.4)))))

;;;###autoload
(defun modus-themes-contrast (c1 c2)
  "Measure WCAG contrast ratio between C1 and C2.
C1 and C2 are color values written in hexadecimal RGB."
  (let ((ct (/ (+ (modus-themes-wcag-formula c1) 0.05)
               (+ (modus-themes-wcag-formula c2) 0.05))))
    (max ct (/ ct))))

That granted, color-tools.el provides a lot of useful features and should be the one to use: I will be using it as well (thanks @neeasade for that!).

@tvraman
Copy link
Author

tvraman commented Jan 5, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants