7

I have a custom palette which I want to paint with it by indexing.

Here is my attempt:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

\def\colorPallete{{0.00 0.00 0.52, 0.00 0.77 1.00, 1.00 0.98 0.00, 0.73 0.00 0.00}}

\foreach \x [count=\n] in {0,...,3}{%
\definecolor{currentColor}{rgb}{{\colorPallete[\x]}}
\fill [fill=currentColor] (\n,-2) circle[radius=.3];
}
\end{tikzpicture}
\end{document}

The problem is that the parser cannot deal with the rgb triplet, because if I use a similar array indexation to modify some other parameter like int the following example it works.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

\def\radiousCollection{{.1, .2, .3, .4}}
\foreach \x [count=\n] in {0,...,3}{%
\fill [fill=black] (\n,-2) circle[radius={\radiousCollection[\x]}];
}
\end{tikzpicture}
\end{document}

enter image description here

A possible workaround is to split the three planes of the color palette into three different arrays then the indexation works as in the case of the radious.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}\tiny
\begin{tikzpicture}

\def\colorPalleteR{{0.00, 0.00, 1.00, 0.73}}
\def\colorPalleteG{{0.00,  0.77,  0.98,  0.00}}
\def\colorPalleteB{{1.0,  1.00,  0.00,0.00}}

\foreach \x [   count=\n,
            evaluate=\x as \myRval using ({\colorPalleteR[\x]}),
            evaluate=\x as \myGval using ({\colorPalleteG[\x]}),
            evaluate=\x as \myBval using ({\colorPalleteB[\x]}) ] in {0,...,3}{%

\definecolor{MyColor}{rgb}{\myRval,\myGval,\myBval}
\fill [fill=MyColor] (\n,0) circle[radius=.3];
\draw   (\n,.8) node {R=\myRval}
        (\n,.6) node {G=\myGval}
        (\n,.4) node {B=\myBval};
}
\end{tikzpicture}
\end{document}

enter image description here

Can anyone explain how shall be used in order to parse vectors properly so that there is no need to split the palette by planes?

Sik
  • 1,331

1 Answers1

6

You can only use the array notation inside of tikz/pgf. definecolor isn't from this package. You may use \pgfmathparse to get around this problem:

\documentclass{standalone}
\usepackage{tikz}

\def\colorPallete{{"0.00 0.00 0.52", "0.00 0.77 1.00", "1.00 0.98 0.00", "0.73 0.00 0.00"}}


\begin{document}
    \begin{tikzpicture}
        \foreach \x [count=\n] in {0,...,3}{%
        \pgfmathparse{\colorPallete[\x]};
        \definecolor{currentColor}{rgb}{\pgfmathresult};
        \fill [fill=currentColor] (\n,-2) circle[radius=.3];
        }
    \end{tikzpicture}

\end{document}

result

someonr
  • 8,531
  • well thats the problem, I can't do that. I have 2k+ shapes to color based on an identifier. – Sik Nov 23 '13 at 23:09
  • @Sik Just read the manual again and updated my solution. Hope this is what you are looking for. – someonr Nov 24 '13 at 13:56
  • You are asking me to parse all my data and instead of using a list of ids, I use a list of colors or something like a { id1/rval1 gval1 bval1, id2/rval2 rgval2 bval2, ... idn/rvalN gvalN bvalN} and yes it is definitlly a work around, but a bad one. Because you are forced to iterate over the colors itsef. If I want to change the palette I've to change the array I'm iterating. – Sik Nov 24 '13 at 18:17
  • The only justification to do such a thing would be if the problem I'm posting is related to this http://tex.stackexchange.com/questions/43226/missing-number-error-using-pgfmathsetmacro-with-the-ifthenelse-operator – Sik Nov 24 '13 at 18:18
  • @Sik Ok, I think that I now understand the question. Are you OK with "0.00 0.00 0.52"? I'm updating the answer. – someonr Nov 24 '13 at 19:03
  • yes that was just a dummy palette for a dummy example. But I did try setting the colors as "xx xx xx" and I could not parse them properly either. Maybe I was trying to use it directly {\colorPallete[\x]} and it requires to force the parser by using \pgfmathparse{\colorPallete[\x]} or evaluate=\x as \myColor using ({\colorPallete[\x]}) – Sik Nov 24 '13 at 19:25