summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/colorspace/colorspace.sty
blob: 40e2948f3beede6ec4d5d7c87f6d1a0602fe68fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
%
% Copyright (C) 2015-2019 Javier Bezos http://www.texnia.com
%
% This file may be distributed and/or modified under the conditions of
% the MIT License. A version can be found at the end of this file.
%
% Repository: https://github.com/jbezos/colorspace
%

\ProvidesPackage{colorspace}[2019/03/25 v1.3 PDF color spaces]

\edef\spc@savedcatcodes{%
  \catcode`\noexpand\/=\the\catcode`\/
  \catcode`\noexpand\&=\the\catcode`\& % & is 'other' here!
  \catcode`\noexpand\<=\the\catcode`\<
  \catcode`\noexpand\>=\the\catcode`\>}
\catcode`\/=12
\catcode`\>=12
\catcode`\<=12
\catcode`\&=11 % we use it in macro names

% pdftex vs luatex compatibility

\ifx\pdfextension\@undefined
  \let\spc@Pageresources\pdfpageresources
  \let\spc@Obj\pdfobj
  \let\spc@Lastobj\pdflastobj
  \let\spc@Colorstack\pdfcolorstack
  \let\spc@Colorstackinit\pdfcolorstackinit
\else
  \edef\spc@Pageresources{\pdfvariable pageresources}
  \protected\def\spc@Obj{\pdfextension obj }
  \protected\def\spc@Lastobj{\numexpr\pdffeedback lastobj\relax}
  \protected\def\spc@Colorstack{\pdfextension colorstack}
  \def\spc@Colorstackinit{\pdffeedback colorstackinit}
\fi

\def\spc@unknown#1#2{%
  \@ifundefined{#1}%
    {\PackageError{colorspace}{Unknown #2}%
     {You haven't defined it or perhaps you misspelled it.}}%
    {}}

\@ifundefined{XC@sdef}{\RequirePackage{xcolor}}{}

\long\def\spc@afterfi#1\fi{\fi#1}
% Trim spaces in strings and normalize to single spaces
\def\spc@spaces#1 #2{%
  #1\ifx\@empty#2\else\spc@afterfi\spc@spaces@i{#1}#2\fi}
\def\spc@spaces@i#1{%
  \ifx\\#1\\\else\space\fi\spc@spaces}
\def\spc@dospaces#1{%
  \edef#1{#1\space}%
  \edef#1{\expandafter\spc@spaces#1\@empty}}

% Define a pseudo-model for spot colors. We need define the following
% 3 macros:

\XC@sdef\XC@mod@&spot{&spot}
\def\color@&spot#1#2{%
  \ifx\spc@ir\@undefined
    \expandafter\spc@getref\current@color\@@
  \fi
  \c@lor@arg{#2}%
  \edef#1{\spc@ir\space cs \spc@ir\space CS #2 sc #2 SC}}
\def\XC@clr@&spot@white{0}

% A somewhat dirty hack for converting cmyk to spot. The internal
% format is {}{/&Color cs /&Color CS n sc n SC}{spot}{n}, where n=1 is
% a tint of 100%. The original CMYK values are stored in
% \spc@ascmyk@<name>.

\def\spc@cmyktospot#1#2{%
  \begingroup
    \def\spc@tempc##1 ##2 ##3 ##4 k##5\@@{##1 ##2 ##3 ##4}%
    \def\xcolor@##1##2##3##4{%
      \expandafter\protected@xdef\csname\string\color@#1\endcsname{%
        \noexpand\xcolor@{##1}{/&#2 cs /&#2 CS 1 sc 1 SC}{&spot}{1}}%
      \xdef\spc@tempa{\spc@tempc##2\@@}%
      \expandafter\xdef\csname spc@ascmyk@#1\endcsname{##4}}%
    \csname\string\color@#1\endcsname
  \endgroup}

% The internal PDF name (which is like the public one with a &
% prepended) is available in the first argument of \xcolor@. The
% following macros extract it. 

\def\spc@getir#1{%
  \begingroup
    \def\xcolor@##1##2##3##4{\spc@getref##2\@@}%
    \@nameuse{#1}%
  \endgroup}
\def\spc@getref#1 #2\@@{\gdef\spc@ir{#1}}

% We need to redefine an xcolor macro for the tint operator ! to be
% recognized. It's done in a more modular way than xcolor:

\def\spc@infl@cmyk{\let\spc@inflcommas\@empty}% 4 values=4-4 commas
\def\spc@infl@gray{\def\spc@inflcommas{,,,}}% 1 value=4-1 commas
\def\spc@infl@&spot{%
  \spc@getir{\string\color@\@@nam}%
  \def\spc@inflcommas{,,,}}% 

\def\XC@inflate#1#2#3#4{%
  \def\spc@inflcommas{,}%      Assume 3 values=4-3 commas
  \@nameuse{spc@infl@\@@mod}%
  \edef#4{#2,\spc@inflcommas#3\spc@inflcommas}}

% The following macro stores all spot colors, with the LaTeX name. It
% also stores other color spaces. Internally is an \spc@elt list.

\let\spc@csall\@empty

% Now the main command for defining spot colors. \spc@ir@<latex-name>
% contains {/&Spot X 0 R}

\newcommand\definespotcolor[2]{\protected@edef\spc@tempa{{#1}{#2}}\spc@defsc}
\newcommand\spc@defsc[2][cmyk]{\expandafter\spc@defsc@i\spc@tempa{#1}{#2}}

\def\spc@splitbar#1#2#3/#4\@nil{%
  \def#1{#3}%
  \def#2{#4}}

\let\spc@lab@values\relax

\@namedef{spc@opt@alt=lab}#1{%
  \@expandtwoargs\in@{/}{#1}%
  \ifin@
    \spc@splitbar\spc@lab@values\spc@the@cmyk#1\@nil
  \else
    \def\spc@lab@values{#1}%
    \def\spc@the@cmyk{0,0,0,0}% dummy
  \fi
  \def\spc@tempd##1,{% comma -> space
    \ifx\@empty##1\else##1 \expandafter\spc@tempd\fi}%
  \edef\spc@lab@values{\expandafter\spc@tempd\spc@lab@values,\@empty,}}

\def\spc@defsc@i#1#2#3#4{%
  \def\spc@tempc##1 ##2{% Converts spaces to #20
    ##1%
    \ifx\@empty##2\else
      \ifx\\##1\\\string####20\else\string####20\fi
      \spc@afterfi\spc@tempc##2%
    \fi}%
  \edef\spc@tempb{\spc@tempc#2 \@empty}% Apply to convert
  \def\spc@elt##1##2{%
    \@expandtwoargs\in@{/&\spc@tempb\space}{\@nameuse{spc@ir@##2}}%
    \ifin@
      \PackageError{colorspace}{Duplicated spot name `#2'}%
        {This spot name was taken by `##2'.}%
    \fi}
  \spc@csall
  \@ifundefined{spc@opt@\zap@space#3 \@empty}%
    {\definecolor{#1}{cmyk:#3}{#4}}%
    {\@nameuse{spc@opt@\zap@space#3 \@empty}{#4}%
     \definecolor{#1}{cmyk}{\spc@the@cmyk}}%
  \expandafter\spc@cmyktospot{#1}\spc@tempb % Convert cmyk > spot  
  \ifx\spc@lab@values\relax
    \immediate\spc@Obj{[ /Separation /\spc@tempb\space
      /DeviceCMYK << /FunctionType 2 /Domain [0 1]
      /C0 [0 0 0 0] /C1 [\spc@tempa] /N 1 >> ]}% pdf separation
  \else
    \immediate\spc@Obj{[ /Separation /\spc@tempb\space
      \spc@illum@ref << /FunctionType 2 /Domain [0 1]
      /Range[0 100 -128 127 -128 127]
      /C0 [100 0 0] /C1 [\spc@lab@values] /N 1 >> ]}% pdf separation
    \global\let\spc@lab@values\relax
  \fi
  \expandafter\xdef\csname spc@ir@#1\endcsname{%
    /&\spc@tempb\space\the\spc@Lastobj\space 0 R}% save ref to obj
  \let\spc@elt\relax
  \xdef\spc@csall{\spc@csall\spc@elt2{#1}}}

% The pgf stuff *must* go before \resetpagecolorspace
\AtBeginDocument{%
  \ifx\spc@csall\@empty\else % ie, if spot/iccbased colors only
    \ifx\pgf@sys@pgf@resource@list@colorspaces\@undefined\else
      \def\spc@everycs{\pgf@sys@pgf@resource@list@colorspaces}%
      \edef\spc@tempa{% Remove pgf "static" ColorSpace 
        \noexpand\spc@remcs\the\spc@Pageresources
        \noexpand\spc@remcs{\pgf@sys@pdf@colrospaces@objnum}}%
      \spc@tempa
    \fi
    \resetpagecolorspace
  \fi}

% Options for illuminants:

\def\spc@illum@obj#1{%
  \immediate\spc@Obj{[ /Lab
    << /WhitePoint [#1] /Range [-128 127 -128 127] >> ]}%
  \xdef\spc@illum@ref{\the\spc@Lastobj\space 0 R }}

% 1931, 2-degree observer. c is deprecated

\DeclareOption{illuminant=a}{\spc@illum@obj{1.0985 1 0.3558}}
\DeclareOption{illuminant=c}{\spc@illum@obj{0.9807 1 1.1822}}
\DeclareOption{illuminant=e}{\spc@illum@obj{1 1 1}}
\DeclareOption{illuminant=d50}{\spc@illum@obj{0.9642 1 0.8251}} 
\DeclareOption{illuminant=d55}{\spc@illum@obj{0.9568 1 0.9214}}
\DeclareOption{illuminant=d65}{\spc@illum@obj{0.9504 1 1.0888}}
\DeclareOption{illuminant=d75}{\spc@illum@obj{0.9497 1 1.2261}}

% Page color spaces. The arrays mapping the spot colors to the obj
% refs are saved into the pdf as objects, and then we also save its
% reference for later recovery. Well, \pdfpageresources is not
% exactly "programmer"-friendy. We add the /ColorSpace, but if we
% switch from a previoss one, the latter must be removed, too. This
% is what \spc@remcs does. We also make sure a color space is
% defined once: first we rebuild the argument without spaces and
% then form the macro name using the order in csall, which is
% fixed. The macro name is built with the PDF names.

\let\spc@currcs\@empty
\let\spc@cs@\@empty

\let\spc@everycs\@empty

\newcommand\pagecolorspace[1]{%
  \protected@edef\spc@tempa{{#1}}%
  \expandafter\spc@pagecs\spc@tempa}

\def\spc@pagecs#1{%
  \ifx\spc@currcs\@empty\else
    \edef\spc@tempa{%
      \noexpand\spc@remcs\the\spc@Pageresources
      \noexpand\spc@remcs{\spc@currcs}}%
    \spc@tempa
  \fi
  \let\spc@tempd\@empty % list with known spaces
  \@for\spc@tempa:=#1\do{%
    \ifx\spc@tempa\@empty\else
      \spc@dospaces\spc@tempa
      \spc@unknown{spc@ir@\spc@tempa}{color (space) `\spc@tempa'}%
      \edef\spc@tempd{,\spc@tempd,\spc@tempa,}%
    \fi}%
  \let\spc@tempb\@empty % elements in /ColorSpace
  \let\spc@tempc\@empty % name for macro (\..&..&) with pdf ref
  \def\spc@elt##1##2{%
    \@expandtwoargs\in@{,##2,}{\spc@tempd}%
    \ifin@
      \protected@edef\spc@tempb{%
        \spc@tempb\@nameuse{spc@ir@##2}\space}%
      \protected@edef\spc@tempc{%
        \spc@tempc\@nameuse{spc@ir@##2}&}%
    \fi}%
  \spc@csall % Uses \spc@elt
  \@ifundefined{spc@cs@\spc@tempc}%
    {\immediate\spc@Obj{<< \spc@everycs\space\spc@tempb>>}%
     \expandafter\xdef\csname spc@cs@\spc@tempc\endcsname{\the\spc@Lastobj}}%
    {}%
  \expandafter\let\expandafter\spc@currcs\csname spc@cs@\spc@tempc\endcsname
  \ifx\spc@currcs\@empty\else
    \edef\spc@tempa{\spc@Pageresources{%
      \the\spc@Pageresources\space/ColorSpace \@nameuse{spc@cs@\spc@tempc} 0 R}}%
    \spc@tempa
  \fi}

\newcommand\resetpagecolorspace{%
  \def\spc@elt##1##2{\ifnum##1>1 ##2,\fi}%
  \edef\spc@tempa{{\spc@csall}}%
  \expandafter\pagecolorspace\spc@tempa}

\def\spc@remcs#1\spc@remcs#2{%
  \def\spc@tempc##1 /ColorSpace #2 ##2 R##3\spc@tempc{%
    \spc@Pageresources{##1##3}}%
  \spc@tempc#1\spc@tempc}

% Support for pgf (I hope--)

\def\pgfsys@color@&spot@stroke#1{%
  \spc@getir{\string\color@pgfstrokecolor}%
  \pgfsysprotocol@literal{\spc@ir\space CS}%
  \pgfsysprotocol@literal{#1 SC}}
\def\pgfsys@color@&spot@fill#1{%
  \spc@getir{\string\color@pgffillcolor}%
  \pgfsysprotocol@literal{\spc@ir\space cs}%
  \pgfsysprotocol@literal{#1 sc}}
\def\pgfsys@color@&spot#1{%
  \pgfsys@color@&spot@stroke{#1}%
  \pgfsys@color@&spot@fill{#1}}

% ==== Mixed colors ====

% xspace assumes color models have 1, 3 or 4 components. We need
% modify a couple of macros, but we'll do it only if a 2-component
% space is defined:

\def\spc@twocomps{%
  \let\spc@twocomps\relax
  \gdef\XC@vec##1,##2,##3,##4,##5,##6,##7,##8,##9{%
    \def\@@tmp{}%
    \XC@vec@{##1}{##5}%
    \ifx,##2,\else\XC@vec@{##2}{##6}%
    \ifx,##3,\else\XC@vec@{##3}{##7}%
    \ifx,##4,\else\XC@vec@{##4}{##8}%
    \fi\fi\fi
    \aftergroupdef##9\@@tmp}%
  \gdef\XC@calc@##1,##2,##3,##4,##5:##6{%
    \XC@let@Nc\@@cmd{XC@calc##6}%
    \@@cmd{##1}\@@tmp
    \ifx,##2,%
    \else\@@cmd{##2}\@tempa
    \ifx,##3,\edef\@@tmp{\@@tmp,\@tempa}%
    \else\@@cmd{##3}\@tempb
    \ifx,##4,\edef\@@tmp{\@@tmp,\@tempa,\@tempb}%
    \else\@@cmd{##4}\@tempc\edef\@@tmp{\@@tmp,\@tempa,\@tempb,\@tempc}%
    \fi\fi\fi}}

% The following macros define models with 1 to 4 components. It's
% written with the "copypaste" pattern, so it should be
% improved. Here, the limitations of xcolor become aparent, because
% 5 components would require "-1" commas.

\def\spc@mix@i#1#2{%
  \expandafter\XC@sdef\csname XC@mod@#1\endcsname{#1}%
  \@namedef{color@#1}##1##2{\@nameuse{c@lor@@#1}##2\@@##1}%
  \@namedef{c@lor@@#1}##1\@@##2{%
    \c@lor@arg{##1}%
    \edef##2{#2 cs #2 CS ##1 scn ##1 SCN}}%
  \@namedef{XC@clr@#1@white}{0}%
  \@namedef{spc@infl@#1}{\def\spc@inflcommas{,,,}}} % 1 value  = 4 - 1 commas

\def\spc@mix@ii#1#2{%
  \expandafter\XC@sdef\csname XC@mod@#1\endcsname{#1}%
  \@namedef{color@#1}##1##2{\@nameuse{c@lor@@#1}##2\@@##1}%
  \@namedef{c@lor@@#1}##1,##2\@@##3{%
    \c@lor@arg{##1}%
    \c@lor@arg{##2}%
    \edef##3{#2 cs #2 CS ##1 ##2 scn ##1 ##2 SCN}}%
  \@namedef{XC@clr@#1@white}{0,0}%
  \@namedef{spc@infl@#1}{\def\spc@inflcommas{,,}}} % 2 values  = 4 - 2 commas

\def\spc@mix@iii#1#2{%
  \expandafter\XC@sdef\csname XC@mod@#1\endcsname{#1}%
  \@namedef{color@#1}##1##2{\@nameuse{c@lor@@#1}##2\@@##1}%
  \@namedef{c@lor@@#1}##1,##2,##3\@@##4{%
    \c@lor@arg{##1}%
    \c@lor@arg{##2}%
    \c@lor@arg{##3}%
    \edef##4{#2 cs #2 CS ##1 ##2 ##3 scn ##1 ##2 ##3 SCN}}%
  \@namedef{XC@clr@#1@white}{0,0,0}%
  \@namedef{spc@infl@#1}{\def\spc@inflcommas{,}}} % 3 values  = 4 - 1 commas

\def\spc@mix@iv#1#2{%
  \expandafter\XC@sdef\csname XC@mod@#1\endcsname{#1}%
  \@namedef{color@#1}##1##2{\@nameuse{c@lor@@#1}##2\@@##1}%
  \@namedef{c@lor@@#1}##1,##2,##3,##4\@@##5{%
    \c@lor@arg{##1}%
    \c@lor@arg{##2}%
    \c@lor@arg{##3}%
    \c@lor@arg{##4}%
    \edef##5{#2 cs #2 CS ##1 ##2 ##3 ##4 scn ##1 ##2 ##3 ##4 SCN}}%
  \@namedef{XC@clr@#1@white}{0,0,0,0}%
  \@namedef{spc@infl@#1}{\let\spc@inflcommas\@empty}} % 4 value  = 4 -
                                % 0 commas

% A few auxliary macros:

\def\spc@nl{^^J}

\def\spc@getirbase/&#1 #2\@@{#1}
\def\spc@getirbaseC/&#1 #2\@@{#1 #2}

\def\spc@writecomp#1#2{%
  \advance\@tempcntb\@ne
  \edef#1{#1
    \the\numexpr\@tempcntb-\@tempcnta\relax\space
    index -#2\space mul 1.0 add mul\spc@nl}}

\def\spc@writecmykcomps#1,#2,#3,#4\@@{%
  \spc@writecomp\spc@@cyan{#1}%
  \spc@writecomp\spc@@magenta{#2}%
  \spc@writecomp\spc@@yellow{#3}%
  \spc@writecomp\spc@@black{#4}}

% And the main macro:

\newif\ifspc@dflt

\newcommand\definecolorspace{%
  \@ifstar
    {\spc@dflttrue\spc@defcs}%
    {\spc@dfltfalse\spc@defcs}}

\@onlypreamble\definecolorspace

\def\spc@defcs#1#2#3{%
  \@ifundefined{spc@defcs@#2}%
    {\spc@unknown{spc@defcs@#2}{color space type `#2'}}%
    {\protected@edef\spc@tempa{%
       \expandafter\noexpand\csname spc@defcs@#2\endcsname
       {#1}{#3}}%
     \spc@tempa}}

\def\spc@defcs@mixed#1#2{%
  \ifspc@dflt
     \PackageError{colorspace}{Unsupported}%
       {`mixed' only works with the unstarred command}%
  \fi
  \begingroup
    % the process part of the tint transform:
    \def\spc@@Pcyan{1.0\spc@nl}\def\spc@@Pmagenta{1.0\spc@nl}%
    \def\spc@@Pyellow{1.0\spc@nl}\def\spc@@Pblack{1.0\spc@nl}%
    % the spot part:
    \let\spc@@cyan\@empty\let\spc@@magenta\@empty
    \let\spc@@yellow\@empty\let\spc@@black\@empty
    % position on stack
    \edef\spc@@poscyan{0}\edef\spc@@posmagenta{1}%
    \edef\spc@@posyellow{2}\edef\spc@@posblack{3}%
    \@tempcntb\z@ % Number of colors
    \let\spc@pdfdoms\@empty % stores /Domain
    \let\spc@@pops\@empty
    \@for\spc@tempa:=#2\do{%
      \advance\@tempcntb\@ne % Count colors 
      \edef\spc@@pops{\spc@@pops\space pop}%  
      \edef\spc@pdfdoms{\spc@pdfdoms0 1 }}%
    \ifcase\@tempcntb\or1\or\spc@twocomps\or\or\else
      \PackageError{colorspace}{Too many inks}%
       {Currently, only up to 4 components are supported}%
    \fi 
    \@tempcnta\z@ % position on stack
    \let\spc@pdfdevn\@empty % stores names for /DeviceN
    \let\spc@pdfclrnts\@empty % stores /Colorants
    \let\spc@name\@empty % stores name for page /ColorSpace
    \def\spc@ir@cyan{Cyan}\def\spc@ir@magenta{Magenta}%
    \def\spc@ir@yellow{Yellow}\def\spc@ir@black{Black}%
    \@for\spc@tempa:=#2\do{%
      \spc@dospaces\spc@tempa
      \advance\@tempcnta\@ne
      \edef\spc@ir{\csname spc@ir@\spc@tempa\endcsname}%
      \@expandtwoargs\in@{,\spc@tempa,}{,cyan,magenta,yellow,black,}%
      \ifin@
        \edef\spc@pdfdevn{\spc@pdfdevn\space/\spc@ir}%
        \edef\spc@name{\spc@name &\spc@ir}%
        \expandafter
        \edef\csname spc@@P\spc@tempa\endcsname{%
          \the\numexpr\@tempcntb+\@nameuse{spc@@pos\spc@tempa}%
            -\@tempcnta\relax\space
          index neg 1.0 add\spc@nl}% ==========
      \else
        \@ifundefined{spc@ascmyk@\spc@tempa}%
          {\spc@unknown{spc@ascmyk@\spc@tempa}{spot color
             `\spc@tempa'}}%
          {}%
        \edef\spc@pdfdevn{\spc@pdfdevn\space
          /\expandafter\spc@getirbase\spc@ir\@@}%
        \edef\spc@pdfclrnts{\spc@pdfclrnts\space
          /\expandafter\spc@getirbaseC\spc@ir\@@}%
        \edef\spc@name{\spc@name
          &\expandafter\spc@getirbase\spc@ir\@@}%
        \expandafter\expandafter\expandafter\spc@writecmykcomps
          \csname spc@ascmyk@\spc@tempa\endcsname\@@
        \advance\@tempcntb-4\relax
      \fi}%
    \immediate\spc@Obj stream attr
      {/FunctionType 4
       /Domain [ \spc@pdfdoms]
       /Range [ 0 1 0 1 0 1 0 1 ]}%
      {\string{\space
       \spc@@Pcyan\spc@@cyan neg 1.0 add\spc@nl
       \spc@@Pmagenta\spc@@magenta neg 1.0 add\spc@nl
       \spc@@Pyellow\spc@@yellow neg 1.0 add\spc@nl
       \spc@@Pblack\spc@@black neg 1.0 add\spc@nl
       \the\numexpr4+\@tempcntb\relax\space 4 roll\spc@@pops
       \space\string}}%
    \immediate\spc@Obj{[ /DeviceN [\spc@pdfdevn\space] /DeviceCMYK
      \the\spc@Lastobj\space0 R
      << /Colorants <<\spc@pdfclrnts\space >> >> ]}%
    \expandafter\xdef\csname spc@ir@#1\endcsname{%
      /\spc@name\space\the\spc@Lastobj\space 0 R}% save ref to obj
    \let\spc@elt\relax
    \xdef\spc@csall{\spc@csall\spc@elt2{#1}}%
    \xdef\spc@tempa{%
      \expandafter\noexpand
      \csname spc@mix@\romannumeral\@tempcntb\endcsname
      {#1}{/\spc@name}}%
  \endgroup
  \spc@tempa}

% === ICC based default spaces ===

% luatex removed pdffiledump, so we use lua code.

\def\spc@defcs@iccbased#1#2{%
  \ifspc@dflt\else
     \PackageError{colorspace}{Unsupported}%
       {`iccbased' only works with the starred command}%
  \fi
  \begingroup
    % hex equivalents (which is the filedump format) and literal
    % (which is the lua format)
    \@namedef{spc@@47524159}{0}\@namedef{spc@@GRAY}{0}%
    \@namedef{spc@@52474220}{1}\@namedef{spc@@RGB }{1}%
    \@namedef{spc@@434D594B}{2}\@namedef{spc@@CMYK}{2}%
    \def\spc@@space{\ifcase\spc@tempa Gray\or RGB\or CMYK\fi}%
    \def\spc@@spacelc{\ifcase\spc@tempa gray\or rgb\or cmyk\fi}%
    % read the icc file and extract the space it applies to
    \ifx\pdffiledump\@undefined
      \directlua{
      local icc = io.open([[#2]], "r")
      if icc then
        icc:seek("set", 16)
        tex.print([[\noexpand\csname @namedef\endcsname{spc@tempa}{]]
                  .. icc:read(4) .. [[}]])
      end}%
    \else
      \edef\spc@tempa{\pdffiledump offset 16 length 4 {#2}}%
    \fi
    \@ifundefined{spc@@\spc@tempa}%
       {\PackageError{colorspace}{Unsupported ICC space}%
        {Currently, only Gray, RGB and CMYK are supported}}%
       {\edef\spc@tempa{\@nameuse{spc@@\spc@tempa}}}%
    \immediate\spc@Obj
      stream attr {\ifcase\spc@tempa
        /N 1 /Alternate /DeviceGray\or
        /N 3 /Alternate /DeviceRGB\or
        /N 4 /Alternate /DeviceCMYK\fi}% 
      file {#2}%
    \immediate\spc@Obj{[ /ICCBased \the\spc@Lastobj\space 0 R ]}% 
      % to the default color list, add an intermediate name *<model>
      % (only once) containing the last setting (add also the
      % actual name with flag 1 instead of 2)
    \let\spc@elt\relax
    \@ifundefined{spc@ir@*\spc@@spacelc}%
      {\xdef\spc@csall{\spc@csall\spc@elt2{*\spc@@spacelc}}}%
      {} % -- do nothing
    \expandafter\xdef\csname spc@ir@*\spc@@spacelc\endcsname{%
      /Default\spc@@space\space\the\spc@Lastobj\space 0 R}%
    \xdef\spc@csall{\spc@csall\spc@elt1{#1}}%
    \expandafter\xdef\csname spc@ir@#1\endcsname{%
      /Default\spc@@space\space\the\spc@Lastobj\space 0 R}% 
  \endgroup}

% ==== Overprint/Knokout ====

\DeclareOption{overprint}{\def\spc@op{1}}
\DeclareOption{opm=0}{\def\spc@op{0}\def\spc@opm{0}}
\DeclareOption{opm=1}{}
\DeclareOption{knockout}{\def\spc@op{2}} % After opm!
\def\spc@opm{1}
\ProcessOptions\relax

\ifx\spc@op\@undefined
  \spc@savedcatcodes
\endinput\fi % One line!

\def\spc@op@no{2}
\@namedef{spc@op@1}{1}
\@namedef{spc@op@0}{0}

\newcommand\overprintstate[1]{%
  \spc@unknown{spc@op@#1}{overprint state `#1'}%
  \ifnum\csname spc@op@#1\endcsname=\spc@op\else
    \expandafter\let\expandafter\spc@op\csname spc@op@#1\endcsname
    \spc@setop
  \fi}

\newcommand\textoverprint[2][\spc@opm]{{\overprintstate{#1}#2}}

\immediate\spc@Obj{<< /Type /ExtGState /op false /OP false >>}%
\edef\spc@tempa{/SPCko \the\spc@Lastobj\space 0 R }%
\immediate\spc@Obj{<< /Type /ExtGState /op true /OP true /OPM 0 >>}%
\edef\spc@tempa{\spc@tempa/SPCmz \the\spc@Lastobj\space 0 R }%
\immediate\spc@Obj{<< /Type /ExtGState /op true /OP true /OPM 1 >>}%
\edef\spc@tempa{\spc@tempa/SPCop \the\spc@Lastobj\space 0 R }%
\immediate\spc@Obj{<< \spc@tempa>>}%
\edef\spc@op@saveobj{\the\spc@Lastobj}

\AtBeginDocument{%
  \edef\spc@tempa{\global\spc@Pageresources{\the\spc@Pageresources
    \space /ExtGState \spc@op@saveobj\space 0 R }}%
  \spc@tempa}

\def\spc@setop{%
  \spc@Colorstack\spc@opstack push{\spc@getop}%
  \aftergroup\spc@resetop}
\def\spc@resetop{\spc@Colorstack\spc@opstack pop\relax}
\def\spc@getop{%
  \ifcase\spc@op
    /SPCmz gs
  \or
    /SPCop gs
  \or
    /SPCko gs
  \fi}

\chardef\spc@opstack\spc@Colorstackinit page direct{\spc@getop}

\spc@savedcatcodes

\endinput

MIT License
-----------

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.