[metapost] Problem with simulated gradient

Taco Hoekwater taco at elvenkind.com
Thu Mar 19 22:27:53 CET 2009

Jesús Guillermo Andrade wrote:
> The idea would be to begin with any given color in the leftmost part of 
> the polygon and have it full black to the left.
> Thanks again man.

I didn't check earlier as I thought to myself that you might as
well do the calculus yourself. I just bumped the i multiplier in
the x coordinate of the shifted until it looked like there was
a visible gradient.

I had some time to spare anyway, so here is how it works /
should work.

To get the gradient to behave nicely, you have to synchronize two
things: the end of the shift (so that is coincides with the rightmost
bit of the "eme" path, which is at 4.1u), and the moment where you
fully reach the final color, which should be at the same horizontal

1. At the end of the for loop in my adjustment to you original,
   you had a total x shift of -6u+10i with i_max=200, so

     shift (-6cm + 10*200(pt),0)

   10*200pt is about 70.6cm, which makes the gradient squares
   stop at an x position of 64.6cm instead of the desired

2. The color of the gradient reaches (1,1,1,1) at half of the
   total distance, (namely at s=100), which is at an x location
   of about 29.7cm, so that was also way off.

To rectify situation 1, you have to pick an i multiplier for the x
shift that makes the shift stop at 4.1u, meaning you have to travel
10.1u inside 200 i's, so that is

   for i=0 step s until 200:
      fill sq shifted (-6u+(i/200)*10.1u,0)

To rectify 2, you have to make the color reach its terminus
at "200", so divide by that instead of by 100:

   for i=0 step s until 200:
      fill sq shifted (-6u+(i/200)*10.1u,0)
          withcmykcolor (i/200)[(1,0.60,.60,0),(1,1,1,1)];

A thing you may rethink is that s=.1 is a whole lot of steps.
Unless you plan to use the logo in a really large size, you can
set s a bit higher and the result image will be smaller on disk
and it will also be drawn quite a bit faster.

Best wishes,

More information about the metapost mailing list