# Randomly assign background colour for each frame

How can a randomly picked background colour be chosen for each beamer frame?

## Note:

This question is asked to round out the question Randomly assign color to background in the PDF whenever page is turned which asks for a solution which alters the colour when the pdf is displayed. On contrast the present question deals with randomly assigning colours during compilation.

Here is one way of doing this in ConTeXt. To set the color of a page, we need:

\definebackgrounds[page][background=color, backgroundcolor=...name...]


At each page, the setups specified in definebackgrounds are re-evaluated. So, to change the color of each page, we can simply change the value of the background color to a random color. It is easier to do so in Lua. So, here is a complete example:

\startluacode
local random = math.random
local format = string.format
commands.change_color = function()
options = { format("r=%0.3f, g=%0.3f, b=%0.3f", random(), random(), random()) }
context.definecolor( {"randomcolor"}, options)
end
\stopluacode

\startsetups change:color
\ctxcommand{change_color()}
\stopsetups

\setupbackgrounds
[page]
[
background=color,
setups=change:color,
backgroundcolor=randomcolor,
]

% Just to visualize the result
\setuppapersize[A6][A2,landscape]
\setuppaper[nx=5,ny=3]
\setuparranging[XY]

\starttext

\dorecurse{15}{\null\page}

\stoptext


which gives

\documentclass{beamer}

\usepackage{pgf}
\usepackage{pgffor}

\pgfmathsetseed{\number\pdfrandomseed} % seed for random generator

\setbeamertemplate{background canvas}{%
\pgfmathsetmacro{\R}{random(0,10000)/10000}%
\pgfmathsetmacro{\G}{random(0,10000)/10000}%
\pgfmathsetmacro{\B}{random(0,10000)/10000}%
\definecolor{bgcolor}{rgb}{\R,\G,\B}%
\color{bgcolor!40}\rule{\paperwidth}{\paperheight}
}

\begin{document}

\foreach \n in {1,...,100}{
\begin{frame}
test
\end{frame}
}

\end{document}


• Just for curiosity, how did you compose the image? – Ignasi Sep 5 '14 at 12:12
• The image is just a screenshot of the resulting pdf opened with Preview. No magic :) – samcarter_is_at_topanswers.xyz Sep 5 '14 at 12:14