[Tex/LaTex] How to use a large number of arbitrary rgb colors in a TikZ image

colormemorytikz-pgf

I have a document with some TikZ images in it, which are automatically generated from a program I have written. All images are fairly similar, and each of them contains of a large number of squares with different, arbitrary rgb colors. My question is: What is the best way of coloring all this large number of squares?

The method I have used so far consists of defining the color fillcolor by using the macro \definecolor, before drawing each square, and of using that color to fill the square. That means that each time I'm drawing a new square, I'm defining fillcolor again.

However, I'm not so sure that is the way to do it. pdflatex seems to require very much memory when compiling the document, and, for some strange reason, runs out of memory if I include more than 5 of the images (even if I increase the amount of memory tenfold when going from 5 to 6 images). Is there any chance that it stores all the colors I have defined, and that I need to make pdflatex "forget" about fillcolor before calling \definecolor again?

Update: Below is an example of one of the TikZ images I want in my document (it's a vizualization of the phase fraction field in a two-phase fluid simulation using the finite volume method and the volume of fluid method, and yes, I know, the fluid interface isn't exactly crisp):

TikZ image containing 1990 squares in different colors

This particular image consists of 1990 filled squares and 4 unfilled squares.

First, for each square, fillcolor is defined with \definecolor, and the square is filled with that color:

\definecolor{fillcolor}{rgb}{1.000000,0.241735,0.000000}
\fill[fillcolor] (0.203125,0.578125) rectangle (0.218750,0.593750);

Then the sides of each square is drawn, for example

\draw (0.203125,0.578125) rectangle (0.218750,0.593750);

Best Answer

As I said in a comment, if \definecolor, simply stores the color definition in a TeX macro, then redefining it will recycle the same macro, and thus will not use more memory.

However it is also possible that \defincolor is pushing some kind of literal PDF code which defines the color for the PDF engine. In this case, as far as I can understand it, no more memory should be used either for redefining a lot of times the same color.

Anyway, just in case, it is possible to use a RGB color without previous definition. Here is how.

  • In principle, tikz recognizes a color specificacion of the form {rgb:red,255;green,255;blue:255}. But it is not interpreted as one would expect. For example [fill={rgb:red,255;green,255;blue,0}] will not produce yellow, but a black tinted yellow instead (see figure below). I could not find documentation about how this specification works. Apparently all red, green, blue values are summed up, and then each particular value divided by the total. That, is, what we specify aren't absolute byte values, but relative weights. So, to get yellow we can write: {rgb:red,1;green:1;blue:-1}. I find this syntax rather inconvenient (moreover, how could we get "white"?)

  • PGF uses a internal macro: \pgfsys@color@rgb@fill which has the expected behaviour. It receives three numeric values for the r, g and b components, as 0-255 values. So we can use this macro to set the fill color just before the filling command.

Here is a code which shows several options:

\documentclass{article}
\usepackage{tikz}

% Define a more convenient macro without @ in its name
\makeatletter
\def\fillRGB#1#2#3{\pgfsys@color@rgb@fill{#1}{#2}{#3}}
\makeatother
\begin{document}

% Standard xcolor defined color
\definecolor{RGByellow}{RGB}{255,255,0}

% Some examples
\tikz{\node[draw,fill=RGByellow]{defined colour};}

\tikz{\node[draw,fill={rgb:red,255;green,255;blue,0}]{Tikz rgb: 255,255,0};}

\tikz{\node[draw,fill={rgb:red,1;green,1;blue,-1}]{Tikz rgb: 1,1,-1};}

\tikz{\fillRGB{255}{255}{0}\node[fill,draw]{{\tt fillRGB: 255,255,0} };}

\end{document}

The result:

Result