Debian Codegraph @

texmf-dist/source/latex/l3kernel/l3color.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3color.dtx
4 %
5 % Copyright (C) 2017-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % http://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3kernel bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \documentclass[full]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 % \title{^^A
34 % The \pkg{l3color} module\\ Color support^^A
35 % }
36 %
37 % \author{^^A
38 % The \LaTeX{} Project\thanks
39 % {^^A
40 % E-mail:
41 % \href{mailto:[email protected]}
42 % {[email protected]}^^A
43 % }^^A
44 % }
45 %
46 % \date{Released 2026-05-15}
47 %
48 % \maketitle
49 %
50 % \begin{documentation}
51 %
52 % \section{Color in boxes}
53 %
54 % Controlling the color of text in boxes requires a small number of control
55 % functions, so that the boxed material uses the color at the point where
56 % it is set, rather than where it is used.
57 %
58 % \begin{function}[added = 2011-09-03]{\color_group_begin:, \color_group_end:}
59 % \begin{syntax}
60 % \cs{color_group_begin:}
61 % \ldots
62 % \cs{color_group_end:}
63 % \end{syntax}
64 % Creates a color group: one used to \enquote{trap} color settings.
65 % This grouping is built in to for example \cs{hbox_set:Nn}.
66 % \end{function}
67 %
68 % \begin{function}[added = 2011-09-03]{\color_ensure_current:}
69 % \begin{syntax}
70 % \cs{color_ensure_current:}
71 % \end{syntax}
72 % Ensures that material inside a box uses the foreground color
73 % at the point where the box is set, rather than that in force when the
74 % box is used. This function should usually be used within a
75 % \cs{color_group_begin:} \ldots \cs{color_group_end:} group.
76 % \end{function}
77 %
78 % \section{Color models}
79 %
80 % A color \emph{model} is a way to represent sets of colors. Different models
81 % are particularly suitable for different output methods, \emph{e.g.}~screen
82 % or print. Parameter-based models can describe a very large number of unique
83 % colors, and have a varying number of \emph{axes} which define a color
84 % space. In contrast, various proprietary models are available which define
85 % \emph{spot} colors (more formally separations).
86 %
87 % Core models are used to pass color information to output; these are
88 % \enquote{native} to \pkg{l3color}. Core models use real numbers in the range
89 % $[0,1]$ to represent values. The core models supported here are
90 % \begin{itemize}
91 % \item \texttt{gray} Grayscale color, with a single axis running from
92 % $0$ (fully black) to $1$ (fully white)
93 % \item \texttt{rgb} Red-green-blue color, with three axes, one for each of
94 % the components
95 % \item \texttt{cmyk} Cyan-magenta-yellow-black color, with four axes, one for
96 % each of the components
97 % \end{itemize}
98 %
99 % There are also interface models: these are convenient for users but have
100 % to be manipulated before storing/passing to the backend. Interface models
101 % are primarily integer-based: see below for more detail. The supported
102 % interface models are
103 % \begin{itemize}
104 % \item \texttt{Gray} Grayscale color, with a single axis running from
105 % $0$ (fully black) to $15$ (fully white)
106 % \item \texttt{hsb} Hue-saturation-brightness color, with three axes, all
107 % real values in the range $[0,1]$ for hue saturation and brightness
108 % \item \texttt{Hsb} Hue-saturation-brightness color, with three axes, integer
109 % in the range $[0,360]$ for hue, real values in the range $[0,1]$ for
110 % saturation and brightness
111 % \item \texttt{HSB} Hue-saturation-brightness color, with three axes, integers
112 % in the range $[0,240]$ for hue, saturation and brightness
113 % \item \texttt{HTML} HTML format representation of RGB color given as a
114 % single six-digit hexadecimal number
115 % \item \texttt{RGB} Red-green-blue color, with three axes, one for each of
116 % the components, values as integers from $0$ to $255$
117 % \item \texttt{oklch} Lightness-chromacity-hue color in the Oklab color space
118 % (\url{https://bottosson.github.io/posts/oklab}), which models human
119 % perception, with three axes, real values in the range $[0,1]$ for
120 % lightness, real values in the range $[0, 0.4]$ for chromacity and real
121 % values in the range $[0, 360]$ for hue\footnote{Be aware, that for now,
122 % this is an input model only. Color blending will not benefit from Oklab
123 % color space. It is advised to only input colors inside the sRGB gamut,
124 % i.e., mapping to $[0,1]^{3}$ in the core \texttt{rgb} model. Other colors
125 % will be crudely clipped to fit inside. Thus, for most hues and
126 % lightnesses, the chroma should actually remain below $0.2$.}
127 % \item \texttt{oklab} Oklab color, with three axes, real values in the range
128 % $[0,1]$ for lightness and real values in the range $[-0.4, 0.4]$ for the
129 % position on the green/red- and yellow/blue-axes^^A
130 % \addtocounter{footnote}{-1}\addtocounter{Hfootnote}{-1}\footnotemark
131 % \item \texttt{wave} Light wavelength, a real number in the range
132 % $380$ to $780$ (nanometres)
133 % \end{itemize}
134 % All interface models are internally stored as |rgb|.
135 %
136 % Finally, there are a small number of models which are parsed to allow
137 % data transfer from \pkg{xcolor} but which should not be used by end-users.
138 % These are
139 % \begin{itemize}
140 % \item \texttt{cmy} Cyan-magenta-yellow color with three axes, one for
141 % each of the components; converted to |cmyk|
142 % \item \texttt{tHsb} \enquote{Tuned} hue-saturation-brightness color with three
143 % axes, integer in the range $[0,360]$ for hue, real values in the range
144 % $[0,1]$ for saturation and brightness; converted to |rgb| using the
145 % standard tuning map defined by \pkg{xcolor}
146 % \item \texttt{\&spot} Spot color tint with one value; treated as a gray
147 % tint as spot color data is not available for extraction
148 % \end{itemize}
149 %
150 % To allow parsing of data from \pkg{xcolor}, any leading model up the first
151 % \texttt{:} will be discarded; the approach of selecting an internal form
152 % for data is \emph{not} used in \pkg{l3color}.
153 %
154 % Additional models may be created to allow mixing of separation colors
155 % with each other or with those from other models. See
156 % Section~\ref{l3color:sec:new-models} for more detail of color support
157 % for additional models.
158 %
159 % When color is selected by model, the \meta{value(s)} given are specified as
160 % a comma-separated list. The length of the list will therefore be determined
161 % by the detail of the model involved.
162 %
163 % Color models (and interconversion) are complex, and more details are given
164 % in the manual to the \LaTeXe{} \pkg{xcolor} package and in the
165 % \emph{PostScript Language Reference Manual}, published by Addison--Wesley.
166 %
167 % \section{Color expressions}
168 %
169 % In addition to allowing specification of color by model and values,
170 % \pkg{l3color} also supports color expressions. These are created
171 % by combining one or more color names, with the amount of each specified
172 % as a value in the range $0$--$100$. The value should be given between
173 % |!| symbols in the expression. Thus for example
174 % \begin{verbatim}
175 % red!50!green
176 % \end{verbatim}
177 % is a mixture of $50\,\%$ red and $50\,\%$ green. A trailing value is
178 % interpreted as implicitly followed by |!white|, and so
179 % \begin{verbatim}
180 % red!25
181 % \end{verbatim}
182 % specifies $25\,\%$ red mixed with $75\,\%$ white.
183 %
184 % Where the models for the mixed colors are different, the model of the first
185 % color is used. Thus
186 % \begin{verbatim}
187 % red!50!cyan
188 % \end{verbatim}
189 % will result in a color specification using the |rgb| model, made up of
190 % $50\,\%$ red and $50\,\%$ of cyan \emph{expressed in \texttt{rgb}}.
191 % This may be important as color model interconversion is not exact.
192 %
193 % The one exception to the above is where the first model in an expression is
194 % |gray|. In this case, the order of mixing is \enquote{swapped} internally, so
195 % that for example
196 % \begin{verbatim}
197 % black!50!red
198 % \end{verbatim}
199 % has the same result as
200 % \begin{verbatim}
201 % red!50!black
202 % \end{verbatim}
203 % (the predefined colors |black| and |white| use the |gray| model).
204 %
205 % Where more than two colors are mixed in an expression, evaluation takes place
206 % in a stepwise fashion. Thus in
207 % \begin{verbatim}
208 % cyan!50!magenta!10!yellow
209 % \end{verbatim}
210 % the sub-expression
211 % \begin{verbatim}
212 % cyan!50!magenta
213 % \end{verbatim}
214 % is first evaluated to give an intermediate color specification, before
215 % the second step
216 % \begin{verbatim}
217 % <intermediate>!10!yellow
218 % \end{verbatim}
219 % where |<intermediate>| represents this transitory calculated value.
220 %
221 % Within a color expression, |.| may be used to represent the color active
222 % for typesetting (the current color). This allows for example
223 % \begin{verbatim}
224 % .!50
225 % \end{verbatim}
226 % to mean a mixture of $50\,\%$ of current color with white.
227 %
228 % (Color expressions supported here are a subset of those provided by
229 % the \LaTeXe{} \pkg{xcolor} package. At present, only such features as are
230 % clearly useful have been added here.)
231 %
232 % \section{Named colors}
233 %
234 % Color names are stored in a single namespace, which makes them accessible
235 % as part of color expressions. Whilst they are not reserved in a technical
236 % sense, the names |black|, |white|, |red|, |green|, |blue|, |cyan|, |magenta|
237 % and |yellow| have special meaning and should not be redefined. Color names
238 % should be made up of letters, numbers and spaces only: other characters are
239 % reserved for use in color expressions. In particular, |.| represents the
240 % current color at the start of a color expression.
241 %
242 % \begin{function}{\color_set:nn}
243 % \begin{syntax}
244 % \cs{color_set:nn} \Arg{name} \Arg{color expression}
245 % \end{syntax}
246 % Evaluates the \meta{color expression} and stores the resulting
247 % color specification as the \meta{name}.
248 % \end{function}
249 %
250 % \begin{function}[updated = 2024-12-24]{\color_set:nnn}
251 % \begin{syntax}
252 % \cs{color_set:nnn} \Arg{name} \Arg{model(s)} \Arg{value(s)}
253 % \end{syntax}
254 % Stores the color specification equivalent to the \meta{model(s)} and
255 % \meta{value(s)} as the \meta{name}. The \meta{value(s)} are expanded before
256 % parsing.
257 % \end{function}
258 %
259 % \begin{function}{\color_set_eq:nn}
260 % \begin{syntax}
261 % \cs{color_set_eq:nn} \Arg{name_1} \Arg{name_2}
262 % \end{syntax}
263 % Copies the color specification in \meta{name_2} to \meta{name_1}. The
264 % special name |.| may be used to represent the current color, allowing
265 % it to be saved to a name.
266 % \end{function}
267 %
268 % \begin{function}[EXP, pTF, added = 2022-08-12]{\color_if_exist:n}
269 % \begin{syntax}
270 % \cs{color_if_exist_p:n} \Arg{name}
271 % \cs{color_if_exist:nTF} \Arg{name} \Arg{true code} \Arg{false code}
272 % \end{syntax}
273 % Tests whether \meta{name} is currently defined to provide a color
274 % specification.
275 % \end{function}
276 %
277 % \begin{function}[added = 2021-05-11]{\color_show:n, \color_log:n}
278 % \begin{syntax}
279 % \cs{color_show:n} \Arg{name}
280 % \cs{color_log:n} \Arg{name}
281 % \end{syntax}
282 % Displays the color specification stored in the \meta{name} on the
283 % terminal or log file.
284 % \end{function}
285 %
286 % \section{Selecting colors}
287 %
288 % General selection of color is safe when split across pages: a stack is
289 % used to ensure that the correct color is re-selected on the new page.
290 %
291 % These commands set the current color (|.|): other more specialized functions
292 % such as fill and stroke selectors do \emph{not} adjust this value.
293 %
294 % \begin{function}[updated = 2024-12-24]{\color_select:n, \color_select:V}
295 % \begin{syntax}
296 % \cs{color_select:n} \Arg{color expression}
297 % \end{syntax}
298 % Parses the \meta{color expression} and then activates the resulting
299 % color specification for typeset material.
300 % \end{function}
301 %
302 % \begin{function}
303 % {\color_select:nn, \color_select:nV, \color_select:Vn, \color_select:VV}
304 % \begin{syntax}
305 % \cs{color_select:nn} \Arg{model(s)} \Arg{value(s)}
306 % \end{syntax}
307 % Activates the color specification equivalent to the \meta{model(s)} and
308 % \meta{value(s)} for typeset material. The \meta{value(s)} are fully
309 % expanded before parsing.
310 % \end{function}
311 %
312 % \begin{variable}{\l_color_fixed_model_tl}
313 % When this is set to a non-empty value, colors will be converted to
314 % the specified model when they are selected. Note that included images
315 % and similar are not influenced by this setting.
316 % \end{variable}
317 %
318 % \section{Colors for fills and strokes}
319 %
320 % Colors for drawing operations and so forth are split into strokes and fills
321 % (the latter may also be referred to as non-stroke color). The fill color is
322 % used for text under normal circumstances. Depending on the backend, stroke
323 % color may use a \emph{stack}, in which case it exhibits the same page breaking
324 % behavior as general color. However, \texttt{dvips}/\texttt{dvisvgm} do not
325 % support this, and so color will need to be contained within a scope, such
326 % as \cs{draw_begin:}/\cs{draw_end:}.
327 %
328 % \begin{function}{\color_fill:n, \color_stroke:n}
329 % \begin{syntax}
330 % \cs{color_fill:n} \Arg{color expression}
331 % \end{syntax}
332 % Parses the \meta{color expression} and then activates the resulting
333 % color specification for filling or stroking.
334 % \end{function}
335 %
336 % \begin{function}[updated = 2024-12-24]{\color_fill:nn, \color_stroke:nn}
337 % \begin{syntax}
338 % \cs{color_fill:nn} \Arg{model(s)} \Arg{value(s)}
339 % \end{syntax}
340 % Activates the color specification equivalent to the \meta{model(s)} and
341 % \meta{value(s)} for filling or stroking. The \meta{value(s)} are fully
342 % expanded before parsing.
343 % \end{function}
344 %
345 % \begin{variable}[module = color]{color.sc}
346 % When using \texttt{dvips}, this PostScript variable holds the stroke color.
347 % \end{variable}
348 %
349 % \subsection{Coloring math mode material}
350 %
351 % Coloring math mode material using \cs[no-index]{color_select:nn(n)} has some restrictions
352 % and often leads to spacing issues and/or poor input syntax. Avoiding generating
353 % \tn{mathord} atoms whilst coloring only those parts of the input which are
354 % required needs careful handling. The functionality here covers this important
355 % use case.
356 %
357 % \begin{function}[added = 2022-01-26, updated = 2024-12-24]
358 % {\color_math:nn, \color_math:nnn}
359 % \begin{syntax}
360 % \cs{color_math:nn} \Arg{color expression} \Arg{content}
361 % \cs{color_math:nnn} \Arg{model(s)} \Arg{value(s)} \Arg{content}
362 % \end{syntax}
363 % Works as for \cs[no-index]{color_select:n(n)} but applies color only to the math mode
364 % \meta{content}. The \meta{value(s)} are fully expanded before parsing.
365 % The function does not generate a group and the \meta{content}
366 % therefore retains its math atom states. Sub/superscripts are also properly
367 % handled.
368 % \end{function}
369 %
370 % \begin{variable}[added = 2022-01-26]{\l_color_math_active_tl}
371 % This list controls which tokens are considered as math active and
372 % should therefore be replaced by their definition during searching for
373 % sub/superscripts.
374 % \end{variable}
375 %
376 % \section{Multiple color models}
377 %
378 % When selecting or setting a color with an explicit model, it is possible
379 % to give values for more than one model at one time. This is particularly
380 % useful where automated conversion between models does not give the desired
381 % outcome. To do this, the list of models and list of values are both subdivided
382 % using |/| characters (as for the similar function in \pkg{xcolor}). For
383 % example, to save a color with explicit |cmyk| and |rgb| values, one could
384 % use
385 % \begin{verbatim}
386 % \color_set:nnn { foo } { cmyk / rgb }
387 % { 0.1 , 0.2 , 0.3 , 0.4 / 0.1, 0.2 , 0.3 }
388 % \end{verbatim}
389 % The manually-specified conversion will be used in preference to automated
390 % calculation whenever the model(s) listed are used: both in expressions and
391 % when a fixed model is active.
392 %
393 % Similarly, the same syntax can be applied to directly selecting a color.
394 % \begin{verbatim}
395 % \color_select:nn { cmyk / rgb }
396 % { 0.1 , 0.2 , 0.3 , 0.4 / 0.1, 0.2 , 0.3 }
397 % \end{verbatim}
398 % Again, this list is used when a fixed model is active: the first entry is used
399 % unless there is a fixed model matching one of the other entries.
400 %
401 % \section{Exporting color specifications}
402 %
403 % The major use of color expressions is in setting typesetting output, but there
404 % are other places in which some form of color information is required. These
405 % may need data in a different format or using a different model to the internal
406 % representation. Thus a set of functions are available to export colors in
407 % different formats.
408 %
409 % Valid export targets are
410 % \begin{itemize}
411 % \item \texttt{backend} Two brace groups: the first containing the
412 % model, the second containing space-separated values appropriate
413 % for the model; this is the format required by backend functions
414 % of \pkg{expl3}
415 % \item \texttt{comma-sep-cmyk} Comma-separated cyan-magenta-yellow-black
416 % values
417 % \item \texttt{comma-sep-rgb} Comma-separated red-green-blue values
418 % suitable for use as a PDF annotation color
419 % \item \texttt{HTML} Uppercase two-digit hexadecimal values, expressing
420 % a red-green-blue color; the digits are \emph{not} separated
421 % \item \texttt{space-sep-cmyk} Space-separated cyan-magenta-yellow-black
422 % values
423 % \item \texttt{space-sep-rgb} Space-separated red-green-blue values
424 % suitable for use as a PDF annotation color
425 % \end{itemize}
426 %
427 % \begin{function}{\color_export:nnN}
428 % \begin{syntax}
429 % \cs{color_export:nnN} \Arg{color expression} \Arg{format} \meta{tl var}
430 % \end{syntax}
431 % Parses the \meta{color expression} as described earlier,
432 % then converts to the \meta{format} specified and assigns the data to the
433 % \meta{tl var}.
434 % \end{function}
435 %
436 % \begin{function}[updated = 2024-12-24]{\color_export:nnnN}
437 % \begin{syntax}
438 % \cs{color_export:nnnN} \Arg{model} \Arg{value(s)} \Arg{format} \meta{tl var}
439 % \end{syntax}
440 % Expresses the combination of \meta{model} and \meta{value(s)} in an
441 % internal representation, then converts to the \meta{format} specified and
442 % assigns the data to the \meta{tl var}. The \meta{value(s)} are fully
443 % expanded before parsing.
444 % \end{function}
445 %
446 % \section{Creating new color models}
447 % \label{l3color:sec:new-models}
448 %
449 % Additional color models are required to support specialist workflows, for
450 % example those involving separations (see
451 % \url{https://helpx.adobe.com/indesign/using/spot-process-colors.html}
452 % for details of the use of separations in print). Color models may be split
453 % into families; for the standard device-based color models (\texttt{DeviceCMYK},
454 % \texttt{DeviceRGB}, \texttt{DeviceGray}), these are synonymous. This
455 % is not generally the case: see the PDF reference for more details. (Note that
456 % \pkg{l3color} uses the shorter names \texttt{cmyk}, etc.)
457 %
458 % \begin{function}{\color_model_new:nnn}
459 % \begin{syntax}
460 % \cs{color_model_new:nnn} \Arg{model} \Arg{family} \Arg{params}
461 % \end{syntax}
462 % Creates a new \meta{model} which is derived from the color model \meta{family}.
463 % The latter should be one of
464 % \begin{itemize}
465 % \item \texttt{DeviceN}
466 % \item \texttt{ICCBased}
467 % \item \texttt{Separation}
468 % \end{itemize}
469 % (The \meta{family} may be given in mixed case as-in the PDF reference:
470 % internally, case of these strings is folded.)
471 % Depending on the \meta{family}, one or more \meta{params} are mandatory or
472 % optional.
473 % \end{function}
474 %
475 % For a \texttt{Separation} space, there are three \emph{compulsory} keys.
476 % \begin{itemize}
477 % \item \texttt{name} The name of the Separation, for example the formal
478 % name of a spot color ink. Such a \meta{name} may contain spaces, etc.,
479 % which are not permitted in the \meta{model}.
480 % \item \texttt{alternative-model} An alternative device colorspace, one of
481 % \texttt{cmyk}, \texttt{rgb}, \texttt{gray} or \texttt{CIELAB}. The three
482 % parameter-based models work as described above; see below for
483 % details of CIELAB colors.
484 % \item \texttt{alternative-values} A comma-separated list of values
485 % appropriate to the \texttt{alternative-model}. This information is used by
486 % the PDF application if the \texttt{Separation} is not available.
487 % \end{itemize}
488 %
489 % CIELAB color separations are created using the
490 % \texttt{alternative-model = CIELAB} setting. These colors must also have an
491 % \texttt{illuminant} key, one of \texttt{a}, \texttt{c}, \texttt{e},
492 % \texttt{d50}, \texttt{d55}, \texttt{d65} or \texttt{d75}. The
493 % \texttt{alternative-values} in this case are the three parameters $L*$, $a*$
494 % and $b*$ of the CIELAB model. Full details of this device-independent color
495 % approach are given in the documentation to the \pkg{colorspace} package.
496 %
497 % CIELAB colors \emph{cannot} be converted into other device-dependent color
498 % spaces, and as such, mixing can only occur if colors set up using the CIELAB
499 % model are also given with an alternative parameter-based model. If that is
500 % not the case, \pkg{l3color} will fallback to using black as the colorant in
501 % any mixing.
502 %
503 % For a \texttt{DeviceN} space, there is one \emph{compulsory} key.
504 % \begin{itemize}
505 % \item \texttt{names} The names of the components of the \texttt{DeviceN}
506 % space. Each should be either the \meta{name} of a \texttt{Separation} model,
507 % a process color name (\texttt{cyan}, etc.) or the special name \texttt{none}.
508 % \end{itemize}
509 %
510 % For a \texttt{ICCBased} space, there is one \emph{compulsory} key.
511 % \begin{itemize}
512 % \item \texttt{file} The name of the file containing the profile.
513 % \end{itemize}
514 %
515 % \subsection{Color profiles}
516 %
517 % Color profiles are used to ensure color accuracy by linking to collaboration.
518 % Applying a profile can be used to standardize color which is otherwise
519 % device-dependent.
520 %
521 % \begin{function}[added = 2021-02-23]{\color_profile_apply:nn}
522 % \begin{syntax}
523 % \cs{color_profile_apply:nn} \Arg{profile} \Arg{model}
524 % \end{syntax}
525 % This function applies a \meta{profile} to one of the device \meta{models}.
526 % The profile will then apply to all color of the selected \meta{model}. The
527 % \meta{profile} should specify an ICC profile file. The \meta{model} has to
528 % be one the standard device models: \texttt{cmyk}, \texttt{gray} or
529 % \texttt{rgb}.
530 % \end{function}
531 %
532 % \end{documentation}
533 %
534 % \begin{implementation}
535 %
536 % \section{\pkg{l3color} implementation}
537 %
538 % \begin{macrocode}
539 %<*code>
540 % \end{macrocode}
541 %
542 % \begin{macrocode}
543 %<@@=color>
544 % \end{macrocode}
545 %
546 % \subsection{Basics}
547 %
548 % \begin{variable}
549 % {\l_@@_current_tl}
550 % The color currently active for foreground (text, etc.) material.
551 % This is stored in the form of a color model followed by one or more
552 % values. There are four pre-defined models, three of which take numerical
553 % values in the range $[0,1]$:
554 % \begin{itemize}
555 % \item \texttt{gray \meta{gray}} Grayscale color with the \meta{gray}
556 % value running from $0$ (fully black) to $1$ (fully white)
557 % \item \texttt{cmyk \meta{cyan} \meta{magenta} \meta{yellow} \meta{black}}
558 % \item \texttt{rgb \meta{red} \meta{green} \meta{blue}}
559 % \end{itemize}
560 % Notice that the value are separated by spaces. There is a fourth pre-defined
561 % model using a string value and a numerical one:
562 % \begin{itemize}
563 % \item \texttt{spot \meta{name} \meta{tint}} A pre-defined spot color,
564 % where the \meta{name} should be a pre-defined string color name and the
565 % \meta{tint} should be in the range $[0,1]$.
566 % \end{itemize}
567 %
568 % Additional models may be created to allow mixing of spot colors. The
569 % number of data entries these require will depend on the number of
570 % colors to be mixed.
571 % \begin{texnote}
572 % The content of \cs{l_@@_current_tl} comprises two brace groups, the
573 % first containing the color model and the second containing the value(s)
574 % applicable in that model.
575 % \end{texnote}
576 % \end{variable}
577 %
578 % \begin{macro}{\color_group_begin:, \color_group_end:}
579 % Grouping for color is the same as using the basic \cs{group_begin:}
580 % and \cs{group_end:} functions. However, for semantic reasons, they
581 % are renamed here.
582 % \begin{macrocode}
583 \cs_new_eq:NN \color_group_begin: \group_begin:
584 \cs_new_eq:NN \color_group_end: \group_end:
585 % \end{macrocode}
586 % \end{macro}
587 %
588 % \begin{macro}{\color_ensure_current:}
589 % A driver-independent wrapper for setting the foreground color to the
590 % current color \enquote{now}.
591 % \begin{macrocode}
592 \cs_new_protected:Npn \color_ensure_current:
593 { \@@_select:N \l_@@_current_tl }
594 % \end{macrocode}
595 % \end{macro}
596 %
597 % \begin{variable}{\s_@@_stop}
598 % Internal scan marks.
599 % \begin{macrocode}
600 \scan_new:N \s_@@_stop
601 % \end{macrocode}
602 % \end{variable}
603 %
604 % \begin{macro}{\@@_select:N, \@@_select_math:N}
605 % \begin{macro}{\@@_select:nn}
606 % Take an internal color specification and pass it to the driver. This code
607 % is needed to ensure the current color but will also be used by the
608 % higher-level material.
609 % \begin{macrocode}
610 \cs_new_protected:Npn \@@_select:N #1
611 {
612 \exp_after:wN \@@_select:nn #1
613 \group_insert_after:N \@@_backend_reset:
614 }
615 \cs_new_protected:Npn \@@_select_math:N #1
616 { \exp_after:wN \@@_select:nn #1 }
617 \cs_new_protected:Npn \@@_select:nn #1#2
618 { \use:c { @@_backend_select_ #1 :n } {#2} }
619 % \end{macrocode}
620 % \end{macro}
621 % \end{macro}
622 %
623 % \begin{variable}{\l_@@_current_tl}
624 % The current color, with the model and
625 % \begin{macrocode}
626 \tl_new:N \l_@@_current_tl
627 \tl_set:Nn \l_@@_current_tl { { gray } { 0 } }
628 % \end{macrocode}
629 % \end{variable}
630 %
631 % \subsection{Predefined color names}
632 %
633 % The ability to predefine colors with a name is a key part of this module and
634 % means there has to be a method for storing the results. At first sight, it
635 % seems natural to follow the usual \pkg{expl3} model and create a
636 % \texttt{color} variable type for the process. That would then allow both
637 % local and global colors, constant colors and the like. However, these names
638 % need to be accessible in some form at the user level, for selection of colors
639 % either simply by name or as part of a more complex expression. This does not
640 % require that the full name is exposed but does require that they can be
641 % looked up in a predictable way. As such, it is more useful to expose just the
642 % color names as part of the interface, with the result that only local color
643 % names can be created. (This is also seen for example in key creation in
644 % \pkg{l3keys}.) As a result, color names are declarative (no \texttt{new}
645 % functions).
646 %
647 % Since there is no need to manipulate colors \emph{en masse}, each is stored
648 % in a two-part structure: a \texttt{prop} for the colors themselves, and a
649 % \texttt{tl} for the default model for each color.
650 %
651 % \subsection{Setup}
652 %
653 % \begin{variable}{\l_@@_tmp_int, \l_@@_tmp_tl}
654 % \begin{macrocode}
655 \int_new:N \l_@@_tmp_int
656 \tl_new:N \l_@@_tmp_tl
657 % \end{macrocode}
658 % \end{variable}
659 %
660 % \begin{variable}{\s_@@_mark}
661 % Internal scan marks. \cs{s_@@_stop} is already defined in \pkg{l3color-base}.
662 % \begin{macrocode}
663 \scan_new:N \s_@@_mark
664 % \end{macrocode}
665 % \end{variable}
666 %
667 % \begin{variable}{\l_@@_ignore_error_bool}
668 % Used to avoid issuing multiple errors if there is a change-of-model with
669 % input container an error.
670 % \begin{macrocode}
671 \bool_new:N \l_@@_ignore_error_bool
672 % \end{macrocode}
673 % \end{variable}
674 %
675 % \subsection{Utility functions}
676 %
677 % \begin{macro}[pTF, EXP]{\color_if_exist:n}
678 % A simple wrapper to avoid needing to have the lookup repeated in too many
679 % places.To guard against a color created in a group, we need to test for
680 % entries in the |prop|.
681 % \begin{macrocode}
682 \prg_new_conditional:Npnn \color_if_exist:n #1 { p , T, F, TF }
683 {
684 \prop_if_exist:cTF { l_@@_named_ #1 _prop }
685 {
686 \prop_if_empty:cTF { l_@@_named_ #1 _prop }
687 \prg_return_false:
688 \prg_return_true:
689 }
690 \prg_return_false:
691 }
692 % \end{macrocode}
693 % \end{macro}
694 %
695 % \begin{macro}[EXP]{\@@_model:N, \@@_values:N}
696 % Simple abstractions.
697 % \begin{macrocode}
698 \cs_new:Npn \@@_model:N #1 { \exp_after:wN \use_i:nn #1 }
699 \cs_new:Npn \@@_values:N #1 { \exp_after:wN \use_ii:nn #1 }
700 % \end{macrocode}
701 % \end{macro}
702 %
703 % \begin{macro}{\@@_extract:nNN, \@@_extract:VNN}
704 % Recover the values for the standard model for a color.
705 % \begin{macrocode}
706 \cs_new_protected:Npn \@@_extract:nNN #1#2#3
707 {
708 \tl_set_eq:Nc #2 { l_@@_named_ #1 _tl }
709 \prop_get:cVN { l_@@_named_ #1 _prop } #2 #3
710 }
711 \cs_generate_variant:Nn \@@_extract:nNN { V }
712 % \end{macrocode}
713 % \end{macro}
714 %
715 % \subsection{Model conversion}
716 %
717 % \begin{macro}{\@@_convert:nnN, \@@_convert:VVN}
718 % \begin{macro}{\@@_convert:nnnN, \@@_convert:nVnN, \@@_convert:nnVN}
719 % \begin{macro}[EXP]
720 % {
721 % \@@_convert_gray_gray:w,
722 % \@@_convert_gray_rgb:w,
723 % \@@_convert_gray_cmyk:w,
724 % \@@_convert_cmyk_gray:w,
725 % \@@_convert_cmyk_rgb:w,
726 % \@@_convert_cmyk_cmyk:w,
727 % \@@_convert_rgb_gray:w,
728 % \@@_convert_rgb_rgb:w,
729 % \@@_convert_rgb_cmyk:w
730 % }
731 % \begin{macro}[EXP]{\@@_convert_rgb_cmyk:nnn}
732 % \begin{macro}[EXP]{\@@_convert_rgb_cmyk:nnnn}
733 % Model conversion is carried out using standard formulae for base models,
734 % as described in the manual for \pkg{xcolor} (see also the \emph{PostScript
735 % Language Reference Manual}). For other models direct conversion might not
736 % be defined, so we go through the fallback models if necessary.
737 % \begin{macrocode}
738 \cs_new_protected:Npn \@@_convert:nnN #1#2#3
739 { \@@_convert:nnVN {#1} {#2} #3 #3 }
740 \cs_generate_variant:Nn \@@_convert:nnN { VV }
741 \cs_generate_variant:Nn \exp_last_unbraced:Nf { c }
742 \cs_new_protected:Npn \@@_convert:nnnN #1#2#3#4
743 {
744 \tl_set:Ne #4
745 {
746 \cs_if_exist_use:cTF { @@_convert_ #1 _ #2 :w }
747 { #3 \s_@@_stop }
748 {
749 \cs_if_exist:cTF { @@_convert_ \use:c { c_@@_fallback_ #1 _tl } _ #2 :w }
750 {
751 \exp_last_unbraced:cf
752 { @@_convert_ \use:c { c_@@_fallback_ #1 _tl } _ #2 :w }
753 { \use:c { @@_convert_ #1 _ \use:c { c_@@_fallback_ #1 _tl } :w } #3 \s_@@_stop }
754 \s_@@_stop
755 }
756 {
757 \exp_last_unbraced:cf
758 { @@_convert_ \use:c { c_@@_fallback_ #2 _tl } _ #2 :w }
759 {
760 \cs_if_exist_use:cTF { @@_convert_ #1 _ \use:c { c_@@_fallback_ #2 _tl } :w }
761 { #3 \s_@@_stop }
762 {
763 \exp_last_unbraced:cf
764 { @@_convert_ \use:c { c_@@_fallback_ #1 _tl } _ \use:c { c_@@_fallback_ #2 _tl } :w }
765 { \use:c { @@_convert_ #1 _ \use:c { c_@@_fallback_ #1 _tl } :w } #3 \s_@@_stop }
766 \s_@@_stop
767 }
768 }
769 \s_@@_stop
770 }
771 }
772 }
773 }
774 \cs_generate_variant:Nn \@@_convert:nnnN { nV , nnV }
775 \cs_new:Npn \@@_convert_gray_gray:w #1 \s_@@_stop
776 { #1 }
777 \cs_new:Npn \@@_convert_gray_rgb:w #1 \s_@@_stop
778 { #1 ~ #1 ~ #1 }
779 \cs_new:Npn \@@_convert_gray_cmyk:w #1 \s_@@_stop
780 { 0 ~ 0 ~ 0 ~ \fp_eval:n { 1 - #1 } }
781 % \end{macrocode}
782 % These rather odd values are based on \textsc{ntsc} television: the set are
783 % used for the |cmyk| conversion.
784 % \begin{macrocode}
785 \cs_new:Npn \@@_convert_rgb_gray:w #1 ~ #2 ~ #3 \s_@@_stop
786 { \fp_eval:n { 0.3 * #1 + 0.59 * #2 + 0.11 * #3 } }
787 \cs_new:Npn \@@_convert_rgb_rgb:w #1 \s_@@_stop
788 { #1 }
789 % \end{macrocode}
790 % The conversion from |rgb| to |cmyk| is the most complex: a two-step
791 % procedure which requires \emph{black generation} and \emph{undercolor
792 % removal} functions. The PostScript reference describes them as
793 % device-dependent, but following \pkg{xcolor} we assume they are linear.
794 % Moreover, as the likelihood of anyone using a non-unitary matrix here is
795 % tiny, we simplify and treat those two concepts as no-ops. To allow code
796 % sharing with parsing of |cmy| values, we have an intermediate function
797 % here (\cs{@@_convert_rgb_cmyk:nnn}) which actually takes |cmy| values
798 % as input.
799 % \begin{macrocode}
800 \cs_new:Npn \@@_convert_rgb_cmyk:w #1 ~ #2 ~ #3 \s_@@_stop
801 {
802 \exp_args:Neee \@@_convert_rgb_cmyk:nnn
803 { \fp_eval:n { 1 - #1 } }
804 { \fp_eval:n { 1 - #2 } }
805 { \fp_eval:n { 1 - #3 } }
806 }
807 \cs_new:Npn \@@_convert_rgb_cmyk:nnn #1#2#3
808 {
809 \exp_args:Ne \@@_convert_rgb_cmyk:nnnn
810 { \fp_eval:n { min( #1, #2 , #3 ) } } {#1} {#2} {#3}
811 }
812 \cs_new:Npn \@@_convert_rgb_cmyk:nnnn #1#2#3#4
813 {
814 \fp_eval:n { min ( 1 , max ( 0 , #2 - #1 ) ) } \c_space_tl
815 \fp_eval:n { min ( 1 , max ( 0 , #3 - #1 ) ) } \c_space_tl
816 \fp_eval:n { min ( 1 , max ( 0 , #4 - #1 ) ) } \c_space_tl
817 #1
818 }
819 \cs_new:Npn \@@_convert_cmyk_gray:w #1 ~ #2 ~ #3 ~ #4 \s_@@_stop
820 { \fp_eval:n { 1 - min ( 1 , 0.3 * #1 + 0.59 * #2 + 0.11 * #3 + #4 ) } }
821 \cs_new:Npn \@@_convert_cmyk_rgb:w #1 ~ #2 ~ #3 ~ #4 \s_@@_stop
822 {
823 \fp_eval:n { 1 - min ( 1 , #1 + #4 ) } \c_space_tl
824 \fp_eval:n { 1 - min ( 1 , #2 + #4 ) } \c_space_tl
825 \fp_eval:n { 1 - min ( 1 , #3 + #4 ) }
826 }
827 \cs_new:Npn \@@_convert_cmyk_cmyk:w #1 \s_@@_stop
828 { #1 }
829 % \end{macrocode}
830 % \end{macro}
831 % \end{macro}
832 % \end{macro}
833 % \end{macro}
834 % \end{macro}
835 %
836 % \subsection{Color expressions}
837 %
838 % \begin{variable}
839 % {\l_@@_model_tl, \l_@@_value_tl, \l_@@_next_model_tl, \l_@@_next_value_tl}
840 % Working space to store the color data whilst doing calculations: keeping
841 % it on the stack is attractive but gets tricky (return is non-trivial).
842 % \begin{macrocode}
843 \tl_new:N \l_@@_model_tl
844 \tl_new:N \l_@@_value_tl
845 \tl_new:N \l_@@_next_model_tl
846 \tl_new:N \l_@@_next_value_tl
847 % \end{macrocode}
848 % \end{variable}
849 %
850 % \begin{macro}{\@@_parse:nN}
851 % \begin{macro}{\@@_parse_aux:nN}
852 % \begin{macro}{\@@_parse_eq:Nn}
853 % \begin{macro}{\@@_parse_eq:nNn}
854 % \begin{macro}{\@@_parse:Nw}
855 % \begin{macro}{\@@_parse_loop_init:Nnn}
856 % \begin{macro}{\@@_parse_loop:w}
857 % \begin{macro}{\@@_parse_loop_check:nn}
858 % \begin{macro}{\@@_parse_loop:nn}
859 % \begin{macro}{\@@_parse_gray:n, \@@_parse_std:n}
860 % \begin{macro}{\@@_parse_break:w}
861 % \begin{macro}{\@@_parse_end:}
862 % \begin{macro}[EXP]{\@@_parse_mix:Nnnn, \@@_parse_mix:NVVn}
863 % \begin{macro}[EXP]{\@@_parse_mix:nNnn}
864 % \begin{macro}[EXP]
865 % {
866 % \@@_parse_mix_gray:nw ,
867 % \@@_parse_mix_rgb:nw ,
868 % \@@_parse_mix_cmyk:nw
869 % }
870 % The main function for parsing color expressions removes actives but
871 % otherwise expands, then starts working through the expression itself.
872 % At the end, we apply the payload.
873 % \begin{macrocode}
874 \cs_new_protected:Npe \@@_parse:nN #1#2
875 {
876 \tl_set:Ne \exp_not:c { l_@@_named_ . _tl }
877 { \exp_not:N \@@_model:N \exp_not:N \l_@@_current_tl }
878 \prop_put:NVe \exp_not:c { l_@@_named_ . _prop }
879 \exp_not:c { l_@@_named_ . _tl }
880 { \exp_not:N \@@_values:N \exp_not:N \l_@@_current_tl }
881 \exp_not:N \exp_args:Ne \exp_not:N \@@_parse_aux:nN
882 { \exp_not:N \tl_to_str:n {#1} } #2
883 }
884 % \end{macrocode}
885 % Before going to all of the effort of parsing an expression, these two
886 % precursor functions look for a pre-defined name, either on its own or
887 % with a trailing |!| (which is the same thing).
888 % \begin{macrocode}
889 \cs_new_protected:Npn \@@_parse_aux:nN #1#2
890 {
891 \color_if_exist:nTF {#1}
892 { \@@_parse_set_eq:Nn #2 {#1} }
893 { \@@_parse:Nw #2#1 ! \s_@@_stop }
894 \@@_check_model:N #2
895 }
896 \cs_new_protected:Npn \@@_parse_set_eq:Nn #1#2
897 {
898 \tl_if_empty:NTF \l_color_fixed_model_tl
899 { \exp_args:Nv \@@_parse_set_eq:nNn { l_@@_named_ #2 _tl } }
900 { \exp_args:NV \@@_parse_set_eq:nNn \l_color_fixed_model_tl }
901 #1 {#2}
902 }
903 % \end{macrocode}
904 % Here, we have to allow for the case where there is a fixed model:
905 % that can't be swept up by generic conversion as we are dealing with a
906 % named color.
907 % \begin{macrocode}
908 \cs_new_protected:Npn \@@_parse_set_eq:nNn #1#2#3
909 {
910 \prop_get:cnNTF
911 { l_@@_named_ #3 _prop } {#1}
912 \l_@@_value_tl
913 { \tl_set:Ne #2 { {#1} { \l_@@_value_tl } } }
914 {
915 \tl_set_eq:Nc \l_@@_model_tl { l_@@_named_ #3 _tl }
916 \prop_get:cVN { l_@@_named_ #3 _prop } \l_@@_model_tl
917 \l_@@_value_tl
918 \@@_convert:nnN
919 \l_@@_model_tl {#1} \l_@@_value_tl
920 \tl_set:Ne #2
921 {
922 {#1}
923 { \l_@@_value_tl }
924 }
925 }
926 }
927 \cs_new_protected:Npn \@@_parse:Nw #1#2 ! #3 \s_@@_stop
928 {
929 \color_if_exist:nTF {#2}
930 {
931 \tl_if_blank:nTF {#3}
932 { \@@_parse_set_eq:Nn #1 {#2} }
933 { \@@_parse_loop_init:Nnn #1 {#2} {#3} }
934 }
935 {
936 \msg_error:nnn { color } { unknown-color } {#2}
937 \tl_set:Nn \l_@@_current_tl { { gray } { 0 } }
938 }
939 }
940 % \end{macrocode}
941 % Once we establish that a full parse is needed, the next job is to get the
942 % detail of the first color. That will determine the model we use for the
943 % calculation: splitting here makes checking that a bit easier.
944 % \begin{macrocode}
945 \cs_new_protected:Npn \@@_parse_loop_init:Nnn #1#2#3
946 {
947 \group_begin:
948 \@@_extract:nNN {#2} \l_@@_model_tl \l_@@_value_tl
949 \@@_parse_loop:w #3 ! ! ! ! \s_@@_stop
950 \tl_set:Ne \l_@@_tmp_tl
951 { { \l_@@_model_tl } { \l_@@_value_tl } }
952 \exp_args:NNNV \group_end:
953 \tl_set:Nn #1 \l_@@_tmp_tl
954 }
955 % \end{macrocode}
956 % This is the loop proper: there can be an open-ended set of colors to parse,
957 % separated by |!| tokens. There are a few cases to look out for. At the end
958 % of the expression and with we find a mix of $100$ then we simply skip the
959 % next color entirely (we can't stop the loop as there might be a further
960 % valid color to mix in). On the other hand, if we get a mix of $0$ then
961 % drop everything so far and start again. There is also a trailing
962 % |white| to \enquote{read in} if the final explicit data is a mix.
963 % Those conditions are separate from actually looping, which is therefore
964 % sorted out by checking if we have further data to process: in contrast
965 % to \pkg{xcolor}, we don't allow |!!| so the test can be simplified.
966 % \begin{macrocode}
967 \cs_new_protected:Npn \@@_parse_loop:w #1 ! #2 ! #3 ! #4 ! #5 \s_@@_stop
968 {
969 \tl_if_blank:nF {#1}
970 {
971 \bool_lazy_and:nnTF
972 { \fp_compare_p:nNn {#1} > { 0 } }
973 { \fp_compare_p:nNn {#1} < { 100 } }
974 {
975 \use:e
976 {
977 \@@_parse_loop:nn {#1}
978 { \tl_if_blank:nTF {#2} { white } {#2} }
979 }
980 }
981 { \@@_parse_loop_check:nn {#1} {#2} }
982 }
983 \tl_if_blank:nF {#3}
984 { \@@_parse_loop:w #3 ! #4 ! #5 \s_@@_stop }
985 \@@_parse_end:
986 }
987 % \end{macrocode}
988 % As these are unusual cases, we accept slower performance here for clearer
989 % code: check for the error conditions, handle the boundary cases after
990 % that.
991 % \begin{macrocode}
992 \cs_new_protected:Npn \@@_parse_loop_check:nn #1#2
993 {
994 \bool_if:NF \l_@@_ignore_error_bool
995 {
996 \bool_lazy_or:nnT
997 { \fp_compare_p:nNn {#1} < { 0 } }
998 { \fp_compare_p:nNn {#1} > { 100 } }
999 { \msg_error:nnnnn { color } { out-of-range } {#1} { 0 } { 100 } }
1000 }
1001 \fp_compare:nNnF {#1} > \c_zero_fp
1002 {
1003 \tl_if_blank:nTF {#2}
1004 { \@@_extract:nNN { white } }
1005 { \@@_extract:nNN {#2} }
1006 \l_@@_model_tl \l_@@_value_tl
1007 }
1008 }
1009 % \end{macrocode}
1010 % The \enquote{payload} of calculation in the loop first. If the model for
1011 % the upcoming color is different from that of the existing (partial) color,
1012 % convert the model. For |gray| the two are flipped round so that the outcome
1013 % is something with \enquote{real} color. We are then in a position to do the
1014 % actual calculation itself. The two auxiliaries here give us a way to break
1015 % the loop should an invalid name be found.
1016 % \begin{macrocode}
1017 \cs_new_protected:Npn \@@_parse_loop:nn #1#2
1018 {
1019 \color_if_exist:nTF {#2}
1020 {
1021 \@@_extract:nNN {#2} \l_@@_next_model_tl \l_@@_next_value_tl
1022 \tl_if_eq:NNF \l_@@_model_tl \l_@@_next_model_tl
1023 {
1024 \str_if_eq:VnTF \l_@@_model_tl { gray }
1025 { \@@_parse_gray:n {#2} }
1026 { \@@_parse_std:n {#2} }
1027 }
1028 \tl_set:Ne \l_@@_value_tl
1029 {
1030 \@@_parse_mix:NVVn
1031 \l_@@_model_tl \l_@@_value_tl \l_@@_next_value_tl {#1}
1032 }
1033 }
1034 {
1035 \msg_error:nnn { color } { unknown-color } {#2}
1036 \@@_extract:nNN { black } \l_@@_model_tl \l_@@_value_tl
1037 \@@_parse_break:w
1038 }
1039 }
1040 % \end{macrocode}
1041 % The \texttt{gray} model needs special handling: the models need to be
1042 % swapped: we do that using a dedicated function.
1043 % \begin{macrocode}
1044 \cs_new_protected:Npn \@@_parse_gray:n #1
1045 {
1046 \tl_set_eq:NN \l_@@_model_tl \l_@@_next_model_tl
1047 \tl_set:Nn \l_@@_next_model_tl { gray }
1048 \exp_args:NnV \@@_convert:nnN { gray } \l_@@_model_tl
1049 \l_@@_value_tl
1050 \prop_get:cVN { l_@@_named_ #1 _prop } \l_@@_model_tl
1051 \l_@@_next_value_tl
1052 }
1053 \cs_new_protected:Npn \@@_parse_std:n #1
1054 {
1055 \prop_get:cVNF { l_@@_named_ #1 _prop }
1056 \l_@@_model_tl
1057 \l_@@_next_value_tl
1058 {
1059 \@@_convert:VVN
1060 \l_@@_next_model_tl
1061 \l_@@_model_tl
1062 \l_@@_next_value_tl
1063 }
1064 }
1065 \cs_new_protected:Npn \@@_parse_break:w #1 \@@_parse_end: { }
1066 \cs_new_protected:Npn \@@_parse_end: { }
1067 % \end{macrocode}
1068 % Do the vector arithmetic: mainly a question of shuffling input, along
1069 % with one pre-calculation to keep down the use of division.
1070 % \begin{macrocode}
1071 \cs_new:Npn \@@_parse_mix:Nnnn #1#2#3#4
1072 {
1073 \exp_args:Nf \@@_parse_mix:nNnn
1074 { \fp_eval:n { #4 / 100 } }
1075 #1 {#2} {#3}
1076 }
1077 \cs_generate_variant:Nn \@@_parse_mix:Nnnn { NVV }
1078 \cs_new:Npn \@@_parse_mix:nNnn #1#2#3#4
1079 {
1080 \use:c { @@_parse_mix_ #2 :nw } {#1}
1081 #3 \s_@@_mark #4 \s_@@_stop
1082 }
1083 \cs_new:Npn \@@_parse_mix_gray:nw #1#2 \s_@@_mark #3 \s_@@_stop
1084 { \fp_eval:n { #2 * #1 + #3 * ( 1 - #1 ) } }
1085 \cs_new:Npn \@@_parse_mix_rgb:nw
1086 #1#2 ~ #3 ~ #4 \s_@@_mark #5 ~ #6 ~ #7 \s_@@_stop
1087 {
1088 \fp_eval:n { #2 * #1 + #5 * ( 1 - #1 ) } \c_space_tl
1089 \fp_eval:n { #3 * #1 + #6 * ( 1 - #1 ) } \c_space_tl
1090 \fp_eval:n { #4 * #1 + #7 * ( 1 - #1 ) }
1091 }
1092 \cs_new:Npn \@@_parse_mix_cmyk:nw
1093 #1#2 ~ #3 ~ #4 ~ #5 \s_@@_mark #6 ~ #7 ~ #8 ~ #9 \s_@@_stop
1094 {
1095 \fp_eval:n { #2 * #1 + #6 * ( 1 - #1 ) } \c_space_tl
1096 \fp_eval:n { #3 * #1 + #7 * ( 1 - #1 ) } \c_space_tl
1097 \fp_eval:n { #4 * #1 + #8 * ( 1 - #1 ) } \c_space_tl
1098 \fp_eval:n { #5 * #1 + #9 * ( 1 - #1 ) }
1099 }
1100 % \end{macrocode}
1101 % \end{macro}
1102 % \end{macro}
1103 % \end{macro}
1104 % \end{macro}
1105 % \end{macro}
1106 % \end{macro}
1107 % \end{macro}
1108 % \end{macro}
1109 % \end{macro}
1110 % \end{macro}
1111 % \end{macro}
1112 % \end{macro}
1113 % \end{macro}
1114 % \end{macro}
1115 % \end{macro}
1116 %
1117 % \begin{macro}[EXP]
1118 % {
1119 % \@@_parse_model_gray:w, \@@_parse_model_rgb:w,
1120 % \@@_parse_model_cmyk:w
1121 % }
1122 % \begin{macro}[EXP]{\@@_parse_number:n}
1123 % \begin{macro}[EXP]{\@@_parse_number:w}
1124 % Turn the input into internal form, also tidying up the number quickly.
1125 % \begin{macrocode}
1126 \cs_new:Npn \@@_parse_model_gray:w #1 , #2 \s_@@_stop
1127 { { gray } { \@@_parse_number:n {#1} } }
1128 \cs_new:Npn \@@_parse_model_rgb:w #1 , #2 , #3 , #4 \s_@@_stop
1129 {
1130 { rgb }
1131 {
1132 \@@_parse_number:n {#1} ~
1133 \@@_parse_number:n {#2} ~
1134 \@@_parse_number:n {#3}
1135 }
1136 }
1137 \cs_new:Npn \@@_parse_model_cmyk:w #1 , #2 , #3 , #4 , #5 \s_@@_stop
1138 {
1139 { cmyk }
1140 {
1141 \@@_parse_number:n {#1} ~
1142 \@@_parse_number:n {#2} ~
1143 \@@_parse_number:n {#3} ~
1144 \@@_parse_number:n {#4}
1145 }
1146 }
1147 \cs_new:Npn \@@_parse_number:n #1
1148 { \@@_parse_number:w #1 . 0 . \s_@@_stop }
1149 \cs_new:Npn \@@_parse_number:w #1 . #2 . #3 \s_@@_stop
1150 { \tl_if_blank:nTF {#1} { 0 } {#1} . #2 }
1151 % \end{macrocode}
1152 % \end{macro}
1153 % \end{macro}
1154 % \end{macro}
1155 %
1156 % \begin{macro}[EXP]
1157 % {
1158 % \@@_parse_model_Gray:w, \@@_parse_model_hsb:w,
1159 % \@@_parse_model_Hsb:w, \@@_parse_model_HSB:w,
1160 % \@@_parse_model_HTML:w, \@@_parse_model_RGB:w
1161 % }
1162 % \begin{macro}[EXP]{\@@_parse_model_hsb:nnn, \@@_parse_model_hsb_aux:nnn}
1163 % \begin{macro}[EXP]{\@@_parse_model_hsb:nnnn}
1164 % \begin{macro}[EXP]{\@@_parse_model_hsb:nnnnn}
1165 % \begin{macro}[EXP]
1166 % {
1167 % \@@_parse_model_hsb_0:nnnn ,
1168 % \@@_parse_model_hsb_1:nnnn ,
1169 % \@@_parse_model_hsb_2:nnnn ,
1170 % \@@_parse_model_hsb_3:nnnn ,
1171 % \@@_parse_model_hsb_4:nnnn ,
1172 % \@@_parse_model_hsb_5:nnnn
1173 % }
1174 % \begin{macro}[EXP]{\@@_parse_model_wave:w}
1175 % \begin{macro}[EXP]
1176 % {\@@_parse_model_wave_auxi:nn, \@@_parse_model_wave_auxii:nn}
1177 % \begin{macro}[EXP]{\@@_parse_model_wave_rho:n}
1178 % \begin{macrocode}
1179 \cs_new:Npn \@@_parse_model_Gray:w #1 , #2 \s_@@_stop
1180 { { gray } { \fp_eval:n { #1 / 15 } } }
1181 \cs_new:Npn \@@_parse_model_hsb:w #1 , #2 , #3 , #4 \s_@@_stop
1182 { \@@_parse_model_hsb:nnn {#1} {#2} {#3} }
1183 \cs_new:Npn \@@_parse_model_Hsb:w #1 , #2 , #3 , #4 \s_@@_stop
1184 {
1185 \exp_args:Ne \@@_parse_model_hsb:nnn { \fp_eval:n { #1 / 360 } }
1186 {#2} {#3}
1187 }
1188 % \end{macrocode}
1189 % The conversion here is non-trivial but is described at length
1190 % in the \pkg{xcolor} manual. For ease, we calculate the integer
1191 % and fractional parts of the hue first, then use them to work out the
1192 % possible values for $r$, $g$ and $b$ before putting them in the correct
1193 % places.
1194 % \begin{macrocode}
1195 \cs_new:Npn \@@_parse_model_hsb:nnn #1#2#3
1196 {
1197 { rgb }
1198 {
1199 \exp_args:Ne \@@_parse_model_hsb_aux:nnn
1200 { \fp_eval:n { 6 * (#1) } } {#2} {#3}
1201 }
1202 }
1203 \cs_new:Npn \@@_parse_model_hsb_aux:nnn #1#2#3
1204 {
1205 \exp_args:Nee \@@_parse_model_hsb_aux:nnnn
1206 { \fp_eval:n { floor(#1) } } { \fp_eval:n { #1 - floor(#1) } }
1207 {#2} {#3}
1208 }
1209 \cs_new:Npn \@@_parse_model_hsb_aux:nnnn #1#2#3#4
1210 {
1211 \use:e
1212 {
1213 \exp_not:N \@@_parse_model_hsb_aux:nnnnn
1214 { \@@_parse_number:n {#4} }
1215 { \fp_eval:n { round(#4 * (1 - #3) ,5) } }
1216 { \fp_eval:n { round(#4 * ( 1 - #3 * #2 ) ,5) } }
1217 { \fp_eval:n { round(#4 * ( 1 - #3 * (1 - #2) ) ,5) } }
1218 {#1}
1219 }
1220 }
1221 \cs_new:Npn \@@_parse_model_hsb_aux:nnnnn #1#2#3#4#5
1222 { \use:c { @@_parse_model_hsb_ #5 :nnnn } {#1} {#2} {#3} {#4} }
1223 \cs_new:cpn { @@_parse_model_hsb_0:nnnn } #1#2#3#4 { #1 ~ #4 ~ #2 }
1224 \cs_new:cpn { @@_parse_model_hsb_1:nnnn } #1#2#3#4 { #3 ~ #1 ~ #2 }
1225 \cs_new:cpn { @@_parse_model_hsb_2:nnnn } #1#2#3#4 { #2 ~ #1 ~ #4 }
1226 \cs_new:cpn { @@_parse_model_hsb_3:nnnn } #1#2#3#4 { #2 ~ #3 ~ #1 }
1227 \cs_new:cpn { @@_parse_model_hsb_4:nnnn } #1#2#3#4 { #4 ~ #2 ~ #1 }
1228 \cs_new:cpn { @@_parse_model_hsb_5:nnnn } #1#2#3#4 { #1 ~ #2 ~ #3 }
1229 \cs_new:cpn { @@_parse_model_hsb_6:nnnn } #1#2#3#4 { #1 ~ #2 ~ #2 }
1230 \cs_new:Npn \@@_parse_model_HSB:w #1 , #2 , #3 , #4 \s_@@_stop
1231 {
1232 \exp_args:Neee \@@_parse_model_hsb:nnn
1233 { \fp_eval:n { round((#1) / 240,5) } }
1234 { \fp_eval:n { round((#2) / 240,5) } }
1235 { \fp_eval:n { round((#3) / 240,5) } }
1236 }
1237 \cs_new:Npn \@@_parse_model_HTML:w #1 , #2 \s_@@_stop
1238 { \@@_parse_model_HTML_aux:w #1 0 0 0 0 0 0 \s_@@_stop }
1239 \cs_new:Npn \@@_parse_model_HTML_aux:w #1#2#3#4#5#6#7 \s_@@_stop
1240 {
1241 { rgb }
1242 {
1243 \fp_eval:n { round(\int_from_hex:n {#1#2} / 255,5) } ~
1244 \fp_eval:n { round(\int_from_hex:n {#3#4} / 255,5) } ~
1245 \fp_eval:n { round(\int_from_hex:n {#5#6} / 255,5) }
1246 }
1247 }
1248 \cs_new:Npn \@@_parse_model_RGB:w #1 , #2 , #3 , #4 \s_@@_stop
1249 {
1250 { rgb }
1251 {
1252 \fp_eval:n { round((#1) / 255,5) } ~
1253 \fp_eval:n { round((#2) / 255,5) } ~
1254 \fp_eval:n { round((#3) / 255,5) }
1255 }
1256 }
1257 % \end{macrocode}
1258 % Following the description in the \pkg{xcolor} manual. As we always use |rgb|,
1259 % there is no need to find the sixth, we just pas the information straight
1260 % to the |hsb| auxiliary defined earlier.
1261 % \begin{macrocode}
1262 \cs_new:Npn \@@_parse_model_wave:w #1 , #2 \s_@@_stop
1263 {
1264 { rgb }
1265 {
1266 \fp_compare:nNnTF {#1} < { 420 }
1267 { \@@_parse_model_wave_auxi:nn {#1} { 0.3 + 0.7 * (#1 - 380) / 40 }
1268 }
1269 {
1270 \fp_compare:nNnTF {#1} > { 700 }
1271 { \@@_parse_model_wave_auxi:nn {#1} { 0.3 + 0.7 * (#1 - 780) / -80 } }
1272 { \@@_parse_model_wave_auxi:nn {#1} { 1 } }
1273 }
1274 }
1275 }
1276 \cs_new:Npn \@@_parse_model_wave_auxi:nn #1#2
1277 {
1278 \fp_compare:nNnTF {#1} < { 440 }
1279 {
1280 \@@_parse_model_wave_auxii:nn
1281 { 4 + \@@_parse_model_wave_rho:n { (#1 - 440) / -60 } }
1282 {#2}
1283 }
1284 {
1285 \fp_compare:nNnTF {#1} < { 490 }
1286 {
1287 \@@_parse_model_wave_auxii:nn
1288 { 4 - \@@_parse_model_wave_rho:n { (#1 - 440) / 50 } }
1289 {#2}
1290 }
1291 {
1292 \fp_compare:nNnTF {#1} < { 510 }
1293 {
1294 \@@_parse_model_wave_auxii:nn
1295 { 2 + \@@_parse_model_wave_rho:n { (#1 - 510) / -20 } }
1296 {#2}
1297 }
1298 {
1299 \fp_compare:nNnTF {#1} < { 580 }
1300 {
1301 \@@_parse_model_wave_auxii:nn
1302 { 2 - \@@_parse_model_wave_rho:n { (#1 - 510) / 70 } }
1303 {#2}
1304 }
1305 {
1306 \fp_compare:nNnTF {#1} < { 645 }
1307 {
1308 \@@_parse_model_wave_auxii:nn
1309 { \@@_parse_model_wave_rho:n { (#1 - 645) / -65 } }
1310 {#2}
1311 }
1312 { \@@_parse_model_wave_auxii:nn { 0 } {#2} }
1313 }
1314 }
1315 }
1316 }
1317 }
1318 \cs_new:Npn \@@_parse_model_wave_auxii:nn #1#2
1319 {
1320 \exp_args:Neee \@@_parse_model_hsb_aux:nnn
1321 { \fp_eval:n {#1} }
1322 { 1 }
1323 { \@@_parse_model_wave_rho:n {#2} }
1324 }
1325 \cs_new:Npn \@@_parse_model_wave_rho:n #1
1326 { \fp_eval:n { min(1, max(0,#1) ) } }
1327 % \end{macrocode}
1328 % \end{macro}
1329 % \end{macro}
1330 % \end{macro}
1331 % \end{macro}
1332 % \end{macro}
1333 % \end{macro}
1334 % \end{macro}
1335 % \end{macro}
1336 %
1337 % \begin{macro}{\@@_parse_model_cmy:w}
1338 % Simply pass data to the conversion functions.
1339 % \begin{macrocode}
1340 \cs_new:Npn \@@_parse_model_cmy:w #1 , #2 , #3 , #4 \s_@@_stop
1341 {
1342 { cmyk }
1343 { \@@_convert_rgb_cmyk:nnn {#1} {#2} {#3} }
1344 }
1345 % \end{macrocode}
1346 % \end{macro}
1347 %
1348 % \begin{macro}{\@@_parse_model_tHsb:w}
1349 % \begin{macro}{\@@_parse_model_tHsb:n}
1350 % \begin{macro}{\@@_parse_model_tHsb:nw}
1351 % There are three stages to the process here: bring the |tH| argument into
1352 % the normal range, divide through to get to |hsb| and finally convert that
1353 % to |rgb|. The final stage can be delegated to the parsing function for
1354 % |hsb|, and the conversion from |Hsb| to |hsb| is trivial, so the main focus
1355 % here is the first stage. We use a simple expandable loop to do the work,
1356 % and we implement the equation given in the \pkg{xcolor} manual
1357 % (number~85 there) as a simple expression.
1358 % \begin{macrocode}
1359 \cs_new:Npn \@@_parse_model_tHsb:w #1 , #2 , #3 , #4 \s_@@_stop
1360 {
1361 \exp_args:Ne \@@_parse_model_hsb:nnn
1362 { \@@_parse_model_tHsb:n {#1} } {#2} {#3}
1363 }
1364 \cs_new:Npn \@@_parse_model_tHsb:n #1
1365 {
1366 \@@_parse_model_tHsb:nw {#1}
1367 0 , 0 ;
1368 60 , 30 ;
1369 120 , 60 ;
1370 180 , 120 ;
1371 210 , 180 ;
1372 240 , 240 ;
1373 360 , 360 ;
1374 \q_recursion_tail , ;
1375 \q_recursion_stop
1376 }
1377 \cs_new:Npn \@@_parse_model_tHsb:nw #1 #2 , #3 ; #4 , #5 ;
1378 {
1379 \quark_if_recursion_tail_stop_do:nn {#4} { 0 }
1380 \fp_compare:nNnTF {#1} > {#4}
1381 { \@@_parse_model_tHsb:nw {#1} #4 , #5 ; }
1382 {
1383 \use_i_delimit_by_q_recursion_stop:nw
1384 { \fp_eval:n { ((#1 - #2) / (#4 - #2) * (#5 - #3) + #3) / 360 } }
1385 }
1386 }
1387 % \end{macrocode}
1388 % \end{macro}
1389 % \end{macro}
1390 % \end{macro}
1391 %
1392 % \begin{macro}{\@@_parse_model_&spot:w}
1393 % We cannot extract data here from that passed by \pkg{xcolor}, so
1394 % we fall back on a black tint.
1395 % \begin{macrocode}
1396 \cs_new:cpn { @@_parse_model_&spot:w } #1 , #2 \s_@@_stop
1397 { { gray } { #1 } }
1398 % \end{macrocode}
1399 % \end{macro}
1400 %
1401 % \begin{macro}[EXP]{\@@_parse_model_linearrgb:nnn}
1402 % \begin{macro}[EXP]{\@@_parse_model_linearrgb_aux:n}
1403 % Encode the tristimulus values of the sRGB primaries according to sRGB.
1404 % That is, take the actual physical light intensities of the primaries
1405 % defined by the default RGB color space sRGB
1406 % (\href{https://webstore.iec.ch/en/publication/6168}{IEC 61966-2-1}) and
1407 % transform them with the gamma function defined in the same
1408 % standard\footnote{For background information see
1409 % \url{https://www.w3.org/Graphics/Color/sRGB}.}:
1410 % \[ x_{\mathrm{standard}} = \left\{\begin{array}{ll}
1411 % 12.92 \cdot x & x<0.0031308 \\
1412 % 1.055 \cdot x^{1/2.4} - 0.055 & x\ge0.0031308
1413 % \end{array}\right. \]
1414 % Additionally clip values to the range $[0,1]$.
1415 % \begin{macrocode}
1416 \cs_new:Npn \@@_parse_model_linearrgb:nnn #1#2#3
1417 {
1418 { rgb }
1419 {
1420 \@@_parse_model_linearrgb_aux:n { #1 } ~
1421 \@@_parse_model_linearrgb_aux:n { #2 } ~
1422 \@@_parse_model_linearrgb_aux:n { #3 }
1423 }
1424 }
1425 \cs_new:Npn \@@_parse_model_linearrgb_aux:n #1
1426 {
1427 \fp_compare:nNnTF { #1 } < { 0.0031308 }
1428 { \fp_eval:n { 12.92 * max(0, #1) } }
1429 { \fp_eval:n { 1.055 * min(1, #1) ^ 0.4166666666666667 - 0.055 } }
1430 }
1431 % \end{macrocode}
1432 % \end{macro}
1433 % \end{macro}
1434 % \begin{macro}[EXP]
1435 % {
1436 % \@@_parse_model_lms:nnn ,
1437 % \@@_parse_model_cbrtlms:nnn ,
1438 % \@@_parse_model_oklab:nnn
1439 % }
1440 % \begin{macro}[EXP]{\@@_parse_model_oklab:w}
1441 % The \href{https://bottosson.github.io/posts/oklab}{Oklab color space} is
1442 % defined by first transforming the physical light intensity to the
1443 % biophysical responses of the L, M and S cones in a typical human eye, then
1444 % taking those responses to the power of $\frac{1}{3}$ (which approximately
1445 % fits human lightness perception), and finally applying one more matrix
1446 % transformation $M_{2}$ to obtain handy $L$, $a$, and $b$ values. Here, we
1447 % just go the other way round:
1448 %
1449 % First, transform $L$, $a$ and $b$ into the cube roots of $l$, $m$ and $s$
1450 % cone response intensities using the inverse of Oklab's defining matrix
1451 % $M_{2}$:
1452 % \[ \left(\begin{array}{c} l' \\ m' \\ s' \end{array}\right)
1453 % = \left(\begin{array}{ccc}
1454 % 1 & 0.3963377774 & 0.2158037573 \\
1455 % 1 & - 0.1055613458 & - 0.0638541728 \\
1456 % 1 & - 0.0894841775 & - 1.2914855480
1457 % \end{array}\right) \left(\begin{array}{c} L \\ a \\ b \end{array}\right) \]
1458 % \begin{macrocode}
1459 \cs_new:Npn \@@_parse_model_oklab:nnn #1#2#3
1460 {
1461 \exp_args:Neee \@@_parse_model_cbrtlms:nnn
1462 { \fp_eval:n { #1 + 0.3963377774 * #2 + 0.2158037573 * #3 } }
1463 { \fp_eval:n { #1 - 0.1055613458 * #2 - 0.0638541728 * #3 } }
1464 { \fp_eval:n { #1 - 0.0894841775 * #2 - 1.2914855480 * #3 } }
1465 }
1466 % \end{macrocode}
1467 % Next, take those cube roots to the power of $3$ to obtain the actual cone
1468 % responses:
1469 % \[ l = l'^{3}, \quad m = m'^{3}, \quad s = s'^{3} \]
1470 % \begin{macrocode}
1471 \cs_new:Npn \@@_parse_model_cbrtlms:nnn #1#2#3
1472 {
1473 \exp_args:Neee \@@_parse_model_lms:nnn
1474 { \fp_eval:n { #1 ^ 3 } }
1475 { \fp_eval:n { #2 ^ 3 } }
1476 { \fp_eval:n { #3 ^ 3 } }
1477 }
1478 % \end{macrocode}
1479 % Finally, transform $l$, $m$ and $s$ cone responses to linear sRGB
1480 % intensity.
1481 % \[ \left(\begin{array}{c} r \\ g \\ b \end{array}\right)
1482 % = \left(\begin{array}{ccc}
1483 % 4.0767416621 & - 3.3077115913 & 0.2309699292 \\
1484 % -1.2684380046 & 2.6097574011 & - 0.3413193965 \\
1485 % -0.0041960863 & - 0.7034186147 & 1.7076147010
1486 % \end{array}\right) \left(\begin{array}{c} l \\ m \\ s \end{array}\right) \]
1487 % \begin{macrocode}
1488 \cs_new:Npn \@@_parse_model_lms:nnn #1#2#3
1489 {
1490 \exp_args:Neee \@@_parse_model_linearrgb:nnn
1491 { \fp_eval:n { 4.0767416621 * #1 - 3.3077115913 * #2 + 0.2309699292 * #3 } }
1492 { \fp_eval:n { -1.2684380046 * #1 + 2.6097574011 * #2 - 0.3413193965 * #3 } }
1493 { \fp_eval:n { -0.0041960863 * #1 - 0.7034186147 * #2 + 1.7076147010 * #3 } }
1494 }
1495 % \end{macrocode}
1496 % The actual parse macro with \emph{weird} signature.
1497 % \begin{macrocode}
1498 \cs_new:Npn \@@_parse_model_oklab:w #1 , #2 , #3 , #4 \s_@@_stop
1499 { \@@_parse_model_oklab:nnn { #1 } { #2 } { #3 } }
1500 % \end{macrocode}
1501 % \end{macro}
1502 % \begin{macro}[EXP]{\@@_parse_model_oklch:w}
1503 % Oklch is Oklab in cylinder coordinates with lightness
1504 % $L\in\left[0,1\right]$, chroma $c$ giving the color intensity in values up
1505 % to around $0.35$ and hue $h$ given in degrees (typically given as value
1506 % from $0$ to $360$):
1507 % \[ L = L, \quad a = h \cdot \cos(c), \quad b = h \cdot \sin(c) \]
1508 % \begin{macrocode}
1509 \cs_new:Npn \@@_parse_model_oklch:w #1 , #2 , #3 , #4 \s_@@_stop
1510 {
1511 \exp_args:Nnee \@@_parse_model_oklab:nnn
1512 {#1}
1513 { \fp_eval:n { #2 * cosd(#3) } }
1514 { \fp_eval:n { #2 * sind(#3) } }
1515 }
1516 % \end{macrocode}
1517 % \end{macro}
1518 % \end{macro}
1519 %
1520 % \subsection{Selecting colors (and color models)}
1521 %
1522 % \begin{variable}{\l_color_fixed_model_tl}
1523 % For selecting a single fixed model.
1524 % \begin{macrocode}
1525 \tl_new:N \l_color_fixed_model_tl
1526 % \end{macrocode}
1527 % \end{variable}
1528 %
1529 % \begin{macro}{\@@_check_model:N}
1530 % \begin{macro}{\@@_check_model:nn}
1531 % Check that the model in use is the one required.
1532 % \begin{macrocode}
1533 \cs_new_protected:Npn \@@_check_model:N #1
1534 {
1535 \tl_if_empty:NF \l_color_fixed_model_tl
1536 {
1537 \exp_after:wN \@@_check_model:nn #1
1538 \tl_if_eq:NNF \l_@@_model_tl \l_color_fixed_model_tl
1539 {
1540 \@@_convert:VVN \l_@@_model_tl \l_color_fixed_model_tl
1541 \l_@@_value_tl
1542 }
1543 \tl_set:Ne #1
1544 { { \l_color_fixed_model_tl } { \l_@@_value_tl } }
1545 }
1546 }
1547 \cs_new_protected:Npn \@@_check_model:nn #1#2
1548 {
1549 \tl_set:Nn \l_@@_model_tl {#1}
1550 \tl_set:Nn \l_@@_value_tl {#2}
1551 }
1552 % \end{macrocode}
1553 % \end{macro}
1554 % \end{macro}
1555 %
1556 % \begin{macro}{\@@_finalize_current:}
1557 % A backend-neutral location for \enquote{last minute} manipulations before
1558 % handing off to the backend code. We set the special |.| syntax here: this
1559 % will therefore always be available. The finalization is separate from the
1560 % main function so it can also be applied to \emph{e.g.}~page color.
1561 % \begin{macrocode}
1562 \cs_new_protected:Npe \@@_finalize_current:
1563 {
1564 \tl_set:Ne \exp_not:c { l_@@_named_ . _tl }
1565 { \exp_not:N \@@_model:N \exp_not:N \l_@@_current_tl }
1566 \prop_clear:N \exp_not:c { l_@@_named_ . _prop }
1567 \prop_put:NVe \exp_not:c { l_@@_named_ . _prop }
1568 \exp_not:c { l_@@_named_ . _tl }
1569 { \exp_not:N \@@_values:N \exp_not:N \l_@@_current_tl }
1570 }
1571 % \end{macrocode}
1572 % \end{macro}
1573 %
1574 % \begin{macro}{\color_select:n, \color_select:V}
1575 % \begin{macro}
1576 % {\color_select:nn, \color_select:nV, \color_select:Vn, \color_select:VV}
1577 % \begin{macro}{\@@_select_main:Nnn}
1578 % \begin{macro}{\@@_select_main:Nw, \@@_select_loop:Nw}
1579 % \begin{macro}{\@@_select:nnN}
1580 % \begin{macro}{\@@_select_swap:Nnn}
1581 % Parse the input expressions then get the backend to actually activate
1582 % them. The main complexity here is the need to check through multiple models.
1583 % That is done \enquote{locally} here as the approach is subtly different to
1584 % when different models are being stored.
1585 % \begin{macrocode}
1586 \cs_new_protected:Npn \color_select:n #1
1587 {
1588 \@@_parse:nN {#1} \l_@@_current_tl
1589 \@@_finalize_current:
1590 \@@_select:N \l_@@_current_tl
1591 }
1592 \cs_generate_variant:Nn \color_select:n { V }
1593 \cs_new_protected:Npn \color_select:nn #1#2
1594 {
1595 \@@_select_main:Nnn \l_@@_current_tl {#1} {#2}
1596 \@@_finalize_current:
1597 \@@_select:N \l_@@_current_tl
1598 }
1599 \cs_generate_variant:Nn \color_select:nn { nV , V , VV }
1600 % \end{macrocode}
1601 % If the first color model is the fixed one, or if there is no fixed
1602 % model, we don't need most of the data: just set up and apply the backend
1603 % function.
1604 % \begin{macrocode}
1605 \cs_new_protected:Npn \@@_select_main:Nnn #1#2#3
1606 {
1607 \use:e
1608 {
1609 \exp_not:N \@@_select_main:Nw \exp_not:N #1
1610 \exp_not:n {#2} / / \exp_not:N \s_@@_mark
1611 #3 / / \exp_not:N \s_@@_stop
1612 }
1613 }
1614 \cs_new_protected:Npn \@@_select_main:Nw
1615 #1 #2 / #3 / #4 \s_@@_mark #5 / #6 / #7 \s_@@_stop
1616 {
1617 \@@_select:nnN {#2} {#5} #1
1618 \bool_lazy_or:nnF
1619 { \tl_if_empty_p:N \l_color_fixed_model_tl }
1620 { \str_if_eq_p:nV {#2} \l_color_fixed_model_tl }
1621 { \@@_select_loop:Nw #1 #3 / #4 \s_@@_mark #6 / #7 \s_@@_stop }
1622 }
1623 % \end{macrocode}
1624 % If a fixed model applies, we need to check each possible value in order.
1625 % If there is no hit at all, fall back on the generic formula-based
1626 % interchange.
1627 % \begin{macrocode}
1628 \cs_new_protected:Npn \@@_select_loop:Nw
1629 #1 #2 / #3 \s_@@_mark #4 / #5 \s_@@_stop
1630 {
1631 \str_if_eq:nVTF {#2} \l_color_fixed_model_tl
1632 { \@@_select:nnN {#2} {#4} #1 }
1633 {
1634 \tl_if_blank:nTF {#2}
1635 { \exp_after:wN \@@_select_swap:Nnn \exp_after:wN #1 #1 }
1636 { \@@_select_loop:Nw #1 #3 \s_@@_mark #5 \s_@@_stop }
1637 }
1638 }
1639 \cs_new_protected:Npn \@@_select:nnN #1#2#3
1640 {
1641 \cs_if_exist:cTF { @@_parse_model_ #1 :w }
1642 {
1643 \tl_set:Ne #3
1644 { \use:c { @@_parse_model_ #1 :w } #2 , 0 , 0 , 0 , 0 \s_@@_stop }
1645 }
1646 { \msg_error:nnn { color } { unknown-model } {#1} }
1647 }
1648 \cs_new_protected:Npn \@@_select_swap:Nnn #1#2#3
1649 {
1650 \@@_convert:nVnN {#2} \l_color_fixed_model_tl {#3} \l_@@_value_tl
1651 \tl_set:Ne #1
1652 { { \l_color_fixed_model_tl } { \l_@@_value_tl } }
1653 }
1654 % \end{macrocode}
1655 % \end{macro}
1656 % \end{macro}
1657 % \end{macro}
1658 % \end{macro}
1659 % \end{macro}
1660 % \end{macro}
1661 %
1662 % \subsection{Math color}
1663 %
1664 % The approach here is the same as for the \LaTeXe{} \cs{mathcolor} command,
1665 % but as we are working at the \pkg{expl3} level we can make some minor
1666 % changes.
1667 %
1668 % \begin{macro}{\l_color_math_active_tl}
1669 % Tokens representing active sub/superscripts.
1670 % \begin{macrocode}
1671 \tl_new:N \l_color_math_active_tl
1672 \tl_set:Nn \l_color_math_active_tl { ' }
1673 % \end{macrocode}
1674 % \end{macro}
1675 %
1676 % \begin{macro}{\g_@@_math_seq}
1677 % Not all engines have multiple color stacks, and at the same time we are
1678 % not expecting breaking within a colored math fragment. So we track the
1679 % color stack ourselves.
1680 % \begin{macrocode}
1681 \seq_new:N \g_@@_math_seq
1682 % \end{macrocode}
1683 % \end{macro}
1684 %
1685 % \begin{macro}{\color_math:nn}
1686 % \begin{macro}{\color_math:nnn}
1687 % \begin{macro}{\@@_math:nn}
1688 % The basic set up here is relatively simple: store the current color,
1689 % parse the new color as-normal, then switch color before inserting the
1690 % tokens we are asked to change. The tricky part is right at the end,
1691 % handling the reset.
1692 % \begin{macrocode}
1693 \cs_new_protected:Npn \color_math:nn #1#2
1694 {
1695 \@@_math:nn {#2}
1696 { \@@_parse:nN {#1} \l_@@_current_tl }
1697 }
1698 \cs_new_protected:Npn \color_math:nnn #1#2#3
1699 {
1700 \@@_math:nn {#3}
1701 { \@@_select_main:Nnn \l_@@_current_tl {#1} {#2} }
1702 }
1703 \cs_new_protected:Npn \@@_math:nn #1#2
1704 {
1705 \seq_gpush:NV \g_@@_math_seq \l_@@_current_tl
1706 #2
1707 \@@_select_math:N \l_@@_current_tl
1708 #1
1709 \@@_math_scan:w
1710 }
1711 % \end{macrocode}
1712 % \end{macro}
1713 % \end{macro}
1714 % \end{macro}
1715 %
1716 % \begin{macro}
1717 % {
1718 % \@@_math_scan:w ,
1719 % \@@_math_scan_auxi: ,
1720 % \@@_math_scan_auxii: ,
1721 % \@@_math_scan_end:
1722 % }
1723 % The complication when changing the color back is due to the fact
1724 % that the \cs{color_math:nn(n)} may be followed by \verb=^= or \verb=_=
1725 % or the hidden superscript (for example \texttt{'}) and its argument may
1726 % end in a \tn{mathop} in which case the sub- and superscripts may be
1727 % attached as \cs{limits} instead of after the material. All cases
1728 % need separate treatment. To avoid repeatedly collecting the same
1729 % token, we first check for an alignment tab: assuming we don't have
1730 % one of those, we can \enquote{recycle} \cs{l_peek_token} safely.
1731 % As we have an explicit \cs{c_alignment_token}, there needs to be
1732 % an align-safe group present.
1733 % \begin{macrocode}
1734 \cs_new_protected:Npn \@@_math_scan:w
1735 {
1736 \peek_remove_filler:n
1737 {
1738 \group_align_safe_begin:
1739 \peek_catcode:NTF \c_alignment_token
1740 {
1741 \group_align_safe_end:
1742 \@@_math_scan_end:
1743 }
1744 {
1745 \group_align_safe_end:
1746 \@@_math_scan_auxi:
1747 }
1748 }
1749 }
1750 % \end{macrocode}
1751 % Dealing with literal |_| and |^| is easy, and as we have exactly two cases,
1752 % we can hard-code this. We use a hard-coded list for limits: these are all
1753 % primitives. The \cs{use_none:n} here also removes the test token so it is
1754 % left just in the right place.
1755 % \begin{macrocode}
1756 \cs_new_protected:Npn \@@_math_scan_auxi:
1757 {
1758 \token_case_catcode:NnTF \l_peek_token
1759 {
1760 \c_math_subscript_token { }
1761 \c_math_superscript_token { }
1762 }
1763 { \@@_math_scripts:Nw }
1764 {
1765 \token_case_meaning:NnTF \l_peek_token
1766 {
1767 \tex_limits:D { \tex_limits:D }
1768 \tex_nolimits:D { \tex_nolimits:D }
1769 \tex_displaylimits:D { \tex_displaylimits:D }
1770 }
1771 { \@@_math_scan:w \use_none:n }
1772 { \@@_math_scan_auxii: }
1773 }
1774 }
1775 % \end{macrocode}
1776 % The one final case to handle is math-active tokens, most obviously
1777 % \texttt{'}, as these won't be covered earlier.
1778 % \begin{macrocode}
1779 \cs_new_protected:Npn \@@_math_scan_auxii:
1780 {
1781 \tl_map_inline:Nn \l_color_math_active_tl
1782 {
1783 \token_if_eq_meaning:NNT \l_peek_token ##1
1784 {
1785 \tl_map_break:n
1786 {
1787 \use_i:nn
1788 { \@@_math_scan_auxiii:N ##1 }
1789 }
1790 }
1791 \@@_math_scan_end:
1792 }
1793 }
1794 \cs_new_protected:Npn \@@_math_scan_auxiii:N #1
1795 {
1796 \exp_after:wN \exp_after:wN \exp_after:wN \@@_math_scan:w
1797 \char_generate:nn { `#1 } { 13 }
1798 }
1799 \cs_new_protected:Npn \@@_math_scan_end:
1800 {
1801 \@@_backend_reset:
1802 \seq_gpop:NN \g_@@_math_seq \l_@@_current_tl
1803 }
1804 % \end{macrocode}
1805 % \end{macro}
1806 %
1807 % \begin{macro}{\@@_math_scripts:Nw}
1808 % \begin{macro}{\@@_math_script_aux:N}
1809 % The tricky part of handling sub and superscripts is that we have
1810 % to reset color to the one that is on the stack but reset it back
1811 % to what it was before to allow for cases like
1812 % \begin{verbatim}
1813 % \[ \color_math:n { red } { a + \sum } _ { i = 1 } ^ { n } \]
1814 % \end{verbatim}
1815 % Here, \TeX{} constructs a \cs{vbox} stacking subscript, summation
1816 % sign, and superscript. So technically the superscript comes first
1817 % and the \cs{sum} that should get colored red is the middle.
1818 %
1819 % The approach here is to set up a brace group immediately after the
1820 % script token, then to set the color appropriately in that argument.
1821 % We need an extra group to keep the color contained, and as we
1822 % need to allow for an explicit closing brace in the source, the
1823 % inner group also is a brace one rather than \cs{group_begin:}-based.
1824 % At the end of the outer group we need to insert \cs{@@_math_scan:w}
1825 % to continue the search for a second script token.
1826 %
1827 % Notice that here we \emph{don't} need to use the math-specific
1828 % color selector as we can allow the
1829 % |\group_insert_after:N \@@_backend_reset:| to operate normally.
1830 % \begin{macrocode}
1831 \cs_new_protected:Npn \@@_math_scripts:Nw #1
1832 {
1833 #1
1834 \c_group_begin_token
1835 \c_group_begin_token
1836 \seq_get:NN \g_@@_math_seq \l_@@_current_tl
1837 \@@_select:N \l_@@_current_tl
1838 \group_insert_after:N \c_group_end_token
1839 \group_insert_after:N \@@_math_scan:w
1840 \peek_remove_filler:n
1841 {
1842 \peek_catcode_remove:NF \c_group_begin_token
1843 { \@@_math_script_aux:N }
1844 }
1845 }
1846 % \end{macrocode}
1847 % Deal with the case where we do not have an explicit brace pair in the
1848 % source.
1849 % \begin{macrocode}
1850 \cs_new_protected:Npn \@@_math_script_aux:N #1 { #1 \c_group_end_token }
1851 % \end{macrocode}
1852 % \end{macro}
1853 % \end{macro}
1854 %
1855 % \subsection{Fill and stroke color}
1856 %
1857 % \begin{macro}{\color_fill:n, \color_stroke:n}
1858 % \begin{macro}{\color_fill:nn, \color_stroke:nn}
1859 % \begin{macro}{\@@_draw:nnn}
1860 % \begin{macrocode}
1861 \cs_new_protected:Npn \color_fill:n #1
1862 {
1863 \@@_parse:nN {#1} \l_@@_current_tl
1864 \exp_after:wN \@@_draw:nnn \l_@@_current_tl { fill }
1865 }
1866 \cs_new_protected:Npn \color_stroke:n #1
1867 {
1868 \@@_parse:nN {#1} \l_@@_current_tl
1869 \exp_after:wN \@@_draw:nnn \l_@@_current_tl { stroke }
1870 }
1871 \cs_new_protected:Npn \color_fill:nn #1#2
1872 {
1873 \@@_select_main:Nnn \l_@@_current_tl {#1} {#2}
1874 \exp_after:wN \@@_draw:nnn \l_@@_current_tl { fill }
1875 }
1876 \cs_new_protected:Npn \color_stroke:nn #1#2
1877 {
1878 \@@_select_main:Nnn \l_@@_current_tl {#1} {#2}
1879 \exp_after:wN \@@_draw:nnn \l_@@_current_tl { stroke }
1880 }
1881 \cs_new_protected:Npn \@@_draw:nnn #1#2#3
1882 {
1883 \use:c { @@_backend_ #3 _ #1 :n } {#2}
1884 \exp_args:Nc \group_insert_after:N { @@_backend_ #3 _ reset: }
1885 }
1886 % \end{macrocode}
1887 % \end{macro}
1888 % \end{macro}
1889 % \end{macro}
1890 %
1891 % \subsection{Defining named colors}
1892 %
1893 % \begin{variable}{\l_@@_named_tl}
1894 % Space to store the detail of the named color.
1895 % \begin{macrocode}
1896 \tl_new:N \l_@@_named_tl
1897 % \end{macrocode}
1898 % \end{variable}
1899 %
1900 % \begin{macro}{\color_set:nn}
1901 % \begin{macro}{\@@_set:nnn}
1902 % \begin{macro}{\@@_set:nn}
1903 % \begin{macro}{\@@_set:nnw}
1904 % \begin{macro}{\color_set:nnn, \@@_set_aux:nnn}
1905 % \begin{macro}{\@@_set_colon:nnw}
1906 % \begin{macro}{\@@_set_loop:nw}
1907 % \begin{macro}{\color_set_eq:nn}
1908 % Defining named colors means working through the model list and saving
1909 % both the \enquote{main} color and any equivalents in other models. Even
1910 % if there is only one model, we store a |prop| as well as a |tl|, as there
1911 % could be grouping weirdness, etc. When setting using an expression,
1912 % we need to avoid any fixed model issues, which is done without a group as
1913 % in \pkg{l3keys}.
1914 % \begin{macrocode}
1915 \cs_new_protected:Npn \color_set:nn #1#2
1916 {
1917 \exp_args:NV \@@_set:nnn
1918 \l_color_fixed_model_tl {#1} {#2}
1919 }
1920 \cs_new_protected:Npn \@@_set:nnn #1#2#3
1921 {
1922 \tl_clear:N \l_color_fixed_model_tl
1923 \@@_set:nn {#2} {#3}
1924 \tl_set:Nn \l_color_fixed_model_tl {#1}
1925 }
1926 \cs_new_protected:Npn \@@_set:nn #1#2
1927 {
1928 \str_if_eq:nnF {#1} { . }
1929 {
1930 \@@_parse:nN {#2} \l_@@_named_tl
1931 \tl_clear_new:c { l_@@_named_ #1 _tl }
1932 \tl_set:ce { l_@@_named_ #1 _tl }
1933 { \@@_model:N \l_@@_named_tl }
1934 \prop_clear_new:c { l_@@_named_ #1 _prop }
1935 \prop_put:cve { l_@@_named_ #1 _prop } { l_@@_named_ #1 _tl }
1936 { \@@_values:N \l_@@_named_tl }
1937 \@@_set:nnw {#1} {#2} #2 ! \s_@@_stop
1938 }
1939 }
1940 % \end{macrocode}
1941 % When setting an expression-based color, there could be multiple model
1942 % data available for one or more of the input colors. Where that is true for
1943 % the \emph{first} named color in an expression, we re-parse the expression
1944 % when they are also parameter-based: only |cmyk|, |gray| and |rgb| make
1945 % any sense here. There is a bit of a performance hit but this should be
1946 % rare and taking place during set-up.
1947 % \begin{macrocode}
1948 \cs_new_protected:Npn \@@_set:nnw #1#2#3 ! #4 \s_@@_stop
1949 {
1950 \clist_map_inline:nn { cmyk , gray , rgb }
1951 {
1952 \prop_get:cnNT { l_@@_named_ #3 _prop } {##1} \l_@@_tmp_tl
1953 {
1954 \prop_if_in:cnF { l_@@_named_ #1 _prop } {##1}
1955 {
1956 \group_begin:
1957 \bool_set_true:N \l_@@_ignore_error_bool
1958 \tl_set:cn { l_@@_named_ #3 _tl } {##1}
1959 \@@_parse:nN {#2} \l_@@_tmp_tl
1960 \exp_args:NNNV \group_end:
1961 \tl_set:Nn \l_@@_tmp_tl \l_@@_tmp_tl
1962 \prop_put:cee { l_@@_named_ #1 _prop }
1963 { \@@_model:N \l_@@_tmp_tl }
1964 { \@@_values:N \l_@@_tmp_tl }
1965 }
1966 }
1967 }
1968 }
1969 \cs_new_protected:Npn \color_set:nnn #1#2#3
1970 {
1971 \str_if_eq:nnF {#1} { . }
1972 {
1973 \tl_clear_new:c { l_@@_named_ #1 _tl }
1974 \prop_clear_new:c { l_@@_named_ #1 _prop }
1975 \exp_args:Ne \@@_set_aux:nnn { \tl_to_str:n {#2} }
1976 {#1} {#3}
1977 }
1978 }
1979 \cs_new_protected:Npe \@@_set_aux:nnn #1#2#3
1980 {
1981 \exp_not:N \@@_set_colon:nnw {#2} {#3}
1982 #1 \c_colon_str \c_colon_str \exp_not:N \s_@@_stop
1983 }
1984 \use:e
1985 {
1986 \cs_new_protected:Npn \exp_not:N \@@_set_colon:nnw
1987 #1#2 #3 \c_colon_str #4 \c_colon_str
1988 #5 \exp_not:N \s_@@_stop
1989 }
1990 {
1991 \tl_if_blank:nTF {#4}
1992 { \@@_set_loop:nw {#1} #3 }
1993 { \@@_set_loop:nw {#1} #4 }
1994 / / \s_@@_mark #2 / / \s_@@_stop
1995 }
1996 \cs_new_protected:Npn \@@_set_loop:nw
1997 #1#2 / #3 \s_@@_mark #4 / #5 \s_@@_stop
1998 {
1999 \tl_if_blank:nF {#2}
2000 {
2001 \@@_select:nnN {#2} {#4} \l_@@_named_tl
2002 \tl_set:Ne \l_@@_tmp_tl { \@@_model:N \l_@@_named_tl }
2003 \tl_if_empty:cT { l_@@_named_ #1 _tl }
2004 { \tl_set_eq:cN { l_@@_named_ #1 _tl } \l_@@_tmp_tl }
2005 \prop_put:cVe { l_@@_named_ #1 _prop } \l_@@_tmp_tl
2006 { \@@_values:N \l_@@_named_tl }
2007 \@@_set_loop:nw {#1} #3 \s_@@_mark #5 \s_@@_stop
2008 }
2009 }
2010 \cs_new_protected:Npn \color_set_eq:nn #1#2
2011 {
2012 \color_if_exist:nTF {#2}
2013 {
2014 \tl_clear_new:c { l_@@_named_ #1 _tl }
2015 \prop_clear_new:c { l_@@_named_ #1 _prop }
2016 \str_if_eq:nnTF {#2} { . }
2017 {
2018 \tl_set:ce { l_@@_named_ #1 _tl }
2019 { \@@_model:N \l_@@_current_tl }
2020 \prop_put:cve { l_@@_named_ #1 _prop } { l_@@_named_ #1 _tl }
2021 { \@@_values:N \l_@@_current_tl }
2022 }
2023 {
2024 \tl_set_eq:cc { l_@@_named_ #1 _tl } { l_@@_named_ #2 _tl }
2025 \prop_set_eq:cc { l_@@_named_ #1 _prop } { l_@@_named_ #2 _prop }
2026 }
2027 }
2028 {
2029 \msg_error:nnn { color } { unknown-color } {#2}
2030 }
2031 }
2032 % \end{macrocode}
2033 % \end{macro}
2034 % \end{macro}
2035 % \end{macro}
2036 % \end{macro}
2037 % \end{macro}
2038 % \end{macro}
2039 % \end{macro}
2040 % \end{macro}
2041 %
2042 % A small set of colors are always defined.
2043 % \begin{macrocode}
2044 \color_set:nnn { black } { gray } { 0 }
2045 \color_set:nnn { white } { gray } { 1 }
2046 \color_set:nnn { cyan } { cmyk } { 1 , 0 , 0 , 0 }
2047 \color_set:nnn { magenta } { cmyk } { 0 , 1 , 0 , 0 }
2048 \color_set:nnn { yellow } { cmyk } { 0 , 0 , 1 , 0 }
2049 \color_set:nnn { red } { rgb } { 1 , 0 , 0 }
2050 \color_set:nnn { green } { rgb } { 0 , 1 , 0 }
2051 \color_set:nnn { blue } { rgb } { 0 , 0 , 1 }
2052 % \end{macrocode}
2053 %
2054 % \begin{variable}{\l_@@_named_._prop, \l_@@_named_._tl}
2055 % A special named color: this is always defined though not fixed in
2056 % definition.
2057 % \begin{macrocode}
2058 \prop_new:c { l_@@_named_._prop }
2059 \tl_new:c { l_@@_named_._tl }
2060 \tl_set:ce { l_@@_named_._tl } { \@@_model:N \l_@@_current_tl }
2061 \prop_put:cve { l_@@_named_._prop } { l_@@_named_._tl }
2062 { \@@_values:N \l_@@_current_tl }
2063 % \end{macrocode}
2064 % \end{variable}
2065 %
2066 % \subsection{Exporting colors}
2067 %
2068 % \begin{macro}{\color_export:nnN}
2069 % \begin{macro}{\color_export:nnnN}
2070 % \begin{macro}{\@@_export:nN}
2071 % \begin{macro}{\@@_export:nnnN}
2072 % \begin{macrocode}
2073 \cs_new_protected:Npn \color_export:nnN #1#2#3
2074 {
2075 \group_begin:
2076 \tl_if_exist:cT { c_@@_export_ #2 _tl }
2077 { \tl_set_eq:Nc \l_color_fixed_model_tl { c_@@_export_ #2 _tl } }
2078 \@@_parse:nN {#1} #3
2079 \@@_export:nN {#2} #3
2080 \exp_args:NNNV \group_end:
2081 \tl_set:Nn #3 #3
2082 }
2083 \cs_new_protected:Npn \color_export:nnnN #1#2#3#4
2084 {
2085 \@@_select_main:Nnn #4 {#1} {#2}
2086 \@@_export:nN {#3} #4
2087 }
2088 \cs_new_protected:Npn \@@_export:nN #1#2
2089 { \exp_after:wN \@@_export:nnnN #2 {#1} #2 }
2090 \cs_new:Npn \@@_export:nnnN #1#2#3#4
2091 {
2092 \cs_if_exist_use:cF { @@_export_format_ #3 :nnN }
2093 {
2094 \msg_error:nnn { color } { unknown-export-format } {#3}
2095 \use_none:nnn
2096 }
2097 {#1} {#2} #4
2098 }
2099 % \end{macrocode}
2100 % \end{macro}
2101 % \end{macro}
2102 % \end{macro}
2103 % \end{macro}
2104 %
2105 % \begin{macro}{\@@_export_format_backend:nnN}
2106 % Simple.
2107 % \begin{macrocode}
2108 \cs_new_protected:Npn \@@_export_format_backend:nnN #1#2#3
2109 { \tl_set:Nn #3 { {#1} {#2} } }
2110 % \end{macrocode}
2111 % \end{macro}
2112 %
2113 % \begin{macro}{\@@_export:nnnNN}
2114 % A generic auxiliary for cases where only one model is appropriate.
2115 % \begin{macrocode}
2116 \cs_new_protected:Npn \@@_export:nnnNN #1#2#3#4#5
2117 {
2118 \str_if_eq:nnTF {#2} {#1}
2119 { #5 #4 #3 \s_@@_stop }
2120 {
2121 \@@_convert:nnnN {#2} {#1} {#3} #4
2122 \exp_after:wN #5 \exp_after:wN #4
2123 #4 \s_@@_stop
2124 }
2125 }
2126 % \end{macrocode}
2127 % \end{macro}
2128 %
2129 % \begin{variable}
2130 % {
2131 % \c_@@_export_comma-sep-cmyk_tl ,
2132 % \c_@@_export_comma-sep-rgb_tl ,
2133 % \c_@@_export_HTML_tl ,
2134 % \c_@@_export_space-sep-cmyk_tl ,
2135 % \c_@@_export_space-sep-rgb_tl
2136 % }
2137 % \begin{macrocode}
2138 \tl_const:cn { c_@@_export_comma-sep-cmyk_tl } { cmyk }
2139 \tl_const:cn { c_@@_export_comma-sep-rgb_tl } { rgb }
2140 \tl_const:Nn \c_@@_export_HTML_tl { rgb }
2141 \tl_const:cn { c_@@_export_space-sep-cmyk_tl } { cmyk }
2142 \tl_const:cn { c_@@_export_space-sep-rgb_tl } { rgb }
2143 % \end{macrocode}
2144 % \end{variable}
2145 %
2146 % \begin{macro}
2147 % {
2148 % \@@_export_format_comma-sep-cmyk:nnN ,
2149 % \@@_export_format_comma-sep-rgb:nnN ,
2150 % \@@_export_format_space-sep-cmyk:nnN ,
2151 % \@@_export_format_space-sep-rgb:nnN
2152 % }
2153 % \begin{macrocode}
2154 \group_begin:
2155 \cs_set_protected:Npn \@@_tmp:w #1#2
2156 {
2157 \cs_new_protected:cpe { @@_export_format_ #1 :nnN } ##1##2##3
2158 {
2159 \exp_not:N \@@_export:nnnNN {#2} {##1} {##2} ##3
2160 \exp_not:c { @@_export_ #1 :Nw }
2161 }
2162 }
2163 \@@_tmp:w { comma-sep-cmyk } { cmyk }
2164 \@@_tmp:w { comma-sep-rgb } { rgb }
2165 \@@_tmp:w { HTML } { rgb }
2166 \@@_tmp:w { space-sep-cmyk } { cmyk }
2167 \@@_tmp:w { space-sep-rgb } { rgb }
2168 \group_end:
2169 % \end{macrocode}
2170 % \end{macro}
2171 %
2172 % \begin{macro}{\@@_export_space-sep-cmyk:Nw, \@@_export_comma-sep-cmyk:Nw}
2173 % \begin{macrocode}
2174 \cs_new_protected:cpn { @@_export_comma-sep-cmyk:Nw }
2175 #1#2 ~ #3 ~ #4 ~ #5 \s_@@_stop
2176 { \tl_set:Nn #1 { #2 , #3 , #4 , #5 } }
2177 \cs_new_protected:cpn { @@_export_space-sep-cmyk:Nw } #1#2 \s_@@_stop
2178 { \tl_set:Nn #1 {#2} }
2179 % \end{macrocode}
2180 % \end{macro}
2181 %
2182 % \begin{macro}
2183 % {
2184 % \@@_export_comma-sep-rgb:Nw ,
2185 % \@@_export_HTML:Nw ,
2186 % \@@_export_space-sep-rgb:Nw
2187 % }
2188 % \begin{macro}[EXP]{\@@_export_HTML:n}
2189 % \textsc{html} values must be given in |rgb|: we force conversion if
2190 % required, then do some simple maths.
2191 % \begin{macrocode}
2192 \cs_new_protected:cpn { @@_export_comma-sep-rgb:Nw } #1#2 ~ #3 ~ #4 \s_@@_stop
2193 { \tl_set:Ne #1 { #2 , #3 , #4 } }
2194 \cs_new_protected:Npn \@@_export_HTML:Nw #1#2 ~ #3 ~ #4 \s_@@_stop
2195 {
2196 \tl_set:Ne #1
2197 {
2198 \@@_export_HTML:n {#2}
2199 \@@_export_HTML:n {#3}
2200 \@@_export_HTML:n {#4}
2201 }
2202 }
2203 \cs_new:Npn \@@_export_HTML:n #1
2204 {
2205 \fp_compare:nNnTF {#1} = { 0 }
2206 { 00 }
2207 {
2208 \fp_compare:nNnT { #1 * 255 } < { 16 } { 0 }
2209 \int_to_Hex:n { \fp_to_int:n { #1 * 255 } }
2210 }
2211 }
2212 \cs_new_protected:cpn { @@_export_space-sep-rgb:Nw } #1#2 \s_@@_stop
2213 { \tl_set:Nn #1 {#2} }
2214 % \end{macrocode}
2215 % \end{macro}
2216 % \end{macro}
2217 %
2218 % \subsection{Additional color models}
2219 %
2220 % \begin{variable}{\l_@@_tmp_prop}
2221 % \begin{macrocode}
2222 \prop_new:N \l_@@_tmp_prop
2223 % \end{macrocode}
2224 % \end{variable}
2225 %
2226 % \begin{variable}{\g_@@_model_int}
2227 % A tracker for the total number of new models.
2228 % \begin{macrocode}
2229 \int_new:N \g_@@_model_int
2230 % \end{macrocode}
2231 % \end{variable}
2232 %
2233 % \begin{variable}
2234 % {\c_@@_fallback_cmyk_tl, \c_@@_fallback_gray_tl, \c_@@_fallback_rgb_tl}
2235 % For every colorspace, we define one of the base colorspaces as a fallback.
2236 % The base colorspaces themselves are their own fallback.
2237 % \begin{macrocode}
2238 \tl_const:Nn \c_@@_fallback_cmyk_tl { cmyk }
2239 \tl_const:Nn \c_@@_fallback_gray_tl { gray }
2240 \tl_const:Nn \c_@@_fallback_rgb_tl { rgb }
2241 % \end{macrocode}
2242 % \end{variable}
2243 %
2244 % \begin{variable}{\g_@@_colorants_prop}
2245 % Mapping from names to colorants.
2246 % \begin{macrocode}
2247 \prop_new:N \g_@@_colorants_prop
2248 \prop_gput:Nnn \g_@@_colorants_prop { black } { Black }
2249 \prop_gput:Nnn \g_@@_colorants_prop { blue } { Blue }
2250 \prop_gput:Nnn \g_@@_colorants_prop { cyan } { Cyan }
2251 \prop_gput:Nnn \g_@@_colorants_prop { green } { Green }
2252 \prop_gput:Nnn \g_@@_colorants_prop { magenta } { Magenta }
2253 \prop_gput:Nnn \g_@@_colorants_prop { none } { None }
2254 \prop_gput:Nnn \g_@@_colorants_prop { red } { Red }
2255 \prop_gput:Nnn \g_@@_colorants_prop { yellow } { Yellow }
2256 % \end{macrocode}
2257 % \end{variable}
2258 %
2259 % \begin{variable}
2260 % {
2261 % \c_@@_model_whitepoint_CIELAB_a_tl ,
2262 % \c_@@_model_whitepoint_CIELAB_b_tl ,
2263 % \c_@@_model_whitepoint_CIELAB_e_tl ,
2264 % \c_@@_model_whitepoint_CIELAB_d50_tl ,
2265 % \c_@@_model_whitepoint_CIELAB_d55_tl ,
2266 % \c_@@_model_whitepoint_CIELAB_d65_tl ,
2267 % \c_@@_model_whitepoint_CIELAB_d75_tl
2268 % }
2269 % Whitepoint data for the CIELAB profiles.
2270 % \begin{macrocode}
2271 \tl_const:Nn \c_@@_model_whitepoint_CIELAB_a_tl { 1.0985 ~ 1 ~ 0.3558 }
2272 \tl_const:Nn \c_@@_model_whitepoint_CIELAB_b_tl { 0.9807 ~ 1 ~ 1.1822 }
2273 \tl_const:Nn \c_@@_model_whitepoint_CIELAB_e_tl { 1 ~ 1 ~ 1 }
2274 \tl_const:cn { c_@@_model_whitepoint_CIELAB_d50_tl } { 0.9642 ~ 1 ~ 0.8251 }
2275 \tl_const:cn { c_@@_model_whitepoint_CIELAB_d55_tl } { 0.9568 ~ 1 ~ 0.9214 }
2276 \tl_const:cn { c_@@_model_whitepoint_CIELAB_d65_tl } { 0.9504 ~ 1 ~ 1.0888 }
2277 \tl_const:cn { c_@@_model_whitepoint_CIELAB_d75_tl } { 0.9497 ~ 1 ~ 1.2261 }
2278 % \end{macrocode}
2279 % \end{variable}
2280 %
2281 % \begin{variable}{\c_@@_model_range_CIELAB_tl}
2282 % The range for CIELAB color spaces.
2283 % \begin{macrocode}
2284 \tl_const:Nn \c_@@_model_range_CIELAB_tl { 0 ~ 100 ~ -128 ~ 127 ~ -128 ~ 127 }
2285 % \end{macrocode}
2286 % \end{variable}
2287 %
2288 % \begin{variable}{\g_@@_alternative_model_prop}
2289 % For tracking the alternative model set up for separations, etc.
2290 % \begin{macrocode}
2291 \prop_new:N \g_@@_alternative_model_prop
2292 \clist_map_inline:nn { cyan , magenta , yellow , black }
2293 { \prop_gput:Nnn \g_@@_alternative_model_prop {#1} { cmyk } }
2294 \clist_map_inline:nn { red , green , blue }
2295 { \prop_gput:Nnn \g_@@_alternative_model_prop {#1} { rgb } }
2296 % \end{macrocode}
2297 % \end{variable}
2298 %
2299 % \begin{variable}{\g_@@_alternative_values_prop}
2300 % Same for the values: a bit more involved.
2301 % \begin{macrocode}
2302 \prop_new:N \g_@@_alternative_values_prop
2303 \prop_gput:Nnn \g_@@_alternative_values_prop { cyan } { 1 , 0 , 0 , 0 }
2304 \prop_gput:Nnn \g_@@_alternative_values_prop { magenta } { 0 , 1 , 0 , 0 }
2305 \prop_gput:Nnn \g_@@_alternative_values_prop { yellow } { 0 , 0 , 1 , 0 }
2306 \prop_gput:Nnn \g_@@_alternative_values_prop { black } { 0 , 0 , 0 , 1 }
2307 \prop_gput:Nnn \g_@@_alternative_values_prop { red } { 1 , 0 , 0 }
2308 \prop_gput:Nnn \g_@@_alternative_values_prop { green } { 0 , 1 , 0 }
2309 \prop_gput:Nnn \g_@@_alternative_values_prop { blue } { 0 , 0 , 1 }
2310 % \end{macrocode}
2311 % \end{variable}
2312 %
2313 % \begin{macro}{\color_model_new:nnn, \@@_model_new:nnn}
2314 % Set up a new model: in general this has to be handled by a family-dependent
2315 % function. To avoid some \enquote{interesting} questions with casing, we
2316 % fold the case of the family name. The key--value list should always be
2317 % present, so we convert it up-front to a |prop|, then deal with the detail
2318 % on a per-family basis.
2319 % \begin{macrocode}
2320 \cs_new_protected:Npn \color_model_new:nnn #1#2#3
2321 {
2322 \exp_args:Nee \@@_model_new:nnn
2323 { \tl_to_str:n {#1} }
2324 { \str_casefold:n {#2} } {#3}
2325 }
2326 \cs_new_protected:Npn \@@_model_new:nnn #1#2#3
2327 {
2328 \cs_if_exist:cTF { @@_parse_model_ #1 :w }
2329 {
2330 \msg_error:nnn { color } { model-already-defined } {#1}
2331 }
2332 {
2333 \cs_if_exist:cTF { @@_model_ #2 :n }
2334 {
2335 \prop_set_from_keyval:Nn \l_@@_tmp_prop {#3}
2336 \use:c { @@_model_ #2 :n } {#1}
2337 }
2338 {
2339 \msg_error:nnn { color } { unknown-model-type } {#2}
2340 }
2341 }
2342 }
2343 % \end{macrocode}
2344 % \end{macro}
2345 %
2346 % \begin{macro}{\@@_model_init:nnn, \@@_model_init:nne}
2347 % A shared auxiliary to do the basics of setting up a new model: reserve a
2348 % number, create a white-equivalent, set up links to the backend.
2349 % \begin{macrocode}
2350 \cs_new_protected:Npn \@@_model_init:nnn #1#2#3
2351 {
2352 \int_gincr:N \g_@@_model_int
2353 \clist_map_inline:nn { fill , stroke , select }
2354 {
2355 \cs_new_protected:cpe { @@_backend_ ##1 _ #1 :n } ####1
2356 {
2357 \exp_not:c { @@_backend_ ##1 _ #2 :nn }
2358 { color \int_use:N \g_@@_model_int } {####1}
2359 }
2360 }
2361 \cs_new_protected:cpe { @@_model_ #1 _white: }
2362 {
2363 \prop_put:Nnn \exp_not:N \l_@@_named_white_prop {#1}
2364 { \exp_not:n {#3} }
2365 \exp_not:N \int_compare:nNnF { \tex_currentgrouplevel:D } = 0
2366 { \group_insert_after:N \exp_not:c { @@_model_ #1 _ white: } }
2367 }
2368 \use:c { @@_model_ #1 _white: }
2369 }
2370 \cs_generate_variant:Nn \@@_model_init:nnn { nne }
2371 % \end{macrocode}
2372 % \end{macro}
2373 %
2374 % \begin{macro}{\@@_model_separation:n}
2375 % \begin{macro}{\@@_model_separation:nn}
2376 % \begin{macro}{\@@_model_separation:nnn}
2377 % \begin{macro}{\@@_model_separation:w}
2378 % \begin{macro}
2379 % {
2380 % \@@_model_separation_cmyk:nnnnnn ,
2381 % \@@_model_separation_gray:nnnnnn ,
2382 % \@@_model_separation_rgb:nnnnnn
2383 % }
2384 % \begin{macro}{\@@_model_convert:nnn}
2385 % \begin{macro}{\@@_model_separation_CIELAB:nnnnnn}
2386 % \begin{macro}{\@@_model_separation_CIELAB:nnnnnnn}
2387 % Separations must have a \enquote{real} name, which is pretty easy to find.
2388 % \begin{macrocode}
2389 \cs_new_protected:Npn \@@_model_separation:n #1
2390 {
2391 \prop_get:NnNTF \l_@@_tmp_prop { name }
2392 \l_@@_tmp_tl
2393 {
2394 \exp_args:NV \@@_model_separation:nn
2395 \l_@@_tmp_tl {#1}
2396 }
2397 {
2398 \msg_error:nnn { color }
2399 { separation-requires-name } {#1}
2400 }
2401 }
2402 % \end{macrocode}
2403 % We have two keys to find at this stage: the alternative space model
2404 % and linked values.
2405 % \begin{macrocode}
2406 \cs_new_protected:Npn \@@_model_separation:nn #1#2
2407 {
2408 \prop_get:NnNTF \l_@@_tmp_prop { alternative-model }
2409 \l_@@_tmp_tl
2410 {
2411 \exp_args:NV \@@_model_separation:nnn
2412 \l_@@_tmp_tl {#2} {#1}
2413 }
2414 {
2415 \msg_error:nnn { color }
2416 { separation-alternative-model } {#2}
2417 }
2418 }
2419 \cs_new_protected:Npn \@@_model_separation:nnn #1#2#3
2420 {
2421 \cs_if_exist:cTF { @@_model_separation_ #1 :nnnnnn }
2422 {
2423 \prop_get:NnNTF \l_@@_tmp_prop { alternative-values }
2424 \l_@@_tmp_tl
2425 {
2426 \exp_after:wN \@@_model_separation:w \l_@@_tmp_tl
2427 , 0 , 0 , 0 , 0 \s_@@_stop {#2} {#3} {#1}
2428 }
2429 {
2430 \msg_error:nnn { color }
2431 { separation-alternative-values } {#2}
2432 }
2433 }
2434 {
2435 \msg_error:nnn { color }
2436 { unknown-alternative-model } {#1}
2437 }
2438 }
2439 % \end{macrocode}
2440 % As each alternative space leads to a different requirement for conversion,
2441 % and as there are only a small number of choices, we manually split the data
2442 % and then set up. Notice that mixing tints is really just the same
2443 % as mixing \texttt{gray}. The \texttt{white} color is special, as it allows
2444 % tints to be adjusted without an additional color space. To make sure the
2445 % data is set for that at all group levels, we need to work on a per-level
2446 % basis. Within the output, only the set-up needs the \enquote{real} name
2447 % of the colorspace: we use a simple tracking number for general usage
2448 % as this is a clear namespace without issues of escaping chars.
2449 % \begin{macrocode}
2450 \cs_new_protected:Npn \@@_model_separation:w
2451 #1 , #2 , #3 , #4 , #5 \s_@@_stop #6#7#8
2452 {
2453 \@@_model_init:nnn {#6} { separation } { 0 }
2454 \cs_new_eq:cN { @@_parse_mix_ #6 :nw } \@@_parse_mix_gray:nw
2455 \cs_new:cpn { @@_parse_model_ #6 :w } ##1 , ##2 \s_@@_stop
2456 { {#6} { \@@_parse_number:n {##1} } }
2457 \use:c { @@_model_separation_ #8 :nnnnnn }
2458 {#6} {#7} {#1} {#2} {#3} {#4}
2459 \prop_gput:Nnn \g_@@_alternative_model_prop {#6} {#8}
2460 \prop_gput:Nne \g_@@_colorants_prop {#6}
2461 { \str_convert_pdfname:n {#7} }
2462 }
2463 \cs_new_protected:Npn \@@_model_separation_cmyk:nnnnnn #1#2#3#4#5#6
2464 {
2465 \tl_const:cn { c_@@_fallback_ #1 _tl } { cmyk }
2466 \cs_new:cpn { @@_convert_ #1 _cmyk:w } ##1 \s_@@_stop
2467 {
2468 \fp_eval:n {##1 * #3} ~
2469 \fp_eval:n {##1 * #4} ~
2470 \fp_eval:n {##1 * #5} ~
2471 \fp_eval:n {##1 * #6}
2472 }
2473 \cs_new:cpn { @@_convert_cmyk_ #1 :w } ##1 \s_@@_stop { 1 }
2474 \prop_gput:Nnn \g_@@_alternative_values_prop {#1} { #3 , #4 , #5 , #6 }
2475 \@@_backend_separation_init:nnnnn {#2} { /DeviceCMYK } { }
2476 { 0 ~ 0 ~ 0 ~ 0 } { #3 ~ #4 ~ #5 ~ #6 }
2477 }
2478 \cs_new_protected:Npn \@@_model_separation_rgb:nnnnnn #1#2#3#4#5#6
2479 {
2480 \tl_const:cn { c_@@_fallback_ #1 _tl } { rgb }
2481 \cs_new:cpn { @@_convert_ #1 _rgb:w } ##1 \s_@@_stop
2482 {
2483 \fp_eval:n {##1 * #3} ~
2484 \fp_eval:n {##1 * #4} ~
2485 \fp_eval:n {##1 * #5}
2486 }
2487 \cs_new:cpn { @@_convert_rgb_ #1 :w } ##1 \s_@@_stop { 1 }
2488 \prop_gput:Nnn \g_@@_alternative_values_prop {#1} { #3 , #4 , #5 }
2489 \@@_backend_separation_init:nnnnn {#2} { /DeviceRGB } { }
2490 { 0 ~ 0 ~ 0 } { #3 ~ #4 ~ #5 }
2491 }
2492 \cs_new_protected:Npn \@@_model_separation_gray:nnnnnn #1#2#3#4#5#6
2493 {
2494 \tl_const:cn { c_@@_fallback_ #1 _tl } { gray }
2495 \cs_new:cpn { @@_convert_ #1 _gray:w } ##1 \s_@@_stop
2496 { \fp_eval:n {##1 * #3} }
2497 \cs_new:cpn { @@_convert_gray_ #1 :w } ##1 \s_@@_stop { 1 }
2498 \prop_gput:Nnn \g_@@_alternative_values_prop {#1} {#3}
2499 \@@_backend_separation_init:nnnnn {#2} { /DeviceGray } { } { 0 } {#3}
2500 }
2501 % \end{macrocode}
2502 % Generic model conversion \emph{via} an alternative intermediate.
2503 % \begin{macrocode}
2504 \cs_new_protected:Npn \@@_model_convert:nnn #1#2#3
2505 {
2506 \cs_new:cpe { @@_convert_ #1 _ #3 :w } ##1 \s_@@_stop
2507 {
2508 \exp_not:N \exp_args:NNe \exp_not:N \use:nn
2509 \exp_not:c { @@_convert_ #2 _ #3 :w }
2510 { \exp_not:c { @@_convert_ #1 _ #2 :w } ##1 \s_@@_stop }
2511 \c_space_tl \exp_not:N \s_@@_stop
2512 }
2513 }
2514 % \end{macrocode}
2515 % Setting up for CIELAB needs a bit more work: there is the illuminant and
2516 % the need for an appropriate object.
2517 % \begin{macrocode}
2518 \cs_new_protected:Npn \@@_model_separation_CIELAB:nnnnnn #1#2#3#4#5#6
2519 {
2520 \prop_get:NnNF \l_@@_tmp_prop { illuminant }
2521 \l_@@_tmp_tl
2522 {
2523 \msg_error:nnn { color }
2524 { CIELAB-requires-illuminant } {#1}
2525 \tl_set:Nn \l_@@_tmp_tl { d50 }
2526 }
2527 \exp_args:NV \@@_model_separation_CIELAB:nnnnnnn
2528 \l_@@_tmp_tl {#1} {#2} {#3} {#4} {#5} {#6}
2529 }
2530 % \end{macrocode}
2531 % If a CIELAB space is being set up, we need the illuminant, then create
2532 % the appropriate set up. At present, this doesn't include \texttt{BlackPoint}
2533 % or \texttt{Range} data, but that may be added later. As CIELAB colors
2534 % cannot be converted to anything else, we fallback to producing black in the
2535 % gray colorspace: the user should set up a second model for colors set up this way.
2536 % \begin{macrocode}
2537 \cs_new_protected:Npn \@@_model_separation_CIELAB:nnnnnnn #1#2#3#4#5#6#7
2538 {
2539 \tl_if_exist:cTF { c_@@_model_whitepoint_CIELAB_ #1 _tl }
2540 {
2541 \@@_backend_separation_init_CIELAB:nnn {#1} {#3} { #4 ~ #5 ~ #6 }
2542 \tl_const:cn { c_@@_fallback_ #2 _tl } { gray }
2543 \cs_new:cpn { @@_convert_ #2 _gray:w } ##1 \s_@@_stop
2544 { 0 }
2545 \cs_new:cpn { @@_convert_gray_ #2 :w } ##1 \s_@@_stop
2546 { 1 }
2547 }
2548 {
2549 \msg_error:nnn { color }
2550 { unknown-CIELAB-illuminant } {#1}
2551 }
2552 }
2553 % \end{macrocode}
2554 % \end{macro}
2555 % \end{macro}
2556 % \end{macro}
2557 % \end{macro}
2558 % \end{macro}
2559 % \end{macro}
2560 % \end{macro}
2561 % \end{macro}
2562 %
2563 % \begin{macro}{\@@_model_devicen:n}
2564 % \begin{macro}{\@@_model_devicen:nn}
2565 % \begin{macro}{\@@_model_devicen:nnn}
2566 % \begin{macro}{\@@_model_devicen:nnnn}
2567 % \begin{macro}
2568 % {
2569 % \@@_model_devicen_parse_1:nn ,
2570 % \@@_model_devicen_parse_2:nn ,
2571 % \@@_model_devicen_parse_3:nn ,
2572 % \@@_model_devicen_parse_4:nn ,
2573 % \@@_model_devicen_parse_generic:nn
2574 % }
2575 % \begin{macro}[EXP]{\@@_model_devicen_parse:nw}
2576 % \begin{macro}[EXP]{\@@_model_devicen_mix:nw}
2577 % \begin{macro}{\@@_model_devicen_init:nnn}
2578 % \begin{macro}{\@@_model_devicen_init:nnnn}
2579 % \begin{macro}{\@@_model_devicen_transform:w}
2580 % \begin{macro}
2581 % {
2582 % \@@_model_devicen_transform_1:nnnnn ,
2583 % \@@_model_devicen_transform_3:nnnnn ,
2584 % \@@_model_devicen_transform_4:nnnnn ,
2585 % }
2586 % \begin{macro}{\@@_model_devicen_transform:nnn}
2587 % \begin{macro}[EXP]{\@@_model_devicen_colorant:n}
2588 % \begin{macro}{\@@_model_devicen_convert:nnn}
2589 % \begin{macro}
2590 % {
2591 % \@@_model_devicen_convert_cmyk:n ,
2592 % \@@_model_devicen_convert_gray:n ,
2593 % \@@_model_devicen_convert_rgb:n
2594 % }
2595 % \begin{macro}{\@@_model_devicen_convert:nnnn}
2596 % \begin{macro}[EXP]{\@@_model_devicen_convert:n, \@@_model_devicen_convert_aux:n}
2597 % \begin{macro}[EXP]{\@@_model_devicen_convert:w}
2598 % \begin{macro}[EXP]{\@@_convert_devicen_cmyk:nnnnw}
2599 % \begin{macro}[EXP]{\@@_convert_devicen_cmyk:nnnnnnnnn}
2600 % \begin{macro}[EXP]{\@@_convert_devicen_cmyk_aux:nnnnw}
2601 % \begin{macro}[EXP]{\@@_convert_devicen_gray:nw}
2602 % \begin{macro}[EXP]{\@@_convert_devicen_gray:nnn}
2603 % \begin{macro}[EXP]{\@@_convert_devicen_gray_aux:nw}
2604 % \begin{macro}[EXP]{\@@_convert_devicen_rgb:nnnw}
2605 % \begin{macro}[EXP]{\@@_convert_devicen_rgb:nnnnnnn}
2606 % \begin{macro}[EXP]{\@@_convert_devicen_rgb_aux:nnnw}
2607 % We require a list of component names here: one might call them colorants,
2608 % but it's convenient to use \TeX{} names instead so we slightly adjust the
2609 % terminology.
2610 % \begin{macrocode}
2611 \cs_new_protected:Npn \@@_model_devicen:n #1
2612 {
2613 \prop_get:NnNTF \l_@@_tmp_prop { names }
2614 \l_@@_tmp_tl
2615 {
2616 \exp_args:NV \@@_model_devicen:nn
2617 \l_@@_tmp_tl {#1}
2618 }
2619 {
2620 \msg_error:nnn { color }
2621 { DeviceN-requires-names } {#1}
2622 }
2623 }
2624 % \end{macrocode}
2625 % All valid models will have an alternative listed, either hard-coded for
2626 % the core device ones, or dynamically added for Separations, etc.
2627 % \begin{macrocode}
2628 \cs_new_protected:Npn \@@_model_devicen:nn #1#2
2629 {
2630 \tl_clear:N \l_@@_model_tl
2631 \clist_map_inline:nn {#1}
2632 {
2633 \prop_get:NnNTF \g_@@_alternative_model_prop {##1}
2634 \l_@@_tmp_tl
2635 {
2636 \tl_if_empty:NTF \l_@@_model_tl
2637 { \tl_set_eq:NN \l_@@_model_tl \l_@@_tmp_tl }
2638 {
2639 \str_if_eq:VVF \l_@@_model_tl \l_@@_tmp_tl
2640 {
2641 \msg_error:nnn { color }
2642 { DeviceN-inconsistent-alternative }
2643 {#2}
2644 \clist_map_break:n { \use_none:nnnn }
2645 }
2646 }
2647 }
2648 {
2649 \str_if_eq:nnF {##1} { none }
2650 {
2651 \msg_error:nnn { color }
2652 { DeviceN-no-alternative }
2653 {#2}
2654 }
2655 }
2656 }
2657 \tl_if_empty:NTF \l_@@_model_tl
2658 {
2659 \msg_error:nnn { color }
2660 { DeviceN-no-alternative } {#2}
2661 }
2662 { \exp_args:NV \@@_model_devicen:nnn \l_@@_model_tl {#1} {#2} }
2663 }
2664 % \end{macrocode}
2665 % We now complete the data we require by first finding out how many
2666 % colorants there are, then moving on to begin constructing the function
2667 % required to map to the alternative color space.
2668 % \begin{macrocode}
2669 \cs_new_protected:Npn \@@_model_devicen:nnn #1#2#3
2670 {
2671 \exp_args:Ne \@@_model_devicen:nnnn
2672 { \clist_count:n {#2} } {#1} {#2} {#3}
2673 }
2674 % \end{macrocode}
2675 % At this stage, we have checked everything is in place, so we can set up
2676 % the \TeX{} and backend data structures. As for separations, it's not really
2677 % possible in general to have a fallback, so we simply provide
2678 % \enquote{black} for each element.
2679 % \begin{macrocode}
2680 \cs_new_protected:Npn \@@_model_devicen:nnnn #1#2#3#4
2681 {
2682 \@@_model_init:nne {#4} { devicen }
2683 {
2684 0 \prg_replicate:nn { #1 - 1 } { ~ 0 }
2685 }
2686 \cs_if_exist_use:cF { @@_model_devicen_parse_ #1 :nn }
2687 { \@@_model_devicen_parse_generic:nn }
2688 {#4} {#1}
2689 \@@_model_devicen_init:nnn {#1} {#2} {#3}
2690 \@@_model_devicen_convert:nnne {#4} {#2} {#3}
2691 {
2692 1 \prg_replicate:nn { #1 - 1 } { ~ 1 }
2693 }
2694 }
2695 % \end{macrocode}
2696 % For short lists of DeviceN colors, we can use hand-tuned parsing. This
2697 % lines up with other models, where we allow for up to four components. For
2698 % larger spaces, rather than limit artificially, we use a somewhat slow
2699 % approach based on open-ended commas-lists.
2700 % \begin{macrocode}
2701 \cs_new_protected:cpn { @@_model_devicen_parse_1:nn } #1#2
2702 {
2703 \cs_new:cpn { @@_parse_model_ #1 :w } ##1 , ##2 \s_@@_stop
2704 { {#1} { \@@_parse_number:n {##1} } }
2705 \cs_new_eq:cN { @@_parse_mix_ #1 :nw } \@@_parse_mix_gray:nw
2706 }
2707 \cs_new_protected:cpn { @@_model_devicen_parse_2:nn } #1#2
2708 {
2709 \cs_new:cpn { @@_parse_model_ #1 :w } ##1 , ##2 , ##3 \s_@@_stop
2710 { {#1} { \@@_parse_number:n {##1} ~ \@@_parse_number:n {##2} } }
2711 \cs_new:cpn { @@_parse_mix_ #1 :nw }
2712 ##1##2 ~ ##3 \s_@@_mark ##4 ~ ##5 \s_@@_stop
2713 {
2714 \fp_eval:n { ##2 * ##1 + ##4 * ( 1 - ##1 ) } \c_space_tl
2715 \fp_eval:n { ##3 * ##1 + ##5 * ( 1 - ##1 ) }
2716 }
2717 }
2718 \cs_new_protected:cpn { @@_model_devicen_parse_3:nn } #1#2
2719 {
2720 \cs_new:cpn { @@_parse_model_ #1 :w } ##1 , ##2 , ##3 , ##4 \s_@@_stop
2721 {
2722 {#1}
2723 {
2724 \@@_parse_number:n {##1} ~
2725 \@@_parse_number:n {##2} ~
2726 \@@_parse_number:n {##3}
2727 }
2728 }
2729 \cs_new_eq:cN { @@_parse_mix_ #1 :nw } \@@_parse_mix_rgb:nw
2730 }
2731 \cs_new_protected:cpn { @@_model_devicen_parse_4:nn } #1#2
2732 {
2733 \cs_new:cpn { @@_parse_model_ #1 :w }
2734 ##1 , ##2 , ##3 , ##4 , ##5 \s_@@_stop
2735 {
2736 {#1}
2737 {
2738 \@@_parse_number:n {##1} ~
2739 \@@_parse_number:n {##2} ~
2740 \@@_parse_number:n {##3} ~
2741 \@@_parse_number:n {##4}
2742 }
2743 }
2744 \cs_new_eq:cN { @@_parse_mix_ #1 :nw } \@@_parse_mix_cmyk:nw
2745 }
2746 \cs_new_protected:Npn \@@_model_devicen_parse_generic:nn #1#2
2747 {
2748 \cs_new:cpn { @@_parse_model_ #1 :w } ##1 , ##2 \s_@@_stop
2749 {
2750 {#1}
2751 { \@@_model_devicen_parse:nw {#2} ##1 , ##2 , \q_nil , \s_@@_stop }
2752 }
2753 \cs_new:cpe { @@_parse_mix_ #1 :nw }
2754 ##1 ##2 \s_@@_mark ##3 \s_@@_stop
2755 {
2756 \exp_not:N \@@_model_devicen_mix:nw {##1}
2757 ##2 \c_space_tl \exp_not:N \q_nil \c_space_tl \exp_not:N \s_@@_mark
2758 ##3 \c_space_tl \exp_not:N \q_nil \c_space_tl \exp_not:N \s_@@_stop
2759 }
2760 }
2761 \cs_new:Npn \@@_model_devicen_parse:nw #1#2 , #3 \s_@@_stop
2762 {
2763 \int_compare:nNnT {#1} > 0
2764 {
2765 \quark_if_nil:nTF {#2}
2766 { \prg_replicate:nn {#1} { 0 ~ } }
2767 {
2768 \@@_parse_number:n {#2}
2769 \int_compare:nNnT {#1} > 1 { ~ }
2770 \exp_args:Nf \@@_model_devicen_parse:nw
2771 { \int_eval:n { #1 - 1 } } #3 \s_@@_stop
2772 }
2773 }
2774 }
2775 \cs_new:Npn \@@_model_devicen_mix:nw #1#2 ~ #3 \s_@@_mark #4 ~ #5 \s_@@_stop
2776 {
2777 \fp_eval:n { #2 * #1 + #4 * ( 1 - #1 ) }
2778 \quark_if_nil:oF { \tl_head:w #3 \q_stop }
2779 {
2780 \c_space_tl
2781 \@@_model_devicen_mix:nw {#1} #3 \s_@@_mark #5 \s_@@_stop
2782 }
2783 }
2784 % \end{macrocode}
2785 % To construct the tint transformation, we have to use PostScript. The
2786 % aim is to have the final tint for each device colorant as
2787 % \[
2788 % 1 - \prod_{n} (1 - X_{n} D_{X_{n}})
2789 % \]
2790 % where $X$ is a DeviceN colorant and $D$ is the amount of device colorant
2791 % that the DeviceN colorant maps to. At the start of the process, the
2792 % PostScript stack will contain the $X_{n}$ values, whilst we have the
2793 % $D$ values on a per-DeviceN colorant basis. The more convenient approach
2794 % for us is therefore to take each DeviceN colorant in turn and find the
2795 % value $1 - X_{n} D_{X_{n}}$, multiplying as we go, and finalize with the
2796 % subtraction. That contrasts to \pkg{colorspace}: it splits the process
2797 % up by process color, which works better when you have a fixed list
2798 % of colorants. (\pkg{colorspace} only supports up to $4$ DeviceN colors,
2799 % and only \texttt{cmyk} as the alternative space.) To set this up,
2800 % we first need to know the number of values in the target color space:
2801 % this is easily handled as there are a very small range of possibles.
2802 % Once we have that information, it's relatively easy to build the required
2803 % PostScript using some generic code.
2804 % \begin{macrocode}
2805 \cs_new_protected:Npn \@@_model_devicen_init:nnn #1#2#3
2806 {
2807 \exp_args:Ne \@@_model_devicen_init:nnnn
2808 {
2809 \str_case:nn {#2}
2810 {
2811 { cmyk } { 4 }
2812 { gray } { 1 }
2813 { rgb } { 3 }
2814 }
2815 }
2816 {#1} {#2} {#3}
2817 }
2818 % \end{macrocode}
2819 % As we always need to split the alternative values into parts, we use a
2820 % shared auxiliary and only use a minimal difference between code paths.
2821 % Construction of the tint transformation is as far as possible done using
2822 % loops, which means there are some inefficiencies for device colors in
2823 % the \texttt{DeviceN} space: we roll the stack one-at-a-time even if there
2824 % is a potential shortcut. However, that way there is nothing to special-case.
2825 % Once this is sorted, we can write the tint transform object, which will
2826 % remain as the last object until we sort out the final step: the colorant
2827 % list.
2828 % \begin{macrocode}
2829 \cs_new_protected:Npn \@@_model_devicen_init:nnnn #1#2#3#4
2830 {
2831 \tl_set:Ne \l_@@_tmp_tl
2832 { \prg_replicate:nn {#1} { 1.0 ~ } }
2833 \int_zero:N \l_@@_tmp_int
2834 \clist_map_inline:nn {#4}
2835 {
2836 \int_incr:N \l_@@_tmp_int
2837 \prop_get:NnN \g_@@_alternative_values_prop {##1}
2838 \l_@@_value_tl
2839 \exp_after:wN \@@_model_devicen_transform:w
2840 \l_@@_value_tl , 0 , 0 , 0 , \s_@@_stop {#1} {#2}
2841 }
2842 \tl_put_right:Ne \l_@@_tmp_tl
2843 {
2844 \prg_replicate:nn {#1}
2845 { neg ~ 1.0 ~ add ~ #1 ~ -1 ~ roll ~ }
2846 \int_eval:n { #2 + #1 } ~ #1 ~ roll
2847 \prg_replicate:nn {#2} { ~ pop } ~
2848 #1 ~ 1 ~ roll
2849 }
2850 \use:e
2851 {
2852 \@@_backend_devicen_init:nnn
2853 {
2854 \clist_map_function:nN {#4}
2855 \@@_model_devicen_colorant:n
2856 }
2857 {
2858 \str_case:nn {#3}
2859 {
2860 { cmyk } { /DeviceCMYK }
2861 { gray } { /DeviceGray }
2862 { rgb } { /DeviceRGB }
2863 }
2864 }
2865 { \exp_not:V \l_@@_tmp_tl }
2866 }
2867 }
2868 \cs_new_protected:Npn \@@_model_devicen_transform:w
2869 #1 , #2 , #3 , #4 , #5 \s_@@_stop #6#7
2870 {
2871 \use:c { @@_model_devicen_transform_ #6 :nnnnn }
2872 {#1} {#2} {#3} {#4} {#7}
2873 }
2874 \cs_new_protected:cpn { @@_model_devicen_transform_1:nnnnn } #1#2#3#4#5
2875 { \@@_model_devicen_transform:nnn {#5} { 1 } {#1} }
2876 \cs_new_protected:cpn { @@_model_devicen_transform_3:nnnnn } #1#2#3#4#5
2877 {
2878 \clist_map_inline:nn { #1 , #2 , #3 }
2879 { \@@_model_devicen_transform:nnn {#5} { 3 } {##1} }
2880 }
2881 \cs_new_protected:cpn { @@_model_devicen_transform_4:nnnnn } #1#2#3#4#5
2882 {
2883 \clist_map_inline:nn { #1 , #2 , #3 , #4 }
2884 { \@@_model_devicen_transform:nnn {#5} { 4 } {##1} }
2885 }
2886 \cs_new_protected:Npn \@@_model_devicen_transform:nnn #1#2#3
2887 {
2888 \tl_put_right:Ne \l_@@_tmp_tl
2889 {
2890 \fp_compare:nNnF {#3} = \c_zero_fp
2891 {
2892 \int_eval:n { #1 - \l_@@_tmp_int + #2 } ~ index ~
2893 -#3 ~ mul ~ 1.0 ~ add ~ mul ~
2894 }
2895 #2 ~ -1 ~ roll ~
2896 }
2897 }
2898 \cs_new:Npn \@@_model_devicen_colorant:n #1
2899 {
2900 / \prop_item:Nn \g_@@_colorants_prop {#1} ~
2901 }
2902 % \end{macrocode}
2903 % Here we need to set up conversion from the DeviceN space to the alternative
2904 % at the \TeX{} level. This also means supplying methods for inter-converting
2905 % to other parameter-based spaces. Essentially the approach is exactly the same
2906 % as the PostScript, just expressed in \TeX{} terms.
2907 % \begin{macrocode}
2908 \cs_new_protected:Npn \@@_model_devicen_convert:nnnn #1#2#3
2909 {
2910 \use:c { @@_model_devicen_convert_ #2 :nnn } {#1} {#3}
2911 }
2912 \cs_generate_variant:Nn \@@_model_devicen_convert:nnnn { nnne }
2913 \cs_new_protected:Npn \@@_model_devicen_convert_cmyk:nnn #1#2
2914 {
2915 \tl_const:cn { c_@@_fallback_ #1 _tl } { cmyk }
2916 \@@_model_devicen_convert:nnnnn {#1} { cmyk } { 4 } {#2}
2917 }
2918 \cs_new_protected:Npn \@@_model_devicen_convert_gray:nnn #1#2
2919 {
2920 \tl_const:cn { c_@@_fallback_ #1 _tl } { gray }
2921 \@@_model_devicen_convert:nnnnn {#1} { gray } { 1 } {#2}
2922 }
2923 \cs_new_protected:Npn \@@_model_devicen_convert_rgb:nnn #1#2
2924 {
2925 \tl_const:cn { c_@@_fallback_ #1 _tl } { rgb }
2926 \@@_model_devicen_convert:nnnnn {#1} { rgb } { 3 } {#2}
2927 }
2928 \cs_new_protected:Npn \@@_model_devicen_convert:nnnnn #1#2#3#4#5
2929 {
2930 \cs_new:cpn { @@_convert_ #2 _ #1 :w } ##1 \s_@@_stop {#5}
2931 \cs_new:cpe { @@_convert_ #1 _ #2 :w } ##1 \s_@@_stop
2932 {
2933 \exp_not:c { @@_convert_devicen_ #2 : \prg_replicate:nn {#3} { n } w }
2934 \prg_replicate:nn {#3} { { 1 } }
2935 ##1 ~ \exp_not:N \s_@@_mark
2936 \clist_map_function:nN {#4} \@@_model_devicen_convert:n
2937 {}
2938 \exp_not:N \s_@@_stop
2939 }
2940 }
2941 \cs_new:Npn \@@_model_devicen_convert:n #1
2942 {
2943 {
2944 \exp_args:Ne \@@_model_devicen_convert_aux:n
2945 { \prop_item:Nn \g_@@_alternative_values_prop {#1} }
2946 }
2947 }
2948 \cs_new:Npn \@@_model_devicen_convert_aux:n #1
2949 { \@@_model_devicen_convert_aux:w #1 , , , , \s_@@_stop }
2950 \cs_new:Npn \@@_model_devicen_convert_aux:w #1 , #2 , #3 , #4 , #5 \s_@@_stop
2951 {
2952 {#1}
2953 \tl_if_blank:nF {#2}
2954 {
2955 {#2}
2956 \tl_if_blank:nF {#3}
2957 {
2958 {#3}
2959 \tl_if_blank:nF {#4} { {#4} }
2960 }
2961 }
2962 }
2963 \cs_new:Npn \@@_convert_devicen_cmyk:nnnnw
2964 #1#2#3#4#5 ~ #6 \s_@@_mark #7#8 \s_@@_stop
2965 {
2966 \@@_convert_devicen_cmyk:nnnnnnnnn {#5} {#1} {#2} {#3} {#4} #7
2967 #6 \s_@@_mark #8 \s_@@_stop
2968 }
2969 \cs_new:Npn \@@_convert_devicen_cmyk:nnnnnnnnn #1#2#3#4#5#6#7#8#9
2970 {
2971 \use:e
2972 {
2973 \exp_not:N \@@_convert_devicen_cmyk_aux:nnnnw
2974 { \fp_eval:n { #2 * (1 - (#1 * #6)) } }
2975 { \fp_eval:n { #3 * (1 - (#1 * #7)) } }
2976 { \fp_eval:n { #4 * (1 - (#1 * #8)) } }
2977 { \fp_eval:n { #5 * (1 - (#1 * #9)) } }
2978 }
2979 }
2980 \cs_new:Npn \@@_convert_devicen_cmyk_aux:nnnnw
2981 #1#2#3#4 #5 \s_@@_mark #6 \s_@@_stop
2982 {
2983 \tl_if_blank:nTF {#5}
2984 {
2985 \fp_eval:n { 1 - #1 } ~
2986 \fp_eval:n { 1 - #2 } ~
2987 \fp_eval:n { 1 - #3 } ~
2988 \fp_eval:n { 1 - #4 }
2989 }
2990 {
2991 \@@_convert_devicen_cmyk:nnnnw {#1} {#2} {#3} {#4}
2992 #5 \s_@@_mark #6 \s_@@_stop
2993 }
2994 }
2995 \cs_new:Npn \@@_convert_devicen_gray:nw
2996 #1#2 ~ #3 \s_@@_mark #4#5 \s_@@_stop
2997 {
2998 \@@_convert_devicen_gray:nnn {#2} {#1} #4
2999 #3 \s_@@_mark #5 \s_@@_stop
3000 }
3001 \cs_new:Npn \@@_convert_devicen_gray:nnn #1#2#3
3002 {
3003 \exp_arsgs:Ne \@@_convert_devicen_gray_aux:nw
3004 { \fp_eval:n { #2 * (1 - (#1 * #3)) } }
3005 }
3006 \cs_new:Npn \@@_convert_devicen_gray_aux:nw
3007 #1 #2 \s_@@_mark #3 \s_@@_stop
3008 {
3009 \tl_if_blank:nTF {#2}
3010 { \fp_eval:n { 1 - #1 } }
3011 {
3012 \@@_convert_devicen_gray:nw {#1}
3013 #2 \s_@@_mark #3 \s_@@_stop
3014 }
3015 }
3016 \cs_new:Npn \@@_convert_devicen_rgb:nnnw
3017 #1#2#3#4 ~ #5 \s_@@_mark #6#7 \s_@@_stop
3018 {
3019 \@@_convert_devicen_rgb:nnnnnnn {#4} {#1} {#2} {#3} #6
3020 #5 \s_@@_mark #7 \s_@@_stop
3021 }
3022 \cs_new:Npn \@@_convert_devicen_rgb:nnnnnnn #1#2#3#4#5#6#7
3023 {
3024 \use:e
3025 {
3026 \exp_not:N \@@_convert_devicen_rgb_aux:nnnw
3027 { \fp_eval:n { #2 * (1 - (#1 * #5)) } }
3028 { \fp_eval:n { #3 * (1 - (#1 * #6)) } }
3029 { \fp_eval:n { #4 * (1 - (#1 * #7)) } }
3030 }
3031 }
3032 \cs_new:Npn \@@_convert_devicen_rgb_aux:nnnw
3033 #1#2#3 #4 \s_@@_mark #5 \s_@@_stop
3034 {
3035 \tl_if_blank:nTF {#4}
3036 {
3037 \fp_eval:n { 1 - #1 } ~
3038 \fp_eval:n { 1 - #2 } ~
3039 \fp_eval:n { 1 - #3 }
3040 }
3041 {
3042 \@@_convert_devicen_rgb:nnnw {#1} {#2} {#3}
3043 #4 \s_@@_mark #5 \s_@@_stop
3044 }
3045 }
3046 % \end{macrocode}
3047 % \end{macro}
3048 % \end{macro}
3049 % \end{macro}
3050 % \end{macro}
3051 % \end{macro}
3052 % \end{macro}
3053 % \end{macro}
3054 % \end{macro}
3055 % \end{macro}
3056 % \end{macro}
3057 % \end{macro}
3058 % \end{macro}
3059 % \end{macro}
3060 % \end{macro}
3061 % \end{macro}
3062 % \end{macro}
3063 % \end{macro}
3064 % \end{macro}
3065 % \end{macro}
3066 % \end{macro}
3067 % \end{macro}
3068 % \end{macro}
3069 % \end{macro}
3070 % \end{macro}
3071 % \end{macro}
3072 % \end{macro}
3073 % \end{macro}
3074 %
3075 % \begin{variable}{\c_@@_icc_colorspace_signatures_prop}
3076 % The signatures in the ICC file header indicating the underlying
3077 % colorspace. We map it to three values: The number of components,
3078 % the values corresponding to white, and the range.
3079 % \begin{macrocode}
3080 \prop_const_from_keyval:Nn \c_@@_icc_colorspace_signatures_prop
3081 {
3082 % Gray
3083 47524159 = {1} {1} {0} {},
3084 % RGB
3085 52474220 = {3} {0~0~0} {1~1~1} {},
3086 % CMYK
3087 434D594B = {4} {0~0~0~1} {0~0~0~0} {},
3088 % Lab
3089 4C616220 = {3} {0~0~0} {100~0~0} {0~100~-128~127~-128~127}
3090 }
3091 % \end{macrocode}
3092 % \end{variable}
3093 % \begin{macro}{\@@_model_iccbased:n}
3094 % \begin{macro}{\@@_model_iccbased:nn}
3095 % \begin{macro}{\@@_model_iccbased:nnn, \@@_model_iccbased_aux:nnn}
3096 % For an ICC profile, we need a file name and a number of components. The
3097 % file name is processed here so the backend can treat it as a string.
3098 % \begin{macrocode}
3099 \cs_new_protected:Npn \@@_model_iccbased:n #1
3100 {
3101 \prop_get:NnNTF \l_@@_tmp_prop { file }
3102 \l_@@_tmp_tl
3103 {
3104 \exp_args:NV \@@_model_iccbased:nn
3105 \l_@@_tmp_tl {#1}
3106 }
3107 {
3108 \msg_error:nnn { color }
3109 { ICCBased-requires-file } {#1}
3110 }
3111 }
3112 \cs_new_protected:Npn \@@_model_iccbased:nn #1#2
3113 {
3114 \prop_get:NeNTF \c_@@_icc_colorspace_signatures_prop
3115 { \file_hex_dump:nnn { #1 } { 17 } { 20 } } \l_@@_tmp_tl
3116 {
3117 \exp_last_unbraced:NV \@@_model_iccbased_aux:nnnnnn
3118 \l_@@_tmp_tl { #2 } { #1 }
3119 }
3120 {
3121 \msg_error:nnn { color }
3122 { ICCBased-unsupported-colorspace } {#2}
3123 }
3124 }
3125 % \end{macrocode}
3126 % Here, we can use the same internals as for DeviceN approach as we know the
3127 % number of components. No conversion is possible, so there is no need
3128 % to worry about that at all.
3129 % \begin{macrocode}
3130 \cs_new_protected:Npn \@@_model_iccbased_aux:nnnnnn #1#2#3#4#5#6
3131 {
3132 \@@_model_init:nnn {#5} { iccbased } {#3}
3133 \tl_const:cn { c_@@_fallback_ #5 _tl } { gray }
3134 \cs_new:cpn { @@_convert_ #5 _gray:w } ##1 \s_@@_stop { 0 }
3135 \cs_new:cpn { @@_convert_gray_ #5 :w } ##1 \s_@@_stop { #2 }
3136 \use:c { @@_model_devicen_parse_ #1 :nn } {#5} {#1}
3137 \exp_args:Ne \@@_backend_iccbased_init:nnn
3138 { \file_full_name:n {#6} } {#1} {#4}
3139 }
3140 % \end{macrocode}
3141 % \end{macro}
3142 % \end{macro}
3143 % \end{macro}
3144 %
3145 % \subsection{Applying profiles}
3146 %
3147 % \begin{macro}{\color_profile_apply:nn, \@@_profile_apply:nn}
3148 % \begin{macro}
3149 % {
3150 % \@@_profile_apply_gray:n ,
3151 % \@@_profile_apply_rgb:n ,
3152 % \@@_profile_apply_cmyk:n
3153 % }
3154 % With a limited range of outcomes, this is largely about getting data to the
3155 % backend.
3156 % \begin{macrocode}
3157 \cs_new_protected:Npn \color_profile_apply:nn #1#2
3158 {
3159 \exp_args:Ne \@@_profile_apply:nn
3160 { \file_full_name:n {#1} } {#2}
3161 }
3162 \cs_new_protected:Npn \@@_profile_apply:nn #1#2
3163 {
3164 \cs_if_exist_use:cF { @@_profile_apply_ \tl_to_str:n {#2} :n }
3165 {
3166 \msg_error:nnn { color } { ICC-Device-unknown } {#2}
3167 \use_none:n
3168 }
3169 {#1}
3170 }
3171 \cs_new_protected:Npn \@@_profile_apply_gray:n #1
3172 {
3173 \int_gincr:N \g_@@_model_int
3174 \@@_backend_iccbased_device:nnn {#1} { Gray } { 1 }
3175 }
3176 \cs_new_protected:Npn \@@_profile_apply_rgb:n #1
3177 {
3178 \int_gincr:N \g_@@_model_int
3179 \@@_backend_iccbased_device:nnn {#1} { RGB } { 3 }
3180 }
3181 \cs_new_protected:Npn \@@_profile_apply_cmyk:n #1
3182 {
3183 \int_gincr:N \g_@@_model_int
3184 \@@_backend_iccbased_device:nnn {#1} { CMYK } { 4 }
3185 }
3186 % \end{macrocode}
3187 % \end{macro}
3188 % \end{macro}
3189 %
3190 % \subsection{Diagnostics}
3191 %
3192 % \begin{macro}{\color_show:n, \color_log:n, \@@_show:Nn}
3193 % \begin{macro}[EXP]{\@@_show:n}
3194 % Extract the information about a color and format for the user: the approach
3195 % is similar to the keys module here.
3196 % \begin{macrocode}
3197 \cs_new_protected:Npn \color_show:n
3198 { \@@_show:Nn \msg_show:nneeee }
3199 \cs_new_protected:Npn \color_log:n
3200 { \@@_show:Nn \msg_log:nneeee }
3201 \cs_new_protected:Npn \@@_show:Nn #1#2
3202 {
3203 #1 { color } { show }
3204 {#2}
3205 {
3206 \color_if_exist:nT {#2}
3207 {
3208 \exp_args:Nv \@@_show:n { l_@@_named_ #2 _tl }
3209 \prop_map_function:cN
3210 { l_@@_named_ #2 _prop }
3211 \msg_show_item_unbraced:nn
3212 }
3213 }
3214 { }
3215 { }
3216 }
3217 \cs_new:Npn \@@_show:n #1
3218 {
3219 \msg_show_item_unbraced:nn { model } {#1}
3220 }
3221 % \end{macrocode}
3222 % \end{macro}
3223 % \end{macro}
3224 %
3225 % \subsection{Messages}
3226 %
3227 % \begin{macrocode}
3228 \msg_new:nnnn { color } { CIELAB-requires-illuminant }
3229 { CIELAB~color~space~'#1'~require~an~illuminant. }
3230 {
3231 LaTeX~has~been~asked~to~create~a~separation~color~space~using~
3232 CIELAB~specifications,~but~no~\\ \\
3233 \iow_indent:n { illuminant~=~<basis> }
3234 \\ \\
3235 key~was~given~with~the~correct~information.~LaTeX~will~use~illuminant~
3236 'd50'~for~recovery.
3237 }
3238 \msg_new:nnnn { color } { conversion-not-available }
3239 { No~model~conversion~available~from~'#1'~to~'#2'. }
3240 {
3241 LaTeX~has~been~asked~to~convert~a~color~from~model~'#1'~
3242 to~model'#2',~but~there~is~no~method~available~to~do~that.
3243 }
3244 \msg_new:nnnn { color } { DeviceN-inconsistent-alternative }
3245 { DeviceN~color~spaces~require~a~single~alternative~space. }
3246 {
3247 LaTeX~has~been~asked~to~create~a~DeviceN~color~space~'#1',~
3248 but~the~constituent~colors~do~not~have~a~common~alternative~
3249 color.
3250 }
3251 \msg_new:nnnn { color } { DeviceN-no-alternative }
3252 { DeviceN~color~spaces~require~an~alternative~space. }
3253 {
3254 LaTeX~has~been~asked~to~create~a~DeviceN~color~space~'#1',~
3255 but~the~constituent~colors~do~not~all~have~a~device-based~alternative.
3256 }
3257 \msg_new:nnnn { color } { DeviceN-requires-names }
3258 { DeviceN~color~space~'#1'~require~a~list~of~names. }
3259 {
3260 LaTeX~has~been~asked~to~create~a~DeviceN~color~space,~
3261 but~no~\\ \\
3262 \iow_indent:n { names~=~<names> }
3263 \\ \\
3264 key~was~given~with~the~correct~information.
3265 }
3266 \msg_new:nnnn { color } { ICC-Device-unknown }
3267 { Unknown~device~color~space~'#1'. }
3268 {
3269 LaTeX~has~been~asked~to~apply~an~ICC~profile~but~the~device~color~space~
3270 '#1'~is~unknown.
3271 }
3272 \msg_new:nnnn { color } { ICCBased-unsupported-colorspace }
3273 { ICCBased~color~space~'#1'~uses~an~unsupported~data~color~space. }
3274 {
3275 LaTeX~has~been~asked~to~create~a~ICCBased~colorspace,~but~the~
3276 used~data~colorspace~is~not~supported.~ICC~profiles~used~for~
3277 defining~a~ICCBased~colorspace~should~use~a~Lab,~RGB,~or~
3278 CMYK~data~colorspace.~LaTeX~will~ignore~this~request.
3279 }
3280 \msg_new:nnnn { color } { ICCBased-requires-file }
3281 { ICCBased~color~space~'#1'~require~an~file. }
3282 {
3283 LaTeX~has~been~asked~to~create~an~ICCBased~color~space,~but~no~\\ \\
3284 \iow_indent:n { file~=~<name> }
3285 \\ \\
3286 key~was~given~with~the~correct~information.~LaTeX~will~ignore~this~
3287 request.
3288 }
3289 \msg_new:nnnn { color } { model-already-defined }
3290 { Color~model~'#1'~already~defined. }
3291 {
3292 LaTeX~was~asked~to~define~a~new~color~model~called~'#1',~but~
3293 this~color~model~already~exists.
3294 }
3295 \msg_new:nnnn { color } { out-of-range }
3296 { Input~value~#1~out~of~range~[#2,~#3]. }
3297 {
3298 LaTeX~was~expecting~a~value~in~the~range~[#2,~#3]~as~part~of~a~color,~
3299 but~you~gave~#1.~LaTeX~will~assume~you~meant~the~limit~of~the~range~
3300 and~continue.
3301 }
3302 \msg_new:nnnn { color } { separation-alternative-model }
3303 { Separation~color~space~'#1'~require~an~alternative~model. }
3304 {
3305 LaTeX~has~been~asked~to~create~a~separation~color~space,~
3306 but~no~\\ \\
3307 \iow_indent:n { alternative-model~=~<model> }
3308 \\ \\
3309 key~was~given~with~the~correct~information.
3310 }
3311 \msg_new:nnnn { color } { separation-alternative-values }
3312 { Separation~color~space~'#1'~require~values~for~the~alternative~space. }
3313 {
3314 LaTeX~has~been~asked~to~create~a~separation~color~space,~
3315 but~no~\\ \\
3316 \iow_indent:n { alternative-values~=~<model> }
3317 \\ \\
3318 key~was~given~with~the~correct~information.
3319 }
3320 \msg_new:nnnn { color } { separation-requires-name }
3321 { Separation~color~space~'#1'~require~a~formal~name. }
3322 {
3323 LaTeX~has~been~asked~to~create~a~separation~color~space,~
3324 but~no~\\ \\
3325 \iow_indent:n { name~=~<formal~name> }
3326 \\ \\
3327 key~was~given~with~the~correct~information.
3328 }
3329 \msg_new:nnn { color } { unhandled-model }
3330 {
3331 Unhandled~color~model~in~LaTeX2e~value~"#1":
3332 \\ \\
3333 falling~back~on~grayscale.
3334 }
3335 \msg_new:nnnn { color } { unknown-color }
3336 { Unknown~color~'#1'. }
3337 {
3338 LaTeX~has~been~asked~to~use~a~color~named~'#1',~
3339 but~this~has~never~been~defined.
3340 }
3341 \msg_new:nnnn { color } { unknown-alternative-model }
3342 { Separation~color~space~'#1'~require~an~valid~alternative~space. }
3343 {
3344 LaTeX~has~been~asked~to~create~a~separation~color~space,~
3345 but~the~model~given~as\\ \\
3346 \iow_indent:n { alternative-model~=~<model> }
3347 \\ \\
3348 is~unknown.
3349 }
3350 \msg_new:nnnn { color } { unknown-export-format }
3351 { Unknown~export~format~'#1'. }
3352 {
3353 LaTeX~has~been~asked~to~export~a~color~in~format~'#1',~
3354 but~this~has~never~been~defined.
3355 }
3356 \msg_new:nnnn { color } { unknown-CIELAB-illuminant }
3357 { Unknown~illuminant~model~'#1'. }
3358 {
3359 LaTeX~has~been~asked~to~use~create~a~color~space~using~CIELAB~
3360 illuminant~'#1',~but~this~does~not~exist.
3361 }
3362 \msg_new:nnnn { color } { unknown-model }
3363 { Unknown~color~model~'#1'. }
3364 {
3365 LaTeX~has~been~asked~to~use~a~color~model~called~'#1',~
3366 but~this~model~is~not~set~up.
3367 }
3368 \msg_new:nnnn { color } { unknown-model-type }
3369 { Unknown~color~model~type~'#1'. }
3370 {
3371 LaTeX~has~been~asked~to~create~a~new~color~model~called~'#1',~
3372 but~this~type~of~model~was~never~set~up.
3373 }
3374 \prop_gput:Nnn \g_msg_module_name_prop { color } { LaTeX }
3375 \prop_gput:Nnn \g_msg_module_type_prop { color } { }
3376 % \end{macrocode}
3377 %
3378 % \begin{macrocode}
3379 \msg_new:nnn { color } { show }
3380 {
3381 The~color~#1~
3382 \tl_if_empty:nTF {#2}
3383 { is~undefined. }
3384 { has~the~properties: #2 }
3385 }
3386 % \end{macrocode}
3387 %
3388 % \begin{macrocode}
3389 %</code>
3390 % \end{macrocode}
3391 %
3392 % \end{implementation}
3393 %
3394 % \PrintIndex