High entropy alloy in TikZ
I think the simplest way is to define your colors in a list with \pgfmathdeclarerandomlist
and then you can randomly select an item using \pgfmathrandomitem
.
Since you have a 2D TikZ
image, changing the view means to change the position (coordinates) of the balls.
Code:
\documentclass[tikz]{standalone}
\pgfmathdeclarerandomlist{colors}{%
{red}%
{green}%
{blue}%
{yellow}%
}
\begin{document}
\begin{tikzpicture}[]
\foreach \i in {1,...,12} {
\foreach \j in {1,...,6} {
\foreach \k in {1,...,4} {
\pgfmathrandomitem{\randColor}{colors}
\shade[ball color=\randColor] (\i-\j/3, {0.5*\j+\k}) circle(0.4);
}
}
}
\end{tikzpicture}
\end{document}
By the way, the answers to Drawing 3d crystal lattice with molecular layer in tikz show you other possibilities to build such a cube of "balls". These can be also randomly colored. For instance, for the answer of JLDiaz:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fadings,decorations.pathreplacing}
\pgfmathdeclarerandomlist{colors}{%
{red}%
{green}%
{blue}%
{yellow}%
}
\begin{document}
% You can tweak these
\def\ballradius{0.45}
%
\def\DrawRow#1#2{
\foreach \x in {0,...,#2}
\pgfmathrandomitem{\randColor}{colors}
\shade[ball color=\randColor] ($(#1) +(\x, 0,0)$) circle(\ballradius);
}
\def\DrawOddPlane#1{
\pgfmathsetmacro{\aux}{#1-1}
\foreach \z in {0,...,#1} {
\DrawRow{0,0,\z}{#1}
\if\z#1\relax\else
\DrawRow{0.5,0,\z+0.5}{\aux}
\fi
}
}
\def\DrawEvenPlane#1{
\pgfmathsetmacro{\aux}{#1-1}
\foreach \z in {0,...,#1} {
\DrawRow{0.5,0,\z}{\aux}
\if\z#1\relax\else
\DrawRow{0,0,\z+0.5}{#1}
\fi
}
}
\begin{tikzpicture}
\foreach \y in {0,...,3} {
\begin{scope}[yshift=\y cm]
\DrawOddPlane{3}
\end{scope}
\if\y3\relax\else
\begin{scope}[yshift=\y cm + 0.5cm]
\DrawEvenPlane{3}
\end{scope}
\fi
}
\pgfmathsetmacro{\cubex}{1}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{1}
\draw (3,3,3) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\draw (3,3,3) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw (3,3,3) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{tikzpicture}
\end{document}
With the great help of @Ñako, here's the final result.
% Cartoon of the AlCoCrFeNi high entropy alloy (HEA) with body-centered cubic (BCC) lattice.
\documentclass[tikz]{standalone}
\pgfmathdeclarerandomlist{colors}{{red!80}{teal}{blue!80}{orange}{blue!20}}
\begin{document}
\begin{tikzpicture}
\foreach \i in {1,...,12} {
\foreach \j in {1,...,4} {
\foreach \k in {1,...,4} {
\pgfmathrandomitem{\randColor}{colors}
\shade[ball color=\randColor] (-\i+0.3*\j, -0.2*\j+1.2*\k) circle(0.3);
}
\foreach \k in {1,...,3} {
\pgfmathrandomitem{\randColor}{colors}
\shade[ball color=\randColor] (-\i+0.5+0.3*\j, -0.2*\j+1.2*\k+0.6) circle(0.3);
}
}
}
\foreach \el/\color [count=\n] in {Al/red!80, Co/blue!80, Cr/teal, Fe/orange, Ni/blue!20} {
\shade[ball color=\color] (2, 5.5-\n) circle(0.3) node[right=1em] {\el};
}
\end{tikzpicture}
\end{document}