# [NTG-context] mp: choose colors randomly from list

Otared Kavian otared at gmail.com
Wed Feb 19 11:59:30 CET 2014

Hi Thomas,

One way is to increase the « degree of randomness » by saying
withcolor transparent (1,0.3,mycolor[round(uniformdeviate(10*i)/10)])
or you can define a RandomColor as below:

\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(10*i)/10)]) ;
endfor ;
\stopuniqueMPgraphic

\ctxlua{
CoeffRed = math.random(1,100)/100 ;
CoeffGreen = math.random(1,100)/100 ;
CoeffBlue = math.random(1,100)/100 ;
}
\def\CoeffRed{\ctxlua{tex.print(CoeffRed)}}
\def\CoeffGreen{\ctxlua{tex.print(CoeffGreen)}}
\def\CoeffBlue{\ctxlua{tex.print(CoeffBlue)}}

\definecolor[RandomColor]
[r=\CoeffRed,
g=\CoeffGreen,
b=\CoeffBlue
]

\starttext

\uniqueMPgraphic{colortest}

\startcolor[RandomColor]\bf This is a RandomColor \stopcolor

\stoptext

Best regards: OK

On 19 févr. 2014, at 11:10, Schmitz Thomas A. <thomas.schmitz at uni-bonn.de> wrote:

> 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
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
>
> maillist : ntg-context at ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
> archive  : http://foundry.supelec.fr/projects/contextrev/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________