How to get randomly coloured text in metapost?
Hi, how to get random colours for text inside textext? Take a look at the following example (MKIV). \definecolor [static] [r=.9, g=.7, b=.8] \starttext \startuseMPgraphic {1} def col= (uniformdeviate(1),uniformdeviate(1),uniformdeviate(1)) enddef; % These dots have a random color for i=0 upto 5: fill fullcircle scaled 5mm xshifted i*1cm withcolor col; endfor; % withcolor does not work, »Test« is black for i=0 upto 5: label(textext("Test"), (i*1cm,-1cm)) withcolor col; endfor; % To get a static color I use the following construct for i=0 upto 5: label(textext("\color[static]Test"), (i*1cm,-2cm)); endfor; \stopuseMPgraphic \useMPgraphic {1} \stoptext Is it possible to get the metapost construct »withcolor« to work with text or do I have to take a different approach? Thanks for any help. Marco
On Tuesday 11 May 2010 17:48:09 Marco wrote:
Hi,
how to get random colours for text inside textext? Take a look at the following example (MKIV).
\definecolor [static] [r=.9, g=.7, b=.8] \starttext \startuseMPgraphic {1}
def col= (uniformdeviate(1),uniformdeviate(1),uniformdeviate(1)) enddef;
% These dots have a random color for i=0 upto 5: fill fullcircle scaled 5mm xshifted i*1cm withcolor col; endfor;
% withcolor does not work, »Test« is black for i=0 upto 5: label(textext("Test"), (i*1cm,-1cm)) withcolor col; endfor;
% To get a static color I use the following construct for i=0 upto 5: label(textext("\color[static]Test"), (i*1cm,-2cm)); endfor;
\stopuseMPgraphic \useMPgraphic {1} \stoptext
Is it possible to get the metapost construct »withcolor« to work with text or do I have to take a different approach?
Thanks for any help. Marco
I understood from a much earlier discussion with Hans that "withcolor" will not be made to work with text, whose color therefore has to be set with ConTeXt constructs. Alan
Is it possible to get the metapost construct »withcolor« to work with text or do I have to take a different approach?
I understood from a much earlier discussion with Hans that "withcolor" will not be made to work with text, whose color therefore has to be set with ConTeXt constructs. Thanks for the information. But how to do this? How can I set up the randomness in ConTeXt? I browsed a bit through the sources and found a lua snipped producing random integers. So here's my first attempt.
% only Integers, so not much variation in color :-( \def\r{\ctxlua{commands.getrandomcounta(0,1)}} % Defining the colour at this point makes it kind of static again \definecolor[my][r=\r,g=\r,b=\r] \starttext \startuseMPgraphic {1} % The colour is all the same, but different in each run for i=0 upto 5: label(textext("\color[my]{Test}"), (i*1cm,-2cm)); endfor; % Does not work, results in compile error % for i=0 upto 5: % label(textext("\definecolor[my][r=\r,g=\r,b=\r]\color[my]{Test}"), % (i*1cm,-3cm)); % endfor; \stopuseMPgraphic \useMPgraphic {1} \stoptext Maybe this code is an improvable step in the right direction, maybe it's rubbish. Necessary improvements need to be: i) need of floating point random numbers ii) random colours per call and not per ConTeXt run iii) the use is quite verbose, so need to put it in a macro Any hints appreciated. Marco
On 11-5-2010 7:38, Marco wrote:
label(textext("\definecolor[my][r=\r,g=\r,b=\r]\color[my]{Test}"), % (i*1cm,-3cm));
numeric r, g, b ; r := uniformdeviate 1 ; g := uniformdeviate 1 ; b := uniformdeviate 1 ; textext("\colored[r=" & r & ",g=" & g & ",b=" & b & "]{Test}") etc ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
numeric r, g, b ;
r := uniformdeviate 1 ; g := uniformdeviate 1 ; b := uniformdeviate 1 ;
textext("\colored[r=" & r & ",g=" & g & ",b=" & b & "]{Test}") Thanks for this snipped, Hans. It looks promising, but I get the following compile error:
!mplib : mp terminal: >> "\colored [r=" >> 0.74464 ! Not implemented: (string)&(known numeric). <to be read again> I used the following code: \starttext \startuseMPgraphic {1} numeric r, g, b ; r := uniformdeviate 1 ; g := uniformdeviate 1 ; b := uniformdeviate 1 ; label(textext("\colored[r=" & r & ",g=" & g & ",b=" & b & "]{Test}"), origin); \stopuseMPgraphic \useMPgraphic {1} \stoptext Marco
On Tue, May 11, 2010 at 21:03, Marco wrote:
numeric r, g, b ;
r := uniformdeviate 1 ; g := uniformdeviate 1 ; b := uniformdeviate 1 ;
textext("\colored[r=" & r & ",g=" & g & ",b=" & b & "]{Test}") Thanks for this snipped, Hans. It looks promising, but I get the following compile error:
!mplib : mp terminal: >> "\colored [r=" >> 0.74464 ! Not implemented: (string)&(known numeric). <to be read again>
You need to convert number to string first. Use "decimal(r)" instead of "r". (But I didn't test.) Mojca
On Tue, May 11, 2010 at 9:27 PM, Mojca Miklavec
On Tue, May 11, 2010 at 21:03, Marco wrote:
numeric r, g, b ;
r := uniformdeviate 1 ; g := uniformdeviate 1 ; b := uniformdeviate 1 ;
textext("\colored[r=" & r & ",g=" & g & ",b=" & b & "]{Test}") Thanks for this snipped, Hans. It looks promising, but I get the following compile error:
!mplib : mp terminal: >> "\colored [r=" >> 0.74464 ! Not implemented: (string)&(known numeric). <to be read again>
You need to convert number to string first. Use "decimal(r)" instead of "r". (But I didn't test.)
Mojca ___________________________________________________________________________________ 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 ___________________________________________________________________________________
\starttext \startuseMPgraphic {1} numeric r, g, b ; r := uniformdeviate 1 ; g := uniformdeviate 1 ; b := uniformdeviate 1 ; label(textext("\colored[r=" & decimal(r) & ",g=" & decimal(g) & ",b=" & decimal(b) & "]{Test}"),origin); label(textext("r=" & decimal(r) & ",g=" & decimal(g) & ",b=" & decimal(b) ),origin+(0,-100)); \stopuseMPgraphic \useMPgraphic {1} \stoptext ( tested :-) ) Warning: default is preserve seed (every runs will have the same random number) if you don't purge temporary files *tuo, *tui etc. I'm sure that there is a switch/macros to avoid this, but I don't remember it now -- luigi
participants (5)
-
Alan BRASLAU
-
Hans Hagen
-
luigi scarso
-
Marco
-
Mojca Miklavec