# How can I get different colors in each element of double loop

This is my code

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\usetikzlibrary{arrows}
\usetikzlibrary{decorations.pathmorphing,decorations.pathreplacing,decorations.markings}
\usepackage{xparse}
\usetikzlibrary{angles,positioning,quotes}

\begin{document}
\begin{tikzpicture}[scale=0.4, % Lattice Fundamental domaine q=2
extended line/.style={shorten >=-#1,shorten <=-#1},
extended line/.default=1cm]

\coordinate (A) at (0,0);
\coordinate (B) at (0:3); %Omega_1
\coordinate (D) at (60:3); %Omega_2
\coordinate (C) at ($(B) +(D)$);
\coordinate (F) at (30:{3/sqrt(3)});
\coordinate (F') at ($(C) - (F)$);

\clip ($-1.5*(D)-1.5*(B)$) rectangle ($2.5*(B)+2.5*(D)$);

%Draw lattice lines
\foreach \a in {-1,0,...,2.1}{
\foreach \b in {-1,0,...,2.1}{

\draw[gray,extended line=0.5cm] ($\b*(D)-(B)$) -- ($\b*(D)+2*(B)$);
\draw[gray,extended line=0.5cm] ($\a*(B)-(D)$) -- ($\a*(B)+2*(D)$); }}

%Drow lattice points
\foreach \a in {-1,0,...,2.1}{
\foreach \b in {-1,0,...,2.1}{

\filldraw[] ($\a*(B) + \b*(D)$) circle(2pt);}}

%\filldraw[red] (F) circle(2pt);
%\filldraw[green] (F') circle(2pt);

% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %first rose

\foreach \a/\colora in {-1/{violet!30},0/{violet!50},1/{violet!70},2/{violet!90}}{
\foreach \b in {-2,-1,0,1,2}{
\begin{scope}[shift={($\a*(0:3) + \b*(60:3)$)}]

\filldraw[gray,dashed,fill=\colora,opacity=0.2] (30:{3/sqrt(3)}) -- (0:3) -- ($(0:3) + (60:3) - (30:{3/sqrt(3)})$) -- (60:3) -- cycle ;

\filldraw[rotate around={120:(60:3)},gray,dashed,fill=\colora,opacity=0.2] (30:{3/sqrt(3)}) -- (0:3) -- ($(0:3) + (60:3) - (30:{3/sqrt(3)})$) -- (60:3) -- cycle ;

\filldraw[rotate around={240:(60:3)},gray,dashed,fill=\colora,opacity=0.2] (30:{3/sqrt(3)}) -- (0:3) -- ($(0:3) + (60:3) - (30:{3/sqrt(3)})$) -- (60:3) -- cycle ;
\end{scope}
}}

%Fudamental domaine
\filldraw[gray,dashed,fill=yellow!40!white] (30:{3/sqrt(3)}) -- (0:3) -- ($(0:3) + (60:3) - (30:{3/sqrt(3)})$) -- (60:3) -- cycle ;
\end{tikzpicture}
\end{document}


this is how it looks :

I have a double looping for the scope environment and I want to get a different color for each iteration. I tried to add another loop variable (\colora) in the first loop, but I can't go further. I think I can solve my problem if I know a way to mix 2 colors to generate a new color in a syntax allowed by tikz, I will add \colorb in the second loop and mix the two colors in each iteration.

Update

After trying the way given by Gonzalo Medina, I didn't get what I want exactly. It is my fault not explaining the final goal, So I will try to correct that :

In euclidean geometry : The yellow parallelogram, In the figure above, is what we call fundamental domain (for the action composed by rotation of the angle 0°,120°,240° and translation by Z ω1+ Z ω2, ω1 and ω2 are the generators of the lattice )

Now, If we take a fundamental domain and apply the rotation action only we get (I almost just removed the loops)

After that my Idea was to add the action of translation by adding shift={($\a*(0:3) + \b*(60:3)$)}, theoretically the result will be a covering of all the plan without any overlaying or intersection.

I want to make clear to the reader the procedure of covering the plan this is why I try to have each translation of the red part in the last picture with a different color.

I am not sure if this is a candidate for a separate question or not, but after Explaining that, I will be happy to have comments about my codes and ways to optimize it because It's the first week since I start coding with tikz, and I am full of doubts about may choices and way of doing things.

## Update

I simplified the code using a \pic for the fundamental domain. For the color, I present three options:

1. Randomly selecting shades of a fixed color (red, in my example), gives different coloring for each piece:

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}

\tikzset{
fundamental/.pic={
\draw[,scale=0.4,black,fill=red!\tmp,rotate around=#1]
(60:3) -- ($(0:3) + (60:3) - (30:{3/sqrt(3)})$) -- (0:3) -- (30:{3/sqrt(3)}) -- (60:3);
}
}

\begin{document}
\begin{tikzpicture}[scale=0.4, % Lattice Fundamental domaine q=2
extended line/.style={shorten >=-#1,shorten <=-#1},
extended line/.default=1cm]
\coordinate (A) at (0,0);
\coordinate (B) at (0:3); %Omega_1
\coordinate (D) at (60:3); %Omega_2
\coordinate (C) at ($(B) +(D)$);
\coordinate (F) at (30:{3/sqrt(3)});
\coordinate (F') at ($(C) - (F)$);

\clip ($-1.5*(D)-1.5*(B)$) rectangle ($2.5*(B)+2.5*(D)$);

\foreach \a in {-1,0,1,2}
{
\foreach \b in {-2,-1,0,1}
{
\begin{scope}
\pgfmathparse{95*rnd+5}
\edef\tmp{\pgfmathresult}
\pic at ($\a*(B) + \b*(D)$ ) {fundamental={240:(60:3)}};
\pic at ($\a*(B) + \b*(D)$ ) {fundamental={120:(60:3)}};
\pic at ($\a*(B) + \b*(D)$ ) {fundamental={0:(60:3)}};
\end{scope}
}
}

%Draw lattice lines
\foreach \a in {-1,0,...,2.1}{
\foreach \b in {-1,0,...,2.1}{

\draw[gray,extended line=0.5cm] ($\b*(D)-(B)$) -- ($\b*(D)+2*(B)$);
\draw[gray,extended line=0.5cm] ($\a*(B)-(D)$) -- ($\a*(B)+2*(D)$); }}

%Drow lattice points
\foreach \a in {-1,0,...,2.1}
{
\foreach \b in {-1,0,...,2.1}
{
\filldraw[] ($\a*(B) + \b*(D)$) circle(2pt);
}
}
\end{tikzpicture}

\end{document}


2. Randomly selecting different colors for each piece:

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}

\tikzset{
fundamental/.pic={
\draw[,scale=0.4,black,fill=MyColor!85,opacity=\opac,rotate around=#1]
(60:3) -- ($(0:3) + (60:3) - (30:{3/sqrt(3)})$) -- (0:3) -- (30:{3/sqrt(3)}) -- (60:3);
}
}

\begin{document}
\begin{tikzpicture}[scale=0.4, % Lattice Fundamental domaine q=2
extended line/.style={shorten >=-#1,shorten <=-#1},
extended line/.default=1cm]
\coordinate (A) at (0,0);
\coordinate (B) at (0:3); %Omega_1
\coordinate (D) at (60:3); %Omega_2
\coordinate (C) at ($(B) +(D)$);
\coordinate (F) at (30:{3/sqrt(3)});
\coordinate (F') at ($(C) - (F)$);

\clip ($-1.5*(D)-1.5*(B)$) rectangle ($2.5*(B)+2.5*(D)$);

\foreach \a in {-1,0,1,2}
{
\foreach \b in {-2,-1,0,1}
{
\begin{scope}
\pgfmathparse{250*rnd+5}
\edef\tmpi{\pgfmathresult}
\pgfmathparse{210*rnd+45}
\edef\tmpii{\pgfmathresult}
\pgfmathparse{230*rnd+25}
\edef\tmpiii{\pgfmathresult}
\pgfmathparse{0.75*rnd+0.25}
\edef\opac{\pgfmathresult}
\definecolor{MyColor}{RGB}{\tmpi,\tmpii,\tmpiii}
\pic at ($\a*(B) + \b*(D)$ ) {fundamental={240:(60:3)}};
\pic at ($\a*(B) + \b*(D)$ ) {fundamental={120:(60:3)}};
\pic at ($\a*(B) + \b*(D)$ ) {fundamental={0:(60:3)}};
\end{scope}
}
}

%Draw lattice lines
\foreach \a in {-1,0,...,2.1}{
\foreach \b in {-1,0,...,2.1}{

\draw[gray,extended line=0.5cm] ($\b*(D)-(B)$) -- ($\b*(D)+2*(B)$);
\draw[gray,extended line=0.5cm] ($\a*(B)-(D)$) -- ($\a*(B)+2*(D)$); }}

%Drow lattice points
\foreach \a in {-1,0,...,2.1}
{
\foreach \b in {-1,0,...,2.1}
{
\filldraw[] ($\a*(B) + \b*(D)$) circle(2pt);
}
}
\end{tikzpicture}

\end{document}


3. Using fixed colors for each "row":

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}

\tikzset{
fundamental/.pic={
\draw[,scale=0.4,black,fill=\colora!80!\colorb,opacity=0.7,rotate around=#1]
(60:3) -- ($(0:3) + (60:3) - (30:{3/sqrt(3)})$) -- (0:3) -- (30:{3/sqrt(3)}) -- (60:3);
}
}

\begin{document}
\begin{tikzpicture}[scale=0.4, % Lattice Fundamental domaine q=2
extended line/.style={shorten >=-#1,shorten <=-#1},
extended line/.default=1cm]
\coordinate (A) at (0,0);
\coordinate (B) at (0:3); %Omega_1
\coordinate (D) at (60:3); %Omega_2
\coordinate (C) at ($(B) +(D)$);
\coordinate (F) at (30:{3/sqrt(3)});
\coordinate (F') at ($(C) - (F)$);

\clip ($-1.5*(D)-1.5*(B)$) rectangle ($2.5*(B)+2.5*(D)$);

\foreach \a/\colora in {-1/red,0/green,1/blue,2/purple}
{
\foreach \b/\colorb in {-2/yellow,-1/magenta,0/gray,1/orange}
{
\begin{scope}
\pic at ($\a*(B) + \b*(D)$ ) {fundamental={240:(60:3)}};
\pic at ($\a*(B) + \b*(D)$ ) {fundamental={120:(60:3)}};
\pic at ($\a*(B) + \b*(D)$ ) {fundamental={0:(60:3)}};
\end{scope}
}
}

%Draw lattice lines
\foreach \a in {-1,0,...,2.1}{
\foreach \b in {-1,0,...,2.1}{

\draw[gray,extended line=0.5cm] ($\b*(D)-(B)$) -- ($\b*(D)+2*(B)$);
\draw[gray,extended line=0.5cm] ($\a*(B)-(D)$) -- ($\a*(B)+2*(D)$); }}

%Drow lattice points
\foreach \a in {-1,0,...,2.1}
{
\foreach \b in {-1,0,...,2.1}
{
\filldraw[] ($\a*(B) + \b*(D)$) circle(2pt);
}
}
\end{tikzpicture}

\end{document}


• thank you very much. the truth is that this not the final result I wanted, so please read my Update, I have added further explanation. Commented Aug 20, 2015 at 22:46
• @AymaneFihadi Please see my updated answer. Is it something like that what you had in mind? Commented Aug 21, 2015 at 1:05
• This is perfectly what I had in mind, thank you for all your help. This is the first time I see \pgfmathparse \pgfmathresult they are very helpful. what I didn't understand is the rnd without backslash it seems for me like a normal text, but from the context I can see that it return a number between 0 and 1, is that true, and what it is exactly if isn't a macro. Commented Aug 21, 2015 at 10:45
• @AymaneFihadi You're welcome. rnd generates a pseudo-random number between 0 and 1. It is part of the PGF math engine; using this engine you can perform many mathematical operations. All available functions and operators are described in Section 89 Mathematical Expressions of the PGF manual for version 3.0. Commented Aug 21, 2015 at 13:46