Shading of cubes in 3D picture

The example here is just great (and even better with the implementation in the comment).

I would, however, like to have all the sides on a cube in the same color but have the color change in each layer of cubes.

Here is the code (and a MWE of it's usage):

% Plane partition
\documentclass{minimal}
\usepackage{tikz}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength{\PreviewBorder}{5pt}

\begin{comment}
:Title: Plane partition

Illustration of a plane partition'.

Plane partition: http://mathworld.wolfram.com/PlanePartition.html
\end{comment}

% Three counters
\newcounter{x}
\newcounter{y}
\newcounter{z}

% The angles of x,y,z-axes
\newcommand{\xaxis}{210}
\newcommand{\yaxis}{-30}
\newcommand{\zaxis}{90}

% The top side of a cube
\newcommand{\topside}[3]{%
\fill[fill=yellow, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
shift={(\zaxis:#3)}] (0,0) -- (30:1) -- (0,1) --(150:1)--(0,0);
}

% The left side of a cube
\newcommand{\leftside}[3]{%
\fill[fill=red, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
shift={(\zaxis:#3)}] (0,0) -- (0,-1) -- (210:1) --(150:1)--(0,0);
}

% The right side of a cube
\newcommand{\rightside}[3]{%
\fill[fill=blue, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
shift={(\zaxis:#3)}] (0,0) -- (30:1) -- (-30:1) --(0,-1)--(0,0);
}

% The cube
\newcommand{\cube}[3]{%
\topside{#1}{#2}{#3} \leftside{#1}{#2}{#3} \rightside{#1}{#2}{#3}
}

% Definition of \planepartition
% To draw the following plane partition, just write \planepartition{ {a, b, c}, {d,e} }.
%  a b c
%  d e
\newcommand\planepartition[1]{
\setcounter{x}{-1}
\foreach \a in {#1} {
\setcounter{y}{-1}
\foreach \b in \a {
\setcounter{z}{-1}
\foreach \c in {0,...,\b} {
\cube{\value{x}}{\value{y}}{\value{z}}}
}
}
}
}

\begin{document}

\begin{tikzpicture}
\planepartition{{5,3,2,2},{4,2,2,1},{2,1},{1}}
\end{tikzpicture}

\end{document}


I have never used TikZ (and I do not have the time to learn it at the moment), so I would be very glad if someone would change the code for me to achive the desired result.

P.S. I have also posted this question here.

-
On this site, a question should typically revolve around an abstract issue (e.g. "How do I get a double horizontal line in a table?") rather than a concrete application (e.g. "How do I make this table?"). Questions that look like "Please do this complicated thing for me" tend to get closed because they are "too localized". Please try to make your question clear and simple by giving a minimal working example (MWE): you'll stand a greater chance of getting help. –  Andrew Uzzell Oct 9 '12 at 14:04
@ Andrew Uzzell: I have tried to modify my initial post. I do not know how to make the question "less localized" since I would like to be helped with something concrete. –  Svend Tveskæg Oct 9 '12 at 14:13

This particular example you chose is actually not very hard to read for the task you want. The actual drawing of the cubes is irrelevant. Hence you don't bother too much with TikZ.

The interesting point is that the layers are drawn for a value of the counter z. It is stepped in the definition of \planepartition in the last \foreach statement:

\foreach \c in {0,...,\b} {
...


This tells us that here the different planes are drawn.

The macro \c will loop through the values from 0 to \b (its actual value again is irrelevant). For each of these values the macro \c will hold the current values. We can use this to change the color depending on the layer.

The colors right now are in the three definitions for \topside, \leftside and \rightside when it reads

fill=<color>


Let's change all three cases into

fill=cubecolor


Now there are several possibilities. We could add something like \colorlet{cubecolor}{red!\c0} right after \addtocounter{z}{1}

\foreach \c in {0,...,\b} {
\colorlet{cubecolor}{red!\c0}
...


to get different shadings of red:

We could also define different colors depending on the value of \c. Let's define the following macro:

\newcommand*\cubecolors[1]{%
\ifcase#1\relax
\or\colorlet{cubecolor}{green}%
\or\colorlet{cubecolor}{yellow}%
\or\colorlet{cubecolor}{blue}%
\or\colorlet{cubecolor}{red}%
\or\colorlet{cubecolor}{purple}%
\or\colorlet{cubecolor}{cyan}%
\else
\colorlet{cubecolor}{white}%
\fi
}


and add it at the same place:

\foreach \c in {0,...,\b} {
\cubecolors{\c}
...


We'll get:

-
Very nice, especially the second example! –  Tom Bombadil Oct 9 '12 at 22:20
@cgnieder: Thank you. Very nice. –  Svend Tveskæg Oct 9 '12 at 22:38

I made a few little modifications, you now can get a gradient between two colors:

Code

\documentclass[tikz,border=5mm]{standalone}
\usepackage{xifthen}
\usepackage{verbatim}

\begin{comment}
:Title: Plane partition

Illustration of a plane partition'.

Plane partition: http://mathworld.wolfram.com/PlanePartition.html
\end{comment}

% Three counters
\newcounter{x}
\newcounter{y}
\newcounter{z}

% The angles of x,y,z-axes
\newcommand{\xaxis}{210}
\newcommand{\yaxis}{-30}
\newcommand{\zaxis}{90}

% The top side of a cube
\newcommand{\topside}[3]{%
\pgfmathsetmacro{\cpercent}{\mincolor+(\maxcolor-\mincolor)/(\maxz-1)*#3}
\fill[fill=cubecolorhigh!\cpercent!cubecolorlow, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
shift={(\zaxis:#3)}] (0,0) -- (30:1) -- (0,1) --(150:1)--(0,0);
}

% The left side of a cube
\newcommand{\leftside}[3]{%
\pgfmathsetmacro{\cpercent}{\mincolor+(\maxcolor-\mincolor)/(\maxz-1)*#3}
\fill[fill=cubecolorhigh!\cpercent!cubecolorlow, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
shift={(\zaxis:#3)}] (0,0) -- (0,-1) -- (210:1) --(150:1)--(0,0);
}

% The right side of a cube
\newcommand{\rightside}[3]{%
\pgfmathsetmacro{\cpercent}{\mincolor+(\maxcolor-\mincolor)/(\maxz-1)*#3}
\fill[fill=cubecolorhigh!\cpercent!cubecolorlow, draw=black,shift={(\xaxis:#1)},shift={(\yaxis:#2)},
shift={(\zaxis:#3)}] (0,0) -- (30:1) -- (-30:1) --(0,-1)--(0,0);
}

% The cube
\newcommand{\cube}[3]{%
\topside{#1}{#2}{#3} \leftside{#1}{#2}{#3} \rightside{#1}{#2}{#3}
}

% Definition of \planepartition
% To draw the following plane partition, just write \planepartition{ {a, b, c}, {d,e} }.
%  a b c
%  d e
\newcommand\planepartition[1]{
\setcounter{x}{-1}
\foreach \a in {#1} {
\setcounter{y}{-1}
\foreach \b in \a {
\setcounter{z}{-1}
\foreach \c in {0,...,\b} {
\cube{\value{x}}{\value{y}}{\value{z}}}
}
}
}
}

\begin{document}

\pgfmathsetmacro{\maxz}{5}% height of the highest tower
\pgfmathsetmacro{\mincolor}{20}% minimal percentage on gradient from lower to upper color (0-100)
\pgfmathsetmacro{\maxcolor}{80}% maximal percentage on gradient from lower to upper color (0-100)
\colorlet{cubecolorlow}{orange!75!gray}% color definition for low color (see xcolor manual)
\colorlet{cubecolorhigh}{blue!50!lime}% color definition for high color (see xcolor manual

\begin{tikzpicture}
\planepartition{{5,3,2,2},{4,2,2,1},{2,1},{1}}
\end{tikzpicture}

\pgfmathsetmacro{\maxz}{10}
\pgfmathsetmacro{\mincolor}{0}
\pgfmathsetmacro{\maxcolor}{100}
\colorlet{cubecolorlow}{blue}
\colorlet{cubecolorhigh}{red}

\begin{tikzpicture}
\planepartition{{10,3,2,2},{4,2,2,1},{2,1},{1}}
\end{tikzpicture}

\end{document}


Output

-
Very nice. I have accepted cgnieder's answer, but I could just as well have chosen yours. –  Svend Tveskæg Oct 9 '12 at 22:41