| 1 |
%% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \colordef {Colors <2022-03-07>} % preloaded in format |
| 4 |
|
| 5 |
\_doc ----------------------------- |
| 6 |
The basic colors in CMYK |
| 7 |
\`\Blue` \`\Red` \`\Brown` \`\Green` \`\Yellow` \`\Cyan` \`\Magenta` |
| 8 |
\`\Grey` \`\LightGrey` \`\White` and \`\Black` |
| 9 |
are declared here. |
| 10 |
\_cod ----------------------------- |
| 11 |
|
| 12 |
\_def\Blue {\_setcmykcolor{1 1 0 0}} |
| 13 |
\_def\Red {\_setcmykcolor{0 1 1 0}} |
| 14 |
\_def\Brown {\_setcmykcolor{0 .67 .67 .5}} |
| 15 |
\_def\Green {\_setcmykcolor{1 0 1 0}} |
| 16 |
\_def\Yellow {\_setcmykcolor{0 0 1 0}} |
| 17 |
\_def\Cyan {\_setcmykcolor{1 0 0 0}} |
| 18 |
\_def\Magenta {\_setcmykcolor{0 1 0 0}} |
| 19 |
\_def\Grey {\_setcmykcolor{0 0 0 .5}} |
| 20 |
\_def\LightGrey {\_setcmykcolor{0 0 0 .2}} |
| 21 |
\_def\White {\_setgreycolor{1}} |
| 22 |
\_def\Black {\_setgreycolor{0}} |
| 23 |
|
| 24 |
\_doc ----------------------------- |
| 25 |
By default, the \`\setcmykcolor` \`\setrgbcolor` and \`\setgreycolor` |
| 26 |
macros with `{<componetns>}` parameter |
| 27 |
expand to \^`\_setcolor``{<color-data>}{<fill-op>}{<stroke-op>}` where |
| 28 |
<color-data> is <R> <G> <B> or <C> <M> <Y> <K> or <G> and |
| 29 |
<fill-op> is color operator for filling, <stroke-op> is color operator |
| 30 |
for stroking. |
| 31 |
\_cod ----------------------------- |
| 32 |
|
| 33 |
\_def\_setcmykcolor#1{\_setcolor{#1}kK} |
| 34 |
\_def\_setrgbcolor#1{\_setcolor{#1}{rg}{RG}} |
| 35 |
\_def\_setgreycolor#1{\_setcolor{#1}gG} |
| 36 |
\_public \setcmykcolor \setrgbcolor \setgreycolor ; |
| 37 |
|
| 38 |
\_doc ----------------------------- |
| 39 |
The \`\onlyrgb` declaration redefines \^`\setcmykcolor` to do conversion |
| 40 |
to RGB just before \^`\_setcolor` is used. |
| 41 |
The \`\onlycmyk` declaration redefines \^`\setrgbcolor` to do conversion |
| 42 |
to CMYK just before \^`\_setcolor` is used. |
| 43 |
Moreover, \^`\onlyrgb` re-defines three basic RGB |
| 44 |
colors for RGB color space and re-declares \^`\colordef` as \^`\rgbcolordef`. |
| 45 |
\_cod ----------------------------- |
| 46 |
|
| 47 |
\_def\_onlyrgb{\_def\Red{\_setrgbcolor{1 0 0}}% |
| 48 |
\_def\Green{\_setrgbcolor{0 1 0}}\_def\Blue{\_setrgbcolor{0 0 1}}% |
| 49 |
\_let\_colordef=\_rgbcolordef |
| 50 |
\_def\_setrgbcolor##1{\_setcolor{##1}{rg}{RG}}% |
| 51 |
\_def\_setcmykcolor##1{\_ea\_setcolor\_ea{\_expanded{\_cmyktorgb ##1 ;}}{rg}{RG}}% |
| 52 |
\_public \colordef \setrgbcolor \setcmykcolor ;} |
| 53 |
\_def\_onlycmyk{% |
| 54 |
\_let\_colordef=\_cmykcolordef |
| 55 |
\_def\_setrgbcolor##1{\_ea\_setcolor\_ea{\_expanded{\_rgbtocmyk ##1 ;}}kK}% |
| 56 |
\_def\_setcmykcolor##1{\_setcolor{##1}kK}% |
| 57 |
\_public \colordef \setrgbcolor \setcmykcolor ;} |
| 58 |
\_public \onlyrgb \onlycmyk ; |
| 59 |
|
| 60 |
\_doc ----------------------------- |
| 61 |
The \`\_colorattr` for coloring is allocated and |
| 62 |
\`\_setcolor``{<color-data>}{<fill-op>}{<stroke-op>}` is defined here. |
| 63 |
This macro does `\_colorattr=`\`\_colorcnt` if the <color data> was not used |
| 64 |
before and prepare mapping from this integer value to the <color data> |
| 65 |
and increments \^`\_colorcnt`. |
| 66 |
If the <color data> were used already, then \^`\_setcolor` |
| 67 |
does `\_colorattr=<stored-value>`. |
| 68 |
This work is done by the \`\_translatecolor` macro. The following mapping |
| 69 |
macros are created: |
| 70 |
\begtt \catcode`<=13 |
| 71 |
\_color::<data> <fill-op> ... expands to used <attribute-value> |
| 72 |
\_color:<attribute-value> ... expands to <data> <fill-op> |
| 73 |
\_color-s:<attribute-value> ... expands to <data> <stroke-op> |
| 74 |
\endtt |
| 75 |
\_cod ----------------------------- |
| 76 |
|
| 77 |
\_newattribute \_colorattr |
| 78 |
\_newcount \_colorcnt \_colorcnt=1 % allocations start at 1 |
| 79 |
\_protected\_def\_setcolor{\_colorprefix\_colorattr=\_translatecolor} |
| 80 |
\_def\_translatecolor#1#2#3{\_ifcsname _color::#1 #2\_endcsname\_lastnamedcs\_relax |
| 81 |
\_else |
| 82 |
\_colorcnt |
| 83 |
\_sxdef{_color::#1 #2}{\_the\_colorcnt}% |
| 84 |
\_sxdef{_color:\_the\_colorcnt}{#1 #2}% |
| 85 |
\_sxdef{_color-s:\_the\_colorcnt}{#1 #3}% |
| 86 |
\_incr \_colorcnt |
| 87 |
\_fi |
| 88 |
} |
| 89 |
% Black is the default color. |
| 90 |
\_sdef{_color::0 g}{0} |
| 91 |
\_sdef{_color:0}{0 g} |
| 92 |
\_sdef{_color-s:0}{0 G} |
| 93 |
|
| 94 |
\_doc ----------------------------- |
| 95 |
We support concept of non-local color, i.e. all changes of the color |
| 96 |
attribute are global by setting \`\_colorprefix` to `\global`. |
| 97 |
\`\localcolor` is the default, i.e.\ \^`\_colorprefix` is `\relax`.\nl |
| 98 |
You can write `\global\Red` if you want to have global setting of the |
| 99 |
color. |
| 100 |
\_cod ----------------------------- |
| 101 |
|
| 102 |
\_protected\_def \_localcolor {\_let\_colorprefix=\_relax} |
| 103 |
\_protected\_def \_nolocalcolor {\_let\_colorprefix=\_global} |
| 104 |
\_public \localcolor \nolocalcolor ; |
| 105 |
\_localcolor |
| 106 |
|
| 107 |
\_doc ----------------------------- |
| 108 |
The attribute \`\_transpattr` is allocated and set by the |
| 109 |
\`\transparency``<number>` macro. If such level of the transparency was |
| 110 |
never used in the document then |
| 111 |
\^`\addextgstate{tr<number>}`\code{\{<</ca X /CA X>>\}} is applied |
| 112 |
(where `X` is `(255-<number>)/255`). |
| 113 |
This information is used when shipout is processed (similarly as colors). |
| 114 |
It means `/tr<number> gs` is inserted when the attribute is changed.\nl |
| 115 |
\`\_resetattrs` resets the \^`\_colorattr` and \^`\_transpattr` |
| 116 |
to their initial value `-"7FFFFFFF`. |
| 117 |
\_cod ----------------------------- |
| 118 |
|
| 119 |
\_newattribute\_transpattr |
| 120 |
\_def\_transparency {\_afterassignment\_transparencyA \_transpattr} |
| 121 |
\_def\_transparencyA{% |
| 122 |
\_ifnum\_transpattr<1 \_transpattr=\_noattr \_else |
| 123 |
\_ifnum\_transpattr>255 \_opwarning{\_noexpand\transparency > 255 not allowed}% |
| 124 |
\_transpattr=\_noattr |
| 125 |
\_else |
| 126 |
\_ifcsname _transp:\_the\_transpattr\_endcsname \_else |
| 127 |
\_edef\_transpv{\_expr{(255-\_the\_transpattr)/255}}% |
| 128 |
\_addextgstate{tr\_the\_transpattr}{<</ca \_transpv\_space /CA \_transpv>>}% |
| 129 |
\_sxdef{_transp:\_the\_transpattr}{}% |
| 130 |
\_ifcsname _transp:0\_endcsname \_else |
| 131 |
\_addextgstate{tr0}{<</ca 1 /CA 1>>}% |
| 132 |
\_sxdef{_transp:0}{}% |
| 133 |
\_fi |
| 134 |
\_fi |
| 135 |
\_fi |
| 136 |
\_fi |
| 137 |
} |
| 138 |
\_def\_thetransparency{\_ifnum \_transpattr=-"7FFFFFFF 0\_else \_the\_transpattr \_fi} |
| 139 |
\_def\_resetattrs{\_colorattr=\_noattr \_transpattr=\_noattr} |
| 140 |
|
| 141 |
\_public \transparency \thetransparency ; |
| 142 |
|
| 143 |
\_doc ----------------------------- |
| 144 |
We use Lua codes for RGB to CMYK or CMYK to RGB conversions and for |
| 145 |
addition color components in the \^`\colordef` macro. |
| 146 |
The \`\_rgbtocmyk` `<R> <G> <B> ;` expands to `<C> <M> <Y> <K>` and |
| 147 |
the \`\_cmyktorgb` `<C> <M> <Y> <K> ;` expands to `<R> <G> <B>`. |
| 148 |
The \`\_colorcrop`, \`\_colordefFin` and \`\_douseK` are auxiliary macros |
| 149 |
used in the \^`\colordef`. The `\_colorcrop` rescales color components in order |
| 150 |
to they are in $[0,1]$ interval. The `\colordefFin` expands to the values |
| 151 |
accumulated in Lua code `color_C`, `color_M`, `color_Y` and `color_K`. |
| 152 |
The `\_douseK` applies \^`\useK` to CMYK components. |
| 153 |
\nl |
| 154 |
The `\_tocmyk:<rgb>` or `\_torgb:<cmyk>` control sequences (given by |
| 155 |
\^`\rgbcmykmap`) have precedence. |
| 156 |
\_cod ----------------------------- |
| 157 |
|
| 158 |
\_def\_rgbtocmyk #1 #2 #3 ;{\_trycs{_tocmyk:#1 #2 #3}{% |
| 159 |
\_ea \_stripzeros \_detokenize \_ea{\_directlua{ |
| 160 |
local kr = math.max(#1,#2,#3) |
| 161 |
if (kr==0) then |
| 162 |
tex.print('0. 0. 0. 1 ;') |
| 163 |
else |
| 164 |
tex.print(string.format('\_pcent.3f \_pcent.3f \_pcent.3f \_pcent.3f ;', |
| 165 |
(kr-#1)/kr, (kr-#2)/kr, (kr-#3)/kr, 1-kr)) |
| 166 |
end |
| 167 |
}}}} |
| 168 |
\_def\_cmyktorgb #1 #2 #3 #4 ;{\_trycs{_torgb:#1 #2 #3 #4}{% |
| 169 |
\_ea \_stripzeros \_detokenize \_ea{\_directlua{ |
| 170 |
local kr = 1-#4 |
| 171 |
tex.print(string.format('\_pcent.3f \_pcent.3f \_pcent.3f ;', |
| 172 |
(1-#1)*kr, (1-#2)*kr, (1-#3)*kr)) |
| 173 |
}}}} |
| 174 |
\_def\_colorcrop{\_directlua{ |
| 175 |
local m=math.max(color_C, color_M, color_Y, color_K) |
| 176 |
if (m>1) then |
| 177 |
color_C=color_C/m color_M=color_M/m color_Y=color_Y/m color_K=color_K/m |
| 178 |
end |
| 179 |
}} |
| 180 |
\_def\_colordefFin{\_colorcrop \_ea \_stripzeros \_detokenize \_ea{\_directlua{ |
| 181 |
tex.print(string.format('\_pcent.3f \_pcent.3f \_pcent.3f \_pcent.3f ;', |
| 182 |
color_C, color_M, color_Y, color_K)) |
| 183 |
}}} |
| 184 |
\_def\_douseK{\_colorcrop \_directlua{ |
| 185 |
kr=math.min(color_C, color_M, color_Y) |
| 186 |
if (kr>=1) then |
| 187 |
color_C=0 color_M=0 color_Y=0 color_K=1 |
| 188 |
else |
| 189 |
color_C=(color_C-kr)/(1-kr) color_M=(color_M-kr)/(1-kr) |
| 190 |
color_Y=(color_Y-kr)/(1-kr) color_K=math.min(color_K+kr,1) |
| 191 |
end |
| 192 |
}} |
| 193 |
|
| 194 |
\_doc ----------------------------- |
| 195 |
We have a problem with the `%.3f` directive in Lua code. It prints trailed |
| 196 |
zeros: (0.300 instead desired 0.3) but we want to save PDF file space. The macro |
| 197 |
\`\_stripzeros` removes these trailing zeros at the expand processor |
| 198 |
level. So `\_stripzeros 0.300 0.400 0.560 ;` expands to `.3 .4 .56`. |
| 199 |
\_cod ----------------------------- |
| 200 |
|
| 201 |
\_def\_stripzeros #1.#2 #3{\_ifx0#1\_else#1\_fi.\_stripzeroA #2 0 :% |
| 202 |
\_ifx;#3\_else \_space \_ea\_stripzeros\_ea#3\_fi} |
| 203 |
\_def\_stripzeroA #10 #2:{\_ifx^#2^\_stripzeroC#1:\_else \_stripzeroB#1 0 :\_fi} |
| 204 |
\_def\_stripzeroB #10 #2:{\_ifx^#2^\_stripzeroC#1:\_else #1\_fi} |
| 205 |
\_def\_stripzeroC #1 #2:{#1} |
| 206 |
|
| 207 |
\_doc ----------------------------- |
| 208 |
\`\rgbcmykmap` `{<R> <G> <B>}`\,`{<C> <M> <Y> <K>}` declares mapping from |
| 209 |
RGB to CMYK and from CMYK to RGB for given color. It has precedence |
| 210 |
before general formulae used in the \^`\_rgbtocmyk` and \^`\_cmyktorgb` |
| 211 |
macros. Note, that the values <R> <G> <B> <C> <M> <Y> <K> must be given |
| 212 |
exactly in the same format as in \^`\setcmykcolor` and \^`\setrgbcolor` |
| 213 |
parameters. For example, `0.5` or `.5` or `.50` are different values from |
| 214 |
point of view of this mapping. |
| 215 |
\_cod ----------------------------- |
| 216 |
|
| 217 |
\_def\_rgbcmykmap#1#2{\_sxdef{_torgb:#2}{#1}\_sxdef{_tocmyk:#1}{#2}} |
| 218 |
\_public \rgbcmykmap ; |
| 219 |
|
| 220 |
\_doc ----------------------------- |
| 221 |
The \`\rgbcolordef` and \`\cmykcolordef` use common macro |
| 222 |
\`\_commoncolordef` with different first four parameters. |
| 223 |
The `\_commoncolordef <selector><K><R><G><what-define>{<data>}` does the |
| 224 |
real work. It initializes the Lua variables for summation. |
| 225 |
It expands <data> in the group where color selectors have |
| 226 |
special meaning, then it adjusts the resulting string by \^`\replstring` |
| 227 |
and runs it. Example shows how the <data> are processed: |
| 228 |
\begtt \catcode`\<=13 |
| 229 |
input <data>: ".3\Blue + .6^\KhakiC \useK -\Black" |
| 230 |
expanded to: ".3 !=K 1 1 0 0 +.6^!=R .804 .776 .45 \_useK -!=G 0" |
| 231 |
adjusted to: "\_addcolor .3!=K 1 1 0 0 \_addcolor .6!^R .804 .776 .45 |
| 232 |
\_useK \_addcolor -1!=G 0" |
| 233 |
and this is processed. |
| 234 |
\endtt |
| 235 |
\`\_addcolor` `<coef.>!<mod><type>` expands to `\_addcolor:<mod><type> <coef>` |
| 236 |
for example it expands to `\_addcolor:=K <coef>` followed by one or three or four |
| 237 |
numbers (depending on <type>). <mod> is `=` (use as is) or `^` (use |
| 238 |
complementary color). <type> is `K` for CMYK, `R` for RGB and `G` for |
| 239 |
GREY color space. Uppercase <type> informs that `\cmykcolordef` is |
| 240 |
processed and lowercase <type> informs that `\rgbcolordef` is processed. |
| 241 |
All variants of commands `\_addcolor:<mod><type>` are defined. All of |
| 242 |
them expand to `\_addcolorA <v1> <v2> <v3> <v4>` which adds the values of Lua |
| 243 |
variables. The `\rgbcolordef` uses `\_addcolorA <R> <G> <B> 0` and |
| 244 |
`\cmkykcolordef` uses `\_addcolorA <C> <M> <Y> <K>`. So the Lua variable |
| 245 |
names are a little confusing when `\rgbcolordef` is processed. |
| 246 |
|
| 247 |
Next, `\_commoncolordef` saves resulting values from Lua to `\_tmpb` |
| 248 |
using `\_colordefFin`. If `\rgbcolordef` is processed, then we must to |
| 249 |
remove the last <K> component which is in the format `.0` in such case. |
| 250 |
The `\_stripK` macro does it. |
| 251 |
Finally, the `<what-define>` is defined as `<selector>{<expanded _tmpb>}`, |
| 252 |
for example `\_setcmykclor{1 0 .5 .3}`. |
| 253 |
\_cod ----------------------------- |
| 254 |
|
| 255 |
\_def\_rgbcolordef {\_commoncolordef \_setrgbcolor krg} |
| 256 |
\_def\_cmykcolordef {\_commoncolordef \_setcmykcolor KRG} |
| 257 |
\_def\_commoncolordef#1#2#3#4#5#6{% |
| 258 |
\_begingroup |
| 259 |
\_directlua{color_C=0 color_M=0 color_Y=0 color_K=0}% |
| 260 |
\_def\_setcmykcolor##1{!=#2 ##1 }% |
| 261 |
\_def\_setrgbcolor ##1{!=#3 ##1 }% |
| 262 |
\_def\_setgreycolor##1{!=#4 ##1 }% |
| 263 |
\_let\_useK=\_relax |
| 264 |
\_edef\_tmpb{+#6}% |
| 265 |
\_replstring\_tmpb{+ }{+}\_replstring\_tmpb{- }{-}% |
| 266 |
\_replstring\_tmpb{+}{\_addcolor}\_replstring\_tmpb{-}{\_addcolor-}% |
| 267 |
\_replstring\_tmpb{^!=}{!^}\_replstring\_tmpb{-!}{-1!}% |
| 268 |
\_ifx K#2\_let\_useK=\_douseK \_fi |
| 269 |
\_tmpb |
| 270 |
\_edef\_tmpb{\_colordefFin}% |
| 271 |
\_ifx k#2\_edef\_tmpb{\_ea\_stripK \_tmpb;}\_fi |
| 272 |
\_ea\_endgroup |
| 273 |
\_ea\_def\_ea#5\_ea{\_ea#1\_ea{\_tmpb}}% |
| 274 |
} |
| 275 |
\_def\_addcolor#1!#2#3{\_cs{addcolor:#2#3}#1} |
| 276 |
\_def\_addcolorA #1 #2 #3 #4 #5 {% |
| 277 |
\_def\_tmpa{#1}\_ifx\_tmpa\_empty \_else \_edef\_tmpa{\_tmpa*}\_fi |
| 278 |
\_directlua{color_C=math.max(color_C+\_tmpa#2,0) |
| 279 |
color_M=math.max(color_M+\_tmpa#3,0) |
| 280 |
color_Y=math.max(color_Y+\_tmpa#4,0) |
| 281 |
color_K=math.max(color_K+\_tmpa#5,0) |
| 282 |
}} |
| 283 |
\_sdef{addcolor:=K}#1 #2 #3 #4 #5 {\_addcolorA #1 #2 #3 #4 #5 } |
| 284 |
\_sdef{addcolor:^K}#1 #2 #3 #4 #5 {\_addcolorA #1 (1-#2) (1-#3) (1-#4) #5 } |
| 285 |
\_sdef{addcolor:^G}#1 #2 {\_addcolorA #1 0 0 0 #2 } |
| 286 |
\_sdef{addcolor:=G}#1 #2 {\_addcolorA #1 0 0 0 (1-#2) } |
| 287 |
\_sdef{addcolor:=R}#1 #2 #3 #4 {% |
| 288 |
\_edef\_tmpa{\_noexpand\_addcolorA #1 \_rgbtocmyk #2 #3 #4 ; }\_tmpa |
| 289 |
} |
| 290 |
\_sdef{addcolor:^R}#1 #2 #3 #4 {\_cs{addcolor:=R}#1 (1-#2) (1-#3) (1-#4) } |
| 291 |
|
| 292 |
\_sdef{addcolor:=k}#1 #2 #3 #4 #5 {% |
| 293 |
\_edef\_tmpa{\_noexpand\_addcolorA #1 \_cmyktorgb #2 #3 #4 #5 ; 0 }\_tmpa |
| 294 |
} |
| 295 |
\_sdef{addcolor:^k}#1 #2 #3 #4 #5 {\_cs{addcolor:=k}#1 (1-#2) (1-#3) (1-#4) #5 } |
| 296 |
\_sdef{addcolor:^g}#1 #2 {\_addcolorA #1 (1-#2) (1-#2) (1-#2) 0 } |
| 297 |
\_sdef{addcolor:=g}#1 #2 {\_addcolorA #1 #2 #2 #2 0 } |
| 298 |
\_sdef{addcolor:=r}#1 #2 #3 #4 {\_addcolorA #1 #2 #3 #4 0 } |
| 299 |
\_sdef{addcolor:^r}#1 #2 #3 #4 {\_addcolorA #1 (1-#2) (1-#3) (1-#4) 0 } |
| 300 |
\_def\_stripK#1 .0;{#1} |
| 301 |
\_let\_colordef=\_cmykcolordef % default \_colordef is \_cmykcolordef |
| 302 |
|
| 303 |
\_doc ----------------------------- |
| 304 |
Public versions of \`\colordef` and \`\useK` macros are declared using |
| 305 |
`\_def`, because the internal versions `\_colordef` and `\_useK` are |
| 306 |
changed during processing. |
| 307 |
\_cod ----------------------------- |
| 308 |
|
| 309 |
\_def \useK{\_useK} |
| 310 |
\_def \colordef {\_colordef} |
| 311 |
\_public \cmykcolordef \rgbcolordef ; |
| 312 |
|
| 313 |
\_doc ----------------------------- |
| 314 |
The \LaTeX/ file `x11nam.def` is read by \`\morecolors`. The numbers |
| 315 |
0,1,2,3,4 are transformed to letters O, <none>, B, C, D in the name of the |
| 316 |
color. Colors defined already are not re-defined. The empty \`\_showcolor` |
| 317 |
macro should be re-defined for color catalog printing. For example: |
| 318 |
\begtt |
| 319 |
\def\vr{\vrule height10pt depth2pt width20pt} |
| 320 |
\def\_showcolor{\hbox{\tt\_bslash\_tmpb: \csname\_tmpb\endcsname \vr}\space\space} |
| 321 |
\begmulti 4 \typosize[10/14] |
| 322 |
\morecolors |
| 323 |
\endmulti |
| 324 |
\endtt |
| 325 |
\_cod ----------------------------- |
| 326 |
|
| 327 |
\_def\_morecolors{% |
| 328 |
\_long\_def\_tmp##1\preparecolorset##2##3##4##5{\_tmpa ##5;,,,;} |
| 329 |
\_def\_tmpa##1,##2,##3,##4;{\_ifx,##1,\_else |
| 330 |
\_def\_tmpb{##1}\_replstring\_tmpb{1}{}\_replstring\_tmpb{2}{B}% |
| 331 |
\_replstring\_tmpb{3}{C}\_replstring\_tmpb{4}{D}\_replstring\_tmpb{0}{O}% |
| 332 |
\_ifcsname \_tmpb\_endcsname \_else |
| 333 |
\_sdef{\_tmpb}{\_setrgbcolor{##2 ##3 ##4}}\_showcolor\_fi |
| 334 |
\_ea\_tmpa\_fi |
| 335 |
} |
| 336 |
\_ea\_tmp\_input x11nam.def |
| 337 |
} |
| 338 |
\_let\_showcolor=\_relax % re-define it if you want to print a color catalog |
| 339 |
\_public \morecolors ; |
| 340 |
|
| 341 |
\_endcode % ------------------------------------- |
| 342 |
|
| 343 |
\secc Basic concept |
| 344 |
|
| 345 |
Setting of color in PDF is handled by graphics operators which change the |
| 346 |
graphics context. Colors for fills/strokes are distinguished, but apart from |
| 347 |
that, only one color is active at time and is used for all material drawn by |
| 348 |
following graphics operators, until next color is set. Each PDF content (e.g. |
| 349 |
page or form XObject) has its own graphics context, that is initialized from |
| 350 |
zero. Hence we have different concept of selecting fonts in \TeX/ (it |
| 351 |
depends on \TeX/ groups but does not depends on pages) and color handling in |
| 352 |
PDF. |
| 353 |
|
| 354 |
\TeX/ itself has no concept of colors. Colors have always been handled by |
| 355 |
inserting whatsits (either using `\special` for DVI or using |
| 356 |
`\pdfliteral`/`\pdfcolorstack` for PDF). It is very efficient and \TeX/ doesn't |
| 357 |
even have to know anything about colors, but it is also problematic in many |
| 358 |
ways. |
| 359 |
|
| 360 |
That is the reason why we decided to change color handling from |
| 361 |
`\pdfcolorstack` to \LuaTeX/ attributes in version 1.04 of \OpTeX. Using |
| 362 |
attributes, the color setting behaves exactly like font selection from \TeX/ |
| 363 |
point of view: it respects \TeX/ groups, colors can span more pages, |
| 364 |
independent colors can be set for `\insert`s, etc. Moreover, once a material is |
| 365 |
created (using `\setbox` for example) then it has its fonts and its colors |
| 366 |
frozen and you can rely on it when you are using e.g. `\unhbox`. There are no |
| 367 |
internal whatsits for colors which can interfere with other typesetting |
| 368 |
material. In the end something like setting text to red (`{\Red text}`) should |
| 369 |
have the same nice behavior like setting text to bold (`{\bf text}`). |
| 370 |
|
| 371 |
\LuaTeX/ attributes can be set like count register -- one attribute holds one |
| 372 |
number at a time. But the value of attribute is propagated to each created |
| 373 |
typesetting element until the attribute is unset or set to another value. Very |
| 374 |
much like the font property. We use one attribute \^`\_colorattr` for storing |
| 375 |
the currently selected color (in number form). |
| 376 |
|
| 377 |
Macros \^`\setcmykcolor``{<C> <M> <Y> <K>}` or \^`\setrgbcolor``{<R> <G> <B>}` |
| 378 |
or\nl \^`\setgreycolor``{<Grey>}` are used in color selectors. These |
| 379 |
macros expand to internal \^`\_setcolor` macro which sets the \^`\_colorattr` |
| 380 |
attribute to an integer value and prepares mapping between this value and the real |
| 381 |
color data. This mapping is used just before each `\shipout` in output routine. |
| 382 |
The \^`\_preshipout` pseudo-primitive is used here, it converts attribute |
| 383 |
values to internal PDF commands for selecting colors. |
| 384 |
|
| 385 |
The concept with color attributes has one limitation: the colors cannot be changed |
| 386 |
inside a ligature unless the ligature is broken manually. It means that |
| 387 |
`{\Red f}i` doesn't lead to the expected result but `{\Red f\null}i` does. |
| 388 |
|
| 389 |
\secc Color mixing |
| 390 |
|
| 391 |
The color mixing processed by the \^`\colordef` is done in the subtractive color |
| 392 |
model CMYK. If the result has a component greater than 1 then all |
| 393 |
components are multiplied by a coefficient in order to the maximal component is |
| 394 |
equal to 1. |
| 395 |
|
| 396 |
You can move a shared amount of CMY components (i.e. their minimum) to the |
| 397 |
$K$ component. This saves the color tonners and the result is more true. |
| 398 |
This should be done by \^`\useK` command at the end of a linear combination |
| 399 |
used in `\colordef`. For example |
| 400 |
\begtt |
| 401 |
\colordef \myColor {.3\Green + .4\Blue \useK} |
| 402 |
\endtt |
| 403 |
The `\useK` command exactly does: |
| 404 |
$$ |
| 405 |
\displaylines{ |
| 406 |
k' =\min(C,M,Y), \cr \ C=(C-k')/(1-k'), \ M=(M-k')/(1-k'), \ Y=(Y-k')/(1-k'), \cr |
| 407 |
K = \min(1,K+k'). |
| 408 |
} |
| 409 |
$$ |
| 410 |
|
| 411 |
You can use minus instead of plus in the linear combination in `\colordef`. The |
| 412 |
given color is subtracted in such case and the negative components are |
| 413 |
rounded to zero immediately. For example |
| 414 |
\begtt |
| 415 |
\colordef \Color {\Brown-\Black} |
| 416 |
\endtt |
| 417 |
can be used for removing the black component from the color. You can use |
| 418 |
the `-\Black` trick after `\useK` command to remove grey components |
| 419 |
occurred during color mixing. |
| 420 |
|
| 421 |
Finally, you can use `^` immediately preceded before the macro name of the |
| 422 |
color. Then the complementary color is used here. |
| 423 |
\begtt |
| 424 |
\colordef\mycolor{\Grey+.6^\Blue} % the same as \colordef\mycolor{\Grey+.6\Yellow} |
| 425 |
\endtt |
| 426 |
|
| 427 |
The \^`\rgbcolordef` can be used to mix colors in additive color model RGB. |
| 428 |
If \^`\onlyrgb` is declared, then \^`\colordef` works as \^`\rgbcolordef`. |
| 429 |
|
| 430 |
If a CMYK to RGB or RGB to CMYK conversion is needed then |
| 431 |
direct conversion of given color is used (if declared using |
| 432 |
\^`\rgbcmykmap``{<rgb>}{<cmyk>}`) or the following simple formulae are used |
| 433 |
(ICC profiles are not supported): |
| 434 |
$$ |
| 435 |
\displaylines{ |
| 436 |
\hbox{CMYK to RGB:}\cr |
| 437 |
R = (1-C)(1-K), \ G = (1-M)(1-K), \ B = (1-Y)(1-K). \cr |
| 438 |
\hbox{RGB to CMYK:}\cr |
| 439 |
K'=\max(R,G,B), \ C=(K'-R)/K', \ M=(K'-G)/K', \ Y=(K'-B)/K', \ K=1-K'. |
| 440 |
} |
| 441 |
$$ |
| 442 |
The RGB to CMYK conversion is invoked when a color is declared using `\setrgbcolor` |
| 443 |
and it is used in \^`\colordef` or if it is printed when \^`\onlycmyk` is declared. |
| 444 |
The CMYK to RGB conversion is invoked when a color is declared using \^`\setcmykcolor` |
| 445 |
and it is used in \^`\rgbcolordef` or if it is printed when \^`\onlyrgb` is declared. |
| 446 |
|
| 447 |
\secc Implementation |
| 448 |
|
| 449 |
\_endinput |
| 450 |
|
| 451 |
2022-03-07 \_resetattrs instead \_resetcolor |
| 452 |
2022-03-05 \transparency added |
| 453 |
2021-07-16 colors reimplemented, now they are based on attributes |
| 454 |
2021-05-28 \rgbcmykmap introduced |
| 455 |
2020-04-22 \replstring\tmpb{+ }{+}, {- }{-} added in \colordef, bug fixed |
| 456 |
2020-03-18 introduced |