% theme created by serge guelton from nvidia's graphic chart,
% using http://www.texample.net/tikz/examples/dipolar-magnetic-field as reference
% and beamerthemelankton-keynote.sty

\ProvidesPackage{beamerthemenv} 
\usepackage{color}

%%%
%%% Locally change margin size
\newenvironment{changemargin}[2]{%
    \begin{list}{}{%
    \setlength{\topsep}{0pt}%
    \setlength{\leftmargin}{#1}%
    \setlength{\rightmargin}{#2}%
    \setlength{\listparindent}{\parindent}%
    \setlength{\itemindent}{\parindent}%
    \setlength{\parsep}{\parskip}%
    }%
    \item[]}{\end{list}}


%
%% colors
\selectcolormodel{rgb}

% the default ones
\colorlet{bgcolor}{black}
\colorlet{fgcolor}{white}
\colorlet{mgcolor}{green}

% possibly customized
\DeclareOptionBeamer{bgcolor}[bgcolor]{\colorlet{bgcolor}{#1}}
\DeclareOptionBeamer{mgcolor}[mgcolor]{\colorlet{mgcolor}{#1}}
\DeclareOptionBeamer{fgcolor}[fgcolor]{\colorlet{fgcolor}{#1}}
\ProcessOptionsBeamer

\mode<presentation>
% skip numbering first slide
\addtocounter{framenumber}{-1}

% defs

% fonts and stuff
\renewcommand\sfdefault{phv}
\renewcommand\familydefault{\sfdefault}
\usetheme{default}
\usepackage{tikz}

% some layers
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\usetikzlibrary{arrows,calc}

\useoutertheme{default}
\usepackage{marvosym}
\setbeamerfont{title}{size=\Huge}
\setbeamercolor{title}{bg=bgcolor, fg=fgcolor}
\setbeamercolor{structure}{fg=mgcolor}
\renewcommand\alert[1]{\structure{#1}}
\setbeamercolor{footline}{bg=bgcolor,fg=fgcolor}

\defbeamertemplate*{frametitle}{title}{%
		\textbf\Huge{\insertframetitle}\hfill%
}

\setbeamercolor{normal text}{bg=bgcolor, fg=fgcolor}
\setbeamertemplate{items}[square]
\setbeamertemplate{navigation symbols}{} %no nav symbols

%%% Macro's for 3D figures %%%
\newcommand\pgfmathsinandcos[3]{%
    \pgfmathsetmacro#1{sin(#3)}%
    \pgfmathsetmacro#2{cos(#3)}%
}
\newcommand\LongitudePlane[3][current plane]{%
    \pgfmathsinandcos\sinEl\cosEl{#2} % elevation
    \pgfmathsinandcos\sint\cost{#3}   % azimuth
    \tikzset{#1/.estyle={cm={\cost,\sint*\sinEl,0,\cosEl,(0,0)}}}
}
\newcommand\LatitudePlane[3][current plane]{%
    \pgfmathsinandcos\sinEl\cosEl{#2} % elevation
    \pgfmathsinandcos\sint\cost{#3}   % latitude
    \pgfmathsetmacro\yshift{\cosEl*\sint}
    \tikzset{#1/.estyle={cm={\cost,0,0,\cost*\sinEl,(0,\yshift)}}} %
}
% Computes a point on a field line given r and t
\newcommand{\fieldlinecurve}[2]{%
  {(pow(#1,2))*(3*cos(#2)+cos(3*#2))}, {(pow(#1,2))*(sin(#2)+sin(3*#2))}%
}
\ifbeamer@draftmode
\else
\usebackgroundtemplate{
	\begin{tikzpicture}[scale=1.0,]
	  \def\rectYm{-5}
	  \def\rectYM{5}
	%% some random definitions to have each bg be different
	  \pgfmathsetmacro{\angEl}{20+mod(\c@framenumber,21)}
	  \pgfmathsetmacro{\rectXm}{-10+mod(\c@framenumber,5)}
	  \pgfmathsetmacro{\rectXM}{10-mod(\c@framenumber,5)}
	  %
	  \coordinate (O) at (0,0);
	  % shaded background
	  \begin{pgfonlayer}{background}
	  \shade[top color=bgcolor,bottom color=fgcolor!30!bgcolor] (\rectXm-3,\rectYM) rectangle (\rectXM+3,\rectYm);
	  \end{pgfonlayer}
	  \begin{scope}[rotate around={-11.1:(0,0)},
	  field line/.style={color=mgcolor!30!bgcolor, smooth, opacity=.2,
		variable=\t, samples at={0,-10,...,-360}}
	]
	  \clip[rotate around={11.1:(0,0)}] (\rectXm,\rectYM) rectangle (\rectXM,\rectYm);
	  %



	% Longitudinal planes
	  \foreach \u in {0,-80,...,-160}{
		\LongitudePlane[{{\u}zplane}]{\angEl}{\u}
		\foreach \r in {0.25,0.75,...,2.25} {
		  \draw[{{\u}zplane}, field line]
			plot (\fieldlinecurve{\r}{\t});
		}
	  }
	\foreach \u in {-200,-240,...,-320}{
	  \LongitudePlane[{{\u}zplane}]{\angEl}{\u}
	  \foreach \r in {0.25,0.75,...,2.25}{
		\draw[{{\u}zplane}, dashed, field line]
		  plot (\fieldlinecurve{\r}{\t});
	  }
	}
	% Drawing plane for the B-vectors
	  \LongitudePlane[bzplane]{\angEl}{0}
	\foreach \r in {0.25,0.75,...,2.25}{
	  \draw[bzplane, thick, field line]
		plot (\fieldlinecurve{\r}{\t});
	}
	%
	  \end{scope}
	\end{tikzpicture}
} 
\fi
\setbeamertemplate{headline}
{
\ifbeamer@draftmode
\else
	\ifnum\c@framenumber<1
	\else
	\pgfmathsetmacro{\ClusterHeight}{ceil(.0001+sqrt(\inserttotalframenumber))}
	\vspace{.1em}\hspace{.1em}%
	\begin{tikzpicture}[scale=.1]
	\pgfmathsetmacro{\CurrentFrame}{ \insertframenumber}
	  \foreach \f in { 1, ..., \CurrentFrame }{
	  	\pgfmathsetmacro{\x}{mod(\f-1,\ClusterHeight)}
	  	\pgfmathsetmacro{\y}{floor((\f-1)/\ClusterHeight)}
		\shadedraw[ball color= mgcolor,draw=bgcolor] (-\x ,\y ,0.2) circle(.5);
	  }
	\ifnum\c@framenumber=\inserttotalframenumber
	  \draw[draw=fgcolor,thick,rounded corners=.2pt] (1, \ClusterHeight-1 ) -- (1,-1) -- (-\ClusterHeight,-1) --
	  (-\ClusterHeight, \ClusterHeight-1 ) -- (1, \ClusterHeight-1 );
	\else
	  \draw[draw=fgcolor,thick,rounded corners=.2pt] (1, \ClusterHeight-1 ) -- (1,-1) -- (-\ClusterHeight,-1) --
	  (-\ClusterHeight, \ClusterHeight-1 );
	\fi
	\end{tikzpicture}
	\fi%
\fi%
}
% footline
\setbeamertemplate{footline}
{
	\ifnum\c@framenumber<1
	\else
	\vspace{-1.25em}
		  %% title + slide number
		\leavevmode
		  \vspace{.2em}
		  \tikz \shadedraw[draw=mgcolor,very thick] (0,0) -- (20,0);
		  \vspace{.2em}
			{\hspace{1em}%
			\textbf{\insertshortauthor%
			\hfill%
			\insertshorttitle%
			\hspace{1em}%
			}
			%{\insertframenumber/\inserttotalframenumber}
			}
	\fi
}

%%%
%%% Color and width test
%%% from julien le guen :p
%%% http://jleguen.info/?p=87

\newcommand\makesanitycheckframe{
{
\setbeamertemplate{footline}{
\vspace{-5mm}
\begin{tikzpicture}
    % \mynumber is the total number of blocks in the gradient
    \pgfmathsetmacro{\mynumber}{11}
 
    % It is necessary to compute those separately
    \pgfmathsetmacro{\mylistnumber}{\mynumber-1}
    \pgfmathsetmacro{\mywidth}{128/\mynumber}
 
    % Draw a white to black gradient in \mynumber steps, to check contrast and screen width
    \tikzstyle{box} = [draw, rectangle, minimum height=5mm, minimum width=1mm*\mywidth];
    \foreach \x in {0,..., \mylistnumber}
    {
        \pgfmathsetmacro{\color}{100*\x/\mynumber}
        \pgfmathsetmacro{\textcolor}{100-\color}
        \node [box, fill=black!\color, text=black!\textcolor] at (\x*\mywidth*1mm, -5mm) {\x};
    }
\end{tikzpicture}
}

\begin{frame}[c]{Sanity Check}
\addtocounter{framenumber}{-1}
\begin{changemargin}{-1cm}{+0cm}
\begin{center}
    \begin{tikzpicture}[node distance=6em]
        % Draw a matrix of colored boxes, with color name inside.
        \tikzstyle{box} = [draw, rectangle, minimum height=5em, minimum width=6em]
        \foreach \y/\x/\color in {
                % List of user-defined colors - change the list to match your needs
                1/0/bgcolor/,
                1/1/mgcolor/,
                1/2/fgcolor/
                }
            {
                \node [box, fill=\color] at (\x*6em,-\y*6em) {\color};
            }
    \end{tikzpicture}
\end{center}
 
\end{changemargin}
\end{frame}
}
}
