colo-ini.mkxl /size: 49 Kb    last modification: 2023-12-21 09:44
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}{}
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%D In this documentation we will not go into too much details on palets. Curious
400%D users can find more information on this topic in \from[use of color].
401%D
402%D At the moment we implemented color in \CONTEXT\ color printing was not yet on the
403%D desktop. In spite of this lack our graphics designer made colorfull
404%D illustrations. When printed on a black and white printer, distinctive colors can
405%D come out equally gray. We therefore decided to use only colors that were
406%D distinctive in colors as well as in black and white print.
407%D
408%D Although none of the graphic packages we used supported logical colors and global
409%D color redefition, we build this support into \CONTEXT. This enabled us to
410%D experiment and also prepared us for the future.
411
412%D \macros
413%D   {definepalet}
414%D
415%D Colors are grouped in palets. The colors in such a palet can have colorful names,
416%D but best is to use names that specify their use, like {\em important} or {\em
417%D danger}. As a sort of example \CONTEXT\ has some palets predefined,
418%D like:\footnote{At the time I wrote the palet support, I was reading 'A hort
419%D history of time' of S.~Hawkins, so that's why we stuck to quarks.}
420%D
421%D \starttyping
422%D \definepalet
423%D   [alfa]
424%D   [     top=rood:7,
425%D      bottom=groen:6,
426%D          up=blauw:5,
427%D        down=cyaan:4,
428%D     strange=magenta:3,
429%D       charm=geel:2]
430%D \stoptyping
431%D
432%D It's formal definition is:
433%D
434%D \showsetup{definepalet}
435%D
436%D Visualized, such a palet looks like:
437%D
438%D \startbuffer[palet]
439%D \showpalet [alfa] [horizontal,name,number,value]
440%D \stopbuffer
441%D
442%D \startlinecorrection
443%D \getbuffer[palet]
444%D \stoplinecorrection
445%D
446%D This bar shows both the color and gray alternatives of the palet components (not
447%D visible in black and white print).
448%D
449%D When needed, one can copy a palet by saying:
450%D
451%D \starttyping
452%D \definepalet [TEXcolorpretty] [colorpretty]
453%D \stoptyping
454%D
455%D This saves us some typing in for instance the modules that deal with pretty
456%D verbatim typesetting.
457
458\installcorenamespace{paletlist}
459\installcorenamespace{paletsize}
460
461\let\m_colo_palet\relax
462\let\c_colo_palet\relax
463
464\def\colo_palet_allocate#1%
465  {\letcsname\??paletlist#1\endcsname\empty
466   \ifcsname\??paletsize#1\endcsname\else
467     \expandafter\newinteger\csname\??paletsize#1\endcsname
468   \fi}
469
470\def\colo_palet_prepare#1%
471  {\edef\colo_palet_name{#1}%
472   \ifcsname\??paletlist\colo_palet_name\endcsname
473     \csname\??paletsize\colo_palet_name\endcsname\zerocount
474   \else
475     \colo_palet_allocate\colo_palet_name
476   \fi
477 % \enforced\expandafter\integerdef\expandafter\c_colo_palet\csname\??paletsize\colo_palet_name\endcsname
478   \edef\c_colo_palet{\csname\??paletsize\colo_palet_name\endcsname}% we can't alias  as they are integers
479   \edef\m_colo_palet{\begincsname\??paletlist\colo_palet_name\endcsname}}
480
481\def\colo_palet_extend#1%
482  {\addtocommalist{#1}\m_colo_palet
483   \letcsname\??paletlist\colo_palet_name\endcsname\m_colo_palet
484  %\advanceby\csname\??paletsize\colo_palet_name\endcsname\plusone
485   \advanceby\c_colo_palet\plusone}
486
487\permanent\protected\def\doifelsecolorpalet#1%
488  {\ifcsname\??paletlist#1\endcsname
489     \expandafter\firstoftwoarguments
490   \else
491     \expandafter\secondoftwoarguments
492   \fi}
493
494\mutable\lettonothing\paletlist
495\mutable\let         \paletsize\!!zerocount
496
497\permanent\protected\def\getpaletlist[#1]%
498  {\edef\paletlist{\begincsname\??paletlist#1\endcsname}}
499
500\permanent\protected\def\getpaletsize[#1]%
501  {\ifcsname\??paletsize#1\endcsname
502     \edef\paletsize{\the\lastnamedcs}%
503   \else
504     \let\paletsize\!!zerocount
505   \fi}
506
507\permanent\tolerant\protected\def\definepalet[#1]#*[#2]#*[#3]%
508  {\ifarguments
509     \expandafter\gobblethreearguments
510   \or
511     \expandafter\gobblethreearguments
512   \or
513     \expandafter\colo_palets_define_a
514   \or
515     \expandafter\colo_palets_define_b
516  \fi{#1}{#2}{#3}}
517
518\protected\def\colo_palets_define_a#1#2#3%
519  {\colo_palet_prepare{#1}%
520   \ifhastok={#2}%
521     \edefcsname\??colorpaletspecification#1\endcsname{#2}%
522     \processcommalist[#2]{\colo_palets_define_one{#1}}%
523   \orelse\ifcsname\??colorpaletspecification#2\endcsname
524     \normalexpanded{\colo_palets_define_b{#1}{\lastnamedcs}\empty}%
525   \fi}
526
527\protected\def\colo_palets_define_b#1#2#3%
528  {\colo_palet_prepare{#1}%
529   \edefcsname\??colorpaletspecification#1\endcsname{#2}%
530   \colo_palet_extend{#2}%
531   \colo_palets_define_assign{#1}{#2}{#3}}
532
533% % or, it being not that critital, just this
534%
535% \permanent\tolerant\protected\def\definepalet[#1]#*[#2]#*[#3]%
536%   {\ifarguments
537%    \or
538%    \or
539%      \colo_palet_prepare{#1}%
540%      \ifhastok={#2}%
541%        \edefcsname\??colorpaletspecification#1\endcsname{#2}%
542%        \processcommalist[#2]{\colo_palets_define_one{#1}}%
543%      \orelse\ifcsname\??colorpaletspecification#2\endcsname
544%        \normalexpanded{\colo_palets_define_b{#1}{\lastnamedcs}{}}%
545%      \fi
546%    \or
547%      \colo_palet_prepare{#1}%
548%      \edefcsname\??colorpaletspecification#1\endcsname{#2}%
549%      \colo_palet_extend{#2}%
550%      \colo_palets_define_assign{#1}{#2}{#3}%
551%   \fi}
552
553\def\colo_palets_define_one#1#2% get rid of { }
554  {\ifhastok={#2}% catch empty entries
555     \colo_palets_define_two{#1}[#2]%
556   \else
557     \colo_palets_define_three{#1}{#2}%
558   \fi}
559
560\def\colo_palets_define_two#1[#2=#3]%
561  {\colo_palet_extend{#2}%
562   \colo_palets_define_set{#1}{#2}{#3}}%
563
564\def\colo_palets_define_three#1#2%
565  {\ifcsname\??colorpaletspecification#2\endcsname
566     \processcommacommand[\lastnamedcs]{\colo_palets_define_one{#1}}%
567   \fi}
568
569%D Instead of refering to colors, one can also directly specify a color:
570%D
571%D \starttyping
572%D \definepalet[test][xx=green]
573%D \definepalet[test][xx={y=.4}]
574%D \stoptyping
575
576%D \macros
577%D   {setuppalet}
578%D
579%D Colors are taken from the current palet, if defined. Setting the current palet is
580%D done by:
581%D
582%D \showsetup{setuppalet}
583
584\newtoks\everysetuppalet
585
586% \newtoks\t_colo_prefix % used in mp interface
587
588\permanent\tolerant\protected\def\setuppalet[#1]%
589  {\cdef\currentcolorpalet{#1}%
590   \ifempty\currentcolorpalet
591     % seems to be a reset
592     \lettonothing\currentcolorprefix
593    %\t_colo_prefix\emptytoks
594   \orelse\ifcsname\??paletlist\currentcolorpalet\endcsname
595     \cdef\currentcolorprefix{#1:}%
596    %\t_colo_prefix\expandafter{\currentcolorprefix}%
597   \else
598     \colo_helpers_show_message\m!colors7\currentcolorpalet
599     \lettonothing\currentcolorpalet
600     \lettonothing\currentcolorprefix
601    %\t_colo_prefix\emptytoks
602   \fi
603   \expand\everysetuppalet
604   \colo_helpers_initialize_maintextcolor}
605
606%D \macros
607%D   {showpalet}
608%D
609%D The previous visualization was typeset with:
610%D
611%D \typebuffer[palet]
612%D
613%D This commands is defined as:
614%D
615%D \showsetup{showpalet}
616
617\fetchmodulecommand \showpalet \f!colo_run
618
619%D \macros
620%D   {showcolorcomponents}
621%D
622%D \starttyping
623%D \showcolorcomponents[color-1,color-2]
624%D \stoptyping
625
626\fetchmodulecommand \showcolorcomponents \f!colo_run
627
628%D \macros
629%D   {comparepalet}
630%D
631%D There are some more testing macros available:
632%D
633%D \startbuffer
634%D \comparepalet [alfa]
635%D \stopbuffer
636%D
637%D \typebuffer
638%D
639%D shows the palet colors against a background:
640%D
641%D \startlinecorrection
642%D \getbuffer
643%D \stoplinecorrection
644%D
645%D The formal definition is:
646%D
647%D \showsetup{comparepalet}
648
649\fetchmodulecommand \comparepalet \f!colo_run
650
651%D \macros
652%D   {showcolor}
653%D
654%D But let's not forget that we also have the more traditional non||related colors.
655%D These show up after:
656%D
657%D \starttyping
658%D \showcolor [name]
659%D \stoptyping
660%D
661%D Where \type{name} for instance can be \type{rgb}.
662%D
663%D \showsetup{showcolor}
664
665\fetchmodulecommand \showcolor \f!colo_run
666
667%D This one shows the luminance ratio conform WCAG:
668%D
669%D \starttyping
670%D \starttabulate[||r|]
671%D     \NC red   \NC \colorluminance{red}   \NC \NR
672%D     \NC green \NC \colorluminance{green} \NC \NR
673%D     \NC black \NC \colorluminance{black} \NC \NR
674%D     \NC white \NC \colorluminance{white} \NC \NR
675%D \stoptabulate
676%D
677%D \starttabulate[||r|]
678%D     \NC red : green \NC \colorluminanceratio{red}{green} \NC \NR
679%D     \NC red : black \NC \colorluminanceratio{red}{black} \NC \NR
680%D     \NC red : white \NC \colorluminanceratio{red}{white} \NC \NR
681%D \stoptabulate
682%D
683%D \usecolors[crayola]
684%D
685%D \compareluminance[Jade]      [Onyx] \par
686%D \compareluminance[Goldenrod] [Onyx] \par
687%D \compareluminance[CobaltBlue][Onyx] \par
688%D \stoptyping
689
690\fetchmodulecommand \compareluminance \f!colo_run
691
692%D It would make sense to put the following code in \type {colo-mps}, but it it
693%D rather low level.
694
695%D \macros
696%D   {negatecolorcomponent,negatedcolorcomponent}
697%D
698%D These speak for themselves. See \type {colo-ext} for usage.
699
700\permanent\protected\def\negatecolorcomponent #1{\edef#1{\clf_negatedcolorcomponent{#1}}}
701\permanent          \def\negatedcolorcomponent#1{\clf_negatedcolorcomponent{#1}}
702
703%D \macros
704%D   {MPcolor}
705%D
706%D A very special macro is \type{\MPcolor}. This one can be used to pass a \CONTEXT\
707%D color to \METAPOST.
708%D
709%D \starttyping
710%D \MPcolor{my own red}
711%D \stoptyping
712%D
713%D This macro returns \METAPOST\ code that represents the
714%D color.
715%D
716%D For the moment we keep the next downward compatibility switch, i.e.\ expanded
717%D colors. However, predefined colors and palets are no longer expanded (which is
718%D what I wanted in the first place).
719%D
720%D In \MKIV\ we don't support color separation as we might now assume that printing
721%D houses have the right programs to do it themselves. If it's ever needed in \MKIV\
722%D It is relatively easy to support it in the backend code.
723
724% todo: palets in definecolor
725% todo: {\red xx} vs \red{xx}
726
727% \def\mptexcolor#1{"\dogetattributeid\s!color \somecolorattribute{#1} A"}
728%
729% \startMPpage
730%     fill fullcircle scaled 10cm  ;
731%     fill fullcircle scaled 5cm withprescript \mptexcolor{red} withpostscript \mptexcolor{black} ;
732%     fill fullcircle scaled 3cm  ;
733%     draw btex test etex withprescript \mptexcolor{blue}  ;
734% \stopMPpage
735
736\permanent\protected\def\setcolormodel[#1]% % beware, \setupcolors will overload this, so this one is
737  {\colo_helpers_set_model{#1}}  % only for local usage
738
739\def\colo_helpers_set_model#1% direct
740  {\cdef\currentcolormodel{#1}%
741   \clf_setcolormodel{\currentcolormodel}{\m_colo_weight_gray}} % sets attribute at lua end
742
743\colo_helpers_set_model\s!all
744
745\def\colo_helpers_set_current_model
746  {\ifconditional\c_colo_enabled
747     \ifconditional\c_colo_rgb_supported
748       \ifconditional\c_colo_cmyk_supported
749         \colo_helpers_set_model\s!all
750       \else
751         \colo_helpers_set_model\s!rgb
752       \fi
753     \orelse\ifconditional\c_colo_cmyk_supported
754       \colo_helpers_set_model\s!cmyk
755     \orelse\ifconditional\c_colo_convert_gray
756       \colo_helpers_set_model\s!gray
757     \else
758       \colo_helpers_set_model\s!none
759     \fi
760   \orelse\ifconditional\c_colo_convert_gray
761     \colo_helpers_set_model\s!gray
762   \else
763     \colo_helpers_set_model\s!none
764   \fi}
765
766%D Currently in mkiv transparency is implemented independent of color. This costs a
767%D bit more processing time but gives the possibility to apply transparency
768%D independently in the future. Is this useful? If not we may as well combine them
769%D again in the future. By coupling we are downward compatible. When we decouple we
770%D need to do more tricky housekeeping (e.g. persist color independent
771%D transparencies when color bound ones are nil.
772
773% Since we couple definitions, we could stick to one test. Todo. Same for mpcolor.
774
775\def\v_colo_dummy_name{c_o_l_o_r}
776
777\letcsname\??colorattribute       \v_colo_dummy_name\endcsname\empty
778\letcsname\??transparencyattribute\v_colo_dummy_name\endcsname\empty
779\letcsname\??colorsetter          \v_colo_dummy_name\endcsname\empty
780\letcsname\??transparencysetter   \v_colo_dummy_name\endcsname\empty
781
782\letcsname\??colorsetter                           -\endcsname\empty % used?
783\letcsname\??transparencysetter                    -\endcsname\empty % used?
784
785% new: expandable (see tbl preamble)
786
787\def\colo_helpers_direct_activate
788  {\ifempty\currentcolorprefix
789     \expandafter\colo_helpers_direct_activate_nop
790   \else
791     \expandafter\colo_helpers_direct_activate_yes
792   \fi}
793
794\def\colo_helpers_direct_activate_yes#1%
795  {\ifcsname\??colorsetter\currentcolorprefix#1\endcsname
796     \lastnamedcs
797     \begincsname\??transparencysetter\currentcolorprefix#1\endcsname
798   \orelse\ifcsname\??colorsetter#1\endcsname
799     \lastnamedcs
800     \begincsname\??transparencysetter#1\endcsname
801   \fi}
802
803\def\colo_helpers_direct_activate_nop#1%
804  {\ifcsname\??colorsetter#1\endcsname
805     \lastnamedcs
806     \begincsname\??transparencysetter#1\endcsname
807   \fi}
808
809\def\colo_helpers_activate_dummy
810  {\begincsname\??colorsetter       \v_colo_dummy_name\endcsname
811   \begincsname\??transparencysetter\v_colo_dummy_name\endcsname}
812
813% so far
814
815\permanent\protected\def\colo_helpers_activate#1% two-step is not that much faster but less tracing
816  {\cdef\currentcolorname{#1}%
817   \ifempty\currentcolorprefix
818     \colo_helpers_activate_nop
819   \else
820     \colo_helpers_activate_yes
821   \fi}
822
823\def\colo_helpers_activate_yes
824  {\ifcsname\??colorsetter\currentcolorprefix\currentcolorname\endcsname
825     \lastnamedcs
826     \begincsname\??transparencysetter\currentcolorprefix\currentcolorname\endcsname
827   \orelse\ifcsname\??colorsetter\currentcolorname\endcsname
828     \lastnamedcs
829     \begincsname\??transparencysetter\currentcolorname\endcsname
830   \fi}
831
832\def\colo_helpers_activate_nop
833  {\ifcsname\??colorsetter\currentcolorname\endcsname
834     \lastnamedcs
835     \begincsname\??transparencysetter\currentcolorname\endcsname
836   \fi}
837
838\def\colo_helpers_activate_yes_only
839  {\ifcsname\??colorsetter\currentcolorprefix\currentcolorname\endcsname
840     \lastnamedcs
841   \orelse\ifcsname\??colorsetter\currentcolorname\endcsname
842     \lastnamedcs
843   \fi}
844
845\def\colo_helpers_activate_nop_only
846  {\ifcsname\??colorsetter\currentcolorname\endcsname
847     \lastnamedcs
848   \fi}
849
850\aliased\let\dousecolorparameter\colo_helpers_activate
851
852\permanent\protected\def\dousecolorhashparameter#1#2%
853  {\ifcsname#1#2\endcsname
854     \expandafter\dousecolorparameter\csname#1#2\endcsname
855   \fi}
856
857\permanent\protected\def\dousecurrentcolorparameter
858  {\let\currentcolorname\currentcolorparameter % maybe only when success
859   \ifcsname\??colorsetter\currentcolorprefix\currentcolorparameter\endcsname
860     \lastnamedcs
861     \begincsname\??transparencysetter\currentcolorprefix\currentcolorparameter\endcsname
862   \orelse\ifcsname\??colorsetter\currentcolorparameter\endcsname
863     \lastnamedcs
864     \begincsname\??transparencysetter\currentcolorparameter\endcsname
865   \fi}
866
867\permanent\protected\def\deactivatecolor % sort of public but a bad name
868  {\let\currentcolorname\s!black
869   \c_attr_color       \attributeunsetvalue
870   \c_attr_transparency\attributeunsetvalue}
871
872% todo: check if color is overloading a non-color command
873
874% \let\colo_basics_synchronize\gobbleoneargument  % used in mp interface
875% \let\colo_basics_inherit    \gobbletwoarguments % used in mp interface
876
877\def\colo_basics_allocate#1%
878  {\expandafter\newinteger\csname\??colornumber#1\endcsname
879   \clf_synccolorcount{#1}{\??colornumber#1}}
880
881\def\colo_basics_synchronize#1%
882  {\ifcsname\??colornumber#1\endcsname\else
883     \colo_basics_allocate{#1}%
884   \fi
885   \clf_synccolor{#1}%
886  %\csname\??colornumber#1\endcsname\csname\??colorattribute#1\endcsname
887   \lastnamedcs\csname\??colorattribute#1\endcsname}
888
889\let\colo_basics_inherit\clf_synccolorclone
890
891\newinteger\c_colo_protection
892
893\permanent\protected\def\startprotectedcolors{\advanceby\c_colo_protection\plusone }
894\permanent\protected\def\stopprotectedcolors {\advanceby\c_colo_protection\minusone}
895
896\permanent\tolerant\protected\def\definecolor[#1]#*[#2]%
897  {\iftok{#1}{#2}%
898     % maybe a warning
899   \else
900     \clf_defineprocesscolorlocal{#1}{#2}\v_colo_freeze_state\relax
901     \colo_basics_synchronize{#1}%
902     \ifcase\c_colo_protection
903       \protected\instance\defcsname#1\endcsname{\colo_helpers_activate{#1}}%
904     \fi
905   \fi}
906
907\permanent\tolerant\protected\def\defineglobalcolor[#1]#*[#2]%
908  {\iftok{#1}{#2}%
909     % maybe a warning
910   \else
911     \clf_defineprocesscolorglobal{#1}{#2}\v_colo_freeze_state\relax
912     \colo_basics_synchronize{#1}%
913     \ifcase\c_colo_protection
914       \protected\instance\gdefcsname#1\endcsname{\colo_helpers_activate{#1}}%
915     \fi
916   \fi}
917
918\aliased\let\definenamedcolor\definecolor
919
920\def\colo_basics_defined_and_activated#1%
921  {\clf_defineprocesscolordummy{#1}% we could pass dummy here too
922   \colo_basics_synchronize{\v_colo_dummy_name}%
923   \colo_helpers_activate_dummy}
924
925\permanent\tolerant\protected\def\defineprocesscolor[#1]#*[#2]#*[#3]%
926  {\ifarguments\or
927     \colo_basics_define_process_wrapup{#1}\empty
928   \or
929     \colo_basics_define_process_wrapup{#1}{#2}%
930   \or % we could just pass #3 to clf_... also when it's empty ... todo after split
931     \colo_basics_define_process_wrapup{#1}{\processcolorcomponents{#2},#3}%
932   \fi}
933
934\def\colo_basics_define_process_wrapup#1#2%
935  {\clf_defineprocesscolorlocal{#1}{#2}\v_colo_freeze_state\relax
936   \colo_basics_synchronize{#1}%
937   \ifcase\c_colo_protection
938     \protected\instance\defcsname#1\endcsname{\colo_helpers_activate{#1}}%
939   \fi}
940
941%D Spotcolors used setxvalue but that messes up currentcolor and probably no global
942%D is needed either but they are global at the lua end (true argument) so we keep
943%D that if only because spot colors often are a document wide property
944
945\permanent\tolerant\protected\def\definespotcolor[#1]#*[#2]#*[#3]%
946  {\clf_definespotcolorglobal{#1}{#2}{#3}%
947   \colo_basics_synchronize{#1}%
948   \ifcase\c_colo_protection
949     \protected\instance\gdefcsname#1\endcsname{\colo_helpers_activate{#1}}%
950   \fi}
951
952\permanent\tolerant\protected\def\definemultitonecolor[#1]#*[#2]#*[#3]#*[#4]%
953  {\clf_definemultitonecolorglobal{#1}{#2}{#3}{#4}%
954   \colo_basics_synchronize{#1}%
955   \ifcase\c_colo_protection
956     \protected\instance\gdefcsname#1\endcsname{\colo_helpers_activate{#1}}%
957   \fi}
958
959%D Transparencies (only):
960
961\permanent\tolerant\protected\def\definetransparency[#1]#*[#2]%
962  {\clf_definetransparency{#1}{#2}}
963
964\permanent\tolerant\protected\def\defineglobaltransparency[#1]#*[#2]%
965  {\clf_definetransparencyglobal{#1}{#2}}
966
967% A goodie that replaces the startMPcolor hackery
968
969% \definecolor[red-t]  [r=1,t=0.5,a=1]
970% \definecolor[green-t][g=1,t=0.5,a=1]
971%
972% \defineintermediatecolor[mycolora][0.5,red,green]
973% \defineintermediatecolor[mycolorb][0.5,red-t,green-t]
974%
975% \definecolor[mycolorc][.5(blue,red)]
976% \definecolor[mycolord][.5(blue)]
977% \definecolor[mycolord][-.5(blue,red)] % complement
978% \definecolor[mycolord][-(blue)]       % complement
979%
980% \enabledirectives[colors.pgf]
981% \definecolor[mycolorx][magenta!50!yellow]
982%
983% \starttext
984%     test {\mycolora OEPS} test
985%     test {\mycolorb OEPS} test
986%     test {\mycolorc OEPS} test
987%     test {\mycolord OEPS} test
988%     test {\mycolorx OEPS} test
989% \stoptext
990%
991% Beware: if we say:
992%
993% \definecolor[one][two]
994%
995% Only color one is actually defined and two is not seen at the \LUA\ end. This
996% means that this doesn't work:
997%
998% \definecolor[ColorA][red]
999% \definecolor[ColorB][.5(ColorA)]
1000% \definecolor[ColorC][.5(ColorB,white)]
1001%
1002% But this does work:
1003%
1004% \definecolor[ColorA][1.0(red)]
1005% \definecolor[ColorB][0.5(ColorA)]
1006% \definecolor[ColorC][0.5(ColorB,white)]
1007%
1008% because the fractional definition results in a new definition.
1009
1010\permanent\tolerant\protected\def\defineintermediatecolor[#1]#*[#2]#*[#3]
1011  {\colo_basics_define_intermediate_indeed[#1][#2][#3]}
1012
1013\def\colo_basics_define_intermediate_indeed[#1][#2,#3,#4][#5]%
1014  {\clf_defineintermediatecolor % not global
1015     {#1}%
1016     {#2}%
1017     \rawcolorattribute{#3} %
1018     \rawcolorattribute{#4} %
1019     \rawtransparencyattribute{#3} %
1020     \rawtransparencyattribute{#4} %
1021     {#5}%
1022     \v_colo_freeze_state
1023   \relax
1024   \colo_basics_synchronize{#1}%
1025   \protected\instance\defcsname#1\endcsname{\colo_helpers_activate{#1}}}
1026
1027%D Here is a more efficient helper for pgf:
1028%D
1029%D \starttyping
1030%D \startluacode
1031%D     function commands.pgfxcolorspec(ca) -- {}{}{colorspace}{list}
1032%D         local cv = attributes.colors.values[ca]
1033%D         if cv then
1034%D             local model = cv[1]
1035%D             if model == 2 then
1036%D                 context("{}{}{gray}{%1.3f}",cv[2])
1037%D             elseif model == 3 then
1038%D                 context("{}{}{rgb}{%1.3f,%1.3f,%1.3f}",cv[3],cv[4],cv[5])
1039%D             elseif model == 4 then
1040%D                 context("{}{}{cmyk}{%1.3f,%1.3f,%1.3f,%1.3f}",cv[6],cv[7],cv[8],cv[9])
1041%D             else
1042%D                 context("{}{}{gray}{%1.3f}",cv[2])
1043%D             end
1044%D         else
1045%D             context("{}{}{gray}{0}")
1046%D         end
1047%D     end
1048%D \stopluacode
1049%D
1050%D \def\pgfutil@registercolor#1%
1051%D   {\setevalue{\string\color@#1}{\noexpand\xcolor@\ctxcommand{pgfxcolorspec(\thecolorattribute{#1})}}}
1052%D
1053%D \definecolor [darkblue] [r=0,g=0,b=0.5]
1054%D
1055%D \starttikzpicture
1056%D     \fill [blue] (0,0) circle(1);
1057%D     \fill [darkblue] (0,0) circle(1);
1058%D \stoptikzpicture
1059%D \stoptyping
1060%D
1061%D \stopmode
1062%D
1063%D All this will move to m-tikz.mkxl as it doesn't belong in the core. They are
1064%D mutable because \quote {who knows what gets redefined}.
1065
1066\mutable\let\color@ \relax
1067\mutable\let\xcolor@\relax
1068
1069\mutable\protected\def\pgf@context@registercolor#1{\edefcsname\string\color@#1\endcsname{\noexpand\xcolor@{}{}\clf_pgfxcolorspec\zerocount\thecolorattribute{#1}}}
1070\mutable\protected\def\pgf@context@registergray #1{\edefcsname\string\color@#1\endcsname{\noexpand\xcolor@{}{}\clf_pgfxcolorspec\plustwo  \thecolorattribute{#1}}}
1071\mutable\protected\def\pgf@context@registerrgb  #1{\edefcsname\string\color@#1\endcsname{\noexpand\xcolor@{}{}\clf_pgfxcolorspec\plusthree\thecolorattribute{#1}}}
1072\mutable\protected\def\pgf@context@registercmyk #1{\edefcsname\string\color@#1\endcsname{\noexpand\xcolor@{}{}\clf_pgfxcolorspec\plusfour \thecolorattribute{#1}}}
1073
1074%D \starttyping
1075%D \ifdefined\pgf@context@registercolor
1076%D     \let\pgfutil@registercolor\pgf@context@registercolor
1077%D \fi
1078%D \stoptyping
1079
1080%D \macros
1081%D   {doifcolorelse, doifcolor}
1082%D
1083%D Switching to a color is done by means of the following command. Later on we will
1084%D explain the use of palets. We define ourselves a color conditional first.
1085
1086\permanent\def\doifelsecolor#1%
1087  {\ifcsname\??colorattribute\currentcolorprefix#1\endcsname
1088     \expandafter\firstoftwoarguments
1089   \orelse\ifcsname\??colorattribute#1\endcsname
1090     \expandafter\firstoftwoarguments
1091   \else
1092     \expandafter\secondoftwoarguments
1093   \fi}
1094
1095\aliased\let\doifcolorelse\doifelsecolor
1096
1097\permanent\def\doifcolor#1%
1098  {\ifcsname\??colorattribute\currentcolorprefix#1\endcsname
1099     \expandafter\firstofoneargument
1100   \orelse\ifcsname\??colorattribute#1\endcsname
1101     \expandafter\firstofoneargument
1102   \else
1103     \expandafter\gobbleoneargument
1104   \fi}
1105
1106%D \macros
1107%D   {startregistercolor,stopregistercolor,permitcolormode}
1108%D
1109%D If you only want to register a color, the switch \type {\ifpermitcolormode} can
1110%D be used. That way the nested colors know where to go back to.
1111%D
1112%D We use these macros for implementing text colors (actually, the first application
1113%D was in foreground colors).
1114%D
1115%D \starttyping
1116%D \starttextcolor[red]
1117%D   \dorecurse{10}{\input tufte \color[green]{oeps} \par}
1118%D \stoptextcolor
1119%D \stoptyping
1120%D
1121%D This is more efficient than the alternative:
1122%D
1123%D \starttyping
1124%D \setupbackgrounds[text][foregroundcolor=red]
1125%D \startregistercolor[red]
1126%D   \dorecurse{10}{\input tufte \color[green]{oeps} \par}
1127%D \stopregistercolor
1128%D \stoptyping
1129
1130\mutable\lettonothing\maintextcolor
1131\mutable\def         \defaulttextcolor{black}
1132
1133\permanent\protected\def\inheritmaintextcolor
1134  {\ifempty\maintextcolor\else\colo_helpers_activate\maintextcolor\fi}
1135
1136\permanent\protected\def\onlyinheritmaintextcolor
1137  {\ifempty\maintextcolor
1138     \deactivatecolor
1139   \else
1140     \colo_helpers_activate\maintextcolor
1141   \fi}
1142
1143\appendtoks
1144    \deactivatecolor % public?
1145    \inheritmaintextcolor
1146\to \everybeforeoutput
1147
1148\def\colo_helpers_switch_to_maintextcolor#1%
1149  {\let\maintextcolor\s!themaintextcolor
1150   \definecolor[\maintextcolor][#1]% can be fast one
1151   \colo_helpers_activate\maintextcolor
1152   \clf_registermaintextcolor\thecolorattribute\maintextcolor\relax}
1153
1154\permanent\protected\def\starttextcolor[#1]%
1155  {\ifempty{#1}\else
1156     \colo_helpers_switch_to_maintextcolor{#1}%
1157   \fi}
1158
1159\permanent\protected\lettonothing\stoptextcolor
1160
1161\lettonothing\p_colo_textcolor
1162
1163\def\colo_helpers_initialize_maintextcolor
1164  {\edef\p_colo_textcolor{\directcolorsparameter\c!textcolor}%
1165   \ifempty\p_colo_textcolor
1166     \colo_helpers_switch_to_maintextcolor\defaulttextcolor
1167   \else
1168     \colo_helpers_switch_to_maintextcolor\p_colo_textcolor
1169   \fi}
1170
1171\appendtoks \colo_helpers_initialize_maintextcolor \to \everyjob
1172\appendtoks \colo_helpers_initialize_maintextcolor \to \everysetupcolors
1173
1174\letcsname\??colorsetter       \endcsname\empty \letcsname\??colorattribute       \endcsname\!!zerocount
1175\letcsname\??transparencysetter\endcsname\empty \letcsname\??transparencyattribute\endcsname\!!zerocount
1176
1177\def\colo_helpers_inherited_direct_cs#1{\begincsname\??colorsetter          #1\endcsname}
1178\def\colo_helpers_inherited_direct_ca#1{\ifcsname   \??colorattribute       #1\endcsname\lastnamedcs\else\!!zerocount\fi}
1179\def\colo_helpers_inherited_direct_ts#1{\begincsname\??transparencysetter   #1\endcsname}
1180\def\colo_helpers_inherited_direct_ta#1{\ifcsname   \??transparencyattribute#1\endcsname\lastnamedcs\else\!!zerocount\fi}
1181
1182\let\colo_defcsname\defcsname
1183\let\colo_endcsname\endcsname
1184
1185\appendtoks
1186   \ifconditional\c_colo_expanded
1187     \let\colo_defcsname\edefcsname
1188   \else
1189     \let\colo_defcsname\defcsname
1190   \fi
1191\to \everysetupcolors
1192
1193\def\colo_palets_define_set#1#2#3%
1194  {\ifhastok={#3}% \definepalet[test][xx={y=.4}]
1195     \expandafter\colo_palets_define_assign
1196   \orelse\ifcsname\??colorsetter#3\endcsname
1197     % \definepalet[test][xx=green]
1198     \expandafter\colo_palets_define_inherit
1199   \else
1200     % not entered when making format
1201     \expandafter\colo_palets_define_undefine
1202   \fi
1203   {#1}{#2}{#3}}
1204
1205\def\colo_palets_define_inherit#1#2#3%
1206  {\colo_basics_inherit{#1:#2}{#3}%
1207   \colo_defcsname\??colorsetter          #1:#2\colo_endcsname{\colo_helpers_inherited_direct_cs{#3}}%
1208   \colo_defcsname\??colorattribute       #1:#2\colo_endcsname{\colo_helpers_inherited_direct_ca{#3}}%
1209   \colo_defcsname\??transparencysetter   #1:#2\colo_endcsname{\colo_helpers_inherited_direct_ts{#3}}%
1210   \colo_defcsname\??transparencyattribute#1:#2\colo_endcsname{\colo_helpers_inherited_direct_ta{#3}}}
1211
1212\def\colo_palets_define_undefine#1#2#3%
1213  {\letcsname\??colorsetter          #1:#2\endcsname\undefined
1214   \letcsname\??colorattribute       #1:#2\endcsname\undefined
1215   \letcsname\??transparencysetter   #1:#2\endcsname\undefined
1216   \letcsname\??transparencyattribute#1:#2\endcsname\undefined}
1217
1218\def\colo_palets_define_assign#1#2#3%
1219  {\definecolor[\??colorpalet#1:#2][#3]%
1220   \colo_defcsname\??colorsetter          #1:#2\colo_endcsname{\colo_helpers_inherited_direct_cs{\??colorpalet#1:#2}}%
1221   \colo_defcsname\??colorattribute       #1:#2\colo_endcsname{\colo_helpers_inherited_direct_ca{\??colorpalet#1:#2}}%
1222   \colo_defcsname\??transparencysetter   #1:#2\colo_endcsname{\colo_helpers_inherited_direct_ts{\??colorpalet#1:#2}}%
1223   \colo_defcsname\??transparencyattribute#1:#2\colo_endcsname{\colo_helpers_inherited_direct_ta{\??colorpalet#1:#2}}}
1224
1225\defcsname\??colorattribute        currentcolor\endcsname{\the\c_attr_color}        % for mpcolor
1226\defcsname\??transparencyattribute currentcolor\endcsname{\the\c_attr_transparency} % for mpcolor
1227
1228\def\colo_helpers_inherited_current_ca#1{\ifcsname\??colorattribute       \currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??colorattribute       #1\endcsname\lastnamedcs\else\!!zerocount\fi}
1229\def\colo_helpers_inherited_current_cs#1{\ifcsname\??colorsetter          \currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??colorsetter          #1\endcsname\lastnamedcs\fi}
1230\def\colo_helpers_inherited_current_ta#1{\ifcsname\??transparencyattribute\currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??transparencyattribute#1\endcsname\lastnamedcs\else\!!zerocount\fi}
1231\def\colo_helpers_inherited_current_ts#1{\ifcsname\??transparencysetter   \currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??transparencysetter   #1\endcsname\lastnamedcs\fi}
1232
1233%D Low level defs:
1234
1235\permanent\protected\def\colordefalc#1#2{\edefcsname\??colorattribute       #1\endcsname{#2}%
1236                                         \defcsname \??colorsetter          #1\endcsname{\c_attr_color       #2 }}
1237\permanent\protected\def\colordefagc#1#2{\xdefcsname\??colorattribute       #1\endcsname{#2}%
1238                                         \gdefcsname\??colorsetter          #1\endcsname{\c_attr_color       #2 }} % was not g
1239\permanent\protected\def\colordefalt#1#2{\edefcsname\??transparencyattribute#1\endcsname{#2}%
1240                                         \gdefcsname\??transparencysetter   #1\endcsname{\c_attr_transparency#2 }}
1241\permanent\protected\def\colordefagt#1#2{\xdefcsname\??transparencyattribute#1\endcsname{#2}%
1242                                         \gdefcsname\??transparencysetter   #1\endcsname{\c_attr_transparency#2 }}
1243
1244\permanent\protected\def\colordefflc#1#2{\defcsname \??colorattribute       #1\endcsname{\colo_helpers_inherited_current_ca{#2}}%
1245                                         \defcsname \??colorsetter          #1\endcsname{\colo_helpers_inherited_current_cs{#2}}}
1246\permanent\protected\def\colordeffgc#1#2{\gdefcsname\??colorattribute       #1\endcsname{\colo_helpers_inherited_current_ca{#2}}%
1247                                         \defcsname \??colorsetter          #1\endcsname{\colo_helpers_inherited_current_cs{#2}}}
1248\permanent\protected\def\colordefflt#1#2{\defcsname \??transparencyattribute#1\endcsname{\colo_helpers_inherited_current_ta{#2}}%
1249                                         \defcsname \??transparencysetter   #1\endcsname{\colo_helpers_inherited_current_ts{#2}}}
1250\permanent\protected\def\colordeffgt#1#2{\gdefcsname\??transparencyattribute#1\endcsname{\colo_helpers_inherited_current_ta{#2}}%
1251                                         \defcsname \??transparencysetter   #1\endcsname{\colo_helpers_inherited_current_ts{#2}}}
1252
1253\permanent\protected\def\colordefrlc  #1{\letcsname \??colorattribute       #1\endcsname\undefined
1254                                         \letcsname \??colorsetter          #1\endcsname\undefined}
1255\permanent\protected\def\colordefrgc  #1{\gletcsname\??colorattribute       #1\endcsname\undefined
1256                                         \gletcsname\??colorsetter          #1\endcsname\undefined}
1257\permanent\protected\def\colordefrlt  #1{\letcsname \??transparencyattribute#1\endcsname\undefined
1258                                         \letcsname \??transparencysetter   #1\endcsname\undefined}
1259\permanent\protected\def\colordefrgt  #1{\gletcsname\??transparencyattribute#1\endcsname\undefined
1260                                         \gletcsname\??transparencysetter   #1\endcsname\undefined}
1261
1262%D \macros
1263%D   {colorvalue, grayvalue}
1264%D
1265%D We can typeset the color components using \type {\colorvalue} and \type
1266%D {\grayvalue}. The commands:
1267%D
1268%D \startbuffer
1269%D color value of SomeKindOfRed: \colorvalue{SomeKindOfRed} \crlf
1270%D gray value of SomeKindOfRed: \grayvalue{SomeKindOfRed}
1271%D \stopbuffer
1272%D
1273%D \typebuffer
1274%D
1275%D show us:
1276%D
1277%D \startexample
1278%D \getbuffer
1279%D \stopexample
1280
1281\mutable\let\currentcolorname\s!black % todo
1282\mutable\let\outercolorname  \s!black % todo
1283
1284\mutable\let\colorformatseparator\space
1285
1286\permanent\def\MPcolor#1%
1287  {\clf_mpcolor
1288     \c_attr_colormodel
1289     \colo_helpers_inherited_current_ca{#1} %
1290     \colo_helpers_inherited_current_ta{#1} }
1291
1292\permanent\def\MPcoloronly#1%
1293  {\clf_mpcolor
1294     \c_attr_colormodel
1295     \colo_helpers_inherited_current_ca{#1} %
1296     \zerocount}
1297
1298\permanent\def\MPtransparency#1%
1299  {\clf_mpcolor
1300     \zerocount
1301     \zerocount
1302     \colo_helpers_inherited_current_ta{#1} }
1303
1304\permanent\def\MPoptions#1%
1305  {\clf_mpoptions
1306     \c_attr_colormodel
1307     \colo_helpers_inherited_current_ca{#1} %
1308     \colo_helpers_inherited_current_ta{#1} }
1309
1310\permanent\def\rawcolormodelattribute    {\c_attr_colormodel}
1311\permanent\def\thecolormodelattribute    {\the\c_attr_colormodel}
1312
1313\permanent\def\rawcolorattribute       #1{\ifcsname\??colorattribute       \currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??colorattribute       #1\endcsname\lastnamedcs\else\zerocount\fi}
1314\permanent\def\rawtransparencyattribute#1{\ifcsname\??transparencyattribute\currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??transparencyattribute#1\endcsname\lastnamedcs\else\zerocount\fi}
1315
1316\permanent\def\thecolorattribute       #1{\number\ifcsname\??colorattribute       \currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??colorattribute       #1\endcsname\lastnamedcs\else\zerocount\fi}
1317\permanent\def\thetransparencyattribute#1{\number\ifcsname\??transparencyattribute\currentcolorprefix#1\endcsname\lastnamedcs\orelse\ifcsname\??transparencyattribute#1\endcsname\lastnamedcs\else\zerocount\fi}
1318
1319\permanent\def\internalspotcolorname   #1{\clf_spotcolorname  \rawcolorattribute{#1}}
1320\permanent\def\internalspotcolorparent #1{\clf_spotcolorparent\rawcolorattribute{#1}}
1321\permanent\def\internalspotcolorsize   #1{\clf_spotcolorvalue \rawcolorattribute{#1}}
1322
1323\permanent\def\colorcomponents         #1{\clf_colorcomponents       \rawcolorattribute       {#1}}
1324\permanent\def\transparencycomponents  #1{\clf_transparencycomponents\rawtransparencyattribute{#1}}
1325\permanent\def\processcolorcomponents  #1{\clf_processcolorcomponents\rawcolorattribute       {#1}}
1326
1327\permanent\def\colorvalue              #1{\clf_formatcolor\rawcolorattribute{#1}{\colorformatseparator}}
1328\permanent\def\grayvalue               #1{\clf_formatgray \rawcolorattribute{#1}{\colorformatseparator}}
1329
1330\permanent\def\colorluminance          #1{\clf_formatluminance     \rawcolorattribute{#1} }
1331\permanent\def\colorluminanceratio   #1#2{\clf_formatluminanceratio\rawcolorattribute{#1} \rawcolorattribute{#2} }
1332
1333\permanent\def\doifelseblack           #1{\clf_doifelseblack\rawcolorattribute{#1}}
1334\permanent\def\doifelsedrawingblack      {\clf_doifelsedrawingblack}
1335
1336\aliased\let\doifblackelse        \doifelseblack
1337\aliased\let\doifdrawingblackelse \doifelsedrawingblack
1338
1339%D \macros
1340%D   {forcecolorhack}
1341%D
1342%D We can out this in front of (for instance) a special and so force color to be
1343%D applied (only glyphs, rules and leaders are handled).
1344%D
1345%D \startbuffer
1346%D \framed
1347%D   [background=color,backgroundcolor=yellow,framecolor=red,corner=round]
1348%D   {test}
1349%D \stopbuffer
1350%D
1351%D \typebuffer \getbuffer
1352
1353% ignores in attribute handler
1354%
1355% \def\forcecolorhack{\vrule\s!width\zeropoint\s!height\zeropoint\s!depth\zeropoint}
1356
1357% \normal added else fails in metafun manual (leaders do a hard scan)
1358
1359% \protected\def\forcecolorhack{\leaders\hrule\hskip\zeroskip\relax} % relax is needed !
1360%
1361% I really need to sort this out!
1362
1363% \permanent\protected\def\normalforcecolorhack{\leaders\hrule\s!height\zeropoint\s!depth\zeropoint\hskip\zeroskip\relax}
1364%
1365% \installtexexperiment
1366%   {disablecolorhack}
1367%   {\pushoverloadmode\enforced\permanent\protected\lettonothing\forcecolorhack\popoverloadmode}
1368%   {\pushoverloadmode\enforced\let\forcecolorhack\normalforcecolorhack\popoverloadmode}
1369%
1370% use \hpack container{...}
1371
1372\permanent\protected\lettonothing\forcecolorhack
1373
1374%D We default to the colors defined in \type {colo-imp-rgb} and support both \RGB\
1375%D and \CMYK\ output. Transparencies are defined here:
1376
1377\definecolor[black][s=0]
1378\definecolor[white][s=1]
1379
1380\definetransparency [none]        [0]
1381\definetransparency [normal]      [1]
1382\definetransparency [multiply]    [2]
1383\definetransparency [screen]      [3]
1384\definetransparency [overlay]     [4]
1385\definetransparency [softlight]   [5]
1386\definetransparency [hardlight]   [6]
1387\definetransparency [colordodge]  [7]
1388\definetransparency [colorburn]   [8]
1389\definetransparency [darken]      [9]
1390\definetransparency [lighten]    [10]
1391\definetransparency [difference] [11]
1392\definetransparency [exclusion]  [12]
1393\definetransparency [hue]        [13]
1394\definetransparency [saturation] [14]
1395\definetransparency [color]      [15]
1396\definetransparency [luminosity] [16]
1397
1398%D Some common ones:
1399
1400\definecolor[trace:0][s=.4]
1401\definecolor[trace:1][r=.6]
1402\definecolor[trace:2][g=.6]
1403\definecolor[trace:3][b=.6]
1404\definecolor[trace:4][r=.6,g=.6]
1405\definecolor[trace:5][r=.6,b=.6]
1406\definecolor[trace:6][g=.6,b=.6]
1407\definecolor[trace:7][r=.8,g=.4]
1408\definecolor[trace:8][r=.8,b=.4]
1409\definecolor[trace:9][g=.4,b=.8]
1410
1411\definecolor[trace:w][s=1]
1412\definecolor[trace:z][s=0,t=.5,a=1]
1413\definecolor[trace:r][r=.75,t=.5,a=1]
1414\definecolor[trace:g][g=.75,t=.5,a=1]
1415\definecolor[trace:b][b=.75,t=.5,a=1]
1416%definecolor[trace:c][c=.75,t=.5,a=1]
1417%definecolor[trace:m][m=.75,t=.5,a=1]
1418%definecolor[trace:y][y=.75,t=.5,a=1]
1419\definecolor[trace:c][g=.75,b=.75,t=.5,a=1]
1420\definecolor[trace:m][r=.75,b=.75,t=.5,a=1]
1421\definecolor[trace:y][r=.75,g=.75,t=.5,a=1]
1422\definecolor[trace:s][s=.75,t=.5,a=1]
1423\definecolor[trace:d][s=.25,t=.5,a=1]
1424\definecolor[trace:o][r=1,g=.6,b=.1,t=.5,a=1]
1425
1426\definecolor[trace:dw][s=1]
1427\definecolor[trace:dz][s=0,t=.75,a=1]
1428\definecolor[trace:dr][r=.75,t=.75,a=1]
1429\definecolor[trace:dg][g=.75,t=.75,a=1]
1430\definecolor[trace:db][b=.75,t=.75,a=1]
1431%definecolor[trace:dc][c=.75,t=.75,a=1]
1432%definecolor[trace:dm][m=.75,t=.75,a=1]
1433%definecolor[trace:dy][y=.75,t=.75,a=1]
1434\definecolor[trace:dc][g=.75,b=.75,t=.75,a=1]
1435\definecolor[trace:dm][r=.75,b=.75,t=.75,a=1]
1436\definecolor[trace:dy][r=.75,g=.75,t=.75,a=1]
1437\definecolor[trace:ds][s=.75,t=.75,a=1]
1438\definecolor[trace:dd][s=.25,t=.75,a=1]
1439\definecolor[trace:do][r=1,g=.6,b=.1,t=.75,a=1]
1440
1441\setupcolors
1442  [\c!state=\v!stop, % color will be enabled later on
1443   \c!conversion=\v!yes,
1444   \c!rgb=\v!yes,
1445   \c!cmyk=\v!yes,
1446   \c!spot=\v!yes,
1447   \c!pagecolormodel=\v!none,
1448   \c!expansion=\v!no,
1449   \c!textcolor=,
1450   \c!factor=\v!yes]
1451
1452\setupcolor
1453  [\v!rgb]
1454
1455\protect \endinput
1456