mycolor[1 + round (uniformdeviate 2)]
(or define mycolor[0]-mycolor[2], in which case you can drop the "1 +")
uniformdeviate returns a random number between zero and the value of
the argument. So you do not want (uniformdeviate i).
Alan
On Wed, 19 Feb 2014 11:10:57 +0100
Schmitz Thomas A.
Hi all,
I would like to have mp choose a (transparent) color randomly from a predefined list. My naive approach was (let’s restrict it to three colors):
\startuniqueMPgraphic{colortest} save mycolor ; color mycolor[] ; mycolor[1] := (0.07, 0.21, 0.65) ; mycolor[2] := (0.6, 0.6, 0.4) ; mycolor[3] := (0.5, 0.8, 0.5) ; path p ; p := unitsquare scaled 2cm ; for i=1 upto 3: fill p shifted (i*2.5cm, 0) withcolor transparent (1,0.3,mycolor[round(uniformdeviate(i))]) ; endfor ; \stopuniqueMPgraphic
\starttext
\uniqueMPgraphic{colortest}
\stoptext
Is there a better approach? The selection isn’t random enough for my taste :-) Sometimes, in my real document, I get black color, even though black isn’t in the list, so I suspect that mp doesn’t get a correct value for mycolor[round(uniformdeviate(i))]. And finally: how could I define the colors in a way that I could also use them outside of mp? Or do I have to copy-paste the definitions into \definecolor’s?
All best
Thomas