latex3 / xcolor

Driver-independent color extensions for LaTeX and pdfLaTeX
LaTeX Project Public License v1.3c
28 stars 5 forks source link

Support HSL color model #25

Open lvjr opened 2 years ago

lvjr commented 2 years ago

At present, xcolor supports HSB color model. It would be nice if it also supports HSL color model. In HSL color model, when L goes from 0 to 1, the color goes from black to white. Therefore it is much easier to control color contrast.

u-fischer commented 2 years ago

well https://en.wikipedia.org/wiki/HSL_and_HSV doesn't think much of these color models. And they don't have much use in PDF. I doubt that we will add them. But the formula to convert HSL to HSB looks rather simple, so you could simply use \fpeval to do it:

\documentclass{article}
\usepackage{xcolor}
\ExplSyntaxOn
\cs_new:Npn\lvjr_convert_Hsl:nnn #1#2#3
 {
   #1,
   \fpeval{#3 + #2*min(#3,1-#3)},
   \fpeval
     {
      (#3 + #2*min(#3,1-#3))=0?
      0:
      round(2*(1-#3/(#3 + #2*min(#3,1-#3))),5)
     }   
 }
\fp_trap:nn{invalid_operation}{none}% avoid error from 0/0
\definecolor{test}{Hsb}{\lvjr_convert_Hsl:nnn{180}{0.5}{0.5}}  
\definecolor{testb}{Hsb}{\lvjr_convert_Hsl:nnn{0}{1}{0.5}}
\fp_trap:nn{invalid_operation}{error}%
\ExplSyntaxOff

\begin{document}
\color{test} ccccc
\color{testb} bbbb
\end{document}
lvjr commented 1 year ago

HTML uses HSL but not HSB. For HTML output, it is more natural to use HSL colors.

lvjr commented 1 year ago

In HSL color model, when L goes from 0 to 1, the color goes from black to white. Therefore it is much easier to control color contrast.

WCAG color contrast is used in PDF/UA standard. But RGB, CMY and HSB don't have this feature. So IMHO it may be a good idea to encourage users to choose colors with HSL color model.

mgkurtz commented 11 months ago

In case anyone wants to do add an other color model to xcolor, it could be worth wile to go with Oklch instead, which was recently added to CSS Color Module Level 4 on the strength of both matching human color perception rather well and being comparatively simple to compute. You can try an online color picker here or there.