% --- start of displayed preamble in the book ---
% alphafill as defined above
% --- end of displayed preamble in the book ---

% graphic converted to gray in book using 'color2gray'

picture alphapict_; alphapict_=nullpicture;

vardef alphafill(expr c,col,transparency)=
 alphapict_:=nullpicture;
 alphafill_(currentpicture,c,col,transparency);
 addto currentpicture also alphapict_;
enddef;

def alphafill_(expr p,c,col,transparency)=
 begingroup
   save p_,xmax_,xmin_,ymax_,ymin_; picture p_;
   p_=nullpicture;
   (xmin_,ymin_)=llcorner c;(xmax_,ymax_)=urcorner c;
   addto p_ contour c withcolor transparency[background,col];
   for p__ within p:
     numeric xmin__,xmax__,ymin__,ymax__;
     (xmin__,ymin__)=llcorner p__;
     (xmax__,ymax__)=urcorner p__;
     if (xmax__<=xmin_) or (xmin__>=xmax_):
     else:
       if (ymax__<=ymin_) or (ymin__>=ymax_):
       else:
         if (not clipped p__) and (not bounded p__):
           addto p_ also p__ withcolor
           transparency[(redpart p__,greenpart p__,bluepart p__),col];
         else:
           begingroup save alphapict_;
             picture alphapict_; alphapict_ = nullpicture;
             alphafill_(p__,pathpart p__,col,transparency);
             addto p_ also alphapict_;
           endgroup;
         fi
       fi
     fi
   endfor
   clip p_ to c;
   addto alphapict_ also p_;
 endgroup;
enddef;

defaultfont:="ptmr8r";
warningcheck:=0;
beginfig(1)
numeric u;
u=4cm;
fill unitsquare scaled u
               shifted (u,.5u+2u)
               withcolor blue;
unfill unitsquare
      scaled .8u
      shifted (.1u+u,.1u+.5u+2u);
alphafill (unitsquare scaled u
 shifted (u-.2u,2u),(.8,0,.5),.5);
alphafill (fullcircle shifted (.5,.5)
 xscaled .75u  yscaled 1.5u
 shifted (u+.5u,1.75u+.5u),(1,1,0),.5);
fill unitsquare scaled .5u
    shifted (.25u+u,.25u+.5u+2u)
    withcolor green;
alphafill (unitsquare scaled .5u
    shifted (.15u+u,.35u+.5u+2u),(0,.4,.5),.5);
endfig;
end;