Generate random color in way that works with both pdflatex and lualatex

Generate random color in way that works with both pdflatex and lualatex



Right now, I am generating a random color in pdftex using


definecolorrandomcolorRGB

pdfuniformdeviate 255,
pdfuniformdeviate 255,
pdfuniformdeviate 255



Which works pretty well,
until I try and compile with luatex.



What is the nicest way to generate random colors (either directly or via generating 3 random numbers), that is compatible across both pdftex and luatex?



I suspect something involving pgf is the answer.
Or maybe just a condition with one branch for pdftex (that looks like the current), and one for luatex (that maybe invokes a tiny snippet of luacode).





Is the luatex85 package usable? It brings back all the deleted pdf... macros to lualatex
– daleif
Aug 21 at 11:06


luatex85


pdf...


lualatex





@daleif yes. Expand that into an answer, with a link to the docs maybe and that would be great. Assuming luatex85 is a no-op or something in pdftex
– Lyndon White
Aug 21 at 11:14




3 Answers
3



The luatex85 package does


luatex85


letpdfuniformdeviateuniformdeviate



You can thus do usepackageluatex85 or


usepackageluatex85


documentclassarticle
usepackagexcolor

unlessifdefinedpdfuniformdeviate
letpdfuniformdeviateuniformdeviate
fi

definecolorrandomcolorRGB

pdfuniformdeviate 255,
pdfuniformdeviate 255,
pdfuniformdeviate 255

extractcolorspecrandomcolortest
typeouttest

stop



This will type out something like


rgb0.84706,0.4,0.68234



with both pdflatex or lualatex. I guess that, when random numbers will be available also in xelatex, a common interface will be added to the LaTeX kernel.


pdflatex


lualatex


xelatex



Based on Randomly assign background colour for each frame



Another approach based on pgf:


pgf


documentclassarticle

usepackagepgf
usepackagepgffor
usepackagexcolor

usepackageifluatex
usepackageifxetex

ifluatex
letpdfrandomseedrandomseed
fi

ifxetex
pgfmathsetseedtime
else
pgfmathsetseednumberpdfrandomseed
fi

newcommandrandomcolor%
pgfmathsetmacroRrnd%
pgfmathsetmacroGrnd%
pgfmathsetmacroBrnd%
definecolorrandomcolrgbR,G,B%


begindocument

noindent%
foreach n in 1,...,1350%
randomcolorcolorrandomcolrule0.41cm0.41cm-%


enddocument



enter image description here



There is also the lcg package (short for Linear Congruential Generator), that works with all compilers, including lualatex. The package provides a command rand to generate a random number and store it in the rand counter. For use in definecolor you should convert the counter with thevalue.


lcg


lualatex


rand


rand


definecolor


thevalue



You can set the bounds of the random generator with the package options first and last, and optionally change them within the document using the reinitrand command. The default seed is based on the system clock, using the minutes as unit (so the output will change only once per minute). You can also provide a custom seed.


first


last


reinitrand



MWE:


documentclassarticle
usepackage[first=1,last=255]lcg
usepackagexcolor
randedefcolorathevaluerand
randedefcolorbthevaluerand
randedefcolorcthevaluerand

definecolorrandomcolorRGB

colora,
colorb,
colorc

begindocument
colorboxrandomcolorRandom color: colora,colorb,colorc
enddocument



Result:



enter image description here





Use thevaluerand instead of arabic. arabic is a formatting command and there is no garanty that it gives a number (e.g. hebrew and arabic packages often redefine it).
– Ulrike Fischer
Aug 21 at 19:08


thevaluerand


arabic


arabic





@UlrikeFischer thanks for the improvement, it didn't occur to me to put the there - edited.
– Marijn
Aug 23 at 11:42


the






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)