Randomly assign color to background in the PDF whenever page is turned

Is it possible to change the background color of a Beamer presentation in the PDF in such way, that whenever I switch a page, the color is switched as well?

Note that I do not seek a solution to randomly switch during compilation (although that could be interesting as well), I look for a way to switch the color whenever I switch pages in the final PDF. So going from slide 1 to slide 2 and back should generate two different background colors for slide 1.

• It looks this is a task for JavaScript in TeX. It is pity that the question is not about random switch during compilation. I would like to try to solve such a task... :-) – Malipivo Apr 14 '14 at 7:58
• @Malipivo: There should be place at TeX.SE for 'unwanted' solutions or solutions to questions no one asked before ;-) – user31729 Apr 14 '14 at 8:11
• I saw two days ago a presentation with coloured backgrounds. Sadly while it looked quite good on the laptop it was quite unreadable on the wall. – Ulrike Fischer Apr 14 '14 at 9:15
• OCG (PDF Layers) + JavaScript. – AlexG Apr 14 '14 at 10:07
• randomly assigning colours during compilation has now its own question tex.stackexchange.com/q/171481/36296 – samcarter_is_at_topanswers.xyz Apr 14 '14 at 18:36

Example based on OCGs and JavaScript (pdflatex, AdobeReader required). Background colour is randomly chosen at display time.

\documentclass{beamer}

\usepackage{multido}
\usepackage{animate} %defines \@anim@newocg
\makeatletter
\let\newocg\@anim@newocg
\def\lastOCGobj{\@anim@curocg}
\makeatother

%JavaScript action to be executed on page open event
\pdfpageattr{ /AA <<
/O <<
/S/JavaScript/JS (
if(typeof(bgOCGs)==='undefined'){
bgOCGs=this.getOCGs();
curOCG=bgOCGs[0];
for(i=1;i<bgOCGs.length;i++)bgOCGs[i].state=false;
curOCG.state=true;
}
var l = bgOCGs.length;
randomIndex = Math.floor(Math.random()*l)\%l;
curOCG.state=false;
curOCG=bgOCGs[randomIndex];
curOCG.state=true;
)
>>
>>}

\begin{document}

\newsavebox\bgbox
\multido{\i=1+1}{100}{%
\pgfmathsetmacro{\R}{random(0,10000)/10000}%
\pgfmathsetmacro{\G}{random(0,10000)/10000}%
\pgfmathsetmacro{\B}{random(0,10000)/10000}%
\definecolor{bgcolor}{rgb}{\R,\G,\B}%
\savebox\bgbox{\color{bgcolor!40}\rule{\paperwidth}{\paperheight}}%
\newocg{bg}{\i}%  %create new ocg
\immediate\pdfxform attr{/OC \lastOCGobj}\bgbox%
\expandafter\xdef\csname cbox\i\endcsname{\the\pdflastxform}%
}
\setbeamertemplate{background canvas}{%
\makebox[\paperwidth][l]{%
\rule{0pt}{\paperheight}%
\multido{\i=1+1}{100}{%
\makebox[0pt][l]{\expandafter\pdfrefxform\csname cbox\i\endcsname}%
}%
}%
}

\begin{frame}{Frame 1}
\end{frame}
\begin{frame}{Frame 2}
\end{frame}
\begin{frame}{Frame 3}
\end{frame}
\begin{frame}{Frame 4}
\end{frame}
\end{document}


Since you (essentially) want randomly coloured page backgrounds at displaytime of a PDF, this is only doable with JavaScript. As pointed out in the comments OCGs will help.

I wouldn't be surprised if this is already solved in ConTeXt. :-)