Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to perform a separate stroke on each lineto in a path

Tags:

postscript

I'm trying to use pathforall to apply a different color to each line drawn in a path. When i put "stroke" at the end, it obviously only draws the shape in most recent color. How can i execute a stroke after each color change? The function block of /lineto doesnt accept "stroke" in it. Here's my code:

/Arial 12 selectfont
/color 6 def
/s 20 string def
1 setlinewidth

/#NextColor{
color 10 gt
{
    0
    /color exch def
}
{
    color 4 add
    /color exch def
    
   color 8 div color 15 div color 19 div setrgbcolor
    color 8 div s cvs show
    ( ) show
}
ifelse
} def

 300 300 translate
-72 -72 moveto 72 -72 lineto 72 72 lineto -72 72 lineto -72 -72 lineto -100 -100 lineto 100 -100 lineto 100 100 lineto -100 100 lineto  -100 -100 lineto

{/moveto load}
{/lineto load #NextColor }
{/curveto load}
{/closepath load}
pathforall
stroke
like image 782
aRA Avatar asked Dec 19 '25 08:12

aRA


1 Answers

As Ken says, you can't modify the path while iterating through the path. But you can capture the information and replay it later.

[
  { /move cvx 3 array astore cvx }
  { /line cvx 3 array astore cvx }
  { /curve cvx 7 array astore cvx }
  { /close cvx 1 array astore cvx }
  pathforall
]

This saves everything in an array. Each array element is a little procedure containing the points and a call to move line curve or close. You can execute the whole thing with {exec} forall.

Then you can define your own actions for each of the names. I chose different names from the original operators moveto, lineto, etc. so they can be defined to do something different without interfering with the real operators.

Something like the following definitions ought to do it, or come close.

/move { moveto } def
/line { lineto currentpoint stroke #NextColor moveto } def
/curve { curveto } def
/close { closepath } def

One problem with this approach is that by breaking up sequential line segments into separate lines, you prevent the PostScript implementation from drawing the "joins" between each segment. This may create a suboptimal appearance at corners with sharp angles.

Another approach to consider if the goal is more modestly stated as "a color-changing line" is you could replace stroke with strokepath fill and then replace the fill part of that with shfill. This would make a line drawing with all the appropriate joins added but with a smoothly changing color gradient that shfill can do.

like image 188
luser droog Avatar answered Dec 21 '25 02:12

luser droog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!