# Beamer block color depending on section

My beamer file contain 4 section and in any section some block environment. I would like to create a command that defines a color for block of in specific section depending on the number of that section. I think I must use ifthen package:

\documentclass[]{beamer}
\usepackage{ifthen}
...
\newcommand{\myColor}{%
\ifthenelse{\thesection = 1}{color1}{%
\ifthenelse{\thesection = 2}{color2}{%
\ifthenelse{\thesection = 3}{color3}{%
\ifthenelse{\thesection = 3}{color4}{%
DimGray}%
}%
}%
}%

\begin{document}
\section{first section}
\begin{frame}
\begin{block}[A  block of the first section]
\end{block}
\end{frame}
\section{second section}
\begin{frame}
\begin{block}[A  block of the second section]
\end{block}

\end{frame}

\end{document}


but I don't know how to involve block environment. any suggestion would be appreciate.

Something like this?

\documentclass[]{beamer}

\AtBeginSection{%
\ifnum\value{section}=1
\setbeamercolor{block title}{bg=red}
\fi
\ifnum\value{section}=2
\setbeamercolor{block title}{bg=green}
\fi
}

\begin{document}
\section{first section}
\begin{frame}
\begin{block}{A  block of the first section}
\end{block}
\end{frame}
\section{second section}
\begin{frame}
\begin{block}{A  block of the second section}
\end{block}

\end{frame}

\end{document}


Second version

This approach is a bit closer to the syntax you envisioned in your question.

\documentclass[]{beamer}

\definecolor{zz1}{rgb}{.8,.2,.2}
\definecolor{zz2}{rgb}{.7,.9,.1}
\definecolor{zz3}{rgb}{.6,.4,.8}

\AtBeginSection{%
\setbeamercolor{block title}{bg=zz\thesection}
}

\begin{document}
\section{first section}
\begin{frame}
\begin{block}{A  block of the first section}
\end{block}
\end{frame}
\section{second section}
\begin{frame}
\begin{block}{A  block of the second section}