[latex3-commits] [git/LaTeX3-latex3-latex3] master, pdfescape: Added support for Hsb/HSB/HTML/RGB colors (0c79bb351)

Joseph Wright joseph.wright at morningstar2.co.uk
Thu May 28 13:05:42 CEST 2020


Repository : https://github.com/latex3/latex3
On branches: master,pdfescape
Link       : https://github.com/latex3/latex3/commit/0c79bb351f53ffa7835042ffaa35044df9d775bb

>---------------------------------------------------------------

commit 0c79bb351f53ffa7835042ffaa35044df9d775bb
Author: Joseph Wright <joseph.wright at morningstar2.co.uk>
Date:   Thu May 28 12:05:36 2020 +0100

    Added support for Hsb/HSB/HTML/RGB colors


>---------------------------------------------------------------

0c79bb351f53ffa7835042ffaa35044df9d775bb
 l3experimental/CHANGELOG.md                     |  1 +
 l3experimental/l3color/l3color.dtx              | 98 ++++++++++++++++++++++++-
 l3experimental/l3color/testfiles/m3color002.lvt | 51 +++++++++++++
 l3experimental/l3color/testfiles/m3color002.tlg | 68 +++++++++++++++++
 4 files changed, 217 insertions(+), 1 deletion(-)

diff --git a/l3experimental/CHANGELOG.md b/l3experimental/CHANGELOG.md
index 465d4ddc4..6c72d0d3e 100644
--- a/l3experimental/CHANGELOG.md
+++ b/l3experimental/CHANGELOG.md
@@ -9,6 +9,7 @@ this project uses date-based 'snapshot' version identifiers.
 
 ### Added
 - `\cctab_select:N`
+- Support for `Hsb`, `HSB`, `HTML` and `RGB` color models
 
 ## [2020-05-18]
 
diff --git a/l3experimental/l3color/l3color.dtx b/l3experimental/l3color/l3color.dtx
index 6a6e65ffc..d0fb5bf82 100644
--- a/l3experimental/l3color/l3color.dtx
+++ b/l3experimental/l3color/l3color.dtx
@@ -61,7 +61,9 @@
 % space. In contrast, various proprietary models are available which define
 % \emph{spot} colors.
 %
-% The models supported here are
+% Core models are used to pass color information to output; these are
+% \enquote{native} to \pkg{l3color}. Core models use real numbers in the range
+% $[0,1]$ to represent values. The core models supported here are
 % \begin{itemize}
 %   \item \texttt{gray} Grayscale color, with a single axis running from
 %     $0$ (fully black) to $1$ (fully white)
@@ -73,6 +75,23 @@
 %     (see \url{https://helpx.adobe.com/indesign/using/spot-process-colors.html}
 %     for details of the use of spot colors in print)
 % \end{itemize}
+% There are also interface models: these are convenient for users but have
+% to be manipulated before storing/passing to the backend. Interface models
+% are primarily integer-based: see below for more detail. The supported
+% interface models are
+% \begin{itemize}
+%   \item \texttt{Hsb} Hue-saturation-brightness color, with three axes, integer
+%     in the range $[0,360]$ for hue, real values in the range $[0,1]$ for
+%     saturation and brightness
+%   \item \texttt{HSB} Hue-saturation-brightness color, with three axes, integer
+%     in the range $[0,360]$ for hue, integer values in the range $[0,255]$ for
+%     saturation and brightness
+%   \item \texttt{HTML} HTML format representation of RGB color given as a
+%     single six-digit hexadecimal number
+%   \item \texttt{RGB} Red-green-blue color, with three axes, one for each of
+%     the components, values as integers from $0$ to $255$
+% \end{itemize}
+% All interface models are internally stored as |rgb|.
 % 
 % Additional models may be created to allow mixing of spot colors
 % with each other or with those from other models. See
@@ -677,6 +696,83 @@
 % \end{macro}
 % \end{macro}
 %
+% \begin{macro}[EXP]
+%   {
+%     \@@_parse_model_Hsb:w, \@@_parse_model_HSB:w,
+%     \@@_parse_model_HTML:w, \@@_parse_model_RGB:w
+%   }
+%    \begin{macrocode}
+\cs_new:Npn \@@_parse_model_Hsb:w #1 , #2 , #3 , #4 \s_@@_stop
+  {
+    \exp_args:Ne \@@_parse_model_hsb:nnn { \fp_eval:n { #1 / 360 } }
+      {#2} {#3}
+  }
+%    \end{macrocode}
+%   The conversion here is non-trivial but is described at length
+%   in the \pkg{xcolor} manual. For ease, we calculate the integer
+%   and fractional parts of the hue first, then use them to work out the
+%   possible values for $r$, $g$ and $b$ before putting them in the correct
+%   places.
+%    \begin{macrocode}
+\cs_new:Npn \@@_parse_model_hsb:nnn #1#2#3
+  {
+    rgb ~
+    \exp_args:Ne \@@_parse_model_hsb_aux:nnn
+      { \fp_eval:n { 6 * #1 } } {#2} {#3}
+  }
+\cs_new:Npn \@@_parse_model_hsb_aux:nnn #1#2#3
+  {
+    \exp_args:Nee \@@_parse_model_hsb_aux:nnnn
+      { \fp_eval:n { floor(#1) } } { \fp_eval:n { #1 - floor(#1) } }
+      {#2} {#3}
+  }
+\cs_new:Npn \@@_parse_model_hsb_aux:nnnn #1#2#3#4
+  {
+    \use:e
+      {
+        \exp_not:N \@@_parse_model_hsb_aux:nnnnn
+         { \@@_parse_number:n {#4} }
+         { \fp_eval:n { round(#4 * (1 - #3) ,5) } }
+         { \fp_eval:n { round(#4 * ( 1 - #3 * #2 ) ,5) } }
+         { \fp_eval:n { round(#4 * ( 1 - #3 * (1 - #2) ) ,5) } }
+         {#1}
+      }
+  }
+\cs_new:Npn \@@_parse_model_hsb_aux:nnnnn #1#2#3#4#5
+  { \use:c { @@_parse_model_hsb_ #5 :nnnn } {#1} {#2} {#3} {#4} }
+\cs_new:cpn { @@_parse_model_hsb_0:nnnn } #1#2#3#4 { #1 ~ #4 ~ #2 }
+\cs_new:cpn { @@_parse_model_hsb_1:nnnn } #1#2#3#4 { #3 ~ #1 ~ #2 }
+\cs_new:cpn { @@_parse_model_hsb_2:nnnn } #1#2#3#4 { #2 ~ #1 ~ #4 }
+\cs_new:cpn { @@_parse_model_hsb_3:nnnn } #1#2#3#4 { #2 ~ #3 ~ #1 }
+\cs_new:cpn { @@_parse_model_hsb_4:nnnn } #1#2#3#4 { #4 ~ #2 ~ #1 }
+\cs_new:cpn { @@_parse_model_hsb_5:nnnn } #1#2#3#4 { #1 ~ #2 ~ #3 }
+\cs_new:cpn { @@_parse_model_hsb_6:nnnn } #1#2#3#4 { #1 ~ #2 ~ #2 }
+\cs_new:Npn \@@_parse_model_HSB:w #1 , #2 , #3 , #4 \s_@@_stop
+  {
+    \exp_args:Neee \@@_parse_model_hsb:nnn
+      { \fp_eval:n {#1 / 360} }
+      { \fp_eval:n {#2 / 255} }
+      { \fp_eval:n {#3 / 255} }
+  }
+\cs_new:Npn \@@_parse_model_HTML:w #1 , #2 \s_@@_stop
+  { \@@_parse_model_HTML_aux:w #1 0 0 0 0 0 0 \s_@@_stop }
+\cs_new:Npn \@@_parse_model_HTML_aux:w #1#2#3#4#5#6#7 \s_@@_stop
+  {
+    rgb ~
+    \fp_eval:n { round(\int_from_hex:n {#1#2} / 255,5) } ~
+    \fp_eval:n { round(\int_from_hex:n {#3#4} / 255,5) } ~
+    \fp_eval:n { round(\int_from_hex:n {#5#6} / 255,5) }
+  }
+\cs_new:Npn \@@_parse_model_RGB:w #1 , #2 , #3 , #4 \s_@@_stop
+  {
+    rgb ~
+    \fp_eval:n { round(#1 / 255,5) } ~
+    \fp_eval:n { round(#2 / 255,5) } ~
+    \fp_eval:n { round(#3 / 255,5) }
+  }
+%    \end{macrocode}
+% \end{macro}
+%
 % \subsection{Selecting colors (and color models)}
 %
 % \begin{variable}{\l_color_fixed_model_tl}
diff --git a/l3experimental/l3color/testfiles/m3color002.lvt b/l3experimental/l3color/testfiles/m3color002.lvt
new file mode 100644
index 000000000..3dec4335b
--- /dev/null
+++ b/l3experimental/l3color/testfiles/m3color002.lvt
@@ -0,0 +1,51 @@
+%
+% Copyright (C) 2020 The LaTeX3 Project
+%
+
+\documentclass{minimal}
+
+\input{regression-test}
+
+\RequirePackage[enable-debug]{expl3}
+\RequirePackage{l3color}
+\ExplSyntaxOn
+\debug_on:n { check-declarations , deprecation , log-functions }
+\ExplSyntaxOff
+
+\START
+
+\AUTHOR{Joseph Wright}
+
+\ExplSyntaxOn
+
+\TEST { Hsb~model }
+  {
+    \color_set:nnn { foo1 } { Hsb } { 5 , 0.6 , 0.7 }
+    \color_show:n { foo1 }
+    \color_set:nnn { foo1 } { Hsb } { 65 , 0.6 , 0.7 }
+    \color_show:n { foo1 }
+    \color_set:nnn { foo1 } { Hsb } { 125 , 0.6 , 0.7 }
+    \color_show:n { foo1 }
+    \color_set:nnn { foo1 } { Hsb } { 185 , 0.6 , 0.7}
+    \color_show:n { foo1 }
+    \color_set:nnn { foo1 } { Hsb } { 245 , 0.6 , 0.7 }
+    \color_show:n { foo1 }
+    \color_set:nnn { foo1 } { Hsb } { 305 , 0.6 , 0.7 }
+    \color_show:n { foo1 }
+    \color_set:nnn { foo1 } { Hsb } { 365 , 0.6 , 0.7 }
+    \color_show:n { foo1 }
+    \color_set:nnn { foo1 } { Hsb } { 65 , 0.1 , 0.2 }
+    \color_show:n { foo1 }
+  }
+
+\TEST { Integer/integer-like~models }
+  {
+    \color_set:nnn { foo1 } { HSB } { 30 , 10 , 10 }
+    \color_show:n { foo1 }
+    \color_set:nnn { foo1 } { RGB } { 20 , 40 , 60 }
+    \color_show:n { foo1 }
+    \color_set:nnn { foo2 } { HTML } { 1234AE }
+    \color_show:n { foo2 }
+  }
+
+\END
diff --git a/l3experimental/l3color/testfiles/m3color002.tlg b/l3experimental/l3color/testfiles/m3color002.tlg
new file mode 100644
index 000000000..46f012a5f
--- /dev/null
+++ b/l3experimental/l3color/testfiles/m3color002.tlg
@@ -0,0 +1,68 @@
+This is a generated file for the LaTeX (2e + expl3) validation system.
+Don't change this file in any respect.
+Author: Joseph Wright
+============================================================
+TEST 1: Hsb model
+============================================================
+Defining \l__color_named_foo1_tl on line ...
+The color foo1 has the properties:
+>  model  =>  rgb
+>  value  =>  0.7, 0.315, 0.28.
+<recently read> }
+l. ...  }
+The color foo1 has the properties:
+>  model  =>  rgb
+>  value  =>  0.665, 0.7, 0.28.
+<recently read> }
+l. ...  }
+The color foo1 has the properties:
+>  model  =>  rgb
+>  value  =>  0.28, 0.7, 0.315.
+<recently read> }
+l. ...  }
+The color foo1 has the properties:
+>  model  =>  rgb
+>  value  =>  0.28, 0.665, 0.7.
+<recently read> }
+l. ...  }
+The color foo1 has the properties:
+>  model  =>  rgb
+>  value  =>  0.315, 0.28, 0.7.
+<recently read> }
+l. ...  }
+The color foo1 has the properties:
+>  model  =>  rgb
+>  value  =>  0.7, 0.28, 0.665.
+<recently read> }
+l. ...  }
+The color foo1 has the properties:
+>  model  =>  rgb
+>  value  =>  0.7, 0.28, 0.28.
+<recently read> }
+l. ...  }
+The color foo1 has the properties:
+>  model  =>  rgb
+>  value  =>  0.19833, 0.2, 0.18.
+<recently read> }
+l. ...  }
+============================================================
+============================================================
+TEST 2: Integer/integer-like models
+============================================================
+The color foo1 has the properties:
+>  model  =>  rgb
+>  value  =>  0.0392156862745098, 0.03845, 0.03768.
+<recently read> }
+l. ...  }
+The color foo1 has the properties:
+>  model  =>  rgb
+>  value  =>  0.07843, 0.15686, 0.23529.
+<recently read> }
+l. ...  }
+Defining \l__color_named_foo2_tl on line ...
+The color foo2 has the properties:
+>  model  =>  rgb
+>  value  =>  0.07059, 0.20392, 0.68235.
+<recently read> }
+l. ...  }
+============================================================





More information about the latex3-commits mailing list.