Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I tried to get a random color with xcolor and tikz:

\documentclass{article}
\usepackage[xetex, rgb]{xcolor}
\usepackage{tikz}

\begin{document}

\pgfmathsetseed{1}
\begin{tikzpicture}
    \xdefinecolor{MyColor}{hsb}{rand, 1.0, 1.0}
    \node[fill=MyColor]{Hello};
\end{tikzpicture}
\end{document}

But this does not work, it reports the following error

Missing number, treated as zero

Does anybody know how to get random colors? thanks.

share|improve this question
add comment

2 Answers

up vote 9 down vote accepted
\begin{tikzpicture}
    \pgfmathparse{rnd}
    \xdefinecolor{MyColor}{hsb}{\pgfmathresult, 1.0, 1.0}
    \node[fill=MyColor]{Hello};
\end{tikzpicture}
share|improve this answer
    
I get ERROR: Package pgfbase Error: Unsupported color model 'hsb'. Sorry.. –  Georges Dupéron Sep 4 '13 at 10:33
    
@GeorgesDupéron The xcolor package is required –  egreg Sep 4 '13 at 12:17
    
Yes, I found out that one needs \usepackage[rgb]{xcolor} for most documentclass, but that doesn't work with beamer, since it already loads xcolor with other options, so one needs to \documentclass[rgb]{beamer} instead. –  Georges Dupéron Sep 4 '13 at 12:30
1  
@GeorgesDupéron The recommended way should be \documentclass[xcolor={rgb}]{beamer} –  egreg Sep 4 '13 at 12:31
add comment

The egre solution gives you a Pseudorandom color.

If you use pdftex or one of his son, I suggest you this solution, where for every compilation you will obtain a different color.

\documentclass{standalone}

\usepackage{tikz}


\begin{document}
\begin{tikzpicture}
    \pgfmathparse{rnd}
    \xdefinecolor{MyColor}{rgb}{\pgfmathresult, 1.0, 1.0}
    \node[fill=MyColor](A) at (0,0){Hello};

     \edef\R{\pdfuniformdeviate 255}
     \edef\G{\pdfuniformdeviate 255}
     \edef\B{\pdfuniformdeviate 255}
     \xdefinecolor{MyColor2}{RGB}{\R,\G,\B}
     \node[fill=MyColor2](A2) at (0,-.5){Hello};
\end{tikzpicture}
\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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