[latex3-commits] [git/LaTeX3-latex3-latex3] color-export: Add \color_export:nn(n)N (c744fd251)

Joseph Wright joseph.wright at morningstar2.co.uk
Thu Jun 11 14:06:18 CEST 2020


Repository : https://github.com/latex3/latex3
On branch  : color-export
Link       : https://github.com/latex3/latex3/commit/c744fd25110d9abca349863753b06e304d841e2a

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

commit c744fd25110d9abca349863753b06e304d841e2a
Author: Joseph Wright <joseph.wright at morningstar2.co.uk>
Date:   Thu Jun 11 13:02:18 2020 +0100

    Add \color_export:nn(n)N
    
    Currently just for HTML.


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

c744fd25110d9abca349863753b06e304d841e2a
 l3experimental/CHANGELOG.md                     |   3 +
 l3experimental/l3color/l3color.dtx              | 112 +++++++++++++++++++++++-
 l3experimental/l3color/testfiles/m3color002.lvt |  19 ++++
 l3experimental/l3color/testfiles/m3color002.tlg |  26 ++++++
 4 files changed, 158 insertions(+), 2 deletions(-)

diff --git a/l3experimental/CHANGELOG.md b/l3experimental/CHANGELOG.md
index fc0de0c55..e794d24cf 100644
--- a/l3experimental/CHANGELOG.md
+++ b/l3experimental/CHANGELOG.md
@@ -7,6 +7,9 @@ this project uses date-based 'snapshot' version identifiers.
 
 ## [Unreleased]
 
+### Added
+- `\color_export:nn(n)N` (see #742)
+
 ### Changed
 - Internal color model
 - Internal performance enhancements
diff --git a/l3experimental/l3color/l3color.dtx b/l3experimental/l3color/l3color.dtx
index 8c45bb95c..c80f034e6 100644
--- a/l3experimental/l3color/l3color.dtx
+++ b/l3experimental/l3color/l3color.dtx
@@ -237,14 +237,14 @@
 %   and similar are not influenced by this setting.
 % \end{variable}
 %
-% \section{Core color representation}
+% \section{Internal color representation}
 %
 % To allow data to be handled internally, \pkg{l3color} uses a simple
 % representation of color, comprising two \meta{balanced text} entries, the first
 % the \meta{model} and the second the \meta{values} given
 % \emph{separated by spaces}.
 %
-% This core representation is produced when parsing color expressions.
+% This internal representation is produced when parsing color expressions.
 % 
 % \begin{function}{\color_parse:nN}
 %   \begin{syntax}
@@ -256,6 +256,38 @@
 %   representation).
 % \end{function}
 %
+% \section{Exporting color specifications}
+%
+% The major use of color expressions is in setting typesetting output, but there
+% are other places in which some form of color information is required. These
+% may need data in a different format or using a different model to the internal
+% representation. Thus a set of functions are available to export colors in
+% different formats.
+%
+% Valid export targets are
+% \begin{itemize}
+%    \item \texttt{HTML} Uppercase two-digit hexadecimal values, expressing
+%      a red-green-blue color; the digits are \emph{not} separated
+% \end{itemize}
+%
+% \begin{function}{\color_export:nnN}
+%   \begin{syntax}
+%     \cs{color_export:nnN} \Arg{color expression} \Arg{format} \Arg{tl}
+%   \end{syntax}
+%   Parses the \meta{color expression} as described for \cs{color_parse:nN},
+%   then converts to the \meta{format} specified and assigns the data to the
+%   \meta{tl}.
+% \end{function}
+%
+% \begin{function}{\color_export:nnN}
+%   \begin{syntax}
+%     \cs{color_export:nnnN} \Arg{model} \Arg{value(s)} \Arg{format} \Arg{tl}
+%   \end{syntax}
+%   Expresses the combination of \mea{model} and \meta{value(s)} in an
+%   internal representation, then converts to the \meta{format} specified and
+%   assigns the data to the \meta{tl}.
+% \end{function}
+%
 % \section{Spot colors}
 % \label{l3color:sec:spot}
 %
@@ -942,6 +974,76 @@
 %    \end{macrocode}
 % \end{variable}
 %
+% \subsection{Exporting colors}
+%
+% \begin{macro}{\color_export:nnN}
+% \begin{macro}{\color_export:nnnN}
+% \begin{macro}{\@@_export:nN}
+% \begin{macro}{\@@_export:nnnN}
+%    \begin{macrocode}
+\cs_new_protected:Npn \color_export:nnN #1#2#3
+  {
+    \color_parse:nN {#1} #3
+    \@@_export:nN {#2} #3
+  }
+\cs_new_protected:Npn \color_export:nnnN #1#2#3#4
+  {
+    \@@_direct:nnN {#1} {#2} #4
+    \@@_export:nN {#3} #4
+  }
+\cs_new_protected:Npn \@@_export:nN #1#2
+  { \exp_after:wN \@@_export:nnnN #2 {#1} #2 }
+\cs_new:Npn \@@_export:nnnN #1#2#3#4
+  {
+    \cs_if_exist_use:cF { @@_export_ #3 :nnN }
+      {
+        \__kernel_msg_error:nnn { color } { unknown-export-format } {#3}
+        \use_none:nnn
+      }
+        {#1} {#2} #4
+  }
+%    \end{macrocode}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+%
+% \begin{macro}{\@@_export_HTML:nnN}
+% \begin{macro}{\@@_export_HTML:Nw}
+% \begin{macro}[EXP]{\@@_export_HTML:n}
+%   \textsc{html} values must be given in |rgb|: we force conversion if
+%   required, then do some simple maths.
+%    \begin{macrocode}
+\cs_new_protected:Npn \@@_export_HTML:nnN #1#2#3
+  {
+    \str_if_eq:nnTF {#1} { rgb }
+      { \@@_export_HTML:Nw #3 #2 \s_@@_stop }
+      {
+        \@@_convert:nnnN {#1} { rgb } {#2} #3
+        \exp_after:wN \@@_export_HTML:Nw \exp_after:wN #3
+          #3 \s_@@_stop
+      }
+  }
+\cs_new_protected:Npn \@@_export_HTML:Nw #1#2 ~ #3 ~ #4 \s_@@_stop
+  {
+    \tl_set:Nx #1
+      {
+        \@@_export_HTML:n {#2}
+        \@@_export_HTML:n {#3}
+        \@@_export_HTML:n {#4}
+      }
+  }
+\cs_new:Npn \@@_export_HTML:n #1
+  {
+    \fp_compare:nNnTF {#1} = { 0 }
+      { 00 }
+      { \int_to_Hex:n { \fp_to_int:n { #1 * 255 } } }
+  }
+%    \end{macrocode}
+% \end{macro}
+% \end{macro}
+% \end{macro}
+%
 % \subsection{Diagnostics}
 %
 % \begin{macro}{\color_show:n}
@@ -979,6 +1081,12 @@
     LaTeX~has~been~asked~to~use~a~color~named~'#1',~
     but~this~has~never~been~defined.
   }
+\__kernel_msg_new:nnnn { color } { unknown-export-format }
+  { Unknown~export~format~'#1'. }
+  {
+    LaTeX~has~been~asked~to~export~a~color~in~format~'#1',~
+    but~this~has~never~been~defined.
+  }
 \__kernel_msg_new:nnnn { color } { unknown-model }
   { Unknown~color~model~'#1'. }
   {
diff --git a/l3experimental/l3color/testfiles/m3color002.lvt b/l3experimental/l3color/testfiles/m3color002.lvt
index 3dec4335b..95d5a16f4 100644
--- a/l3experimental/l3color/testfiles/m3color002.lvt
+++ b/l3experimental/l3color/testfiles/m3color002.lvt
@@ -48,4 +48,23 @@
     \color_show:n { foo2 }
   }
 
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+\TEST { Exporting~colors }
+  {
+    \color_export:nnN { blue } { HTML } \l_tmpa_tl
+    \tl_show:N \l_tmpa_tl
+    \color_export:nnN { blue!50!red } { HTML } \l_tmpa_tl
+    \tl_show:N \l_tmpa_tl
+    \color_export:nnN { cyan } { HTML } \l_tmpa_tl
+    \tl_show:N \l_tmpa_tl
+    \color_export:nnnN { gray } { 0.6 } { HTML } \l_tmpa_tl
+    \tl_show:N \l_tmpa_tl
+  }
+
+\TEST { Exporting~color:~errors }
+  {
+    \color_export:nnnN { gray } { 0.6 } { mumble } \l_tmpa_tl
+  }
+
 \END
diff --git a/l3experimental/l3color/testfiles/m3color002.tlg b/l3experimental/l3color/testfiles/m3color002.tlg
index 186c4b163..b86d41c05 100644
--- a/l3experimental/l3color/testfiles/m3color002.tlg
+++ b/l3experimental/l3color/testfiles/m3color002.tlg
@@ -66,3 +66,29 @@ The color foo2 has the properties:
 <recently read> }
 l. ...  }
 ============================================================
+============================================================
+TEST 3: Exporting colors
+============================================================
+> \l_tmpa_tl=0000FF.
+<recently read> }
+l. ...  }
+> \l_tmpa_tl=800080.
+<recently read> }
+l. ...  }
+> \l_tmpa_tl=00FFFF.
+<recently read> }
+l. ...  }
+> \l_tmpa_tl=999999.
+<recently read> }
+l. ...  }
+============================================================
+============================================================
+TEST 4: Exporting color: errors
+============================================================
+! LaTeX3 Error: Unknown export format 'mumble'.
+For immediate help type H <return>.
+ ...                                              
+l. ...  }
+LaTeX has been asked to export a color in format 'mumble', but this has never
+been defined.
+============================================================





More information about the latex3-commits mailing list.