[Tex/LaTex] CMYK ConTeXt colors in TikZ

colorcontexttikz-pgf

Colors defined via the internal \definecolor macro in ConTeXt cannot be used in TikZ when the CMYK system is used.

Is there a way to convert them or tick a setting so that TikZ can use them?

Minimal example:

\usemodule[tikz]
\definecolor[c][c=1]
\starttext
\tikz\node[c]{t};
\stoptext

Expected: cyan “t”
Received: black “t” and Package pgf: Error! Color c has an unsupported color model..


Edit: TikZ does use ConTeXt colors defined with the RGB color model:

\definecolor[r][r=1]
\tikz\node[r]{t};

This gives me a red “t” as expected. So I don’t want to know how to define colors explicitly for use in TikZ, but what code I have to add somewhere add to make the two lines\definecolor[c][c=1] and \tikz\node[c]{t}; from the minimal example work together.

Best Answer

It seems that pgf does not have any built-in support for cmyk when using ConTeXt. As a result, the macro \pgfutil@registercolor, which chooses colour models, is hard-coded not to try anything other than rgb and gray. Thus we need alter that macro and provide the necessary internal conversion:

\usemodule[tikz]
\definecolor[c][c=1]
\starttext
\unprotect
\def\pgfutil@registercolor#1{%
  \edef\pgf@temp{\PDFcolor{#1}}%
  \edef\pgf@marshal{\noexpand\pgfutil@in@{g}{\pgf@temp}}%
  \pgf@marshal
  \ifpgfutil@in@
    \expandafter\pgfutil@context@parse@gray\pgf@temp\pgf@stop{#1}%
  \else
    \edef\pgf@marshal{\noexpand\pgfutil@in@{rg}{\pgf@temp}}%
    \pgf@marshal
    \ifpgfutil@in@
      \expandafter\pgfutil@context@parse@rgb\pgf@temp\pgf@stop{#1}%
    \else
      \edef\pgf@marshal{\noexpand\pgfutil@in@{k}{\pgf@temp}}%
      \pgf@marshal
      \ifpgfutil@in@
        \expandafter\pgfutil@context@parse@cmyk\pgf@temp\pgf@stop{#1}%
      \else 
        \PackageError{pgf}{Color #1 has an unsupported color model.}{}%
        \pgfutil@definecolor{#1}{gray}{0}%
      \fi
    \fi
  \fi
}
\def\pgfutil@context@parse@cmyk#1 #2 #3 #4k#5\pgf@stop #6{%
  \pgfutil@definecolor{#6}{cmyk}{#1,#2,#3,#4}%
}
\def\pgfutil@emu@cmyk#1#2,#3,#4,#5\@nil{%
  \expandafter\def\csname\string\color@#1\endcsname
    {\xcolor@{}{}{cmyk}{#2,#3,#4,#5}}%
}

\protect
\tikz\node[c]{t};
\stoptext

Original answer (to a slightly different question!)

Two things are going on here. First, pgf does not support ConTeXt's \definecolor: there is a note to the effect in the manual. So you need to use \pgfutil@definecolor instead. Secondly, there is no built-in code for a cmyk model when working with ConTeXt. However, this does not seem to be too hard to emulate:

\usemodule[tikz]
\unprotect
\def\pgfutil@emu@cmyk#1#2,#3,#4,#5\@nil{%
  \expandafter\def\csname\string\color@#1\endcsname
    {\xcolor@{}{}{cmyk}{#2,#3,#4,#5}}%
}
\pgfutil@definecolor{c}{cmyk}{1,0,0,0}
\protect
\starttext
\tikz\node[c]{t};
\stoptext