Example: RGB color mixing

Published 2008-10-27 | Author: Henrik Skov Midtiby

The hue (H), saturation (S), value (V) color model is an intuitive way of specifying colors. Internally PGF only supports the RGB, CMY, CMYK and Gray color models, but xcolor can automatically convert from HSV to your target color model.

This illustration shows samples of the HSV color space and how they relate to the RGB color model. The saturation and value parameters are set to S=V=1.

Source: PGF-users mailing list

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

% RGB color mixing
% Author: Henrik Skov Midtiby <http://midtiby.blogspot.com/>
\documentclass{article}
% Set target color model to RGB
\usepackage[rgb]{xcolor}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
% Create the background in the circle, by drawing several slices
% each with a constant color given by the angle (which is converted
% to a color usin the hue, saturation and brightness color space).
\foreach \x in {0,0.0111,...,1} {
\definecolor{currentcolor}{hsb}{\x, 1, 1}
\draw[draw=none, fill=currentcolor]
(-360*\x+88:2) -- (-360*\x+88:3.8)
-- (-360*\x+92:3.8) -- (-360*\x+92:2) -- cycle;
}

% On top of the background draw three spotlights of the primary colors
% red, green and blue (they are primary in an additive colorspace where
% light are mixed)
\draw [draw=none, fill=red] (90:1.5) circle (2cm);
\draw [draw=none, fill=green] (-30:1.5) circle (2cm);
\draw [draw=none, fill=blue] (210:1.5) circle (2cm);

% Draw areas where two of the three primary colors are overlapping.
% These areas are the secondary colors yellow, cyan and magenta.
\begin{scope} % red + green = yellow
\clip (90:1.5) circle(2cm);
\draw [draw=none, fill=yellow] (-30:1.5) circle (2cm);
\end{scope} % blue + red = magenta
\begin{scope}
\clip (210:1.5) circle(2cm);
\draw [draw=none, fill=magenta] (90:1.5) circle (2cm);
\end{scope}
\begin{scope} % green + blue = cyan
\clip (-30:1.5) circle(2cm);
\draw [draw=none, fill=cyan] (210:1.5) circle (2cm);
\end{scope}

% Draw the center area which consists of all the primary colors.
\begin{scope} % red + green + blue = white
\clip (90:1.5) circle(2cm);
\clip (210:1.5) circle(2cm);
\draw [draw=none, fill=white] (-30:1.5) circle (2cm);
\end{scope}

% Draw a circle with markings along the perimeter, indicating which angles
% the hue function connects to certain colors.
\draw (0, 0) circle (3.9cm);
\foreach \x  in {0, 30, ..., 330}
\draw (-\x+90:3.8) -- (-\x+90:4.0) (-\x+90:4.4) node {$\x^\circ$};

% Add labels with names of the primary and secondary colors.
\foreach \x/\text in {0/red, 60/yellow, 120/green, 180/cyan, 240/blue, 300/magenta}
\draw (-\x+90:5.5) node {\text};
\end{tikzpicture}

\end{document}


• #1 Wolfgang Gruber, October 31, 2008 at 4:32 p.m.

The link to the tex-file contains wrong content.

The copied text from this html-file showed 10 errors in the beginning, but after removing the empty(!) line after "\foreach \x in {0, 30, ..., 330}" everything was fine.

Two questions: 1. What is special with this empty line? 2. How to tweak pgf to show the erroneous linenumber in my source file and not in some pgf-library.

Best regards Wolfgang

• #2 Kjell Magne Fauske, October 31, 2008 at 4:54 p.m.

You are right Wolfgang. Thanks for notifying me. I have now uploaded the missing TEX file and fixed the formatting of the html-version. It seems to be a line ending issue. I have not yet updated my automated build script for TeXample.net, so uploading new examples is partially a manual process at the moment. I hope to fix this soon.

The \foreach statement probably don't like the blank line because it expects a path immediately after the {0,30,...,330} part. If we use instead

\foreach \x  in {0, 30, ..., 330} {

\draw (-\x+90:3.8) -- (-\x+90:4.0) (-\x+90:4.4) node {$\x^\circ$};
}


it will work fine even with a blank line.

Regarding your second question I don't have any good answer. This is probably a TeX issue