mp: choose colors randomly from list
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
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
On 19 Feb 2014, at 11:37, Alan BRASLAU
uniformdeviate returns a random number between zero and the value of the argument. So you do not want (uniformdeviate i).
Ah, that explains it. Thanks a lot Alan! Otared Kavian wrote:
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:
I hate myself for not knowing more about math! I don’t see how mp picks its random numbers - when I recompile, I always get the same colors in the same order. They are “random” in the sense that there is no fixed order, but I was surprised that every run produces the same random order. RandomColor is nice, but not for my current purpose because I want to choose from a fixed list (colors are used as backgrounds, so I can only use dark colours, e.g.). Thanks a lot! Thomas
Am 19.02.2014 um 12:11 schrieb Schmitz Thomas A.
I hate myself for not knowing more about math! I don’t see how mp picks its random numbers - when I recompile, I always get the same colors in the same order. They are “random” in the sense that there is no fixed order, but I was surprised that every run produces the same random order. RandomColor is nice, but not for my current purpose because I want to choose from a fixed list (colors are used as backgrounds, so I can only use dark colours, e.g.).
http://www.ntg.nl/pipermail/ntg-context/2012/065815.html Wolfgang
On 19 Feb 2014, at 12:16, Wolfgang Schuster
http://www.ntg.nl/pipermail/ntg-context/2012/065815.html
Wolfgang
Not only I’m bad at math, I don’t even remember the answers to my own questions - thanks Wolfgang for reminding me! I’m getting old and senile, should look into early retirement... Thomas
On Wed, 19 Feb 2014 12:38:38 +0100
Schmitz Thomas A.
Not only I’m bad at math, I don’t even remember the answers to my own questions - thanks Wolfgang for reminding me! I’m getting old and senile, should look into early retirement...
The random numbers generated are deterministic, that is each run will reproduce the same "random" sequence. If you are looking for more randomness, then you need to set "randomseed := numeric expression ;" using something that varies in a less predictable manner. For example, you can set randomseed := minute ; where minute is the number of minutes past the hour of when the job was started. So unless you have the habit of always processing your file at the same minute of the hour, then your colors will vary. Of course, if you want a truly random variable, then you need to use some other physical process, like detecting the moment of passage of cosmic radiation. One point of view is that chance is simply a reflection of our limited knowledge, our "bounded rationality". The classical expression of this is due to Laplace: \startquotation All events, even those that, because of their insignificance, seem not to be governed by the great laws of nature, are a consequence as necessary as the revolution of the Sun. Through our ignorance of their relationship to the system of the universe as a whole, we have them depend on some cause or on chance, according to whether they appear to occur with some regularity or not; But these imaginary causes have successively diminished with the advance of the limits of our knowledge, and disappear entirely before a sound philosophical analysis who sees in them but an expression of our ignorance, for which we ourselves are the real cause. \stopquotation Quantum mechanics has revised this point of view. However, you are the classicist, and according to Aristote an accident (συμβεβηκός) or chance (τὺχη αὐτόματον) or (σύμπτωμα) is just an encounter of two independent casual series. (And here I am exposing MY ignorance of Greek!) :) Alan
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.
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@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 ___________________________________________________________________________________
participants (4)
-
Alan BRASLAU
-
Otared Kavian
-
Schmitz Thomas A.
-
Wolfgang Schuster