[NTG-context] Graded Transparency in MetaFun

Keith McKay mckaymeister at gmail.com
Thu Jun 30 17:25:58 CEST 2022


Hi all

  I'm trying to create graded transparency over an area similar to the 
effect produced using lmt_shade, where one colour goes to another colour 
over a continuum, but with graded transparency we would go from say 0 to 
1 transparency, for a single colour. See the MWE below.

I have tried this by creating a loop which draws a linear path, yshifted 
the thickness of the pen, with transparency calculated for each step   
over the a predetermined height on the page. I have also tried a similar 
way for a circular path but in this case the path is repeatedly drawn 
scaled in a loop. The MWE below works OK but when I zoom into the graded 
transparency areas lines begin to appear which would suggest that their 
is a better way to do what I want. This is borne out if you zoom into a 
lmt-shade area, one colour smoothly becomes another colour. I've put 
examples of this in the MWE. I'm guessing the way lmt_shade areas are 
drawn is the way to go. Is this likely to be easy to code for what I 
want to do?

Best Wishes

Keith McKay

%%%MWE%%

\setuppapersize[A4,portrait]

\usecolors[crayola]

\starttext

\startMPpage %page 1

StartPage;

numeric maxTransp;% Must be between 0 and 1

numeric opHeight; % Opaque area height on page

path t;

string p[]; p[5] = "RadicalRed"; p[10] = "LemonYellow"; p[15] = 
"MiddleGreen"; p[20] = "CadetBlue"; p[25] = "white";

maxTransp := 1;

opHeight := 2.5; %cm

for k= 5, 10, 15, 20, 25:

     draw lmt_text [

     text = "opaque",

     color = "black",

     style = "bold",

anchor = "lft",

position = (-1mm,2mm),

     ] xsized .95TextWidth ysized 7cm shifted(22cm,(k - 2)*cm);

     t := (1cm, k*cm) -- (20cm, k*cm); %Square opaque area

     draw t withpen pensquare scaled .25pt withcolor p[k] 
withtransparency("normal",maxTransp);

     for i = 0 step 0.25until (opHeight*72/2.54):

         r := (i/(opHeight*72/2.54))*maxTransp; %calculate transparency

draw t yshifted (-i-.25)*pt withpen pensquare scaled .25pt withcolor 
p[k] withtransparency("normal",(maxTransp-r));

draw t yshifted (i+.25)*pt withpen pensquare scaled .25pt withcolor p[k] 
withtransparency("normal",(maxTransp-r));

endfor;

endfor;

draw lmt_shade [

     path = fullsquare xyscaled(19cm,2cm),

direction = "up",

alternative = "linear",

colors = { "red", "blue" },

     ] shifted(10.5cm,1.5cm);

StopPage;

\stopMPpage

\page

\startMPpage %page 2

StartPage;

numeric maxTransp;% Must be between 0 and 1

numeric opHeight; % Opaque area height

path t, u;

string p[]; p[5] = "RadicalRed"; p[10] = "LemonYellow"; p[15] = 
"MiddleGreen"; p[20] = "CadetBlue"; p[25] = "white";

maxTransp := 1;

opHeight := 2.5; %centimeters

opHeightpt := (opHeight /2.54)*72.27;

draw lmt_text [

     text = "\rotate{opaque}",

     color = "black",

     style = "bold",

anchor = "lft",

position = (-1mm,2mm),

     ] xsized .25TextHeight shifted(12cm,10cm);

for k= 5, 10, 15, 20, 25:

     t := (1cm, k*cm)--(20cm, k*cm) ; %Square opaque area

     draw t withpen pensquare scaled .25pt withcolor p[k] 
withtransparency("normal",maxTransp);

     for i = 0 step 0.25 until opHeightpt:

         r := (i/(opHeight*72.27/2.54))*maxTransp;

draw t yshifted (i+0.25)*pt withpen pensquare scaled 0.25pt withcolor 
p[k] withtransparency("normal",(maxTransp-r));

draw t yshifted (-i-0.25)*pt withpen pensquare scaled .25pt withcolor 
p[k] withtransparency("normal",(maxTransp-r));

endfor;

endfor;

draw lmt_shade [

     path = fullsquare xyscaled(19cm,2cm),

direction = "up",

alternative = "linear",

colors = { "red", "blue" },

     ] shifted(10.5cm,1.5cm) ;

StopPage;

\stopMPpage

\page

\startMPpage %page 3

StartPage;

draw lmt_text [

     text = "opaque",

     color = "black",

     style = "bold",

anchor = "lft",

position = (-1mm,2mm),

     ] xsized .95TextWidth ysized 7cm shifted(22cm,13cm);

draw lmt_text [

     text = "opaque",

     color = "black",

     style = "bold",

anchor = "lft",

position = (-1mm,2mm),

     ] xsized .95TextWidth ysized 7cm shifted(22cm,3cm);

path t, u;

t := (11cm, 15cm) -- (20cm, 15cm); %Square opaque area

u := fullcircle scaled 5cm shifted (5cm,16cm); %Circular opaque area

for j = (5*72.27/2.54) step -0.25 until 1:

     r := (j/(5*72.27/2.54)) *0.5;

     s := -r + 0.5;

     draw fullcircle scaled (j*pt) shifted (5cm,16cm) withpen pencircle 
scaled 0.25pt withcolor ("RadicalRed") withtransparency ("normal",s);

endfor;

fill fullcircle scaled .25pt shifted (5cm,16cm) withpen pencircle scaled 
0.25pt withcolor ("RadicalRed") withtransparency ("normal",s);

for i = 0.0001 step 0.25 until (10*72.27/2.54):

     r := (i/(10*72.27/2.54));

     draw t yshifted -i*pt withpen pensquare scaled 0.25pt withcolor 
("SpringGreen") withtransparency ("lighten",(1-r));

     draw t yshifted i*pt withpen pensquare scaled 0.25pt withcolor 
("RadicalRed") withtransparency ("lighten",(1-r));

endfor;

draw lmt_shade [

     path = fullcircle scaled 5cm,

alternative = "circular",

     colors = { "red", "blue" },

     ] shifted(5cm,5cm) withtransparency("normal",0.85);

StopPage;

\stopMPpage

\stoptext
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ntg.nl/pipermail/ntg-context/attachments/20220630/0966746e/attachment-0001.htm>


More information about the ntg-context mailing list