6

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.

1 Answer 1

8

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
14
  • It’s a nice answer on how to define CMYK-colors for TikZ from inside a ConTeXt document. But my question was what code i should add at one position to enable TikZ to use ConTeXt’s colors. Sep 9, 2011 at 12:41
  • @flying sheep: That may be rather more tricky - while pgf is usable in plain and ConTeXt, it's pretty clear that the colour support is tied quite closely to LaTeX's approach.
    – Joseph Wright
    Sep 9, 2011 at 12:45
  • See my edited answer on what works already and thus tells me that there must be a way to create hybrid ConTeXt/TikZ colors. I don’t need them to be CMYK internally, just defined via CMYK. Also: Isn’t \unprotect \dosomethingwith@inside \protect the canonical way? Sep 9, 2011 at 12:49
  • 1
    @flying sheep: Answer updated to reflect altered question. On \unprotect, as I'm not a ConTeXt regular I was not sure if that was for @ or only for !, etc.: I've altered the code.
    – Joseph Wright
    Sep 9, 2011 at 13:11
  • 1
    @flying sheep: I've updated the answer again, and it should now work with both MkII and MkIV.
    – Joseph Wright
    Sep 9, 2011 at 15:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .