emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: master 68ae6fa: Improved light/dark colour predicate (bug#41544)


From: Stefan Monnier
Subject: Re: master 68ae6fa: Improved light/dark colour predicate (bug#41544)
Date: Wed, 10 Jun 2020 15:20:05 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

> +(defun color-dark-p (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))))

Could we arrange to share the code with `color-srgb-to-xyz` (in `color.el`)?

> +    (< y (eval-when-compile (expt 0.6 2.2)))))

Where does this 0.6 come from?
I don't see it in https://en.wikipedia.org/wiki/Relative_luminance
[ I don't doubt it's a good choice, but I think we should document
  where these thing come from.  ]


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]