7

I'm visualising color systems with TikZ to my students, and here I'm creating a 3D set of axes which represent red, green, blue and their combinations as shaded spheres.

RGB space

For some reason, the result looks alright on the Blue axis, but doesn't work as expected on Green and Red axes as when their values are increased the spheres stay gray. Where's the problem?

I've taken care that everything is represented in code in the same order, RGB. Code first sets up some variables about axes (x, y, z), then step size/distance between spheres and then the max value for assumed 8-bit color, 255.

The point is that the nested for eaches go through all integer values between 0–255 for red, green and blue (all combinations), the second \tikzmath creates scaled variables to be used with color information, and then with every pass a shaded sphere is drawn with the proper color and positioning on the axes.

\documentclass{memoir}
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}

\begin{tikzpicture}[scale=0.03]
\tikzmath{\x = 300; \y = \x; \z=\x; \step=51;\maxvalue=255;} %optimal step size 51
\draw[->, very thick, red] (0,0,0) -- (\x,0,0) node[right]{R};
\draw[->, very thick, green] (0,0,0) -- (0,\y,0) node[above]{G};
\draw[->, very thick, blue] (0,0,0) -- (0,0,\z) node[below left]{B};

\foreach \red in {0,\step,...,\maxvalue}{
    \foreach \green in {0,\step,...,\maxvalue}{
        \foreach \blue in {0,\step,...,\maxvalue}{

\tikzmath{\reds=\red/\maxvalue*100;\greens=\green/\maxvalue*100;\blues=\blue/\maxvalue*100;}

\shadedraw[ball color=red!\reds!green!\greens!blue!\blues!] (\red,\green,\blue) circle [radius=10cm];
        }
    }
}

\end{tikzpicture}

\end{document}

1 Answer 1

8

I do not know precisely what went wrong but I guess that there is some sort of unitarity problem when the fractions do not add up to 100. Here is some other method that does what I think you want.

\documentclass{memoir}
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}

\begin{tikzpicture}[scale=0.03]
\tikzmath{\x = 300; \y = \x; \z=\x; \step=51;\maxvalue=255;} %optimal step size 51
\draw[->, very thick, red] (0,0,0) -- (\x,0,0) node[right]{R};
\draw[->, very thick, green] (0,0,0) -- (0,\y,0) node[above]{G};
\draw[->, very thick, blue] (0,0,0) -- (0,0,\z) node[below left]{B};

\foreach \red in {0,\step,...,\maxvalue}{
    \foreach \green in {0,\step,...,\maxvalue}{
        \foreach \blue in {0,\step,...,\maxvalue}{

\tikzmath{\reds=(\red/\maxvalue)*255;
\greens=(\green/\maxvalue)*255;\blues=(\blue/\maxvalue)*255;}
\definecolor{mycolor}{RGB}{\reds,\greens,\blues}

\shadedraw[ball color=mycolor] (\red,\green,\blue) circle [radius=10cm];
        }
    }
}

\end{tikzpicture}
\end{document}

enter image description here

EDIT: Appended a version with unique color names. With all these different viewers around one never knows. The output is the same.

\documentclass{memoir}
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}

\begin{tikzpicture}[scale=0.03]
\tikzmath{\x = 300; \y = \x; \z=\x; \step=51;\maxvalue=255;} %optimal step size 51
\draw[->, very thick, red] (0,0,0) -- (\x,0,0) node[right]{R};
\draw[->, very thick, green] (0,0,0) -- (0,\y,0) node[above]{G};
\draw[->, very thick, blue] (0,0,0) -- (0,0,\z) node[below left]{B};

\foreach \red in {0,\step,...,\maxvalue}{
    \foreach \green in {0,\step,...,\maxvalue}{
        \foreach \blue in {0,\step,...,\maxvalue}{

\tikzmath{\reds=(\red/\maxvalue)*255;
\greens=(\green/\maxvalue)*255;\blues=(\blue/\maxvalue)*255;}
\definecolor{mycolor-\reds-\greens-\blues}{RGB}{\reds,\greens,\blues}

\shadedraw[ball color=mycolor-\reds-\greens-\blues] (\red,\green,\blue) circle [radius=10cm];
        }
    }
}

\end{tikzpicture}
\end{document}
3
  • That is very close to how I initially tried to color the spheres; I thought \definecolor didn't work for redefining as well? I'll give this one a shot (and might remove the maxvalue stuff altogether if I end up doing this completely in 8 bit anyway); thank you very much!
    – JoonasD6
    Commented Aug 26, 2018 at 15:36
  • 1
    Did before I read your plea. You deserve it. Just had to test a bit further first. :)
    – JoonasD6
    Commented Aug 26, 2018 at 15:43
  • @JoonasD6 Thanks! I added a second version with unique color names.
    – user121799
    Commented Aug 26, 2018 at 15:52

You must log in to answer this question.

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