19

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.

9

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 enter image description here

26
\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}

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.