12

Today I found something weird. When I use \selectcolormodel{} in a TikZ drawing the model is only applied to \fill and not to \draw[fill=]. Does anyone know why?

\documentclass{article}

%\usepackage[gray]{xcolor} % all draws or fills become gray
\usepackage{xcolor}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

    \draw [fill=cyan] (0,0) rectangle (1,1);
    \fill [cyan] (2,0) rectangle (3,1);

\end{tikzpicture}

\selectcolormodel{gray}

\begin{tikzpicture}

    \draw [fill=cyan] (0,0) rectangle (1,1); % expected this one to change
    \fill [cyan] (2,0) rectangle (3,1);

\end{tikzpicture}

\end{document}

And the result is the one below.

Result

  • 1
    I don't think it is filling versus drawing. \draw [cyan] ... gives you grayscale output, too. And \fill [fill=cyan]... does not. TikZ does some complicated things with colour, some of which are designed to work even if xcolor is not available and I suspect something there is responsible but the code is well beyond my understanding :(. – cfr Oct 10 '15 at 21:27
  • Put the color selection command inside the tikzpicture. TikZ does some initialization and persists throughout the document. Or use every picture keys – percusse Oct 12 '15 at 20:52
  • 2
    Looking at this, I think it's got to be a bug in TikZ (there's no reason to 'expect' this behaviour): have you reported it? – Joseph Wright Dec 26 '15 at 16:39
  • No I did not. Where can I do it? – cacamailg Dec 26 '15 at 19:13
  • sourceforge.net/p/pgf/bugs – Torbjørn T. Dec 27 '15 at 22:49
7

The following workaround redefines the color in the current target color model via \colorlet[named]{<color>}{<color>}. A \colorlet without [named] does not redefine the color in the target color model, which seems also the problem with the TikZ, which uses \colorlet in \pgfsetfillcolor. As "unknown" option, TikZ uses \color instead, which converts the color on usage (depending on switch \ifconvertcolorU).

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

    \draw [fill=cyan] (0,0) rectangle (1,1);
    \fill [cyan] (2,0) rectangle (3,1);

\end{tikzpicture}

\selectcolormodel{gray}

\begin{tikzpicture}

    % \colorlet{cyan}{cyan} alone does not convert the color space
    \colorlet[named]{cyan}{cyan}%

    \draw [fill=cyan] (0,0) rectangle (1,1);
    \fill [cyan] (2,0) rectangle (3,1);

\end{tikzpicture}

\end{document}

Result

  • 1
    Did you have an idea why \usepackage[gray]{xcolor} is working well and why it is not equivalent to \selectcolormodel{gray} ? – Kpym Sep 15 '16 at 9:12
1

Meanwhile this bug has been fixed (or worked around) in the commit 83b22a available from the development repository of pgf/tikz https://sourceforge.net/projects/pgf/

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.