colo-ini.mkxl /size: 50 Kb    last modification: 2025-02-21 11:03
1%D \module
2%D   [       file=colo-ini,
3%D        version=2007.08.08,
4%D          title=\CONTEXT\ Color Macros,
5%D       subtitle=Initialization,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\writestatus{loading}{ConTeXt Color Macros / Initialization}
15
16%D Todo: move color space in color attribute (last two bits)
17
18%D This module implements color. Since \MKII\ and \MKIV\ use a completely
19%D different approach, this module only implements a few generic mechanisms.
20
21\installcorenamespace{color}
22\installcorenamespace{colorattribute}
23\installcorenamespace{transparencyattribute}
24\installcorenamespace{colorsetter}
25\installcorenamespace{transparencysetter}
26\installcorenamespace{colorpaletspecification}
27\installcorenamespace{colorpalet}
28\installcorenamespace{colorstack}
29\installcorenamespace{colorconversions}
30\installcorenamespace{colornumber}
31
32\registerctxluafile{colo-ini}{}
33\registerctxluafile{colo-icc}{autosuffix}
34
35\unprotect
36
37%D In \MKIV\ we have independent color, colorspace and transparency but I'm still
38%D not sure if I keep it that way as it's probably more efficient to combine them
39%D (less attributes and finalizing). If it becomes a bottleneck we can set up a more
40%D complex system with one shared attribute for colorspace, color and transparency.
41%D
42%D When typesetting for paper, we prefer using the \CMYK\ color space, but for
43%D on||screen viewing we prefer \RGB\ (the previous implementation supported only
44%D this scheme). Independant of such specifications, we support some automatic
45%D conversions:
46%D
47%D \startitemize[packed]
48%D \item  convert all colors to \RGB
49%D \item  convert all colors to \CMYK
50%D \item  convert all colors to gray scales
51%D \stopitemize
52%D
53%D These options are communicated by means of:
54
55\newconditional\c_colo_rgb_supported
56\newconditional\c_colo_cmyk_supported
57\newconditional\c_colo_spot_supported   % backend driven
58\newconditional\c_colo_convert_gray     \c_colo_convert_gray\conditionaltrue
59\newconditional\c_colo_enabled
60\newconditional\c_colo_expanded
61
62\let\m_colo_weight_gray\v!yes
63
64\mutable\lettonothing\currentcolormodel
65\mutable\lettonothing\currentcolorname
66\mutable\lettonothing\currentcolorpalet
67\mutable\lettonothing\currentcolorprefix % \currentcolorpalet:
68
69%D \macros
70%D   {definecolor,defineglobalcolor,definenamedcolor,definespotcolor,definemultitonecolor,
71%D    definetransparency}
72%D
73%D \starttyping
74%D \definecolor [blue]   [c=1,m=.38,y=0,k=.64] % pantone pms 2965 uncoated m
75%D \definecolor [yellow] [c=0,m=.28,y=1,k=.06] % pantone pms  124 uncoated m
76%D
77%D \definespotcolor [blue-100]   [blue]   [p=1]
78%D \definespotcolor [yellow-100] [yellow] [p=1]
79%D
80%D \definemultitonecolor [pdftoolscolor] [blue=.12,yellow=.28] [c=.1,m=.1,y=.3,k=.1]
81%D
82%D \defineprocesscolor[myred][r=.5]
83%D \defineprocesscolor[myredish][red][a=1,t=.5]
84%D
85%D \blackrule[color=myred,width=\hsize,height=1cm] \par
86%D \blackrule[color=myredish,width=\hsize,height=1cm]
87%D
88%D \useexternalfigure[demofig][mill.png][object=no]
89%D
90%D \startcombination[4*1]
91%D   {\externalfigure[demofig]}                      {no color}
92%D   {\externalfigure[demofig][color=pdftoolscolor]} {indexed duotone}
93%D   {\externalfigure[demofig][color=blue-100]}      {spot color}
94%D   {\externalfigure[demofig][color=yellow-100]}    {spot color}
95%D \stopcombination
96%D \stoptyping
97
98\permanent\protected\def\loadfoundcolorsetfile#1#2% name foundname / not a user command
99  {\startreadingfile
100   \startcolorset[#1]%
101   \input{#2}%
102   \stopcolorset
103   \showmessage\m!colors4{#1}%
104   \stopreadingfile}
105
106%D \macros
107%D   {startcolor,stopcolor,color,graycolor}
108%D
109%D The local and global commands are here just for compatibility with \MKII.
110%D
111%D \showsetup{startcolor}
112%D
113%D The simple color commands are:
114%D
115%D \showsetup{color}
116%D \showsetup{graycolor}
117
118%D We expect sane behaviour in \MKIV\ so we don't used grouped command any
119%D longer.
120
121% \testfeatureonce{100000}{\color[red]{}} % 1.046 => 0.541
122
123\permanent\protected\def\switchtocolor[#1]{\begincsname#1\endcsname}
124
125% transparency
126
127\permanent\protected\def\transparent[#1]%
128  {\beginsimplegroup
129   \cdef\scratchstringone{#1}%
130   % the \relax catches a non existent csname
131   \ifx\scratchstringone\v!reset
132     \c_attr_transparency\attributeunsetvalue
133   \else
134     \begincsname\??transparencysetter\scratchstringone\endcsname\relax
135   \fi
136   \let\nexttoken}
137
138\permanent\protected\def\starttransparent[#1]%$
139  {\begingroup
140   \cdef\scratchstringone{#1}%
141   \ifx\scratchstringone\v!reset
142     \c_attr_transparency\attributeunsetvalue
143   \else
144     \begincsname\??transparencysetter\scratchstringone\endcsname\relax
145   \fi}
146
147\permanent\protected\def\stoptransparent
148  {\endgroup}
149
150% color
151
152\permanent\protected\def\coloronly[#1]%
153  {\beginsimplegroup
154   \cdef\currentcolorname{#1}%
155   \ifempty\currentcolorprefix
156     \colo_helpers_activate_nop_only
157   \else
158     \colo_helpers_activate_yes_only
159   \fi
160   \let\nexttoken}
161
162\permanent\protected\def\startcoloronly[#1]%$
163  {\begingroup
164   \cdef\currentcolorname{#1}%
165   \ifempty\currentcolorprefix
166     \colo_helpers_activate_nop_only
167   \else
168     \colo_helpers_activate_yes_only
169   \fi}
170
171\permanent\protected\def\stopcoloronly
172  {\endgroup}
173
174% color + transparency
175
176\permanent\protected\def\color[#1]%
177  {\beginsimplegroup
178   \cdef\currentcolorname{#1}%
179   \ifempty\currentcolorprefix
180     \colo_helpers_activate_nop
181   \else
182     \colo_helpers_activate_yes
183   \fi
184   \let\nexttoken}
185
186\permanent\protected\def\graycolor[#1]%
187  {\beginsimplegroup
188   \colo_helpers_set_model\s!gray\colo_helpers_activate{#1}%
189   \let\nexttoken}
190
191\permanent\protected\def\startcolor[#1]%$
192  {\begingroup
193   \cdef\currentcolorname{#1}%
194   \ifempty\currentcolorprefix
195     \colo_helpers_activate_nop
196   \else
197     \colo_helpers_activate_yes
198   \fi}
199
200\permanent\protected\def\stopcolor
201  {\endgroup}
202
203\permanent\protected\def\colored[#1]%
204  {\beginsimplegroup
205   \colo_basics_defined_and_activated{#1}%
206   \let\nexttoken}
207
208\permanent\protected\def\fastcolored[#1]#2%
209  {\begingroup % is this command still needed?
210   \colo_basics_defined_and_activated{#1}%
211   #2%
212   \endgroup}
213
214\permanent\protected\def\directcolored[#1]%
215  {\colo_basics_defined_and_activated{#1}}
216
217\permanent\protected\def\fastcolor[#1]#2%
218  {\begingroup % is this command still needed?
219   \cdef\currentcolorname{#1}%
220   \ifempty\currentcolorprefix
221     \colo_helpers_activate_nop
222   \else
223     \colo_helpers_activate_yes
224   \fi
225   #2%
226   \endgroup}
227
228\permanent\protected\def\directcolor[#1]%
229  {\cdef\currentcolorname{#1}%
230   \ifempty\currentcolorprefix
231     \colo_helpers_activate_nop
232   \else
233     \colo_helpers_activate_yes
234   \fi}
235
236%D The following command is obsolete:
237
238\permanent\protected\def\startcolorpage{\startcolor[\ifempty\maintextcolor\defaulttextcolor\else\maintextcolor\fi]}
239\permanent\protected\def\stopcolorpage {\stopcolor}
240
241\permanent\protected\def\getcolorattributevalue#1#2% obsolete, use \thecolorattribute instead ...
242  {\begingroup
243   \colo_helpers_activate{#1}%
244   \normalexpanded{\endgroup\edef\noexpand#2{\ifnum\c_attr_color=\attributeunsetvalue\else\the\c_attr_color\fi}}}
245
246% \aliased\let\grey\graycolor % these macros are only used in tracing
247
248%D \macros
249%D   {startcurrentcolor,stopcurrentcolor}
250
251\permanent\protected\def\startcurrentcolor{\startcolor[\outercolorname]}
252\permanent\protected\def\stopcurrentcolor {\stopcolor}
253
254%D \macros
255%D   {setupcolor}
256%D
257%D Color definitions can be grouped in files with the name:
258%D
259%D \starttyping
260%D colo-imp-tag.tex
261%D \stoptyping
262%D
263%D Loading such a file is done by:
264%D
265%D \showsetup{usecolors}
266%D
267%D Some default colors are specified in \type {colo-imp-rgb}, which is loaded into
268%D the format by default.
269%D
270%D Some demo palets and groups are defined in \type {colo-imp-dem} which can be
271%D loaded with:
272%D
273%D \starttyping
274%D \usecolors[dem]
275%D \stoptyping
276
277\permanent\protected\def\startcolorset[#1]{\clf_startcolorset{#1}}
278\permanent\protected\def\stopcolorset     {\clf_stopcolorset}
279\permanent\protected\def\usecolors    [#1]{\clf_usecolors{#1}}
280
281\aliased\let\setupcolor\usecolors
282
283\installsetuponlycommandhandler \??color {colors}
284
285\let\colo_helpers_show_message\gobblethreearguments
286
287% \newtoks\everysetupcolors
288
289\let\v_colo_freeze_state\s!false
290
291\defcsname\??colorconversions\v!yes\endcsname
292  {\c_colo_convert_gray\conditionaltrue}
293
294\defcsname\??colorconversions\v!always\endcsname
295  {\c_colo_convert_gray\conditionaltrue
296   \c_colo_rgb_supported\conditionalfalse
297   \c_colo_cmyk_supported\conditionalfalse}
298
299\appendtoks
300   %
301   \ifcstok{\directcolorsparameter\c!spot     }\v!yes  \c_colo_spot_supported\conditionaltrue\else\c_colo_spot_supported\conditionalfalse\fi
302   \ifcstok{\directcolorsparameter\c!expansion}\v!yes  \c_colo_expanded\conditionaltrue      \else\c_colo_expanded\conditionalfalse      \fi
303   \ifcstok{\directcolorsparameter\c!rgb      }\v!yes  \c_colo_rgb_supported\conditionaltrue \else\c_colo_rgb_supported\conditionalfalse \fi
304   \ifcstok{\directcolorsparameter\c!cmyk     }\v!yes  \c_colo_cmyk_supported\conditionaltrue\else\c_colo_cmyk_supported\conditionalfalse\fi
305   \ifcstok{\directcolorsparameter\c!state    }\v!start\c_colo_enabled\conditionaltrue       \else\c_colo_enabled\conditionalfalse       \fi
306   %
307   \edef\m_colo_weight_gray{\directcolorsparameter\c!factor}%
308   %
309   \ifconditional\c_colo_expanded
310     \let\v_colo_freeze_state\s!true
311   \else
312     \let\v_colo_freeze_state\s!false
313   \fi
314   %
315   \c_colo_convert_gray\conditionalfalse
316   \begincsname\??colorconversions\directcolorsparameter\c!conversion\endcsname
317   % too often:
318   \ifconditional\c_colo_rgb_supported \colo_helpers_show_message\m!colors{10}\v!rgb \fi
319   \ifconditional\c_colo_cmyk_supported\colo_helpers_show_message\m!colors{10}\v!cmyk\fi
320   \colo_helpers_set_current_model
321   \ifproductionrun
322     \edef\p_pagecolormodel{\directcolorsparameter\c!pagecolormodel}%
323     \clf_setpagecolormodel{\ifx\p_pagecolormodel\v!auto\currentcolormodel\else\p_pagecolormodel\fi}%
324   \fi
325\to \everysetupcolors
326
327\appendtoks
328    \setupcolors[\c!state=\v!start]%
329    \clf_enablecolor           % this can as well happen when
330    \clf_enabletransparency    % the handler is defined in lua
331    \let\colo_helpers_show_message\showmessage
332\to \everyjob
333
334% handled elsewhere but kind of a reference here:
335%
336% \def\colo_force_colormodel#1#2%
337%   {\setcolorsparameter\c!rgb {#1}%
338%    \setcolorsparameter\c!cmyk{#2}%
339%    \expand\everysetupcolors}
340
341%D We provide stacking independent of grouping.
342
343% the old one
344%
345% \newcount\c_colo_nesting
346%
347% \permanent\protected\def\pushcolor[#1]%
348%   {\global\advanceby\c_colo_nesting\plusone
349%    \edefcsname\??colorstack\the\c_colo_nesting\endcsname
350%      {\c_attr_colormodel  \the\c_attr_colormodel
351%       \c_attr_color       \the\c_attr_color
352%       \c_attr_transparency\the\c_attr_transparency
353%       \relax}% stack
354%    \colo_helpers_activate{#1}}
355%
356% \permanent\protected\def\popcolor
357%   {\csname\??colorstack\the\c_colo_nesting\endcsname
358%    \global\advanceby\c_colo_nesting\minusone}
359%
360% more efficient:
361
362\installmacrostack\m_color_state
363
364\permanent\protected\def\pushcolor[#1]%
365  {\edef\m_color_state
366     {\c_attr_colormodel  \the\c_attr_colormodel
367      \c_attr_color       \the\c_attr_color
368      \c_attr_transparency\the\c_attr_transparency}%
369   \push_macro_m_color_state
370   \colo_helpers_activate{#1}}
371
372\permanent\protected\def\popcolor
373  {\pop_macro_m_color_state
374   \m_color_state\relax}
375
376% a simple one: assumes grouping
377
378\newinteger\c_colo_saved_attribute_color
379\newinteger\c_colo_saved_attribute_transparency
380
381\permanent\protected\def\savecolor
382  {\c_colo_saved_attribute_color       \c_attr_color
383   \c_colo_saved_attribute_transparency\c_attr_transparency}
384
385\permanent\protected\def\restorecolor
386  {\c_attr_color       \c_colo_saved_attribute_color
387   \c_attr_transparency\c_colo_saved_attribute_transparency}
388
389%D Nasty:
390
391% \permanent\def\usedcolorparameterattributes#1%
392%   {\beginlocalcontrol\begingroup
393%    \dousecolorparameter{#1}%
394%    \normalexpanded{\endgroup\noexpand\endlocalcontrol
395%        \noexpand\s!attr \colorattribute        \the\c_attr_color
396%        \noexpand\s!attr \colormodelattribute   \the\c_attr_colormodel
397%        \noexpand\s!attr \transparencyattribute \the\c_attr_transparency}}
398
399\permanent\def\usedcolorparameterattributes#1%
400  {\beginlocalcontrol\begingroup
401   \dousecolorparameter{#1}%
402   \normalexpanded{\endgroup\noexpand\endlocalcontrol
403       % differs a lot
404       \noexpand\s!attr \colorattribute        \the\c_attr_color
405       % one token
406       \noexpand\s!attr \colormodelattribute   \the\c_attr_colormodel
407       % often unset
408       \noexpand\s!attr \transparencyattribute \ifnum\c_attr_transparency=\attributeunsetvalue\attributeunsetvalue\else\the\c_attr_transparency\fi}}
409
410\permanent\def\usedsymbolcolorparameterattributes#1%
411  {\beginlocalcontrol\begingroup
412%    \iftok{#1}\emptytoks
413%      \expandafter\dousecolorparameter\expandafter{\currentcolorname}%
414%    \else
415     \dousecolorparameter{#1}%
416%    \fi
417   \normalexpanded{\endgroup\noexpand\endlocalcontrol
418       % differs a lot
419       \noexpand\s!symbolattr \colorattribute        \the\c_attr_color
420       % one token
421       \noexpand\s!symbolattr \colormodelattribute   \the\c_attr_colormodel
422       % often unset
423       \noexpand\s!symbolattr \transparencyattribute \ifnum\c_attr_transparency=\attributeunsetvalue\attributeunsetvalue\else\the\c_attr_transparency\fi}}
424
425%D In this documentation we will not go into too much details on palets. Curious
426%D users can find more information on this topic in \from[use of color].
427%D
428%D At the moment we implemented color in \CONTEXT\ color printing was not yet on the
429%D desktop. In spite of this lack our graphics designer made colorfull
430%D illustrations. When printed on a black and white printer, distinctive colors can
431%D come out equally gray. We therefore decided to use only colors that were
432%D distinctive in colors as well as in black and white print.
433%D
434%D Although none of the graphic packages we used supported logical colors and global
435%D color redefition, we build this support into \CONTEXT. This enabled us to
436%D experiment and also prepared us for the future.
437
438%D \macros
439%D   {definepalet}
440%D
441%D Colors are grouped in palets. The colors in such a palet can have colorful names,
442%D but best is to use names that specify their use, like {\em important} or {\em
443%D danger}. As a sort of example \CONTEXT\ has some palets predefined,
444%D like:\footnote{At the time I wrote the palet support, I was reading 'A hort
445%D history of time' of S.~Hawkins, so that's why we stuck to quarks.}
446%D
447%D \starttyping
448%D \definepalet
449%D   [alfa]
450%D   [     top=rood:7,
451%D      bottom=groen:6,
452%D          up=blauw:5,
453%D        down=cyaan:4,
454%D     strange=magenta:3,
455%D       charm=geel:2]
456%D \stoptyping
457%D
458%D It's formal definition is:
459%D
460%D \showsetup{definepalet}
461%D
462%D Visualized, such a palet looks like:
463%D
464%D \startbuffer[palet]
465%D \showpalet [alfa] [horizontal,name,number,value]
466%D \stopbuffer
467%D
468%D \startlinecorrection
469%D \getbuffer[palet]
470%D \stoplinecorrection
471%D
472%D This bar shows both the color and gray alternatives of the palet components (not
473%D visible in black and white print).
474%D
475%D When needed, one can copy a palet by saying:
476%D
477%D \starttyping
478%D \definepalet [TEXcolorpretty] [colorpretty]
479%D \stoptyping
480%D
481%D This saves us some typing in for instance the modules that deal with pretty
482%D verbatim typesetting.
483
484\installcorenamespace{paletlist}
485\installcorenamespace{paletsize}
486
487\let\m_colo_palet\relax
488\let\c_colo_palet\relax
489
490\def\colo_palet_allocate#1%
491  {\letcsname\??paletlist#1\endcsname\empty
492   \ifcsname\??paletsize#1\endcsname\else
493     \expandafter\newinteger\csname\??paletsize#1\endcsname
494   \fi}
495
496\def\colo_palet_prepare#1%
497  {\edef\colo_palet_name{#1}%
498   \ifcsname\??paletlist\colo_palet_name\endcsname
499     \csname\??paletsize\colo_palet_name\endcsname\zerocount
500   \else
501     \colo_palet_allocate\colo_palet_name
502   \fi
503 % \enforced\expandafter\integerdef\expandafter\c_colo_palet\csname\??paletsize\colo_palet_name\endcsname
504   \edef\c_colo_palet{\csname\??paletsize\colo_palet_name\endcsname}% we can't alias  as they are integers
505   \edef\m_colo_palet{\begincsname\??paletlist\colo_palet_name\endcsname}}
506
507\def\colo_palet_extend#1%
508  {\addtocommalist{#1}\m_colo_palet
509   \letcsname\??paletlist\colo_palet_name\endcsname\m_colo_palet
510  %\advanceby\csname\??paletsize\colo_palet_name\endcsname\plusone
511   \advanceby\c_colo_palet\plusone}
512
513\permanent\protected\def\doifelsecolorpalet#1%
514  {\ifcsname\??paletlist#1\endcsname
515     \expandafter\firstoftwoarguments
516   \else
517     \expandafter\secondoftwoarguments
518   \fi}
519
520\mutable\lettonothing\paletlist
521\mutable\let         \paletsize\!!zerocount
522
523\permanent\protected\def\getpaletlist[#1]%
524  {\edef\paletlist{\begincsname\??paletlist#1\endcsname}}
525
526\permanent\protected\def\getpaletsize[#1]%
527  {\ifcsname\??paletsize#1\endcsname
528     \edef\paletsize{\the\lastnamedcs}%
529   \else
530     \let\paletsize\!!zerocount
531   \fi}
532
533\permanent\tolerant\protected\def\definepalet[#1]#*[#2]#*[#3]%
534  {\ifarguments
535     \expandafter\gobblethreearguments
536   \or
537     \expandafter\gobblethreearguments
538   \or
539     \expandafter\colo_palets_define_a
540   \or
541     \expandafter\colo_palets_define_b
542  \fi{#1}{#2}{#3}}
543
544\protected\def\colo_palets_define_a#1#2#3%
545  {\colo_palet_prepare{#1}%
546   \ifhastok={#2}%
547     \edefcsname\??colorpaletspecification#1\endcsname{#2}%
548     \processcommalist[#2]{\colo_palets_define_one{#1}}%
549   \orelse\ifcsname\??colorpaletspecification#2\endcsname
550     \normalexpanded{\colo_palets_define_b{#1}{\lastnamedcs}\empty}%
551   \fi}
552
553\protected\def\colo_palets_define_b#1#2#3%
554  {\colo_palet_prepare{#1}%
555   \edefcsname\??colorpaletspecification#1\endcsname{#2}%
556   \colo_palet_extend{#2}%
557   \colo_palets_define_assign{#1}{#2}{#3}}
558
559% % or, it being not that critital, just this
560%
561% \permanent\tolerant\protected\def\definepalet[#1]#*[#2]#*[#3]%
562%   {\ifarguments
563%    \or
564%    \or
565%      \colo_palet_prepare{#1}%
566%      \ifhastok={#2}%
567%        \edefcsname\??colorpaletspecification#1\endcsname{#2}%
568%        \processcommalist[#2]{\colo_palets_define_one{#1}}%
569%      \orelse\ifcsname\??colorpaletspecification#2\endcsname
570%        \normalexpanded{\colo_palets_define_b{#1}{\lastnamedcs}{}}%
571%      \fi
572%    \or
573%      \colo_palet_prepare{#1}%
574%      \edefcsname\??colorpaletspecification#1\endcsname{#2}%
575%      \colo_palet_extend{#2}%
576%      \colo_palets_define_assign{#1}{#2}{#3}%
577%   \fi}
578
579\def\colo_palets_define_one#1#2% get rid of { }
580  {\ifhastok={#2}% catch empty entries
581     \colo_palets_define_two{#1}[#2]%
582   \else
583     \colo_palets_define_three{#1}{#2}%
584   \fi}
585
586\def\colo_palets_define_two#1[#2=#3]%
587  {\colo_palet_extend{#2}%
588   \colo_palets_define_set{#1}{#2}{#3}}%
589
590\def\colo_palets_define_three#1#2%
591  {\ifcsname\??colorpaletspecification#2\endcsname
592     \processcommacommand[\lastnamedcs]{\colo_palets_define_one{#1}}%
593   \fi}
594
595%D Instead of refering to colors, one can also directly specify a color:
596%D
597%D \starttyping
598%D \definepalet[test][xx=green]
599%D \definepalet[test][xx={y=.4}]
600%D \stoptyping
601
602%D \macros
603%D   {setuppalet}
604%D
605%D Colors are taken from the current palet, if defined. Setting the current palet is
606%D done by:
607%D
608%D \showsetup{setuppalet}
609
610\newtoks\everysetuppalet
611
612% \newtoks\t_colo_prefix % used in mp interface
613
614\permanent\tolerant\protected\def\setuppalet[#1]%
615  {\cdef\currentcolorpalet{#1}%
616   \ifempty\currentcolorpalet
617     % seems to be a reset
618     \lettonothing\currentcolorprefix
619    %\t_colo_prefix\emptytoks
620   \orelse\ifcsname\??paletlist\currentcolorpalet\endcsname
621     \cdef\currentcolorprefix{#1:}%
622    %\t_colo_prefix\expandafter{\currentcolorprefix}%
623   \else
624     \colo_helpers_show_message\m!colors7\currentcolorpalet
625     \lettonothing\currentcolorpalet
626     \lettonothing\currentcolorprefix
627    %\t_colo_prefix\emptytoks
628   \fi
629   \expand\everysetuppalet
630   \colo_helpers_initialize_maintextcolor}
631
632%D \macros
633%D   {showpalet}
634%D
635%D The previous visualization was typeset with:
636%D
637%D \typebuffer[palet]
638%D
639%D This commands is defined as:
640%D
641%D \showsetup{showpalet}
642
643\fetchmodulecommand \showpalet \f!colo_run
644
645%D \macros
646%D   {showcolorcomponents}
647%D
648%D \starttyping
649%D \showcolorcomponents[color-1,color-2]
650%D \stoptyping
651
652\fetchmodulecommand \showcolorcomponents \f!colo_run
653
654%D \macros
655%D   {comparepalet}
656%D
657%D There are some more testing macros available:
658%D
659%D \startbuffer
660%D \comparepalet [alfa]
661%D \stopbuffer
662%D
663%D \typebuffer
664%D
665%D shows the palet colors against a background:
666%D
667%D \startlinecorrection
668%D \getbuffer
669%D \stoplinecorrection
670%D
671%D The formal definition is:
672%D
673%D \showsetup{comparepalet}
674
675\fetchmodulecommand \comparepalet \f!colo_run
676
677%D \macros
678%D   {showcolor}
679%D
680%D But let's not forget that we also have the more traditional non||related colors.
681%D These show up after:
682%D
683%D \starttyping
684%D \showcolor [name]
685%D \stoptyping
686%D
687%D Where \type{name} for instance can be \type{rgb}.
688%D
689%D \showsetup{showcolor}
690
691\fetchmodulecommand \showcolor \f!colo_run
692
693%D This one shows the luminance ratio conform WCAG:
694%D
695%D \starttyping
696%D \starttabulate[||r|]
697%D     \NC red   \NC \colorluminance{red}   \NC \NR
698%D     \NC green \NC \colorluminance{green} \NC \NR
699%D     \NC black \NC \colorluminance{black} \NC \NR
700%D     \NC white \NC \colorluminance{white} \NC \NR
701%D \stoptabulate
702%D
703%D \starttabulate[||r|]
704%D     \NC red : green \NC \colorluminanceratio{red}{green} \NC \NR
705%D     \NC red : black \NC \colorluminanceratio{red}{black} \NC \NR
706%D     \NC red : white \NC \colorluminanceratio{red}{white} \NC \NR
707%D \stoptabulate
708%D
709%D \usecolors[crayola]
710%D
711%D \compareluminance[Jade]      [Onyx] \par
712%D \compareluminance[Goldenrod] [Onyx] \par
713%D \compareluminance[CobaltBlue][Onyx] \par
714%D \stoptyping
715
716\fetchmodulecommand \compareluminance \f!colo_run
717
718%D It would make sense to put the following code in \type {colo-mps}, but it it
719%D rather low level.
720
721%D \macros
722%D   {negatecolorcomponent,negatedcolorcomponent}
723%D
724%D These speak for themselves. See \type {colo-ext} for usage.
725
726\permanent\protected\def\negatecolorcomponent #1{\edef#1{\clf_negatedcolorcomponent{#1}}}
727\permanent          \def\negatedcolorcomponent#1{\clf_negatedcolorcomponent{#1}}
728
729%D \macros
730%D   {MPcolor}
731%D
732%D A very special macro is \type{\MPcolor}. This one can be used to pass a \CONTEXT\
733%D color to \METAPOST.
734%D
735%D \starttyping
736%D \MPcolor{my own red}
737%D \stoptyping
738%D
739%D This macro returns \METAPOST\ code that represents the
740%D color.
741%D
742%D For the moment we keep the next downward compatibility switch, i.e.\ expanded
743%D colors. However, predefined colors and palets are no longer expanded (which is
744%D what I wanted in the first place).
745%D
746%D In \MKIV\ we don't support color separation as we might now assume that printing
747%D houses have the right programs to do it themselves. If it's ever needed in \MKIV\
748%D It is relatively easy to support it in the backend code.
749
750% todo: palets in definecolor
751% todo: {\red xx} vs \red{xx}
752
753% \def\mptexcolor#1{"\dogetattributeid\s!color \somecolorattribute{#1} A"}
754%
755% \startMPpage
756%     fill fullcircle scaled 10cm  ;
757%     fill fullcircle scaled 5cm withprescript \mptexcolor{red} withpostscript \mptexcolor{black} ;
758%     fill fullcircle scaled 3cm  ;
759%     draw btex test etex withprescript \mptexcolor{blue}  ;
760% \stopMPpage
761
762\permanent\protected\def\setcolormodel[#1]% % beware, \setupcolors will overload this, so this one is
763  {\colo_helpers_set_model{#1}}  % only for local usage
764
765\def\colo_helpers_set_model#1% direct
766  {\cdef\currentcolormodel{#1}%
767   \clf_setcolormodel{\currentcolormodel}{\m_colo_weight_gray}} % sets attribute at lua end
768
769\colo_helpers_set_model\s!all
770
771\def\colo_helpers_set_current_model
772  {\ifconditional\c_colo_enabled
773     \ifconditional\c_colo_rgb_supported
774       \ifconditional\c_colo_cmyk_supported
775         \colo_helpers_set_model\s!all
776       \else
777         \colo_helpers_set_model\s!rgb
778       \fi
779     \orelse\ifconditional\c_colo_cmyk_supported
780       \colo_helpers_set_model\s!cmyk
781     \orelse\ifconditional\c_colo_convert_gray
782       \colo_helpers_set_model\s!gray
783     \else
784       \colo_helpers_set_model\s!none
785     \fi
786   \orelse\ifconditional\c_colo_convert_gray
787     \colo_helpers_set_model\s!gray
788   \else
789     \colo_helpers_set_model\s!none
790   \fi}
791
792%D Currently in mkiv transparency is implemented independent of color. This costs a
793%D bit more processing time but gives the possibility to apply transparency
794%D independently in the future. Is this useful? If not we may as well combine them
795%D again in the future. By coupling we are downward compatible. When we decouple we
796%D need to do more tricky housekeeping (e.g. persist color independent
797%D transparencies when color bound ones are nil.
798
799% Since we couple definitions, we could stick to one test. Todo. Same for mpcolor.
800
801\def\v_colo_dummy_name{c_o_l_o_r}
802
803\letcsname\??colorattribute       \v_colo_dummy_name\endcsname\empty
804\letcsname\??transparencyattribute\v_colo_dummy_name\endcsname\empty
805\letcsname\??colorsetter          \v_colo_dummy_name\endcsname\empty
806\letcsname\??transparencysetter   \v_colo_dummy_name\endcsname\empty
807
808\letcsname\??colorsetter                           -\endcsname\empty % used?
809\letcsname\??transparencysetter                    -\endcsname\empty % used?
810
811% new: expandable (see tbl preamble)
812
813\def\colo_helpers_direct_activate
814  {\ifempty\currentcolorprefix
815     \expandafter\colo_helpers_direct_activate_nop
816   \else
817     \expandafter\colo_helpers_direct_activate_yes
818   \fi}
819
820\def\colo_helpers_direct_activate_yes#1%
821  {\ifcsname\??colorsetter\currentcolorprefix#1\endcsname
822     \lastnamedcs
823     \begincsname\??transparencysetter\currentcolorprefix#1\endcsname
824   \orelse\ifcsname\??colorsetter#1\endcsname
825     \lastnamedcs
826     \begincsname\??transparencysetter#1\endcsname
827   \fi}
828
829\def\colo_helpers_direct_activate_nop#1%
830  {\ifcsname\??colorsetter#1\endcsname
831     \lastnamedcs
832     \begincsname\??transparencysetter#1\endcsname
833   \fi}
834
835\def\colo_helpers_activate_dummy
836  {\begincsname\??colorsetter       \v_colo_dummy_name\endcsname
837   \begincsname\??transparencysetter\v_colo_dummy_name\endcsname}
838
839% so far
840
841\permanent\protected\def\colo_helpers_activate#1% two-step is not that much faster but less tracing
842  {\cdef\currentcolorname{#1}%
843   \ifempty\currentcolorprefix
844     \colo_helpers_activate_nop
845   \else
846     \colo_helpers_activate_yes
847   \fi}
848
849\def\colo_helpers_activate_yes
850  {\ifcsname\??colorsetter\currentcolorprefix\currentcolorname\endcsname
851     \lastnamedcs
852     \begincsname\??transparencysetter\currentcolorprefix\currentcolorname\endcsname
853   \orelse\ifcsname\??colorsetter\currentcolorname\endcsname
854     \lastnamedcs
855     \begincsname\??transparencysetter\currentcolorname\endcsname
856   \fi}
857
858\def\colo_helpers_activate_nop
859  {\ifcsname\??colorsetter\currentcolorname\endcsname
860     \lastnamedcs
861     \begincsname\??transparencysetter\currentcolorname\endcsname
862   \fi}
863
864\def\colo_helpers_activate_yes_only
865  {\ifcsname\??colorsetter\currentcolorprefix\currentcolorname\endcsname
866     \lastnamedcs
867   \orelse\ifcsname\??colorsetter\currentcolorname\endcsname
868     \lastnamedcs
869   \fi}
870
871\def\colo_helpers_activate_nop_only
872  {\ifcsname\??colorsetter\currentcolorname\endcsname
873     \lastnamedcs
874   \fi}
875
876\aliased\let\dousecolorparameter\colo_helpers_activate
877
878\permanent\protected\def\dousecolorhashparameter#1#2%
879  {\ifcsname#1#2\endcsname
880     \expandafter\dousecolorparameter\csname#1#2\endcsname
881   \fi}
882
883\permanent\protected\def\dousecurrentcolorparameter
884  {\let\currentcolorname\currentcolorparameter % maybe only when success
885   \ifcsname\??colorsetter\currentcolorprefix\currentcolorparameter\endcsname
886     \lastnamedcs
887     \begincsname\??transparencysetter\currentcolorprefix\currentcolorparameter\endcsname
888   \orelse\ifcsname\??colorsetter\currentcolorparameter\endcsname
889     \lastnamedcs
890     \begincsname\??transparencysetter\currentcolorparameter\endcsname
891   \fi}
892
893\permanent\protected\def\deactivatecolor % sort of public but a bad name
894  {\let\currentcolorname\s!black
895   \c_attr_color       \attributeunsetvalue
896   \c_attr_transparency\attributeunsetvalue}
897
898% todo: check if color is overloading a non-color command
899
900% \let\colo_basics_synchronize\gobbleoneargument  % used in mp interface
901% \let\colo_basics_inherit    \gobbletwoarguments % used in mp interface
902
903\def\colo_basics_allocate#1%
904  {\expandafter\newinteger\csname\??colornumber#1\endcsname
905   \clf_synccolorcount{#1}{\??colornumber#1}}
906
907\def\colo_basics_synchronize#1%
908  {\ifcsname\??colornumber#1\endcsname\else
909     \colo_basics_allocate{#1}%
910   \fi
911   \clf_synccolor{#1}%
912  %\csname\??colornumber#1\endcsname\csname\??colorattribute#1\endcsname
913   \lastnamedcs\csname\??colorattribute#1\endcsname}
914
915\let\colo_basics_inherit\clf_synccolorclone
916
917\newinteger\c_colo_protection
918
919\permanent\protected\def\startprotectedcolors{\advanceby\c_colo_protection\plusone }
920\permanent\protected\def\stopprotectedcolors {\advanceby\c_colo_protection\minusone}
921
922\permanent\tolerant\protected\def\definecolor[#1]#*[#2]%
923  {\iftok{#1}{#2}%
924     % maybe a warning
925   \else
926     \clf_defineprocesscolorlocal{#1}{#2}\v_colo_freeze_state\relax
927     \colo_basics_synchronize{#1}%
928     \ifcase\c_colo_protection
929       \protected\instance\defcsname#1\endcsname{\colo_helpers_activate{#1}}%
930     \fi
931   \fi}
932
933\permanent\tolerant\protected\def\defineglobalcolor[#1]#*[#2]%
934  {\iftok{#1}{#2}%
935     % maybe a warning
936   \else
937     \clf_defineprocesscolorglobal{#1}{#2}\v_colo_freeze_state\relax
938     \colo_basics_synchronize{#1}%
939     \ifcase\c_colo_protection
940       \protected\instance\gdefcsname#1\endcsname{\colo_helpers_activate{#1}}%
941     \fi
942   \fi}
943
944\aliased\let\definenamedcolor\definecolor
945
946\def\colo_basics_defined_and_activated#1%
947  {\clf_defineprocesscolordummy{#1}% we could pass dummy here too
948   \colo_basics_synchronize{\v_colo_dummy_name}%
949   \colo_helpers_activate_dummy}
950
951\permanent\tolerant\protected\def\defineprocesscolor[#1]#*[#2]#*[#3]%
952  {\ifarguments\or
953     \colo_basics_define_process_wrapup{#1}\empty
954   \or
955     \colo_basics_define_process_wrapup{#1}{#2}%
956   \or % we could just pass #3 to clf_... also when it's empty ... todo after split
957     \colo_basics_define_process_wrapup{#1}{\processcolorcomponents{#2},#3}%
958   \fi}
959
960\def\colo_basics_define_process_wrapup#1#2%
961  {\clf_defineprocesscolorlocal{#1}{#2}\v_colo_freeze_state\relax
962   \colo_basics_synchronize{#1}%
963   \ifcase\c_colo_protection
964     \protected\instance\defcsname#1\endcsname{\colo_helpers_activate{#1}}%
965   \fi}
966
967\permanent\tolerant\protected\def\definelabcolor[#1]#*[#2]#*[#3]%
968  {\clf_definelabcolorlocal{#1}{#2}{#3}%
969   \colo_basics_synchronize{#1}%
970   \ifcase\c_colo_protection
971     \protected\instance\gdefcsname#1\endcsname{\colo_helpers_activate{#1}}%
972   \fi}
973
974% \permanent\tolerant\protected\def\definegloballabcolor[#1]#*[#2]#*[#3]%
975%   {\clf_definelabcolorglobal{#1}{#2}{#3}%
976%    \colo_basics_synchronize{#1}%
977%    \ifcase\c_colo_protection
978%      \protected\instance\gdefcsname#1\endcsname{\colo_helpers_activate{#1}}%
979%    \fi}
980
981%D Spotcolors used setxvalue but that messes up currentcolor and probably no global
982%D is needed either but they are global at the lua end (true argument) so we keep
983%D that if only because spot colors often are a document wide property
984
985\permanent\tolerant\protected\def\definespotcolor[#1]#*[#2]#*[#3]%
986  {\clf_definespotcolorglobal{#1}{#2}{#3}%
987   \colo_basics_synchronize{#1}%
988   \ifcase\c_colo_protection
989     \protected\instance\gdefcsname#1\endcsname{\colo_helpers_activate{#1}}%
990   \fi}
991
992\permanent\tolerant\protected\def\definemultitonecolor[#1]#*[#2]#*[#3]#*[#4]%
993  {\clf_definemultitonecolorglobal{#1}{#2}{#3}{#4}%
994   \colo_basics_synchronize{#1}%
995   \ifcase\c_colo_protection
996     \protected\instance\gdefcsname#1\endcsname{\colo_helpers_activate{#1}}%
997   \fi}
998
999%D Transparencies (only):
1000
1001\permanent\tolerant\protected\def\definetransparency[#1]#*[#2]%
1002  {\clf_definetransparency{#1}{#2}}
1003
1004\permanent\tolerant\protected\def\defineglobaltransparency[#1]#*[#2]%
1005  {\clf_definetransparencyglobal{#1}{#2}}
1006
1007% A goodie that replaces the startMPcolor hackery
1008
1009% \definecolor[red-t]  [r=1,t=0.5,a=1]
1010% \definecolor[green-t][g=1,t=0.5,a=1]
1011%
1012% \defineintermediatecolor[mycolora][0.5,red,green]
1013% \defineintermediatecolor[mycolorb][0.5,red-t,green-t]
1014%
1015% \definecolor[mycolorc][.5(blue,red)]
1016% \definecolor[mycolord][.5(blue)]
1017% \definecolor[mycolord][-.5(blue,red)] % complement
1018% \definecolor[mycolord][-(blue)]       % complement
1019%
1020% \enabledirectives[colors.pgf]
1021% \definecolor[mycolorx][magenta!50!yellow]
1022%
1023% \starttext
1024%     test {\mycolora OEPS} test
1025%     test {\mycolorb OEPS} test
1026%     test {\mycolorc OEPS} test
1027%     test {\mycolord OEPS} test
1028%     test {\mycolorx OEPS} test
1029% \stoptext
1030%
1031% Beware: if we say:
1032%
1033% \definecolor[one][two]
1034%
1035% Only color one is actually defined and two is not seen at the \LUA\ end. This
1036% means that this doesn't work:
1037%
1038% \definecolor[ColorA][red]
1039% \definecolor[ColorB][.5(ColorA)]
1040% \definecolor[ColorC][.5(ColorB,white)]
1041%
1042% But this does work:
1043%
1044% \definecolor[ColorA][1.0(red)]
1045% \definecolor[ColorB][0.5(ColorA)]
1046% \definecolor[ColorC][0.5(ColorB,white)]
1047%
1048% because the fractional definition results in a new definition.
1049
1050\permanent\tolerant\protected\def\defineintermediatecolor[#1]#*[#2]#*[#3]
1051  {\colo_basics_define_intermediate_indeed[#1][#2][#3]}
1052
1053\def\colo_basics_define_intermediate_indeed[#1][#2,#3,#4][#5]%
1054  {\clf_defineintermediatecolor % not global
1055     {#1}%
1056     {#2}%
1057     \rawcolorattribute{#3} %
1058     \rawcolorattribute{#4} %
1059     \rawtransparencyattribute{#3} %
1060     \rawtransparencyattribute{#4} %
1061     {#5}%
1062     \v_colo_freeze_state
1063   \relax
1064   \colo_basics_synchronize{#1}%
1065   \protected\instance\defcsname#1\endcsname{\colo_helpers_activate{#1}}}
1066
1067%D Here is a more efficient helper for pgf:
1068%D
1069%D \starttyping
1070%D \startluacode
1071%D     function commands.pgfxcolorspec(ca) -- {}{}{colorspace}{list}
1072%D         local cv = attributes.colors.values[ca]
1073%D         if cv then
1074%D             local model = cv[1]
1075%D             if model == 2 then
1076%D                 context("{}{}{gray}{%1.3f}",cv[2])
1077%D             elseif model == 3 then
1078%D                 context("{}{}{rgb}{%1.3f,%1.3f,%1.3f}",cv[3],cv[4],cv[5])
1079%D             elseif model == 4 then
1080%D                 context("{}{}{cmyk}{%1.3f,%1.3f,%1.3f,%1.3f}",cv[6],cv[7],cv[8],cv[9])
1081%D             else
1082%D                 context("{}{}{gray}{%1.3f}",cv[2])
1083%D             end
1084%D         else
1085%D             context("{}{}{gray}{0}")
1086%D         end
1087%D     end
1088%D \stopluacode
1089%D
1090%D \def\pgfutil@registercolor#1%
1091%D   {\setevalue{\string\color@#1}{\noexpand\xcolor@\ctxcommand{pgfxcolorspec(\thecolorattribute{#1})}}}
1092%D
1093%D \definecolor [darkblue] [r=0,g=0,b=0.5]
1094%D
1095%D \starttikzpicture
1096%D     \fill [blue] (0,0) circle(1);
1097%D     \fill [darkblue] (0,0) circle(1);
1098%D \stoptikzpicture
1099%D \stoptyping
1100%D
1101%D \stopmode
1102%D
1103%D All this will move to m-tikz.mkxl as it doesn't belong in the core. They are
1104%D mutable because \quote {who knows what gets redefined}.
1105
1106\mutable\let\color@ \relax
1107\mutable\let\xcolor@\relax
1108
1109\mutable\protected\def\pgf@context@registercolor#1{\edefcsname\string\color@#1\endcsname{\noexpand\xcolor@{}{}\clf_pgfxcolorspec\zerocount\thecolorattribute{#1}}}
1110\mutable\protected\def\pgf@context@registergray #1{\edefcsname\string\color@#1\endcsname{\noexpand\xcolor@{}{}\clf_pgfxcolorspec\plustwo  \thecolorattribute{#1}}}
1111\mutable\protected\def\pgf@context@registerrgb  #1{\edefcsname\string\color@#1\endcsname{\noexpand\xcolor@{}{}\clf_pgfxcolorspec\plusthree\thecolorattribute{#1}}}
1112\mutable\protected\def\pgf@context@registercmyk #1{\edefcsname\string\color@#1\endcsname{\noexpand\xcolor@{}{}\clf_pgfxcolorspec\plusfour \thecolorattribute{#1}}}
1113
1114%D \starttyping
1115%D \ifdefined\pgf@context@registercolor
1116%D     \let\pgfutil@registercolor\pgf@context@registercolor
1117%D \fi
1118%D \stoptyping
1119
1120%D \macros
1121%D   {doifcolorelse, doifcolor}
1122%D
1123%D Switching to a color is done by means of the following command. Later on we will
1124%D explain the use of palets. We define ourselves a color conditional first.
1125
1126\permanent\def\doifelsecolor#1%
1127  {\ifcsname\??colorattribute\currentcolorprefix#1\endcsname
1128     \expandafter\firstoftwoarguments
1129   \orelse\ifcsname\??colorattribute#1\endcsname
1130     \expandafter\firstoftwoarguments
1131   \else
1132     \expandafter\secondoftwoarguments
1133   \fi}
1134
1135\aliased\let\doifcolorelse\doifelsecolor
1136
1137\permanent\def\doifcolor#1%
1138  {\ifcsname\??colorattribute\currentcolorprefix#1\endcsname
1139     \expandafter\firstofoneargument
1140   \orelse\ifcsname\??colorattribute#1\endcsname
1141     \expandafter\firstofoneargument
1142   \else
1143     \expandafter\gobbleoneargument
1144   \fi}
1145
1146%D \macros
1147%D   {startregistercolor,stopregistercolor,permitcolormode}
1148%D
1149%D If you only want to register a color, the switch \type {\ifpermitcolormode} can
1150%D be used. That way the nested colors know where to go back to.
1151%D
1152%D We use these macros for implementing text colors (actually, the first application
1153%D was in foreground colors).
1154%D
1155%D \starttyping
1156%D \starttextcolor[red]
1157%D   \dorecurse{10}{\input tufte \color[green]{oeps} \par}
1158%D \stoptextcolor
1159%D \stoptyping
1160%D
1161%D This is more efficient than the alternative:
1162%D
1163%D \starttyping
1164%D \setupbackgrounds[text][foregroundcolor=red]
1165%D \startregistercolor[red]
1166%D   \dorecurse{10}{\input tufte \color[green]{oeps} \par}
1167%D \stopregistercolor
1168%D \stoptyping
1169
1170\mutable\lettonothing\maintextcolor
1171\mutable\def         \defaulttextcolor{black}
1172
1173\permanent\protected\def\inheritmaintextcolor
1174  {\ifempty\maintextcolor\else\colo_helpers_activate\maintextcolor\fi}
1175
1176\permanent\protected\def\onlyinheritmaintextcolor
1177  {\ifempty\maintextcolor
1178     \deactivatecolor
1179   \else
1180     \colo_helpers_activate\maintextcolor
1181   \fi}
1182
1183\appendtoks
1184    \deactivatecolor % public?
1185    \inheritmaintextcolor
1186\to \everybeforeoutput
1187
1188\def\colo_helpers_switch_to_maintextcolor#1%
1189  {\let\maintextcolor\s!themaintextcolor
1190   \definecolor[\maintextcolor][#1]% can be fast one
1191   \colo_helpers_activate\maintextcolor
1192   \clf_registermaintextcolor\thecolorattribute\maintextcolor\relax}
1193
1194\permanent\protected\def\starttextcolor[#1]%
1195  {\ifempty{#1}\else
1196     \colo_helpers_switch_to_maintextcolor{#1}%
1197   \fi}
1198
1199\permanent\protected\lettonothing\stoptextcolor
1200
1201\lettonothing\p_colo_textcolor
1202
1203\def\colo_helpers_initialize_maintextcolor
1204  {\edef\p_colo_textcolor{\directcolorsparameter\c!textcolor}%
1205   \ifempty\p_colo_textcolor
1206     \colo_helpers_switch_to_maintextcolor\defaulttextcolor
1207   \else
1208     \colo_helpers_switch_to_maintextcolor\p_colo_textcolor
1209   \fi}
1210
1211\appendtoks \colo_helpers_initialize_maintextcolor \to \everyjob
1212\appendtoks \colo_helpers_initialize_maintextcolor \to \everysetupcolors
1213
1214\letcsname\??colorsetter       \endcsname\empty \letcsname\??colorattribute       \endcsname\!!zerocount
1215\letcsname\??transparencysetter\endcsname\empty \letcsname\??transparencyattribute\endcsname\!!zerocount
1216
1217\def\colo_helpers_inherited_direct_cs#1{\begincsname\??colorsetter          #1\endcsname}
1218\def\colo_helpers_inherited_direct_ca#1{\ifcsname   \??colorattribute       #1\endcsname\lastnamedcs\else\!!zerocount\fi}
1219\def\colo_helpers_inherited_direct_ts#1{\begincsname\??transparencysetter   #1\endcsname}
1220\def\colo_helpers_inherited_direct_ta#1{\ifcsname   \??transparencyattribute#1\endcsname\lastnamedcs\else\!!zerocount\fi}
1221
1222\let\colo_defcsname\defcsname
1223\let\colo_endcsname\endcsname
1224
1225\appendtoks
1226   \ifconditional\c_colo_expanded
1227     \let\colo_defcsname\edefcsname
1228   \else
1229     \let\colo_defcsname\defcsname
1230   \fi
1231\to \everysetupcolors
1232
1233\def\colo_palets_define_set#1#2#3%
1234  {\ifhastok={#3}% \definepalet[test][xx={y=.4}]
1235     \expandafter\colo_palets_define_assign
1236   \orelse\ifcsname\??colorsetter#3\endcsname
1237     % \definepalet[test][xx=green]
1238     \expandafter\colo_palets_define_inherit
1239   \else
1240     % not entered when making format
1241     \expandafter\colo_palets_define_undefine
1242   \fi
1243   {#1}{#2}{#3}}
1244
1245\def\colo_palets_define_inherit#1#2#3%
1246  {\colo_basics_inherit{#1:#2}{#3}%
1247   \colo_defcsname\??colorsetter          #1:#2\colo_endcsname{\colo_helpers_inherited_direct_cs{#3}}%
1248   \colo_defcsname\??colorattribute       #1:#2\colo_endcsname{\colo_helpers_inherited_direct_ca{#3}}%
1249   \colo_defcsname\??transparencysetter   #1:#2\colo_endcsname{\colo_helpers_inherited_direct_ts{#3}}%
1250   \colo_defcsname\??transparencyattribute#1:#2\colo_endcsname{\colo_helpers_inherited_direct_ta{#3}}}
1251
1252\def\colo_palets_define_undefine#1#2#3%
1253  {\letcsname\??colorsetter          #1:#2\endcsname\undefined
1254   \letcsname\??colorattribute       #1:#2\endcsname\undefined
1255   \letcsname\??transparencysetter   #1:#2\endcsname\undefined
1256   \letcsname\??transparencyattribute#1:#2\endcsname\undefined}
1257
1258\def\colo_palets_define_assign#1#2#3%
1259  {\definecolor[\??colorpalet#1:#2][#3]%
1260   \colo_defcsname\??colorsetter          #1:#2\colo_endcsname{\colo_helpers_inherited_direct_cs{\??colorpalet#1:#2}}%
1261   \colo_defcsname\??colorattribute       #1:#2\colo_endcsname{\colo_helpers_inherited_direct_ca{\??colorpalet#1:#2}}%
1262   \colo_defcsname\??transparencysetter   #1:#2\colo_endcsname{\colo_helpers_inherited_direct_ts{\??colorpalet#1:#2}}%
1263   \colo_defcsname\??transparencyattribute#1:#2\colo_endcsname{\colo_helpers_inherited_direct_ta{\??colorpalet#1:#2}}}
1264
1265\defcsname\??colorattribute        currentcolor\endcsname{\the\c_attr_color}        % for mpcolor
1266\defcsname\??transparencyattribute currentcolor\endcsname{\the\c_attr_transparency} % for mpcolor
1267
1268\def\colo_helpers_inherited_current_ca#1{\ifcsname\??colorattribute       \currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??colorattribute       #1\endcsname\lastnamedcs\else\!!zerocount\fi}
1269\def\colo_helpers_inherited_current_cs#1{\ifcsname\??colorsetter          \currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??colorsetter          #1\endcsname\lastnamedcs\fi}
1270\def\colo_helpers_inherited_current_ta#1{\ifcsname\??transparencyattribute\currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??transparencyattribute#1\endcsname\lastnamedcs\else\!!zerocount\fi}
1271\def\colo_helpers_inherited_current_ts#1{\ifcsname\??transparencysetter   \currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??transparencysetter   #1\endcsname\lastnamedcs\fi}
1272
1273%D Low level defs:
1274
1275\permanent\protected\def\colordefalc#1#2{\edefcsname\??colorattribute       #1\endcsname{#2}%
1276                                         \defcsname \??colorsetter          #1\endcsname{\c_attr_color       #2 }}
1277\permanent\protected\def\colordefagc#1#2{\xdefcsname\??colorattribute       #1\endcsname{#2}%
1278                                         \gdefcsname\??colorsetter          #1\endcsname{\c_attr_color       #2 }} % was not g
1279\permanent\protected\def\colordefalt#1#2{\edefcsname\??transparencyattribute#1\endcsname{#2}%
1280                                         \gdefcsname\??transparencysetter   #1\endcsname{\c_attr_transparency#2 }}
1281\permanent\protected\def\colordefagt#1#2{\xdefcsname\??transparencyattribute#1\endcsname{#2}%
1282                                         \gdefcsname\??transparencysetter   #1\endcsname{\c_attr_transparency#2 }}
1283
1284\permanent\protected\def\colordefflc#1#2{\defcsname \??colorattribute       #1\endcsname{\colo_helpers_inherited_current_ca{#2}}%
1285                                         \defcsname \??colorsetter          #1\endcsname{\colo_helpers_inherited_current_cs{#2}}}
1286\permanent\protected\def\colordeffgc#1#2{\gdefcsname\??colorattribute       #1\endcsname{\colo_helpers_inherited_current_ca{#2}}%
1287                                         \defcsname \??colorsetter          #1\endcsname{\colo_helpers_inherited_current_cs{#2}}}
1288\permanent\protected\def\colordefflt#1#2{\defcsname \??transparencyattribute#1\endcsname{\colo_helpers_inherited_current_ta{#2}}%
1289                                         \defcsname \??transparencysetter   #1\endcsname{\colo_helpers_inherited_current_ts{#2}}}
1290\permanent\protected\def\colordeffgt#1#2{\gdefcsname\??transparencyattribute#1\endcsname{\colo_helpers_inherited_current_ta{#2}}%
1291                                         \defcsname \??transparencysetter   #1\endcsname{\colo_helpers_inherited_current_ts{#2}}}
1292
1293\permanent\protected\def\colordefrlc  #1{\letcsname \??colorattribute       #1\endcsname\undefined
1294                                         \letcsname \??colorsetter          #1\endcsname\undefined}
1295\permanent\protected\def\colordefrgc  #1{\gletcsname\??colorattribute       #1\endcsname\undefined
1296                                         \gletcsname\??colorsetter          #1\endcsname\undefined}
1297\permanent\protected\def\colordefrlt  #1{\letcsname \??transparencyattribute#1\endcsname\undefined
1298                                         \letcsname \??transparencysetter   #1\endcsname\undefined}
1299\permanent\protected\def\colordefrgt  #1{\gletcsname\??transparencyattribute#1\endcsname\undefined
1300                                         \gletcsname\??transparencysetter   #1\endcsname\undefined}
1301
1302%D \macros
1303%D   {colorvalue, grayvalue}
1304%D
1305%D We can typeset the color components using \type {\colorvalue} and \type
1306%D {\grayvalue}. The commands:
1307%D
1308%D \startbuffer
1309%D color value of SomeKindOfRed: \colorvalue{SomeKindOfRed} \crlf
1310%D gray value of SomeKindOfRed: \grayvalue{SomeKindOfRed}
1311%D \stopbuffer
1312%D
1313%D \typebuffer
1314%D
1315%D show us:
1316%D
1317%D \startexample
1318%D \getbuffer
1319%D \stopexample
1320
1321\mutable\let\currentcolorname\s!black % todo
1322\mutable\let\outercolorname  \s!black % todo
1323
1324\mutable\let\colorformatseparator\space
1325
1326\permanent\def\MPcolor#1%
1327  {\clf_mpcolor
1328     \c_attr_colormodel
1329     \colo_helpers_inherited_current_ca{#1} %
1330     \colo_helpers_inherited_current_ta{#1} }
1331
1332\permanent\def\MPcoloronly#1%
1333  {\clf_mpcolor
1334     \c_attr_colormodel
1335     \colo_helpers_inherited_current_ca{#1} %
1336     \zerocount}
1337
1338\permanent\def\MPtransparency#1%
1339  {\clf_mpcolor
1340     \zerocount
1341     \zerocount
1342     \colo_helpers_inherited_current_ta{#1} }
1343
1344\permanent\def\MPoptions#1%
1345  {\clf_mpoptions
1346     \c_attr_colormodel
1347     \colo_helpers_inherited_current_ca{#1} %
1348     \colo_helpers_inherited_current_ta{#1} }
1349
1350\permanent\def\rawcolormodelattribute    {\c_attr_colormodel}
1351\permanent\def\thecolormodelattribute    {\the\c_attr_colormodel}
1352
1353\permanent\def\rawcolorattribute       #1{\ifcsname\??colorattribute       \currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??colorattribute       #1\endcsname\lastnamedcs\else\zerocount\fi}
1354\permanent\def\rawtransparencyattribute#1{\ifcsname\??transparencyattribute\currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??transparencyattribute#1\endcsname\lastnamedcs\else\zerocount\fi}
1355
1356\permanent\def\thecolorattribute       #1{\number\ifcsname\??colorattribute       \currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??colorattribute       #1\endcsname\lastnamedcs\else\zerocount\fi}
1357\permanent\def\thetransparencyattribute#1{\number\ifcsname\??transparencyattribute\currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??transparencyattribute#1\endcsname\lastnamedcs\else\zerocount\fi}
1358
1359\permanent\def\internalspotcolorname   #1{\clf_spotcolorname  \rawcolorattribute{#1}}
1360\permanent\def\internalspotcolorparent #1{\clf_spotcolorparent\rawcolorattribute{#1}}
1361\permanent\def\internalspotcolorsize   #1{\clf_spotcolorvalue \rawcolorattribute{#1}}
1362
1363\permanent\def\colorcomponents         #1{\clf_colorcomponents       \rawcolorattribute       {#1}}
1364\permanent\def\transparencycomponents  #1{\clf_transparencycomponents\rawtransparencyattribute{#1}}
1365\permanent\def\processcolorcomponents  #1{\clf_processcolorcomponents\rawcolorattribute       {#1}}
1366
1367\permanent\def\colorvalue              #1{\clf_formatcolor\rawcolorattribute{#1}{\colorformatseparator}}
1368\permanent\def\grayvalue               #1{\clf_formatgray \rawcolorattribute{#1}{\colorformatseparator}}
1369
1370\permanent\def\colorluminance          #1{\clf_formatluminance     \rawcolorattribute{#1} }
1371\permanent\def\colorluminanceratio   #1#2{\clf_formatluminanceratio\rawcolorattribute{#1} \rawcolorattribute{#2} }
1372
1373\permanent\def\doifelseblack           #1{\clf_doifelseblack\rawcolorattribute{#1}}
1374\permanent\def\doifelsedrawingblack      {\clf_doifelsedrawingblack}
1375
1376\aliased\let\doifblackelse        \doifelseblack
1377\aliased\let\doifdrawingblackelse \doifelsedrawingblack
1378
1379%D \macros
1380%D   {forcecolorhack}
1381%D
1382%D We can out this in front of (for instance) a special and so force color to be
1383%D applied (only glyphs, rules and leaders are handled).
1384%D
1385%D \startbuffer
1386%D \framed
1387%D   [background=color,backgroundcolor=yellow,framecolor=red,corner=round]
1388%D   {test}
1389%D \stopbuffer
1390%D
1391%D \typebuffer \getbuffer
1392
1393% ignores in attribute handler
1394%
1395% \def\forcecolorhack{\vrule\s!width\zeropoint\s!height\zeropoint\s!depth\zeropoint}
1396
1397% \normal added else fails in metafun manual (leaders do a hard scan)
1398
1399% \protected\def\forcecolorhack{\leaders\hrule\hskip\zeroskip\relax} % relax is needed !
1400%
1401% I really need to sort this out!
1402
1403% \permanent\protected\def\normalforcecolorhack{\leaders\hrule\s!height\zeropoint\s!depth\zeropoint\hskip\zeroskip\relax}
1404%
1405% \installtexexperiment
1406%   {disablecolorhack}
1407%   {\pushoverloadmode\enforced\permanent\protected\lettonothing\forcecolorhack\popoverloadmode}
1408%   {\pushoverloadmode\enforced\let\forcecolorhack\normalforcecolorhack\popoverloadmode}
1409%
1410% use \hpack container{...}
1411
1412\permanent\protected\lettonothing\forcecolorhack
1413
1414%D We default to the colors defined in \type {colo-imp-rgb} and support both \RGB\
1415%D and \CMYK\ output. Transparencies are defined here:
1416
1417\definecolor[black][s=0]
1418\definecolor[white][s=1]
1419
1420\definetransparency [none]        [0]
1421\definetransparency [normal]      [1]
1422\definetransparency [multiply]    [2]
1423\definetransparency [screen]      [3]
1424\definetransparency [overlay]     [4]
1425\definetransparency [softlight]   [5]
1426\definetransparency [hardlight]   [6]
1427\definetransparency [colordodge]  [7]
1428\definetransparency [colorburn]   [8]
1429\definetransparency [darken]      [9]
1430\definetransparency [lighten]    [10]
1431\definetransparency [difference] [11]
1432\definetransparency [exclusion]  [12]
1433\definetransparency [hue]        [13]
1434\definetransparency [saturation] [14]
1435\definetransparency [color]      [15]
1436\definetransparency [luminosity] [16]
1437
1438%D Some common ones:
1439
1440\definecolor[trace:0][s=.4]
1441\definecolor[trace:1][r=.6]
1442\definecolor[trace:2][g=.6]
1443\definecolor[trace:3][b=.6]
1444\definecolor[trace:4][r=.6,g=.6]
1445\definecolor[trace:5][r=.6,b=.6]
1446\definecolor[trace:6][g=.6,b=.6]
1447\definecolor[trace:7][r=.8,g=.4]
1448\definecolor[trace:8][r=.8,b=.4]
1449\definecolor[trace:9][g=.4,b=.8]
1450
1451\definecolor[trace:w][s=1]
1452\definecolor[trace:z][s=0,t=.5,a=1]
1453\definecolor[trace:r][r=.75,t=.5,a=1]
1454\definecolor[trace:g][g=.75,t=.5,a=1]
1455\definecolor[trace:b][b=.75,t=.5,a=1]
1456%definecolor[trace:c][c=.75,t=.5,a=1]
1457%definecolor[trace:m][m=.75,t=.5,a=1]
1458%definecolor[trace:y][y=.75,t=.5,a=1]
1459\definecolor[trace:c][g=.75,b=.75,t=.5,a=1]
1460\definecolor[trace:m][r=.75,b=.75,t=.5,a=1]
1461\definecolor[trace:y][r=.75,g=.75,t=.5,a=1]
1462\definecolor[trace:s][s=.75,t=.5,a=1]
1463\definecolor[trace:d][s=.25,t=.5,a=1]
1464\definecolor[trace:o][r=1,g=.6,b=.1,t=.5,a=1]
1465
1466\definecolor[trace:dw][s=1]
1467\definecolor[trace:dz][s=0,t=.75,a=1]
1468\definecolor[trace:dr][r=.75,t=.75,a=1]
1469\definecolor[trace:dg][g=.75,t=.75,a=1]
1470\definecolor[trace:db][b=.75,t=.75,a=1]
1471%definecolor[trace:dc][c=.75,t=.75,a=1]
1472%definecolor[trace:dm][m=.75,t=.75,a=1]
1473%definecolor[trace:dy][y=.75,t=.75,a=1]
1474\definecolor[trace:dc][g=.75,b=.75,t=.75,a=1]
1475\definecolor[trace:dm][r=.75,b=.75,t=.75,a=1]
1476\definecolor[trace:dy][r=.75,g=.75,t=.75,a=1]
1477\definecolor[trace:ds][s=.75,t=.75,a=1]
1478\definecolor[trace:dd][s=.25,t=.75,a=1]
1479\definecolor[trace:do][r=1,g=.6,b=.1,t=.75,a=1]
1480
1481\setupcolors
1482  [\c!state=\v!stop, % color will be enabled later on
1483   \c!conversion=\v!yes,
1484   \c!rgb=\v!yes,
1485   \c!cmyk=\v!yes,
1486   \c!spot=\v!yes,
1487   \c!pagecolormodel=\v!none,
1488   \c!expansion=\v!no,
1489   \c!textcolor=,
1490   \c!factor=\v!yes]
1491
1492\setupcolor
1493  [\v!rgb]
1494
1495\protect \endinput
1496