[pstricks] colored lines

Herbert Voss Herbert.Voss at fu-berlin.de
Thu Dec 23 12:43:03 CET 2021


Hello all,

merry christmas to all of you

\documentclass{article}
\usepackage{pstricks-add}
\makeatletter
\define at key[psset]{pstricks}{startColor}[black]{%
   \colorlet{tempcolor}[rgb]{#1}%   we need rgb
   \pst at getcolor{tempcolor}\ps at startColor}
\define at key[psset]{pstricks}{endColor}[white]{%
   \colorlet{tempcolor}[rgb]{#1}%
   \pst at getcolor{tempcolor}\ps at endColor}
\psset[pstricks]{startColor=black,endColor=white}

\def\pscolorLine{\def\pst at par{}\pst at object{pscolorLine}}
\def\pscolorLine at i{%
   \pst at getarrows{%
     \begin at OpenObj
     \pscolorLine at ii}}

\def\pscolorLine at ii(#1){\@ifnextchar({\pscolorLine at iii(#1)}{\pscolorLine at iii(0,0)(#1)}}%

\def\pscolorLine at iii(#1,#2)(#3,#4){%
   \use at par
\ifx\psk at arrowA\@empty\else\psline[linecolor=\ps at startColor]{->}(!#3 0.5 
mul #4 0.5 mul)(#1,#2)\fi
\ifx\psk at arrowB\@empty\else\psline[linecolor=\ps at endColor]{->}(!#1 0.5 
mul #2 0.5 mul)(#3,#4)\fi
   \pst at getcoor{#1,#2}\pst at tempA
   \pst at getcoor{#3,#4}\pst at tempB
   \addto at pscode{
     10 dict begin
     [ \psgetColorValues{\ps at startColor} ] dup == aload length 1 eq { 
dup dup } if
       3 copy 3 array astore /startColor ED
     [ \psgetColorValues{\ps at endColor} ] dup == aload length 1 eq { dup 
dup } if
       3 copy 3 array astore /endColor ED
%   on stack R1 G1 B1 R2 G2 B2
     4 -1 roll               % R1 G1 R2 G2 B2 B1
     sub 256 div /dB ED      % R1 G1 R2 G2
     3 -1 roll               % R1 R2 G2 G1
     sub 256 div /dG ED      % R1 R2
     exch sub 256 div /dR ED
     \pst at tempB /Y2 ED /X2 ED
     \pst at tempA /Y1 ED /X1 ED
     Y2 Y1 sub X2 X1 sub atan /Angle ED
     Y2 Y1 sub Angle sin dup 0 eq { pop }{ div } ifelse /L_line ED     % 
line length
     \ifx\psk at arrowA\@empty\else
       X2 X1 sub L_line div \psk at arrowlength\space mul 
\pst at number\psxunit mul X1 add /X1 ED
       Y2 Y1 sub L_line div \psk at arrowlength\space mul 
\pst at number\psyunit mul Y1 add /Y1 ED
      \fi
     \ifx\psk at arrowB\@empty\else
       X2 X1 sub L_line div \psk at arrowlength\space mul 
\pst at number\psxunit mul X2 exch sub /X2 ED
       Y2 Y1 sub L_line div \psk at arrowlength\space mul 
\pst at number\psyunit mul Y2 exch sub /Y2 ED
      \fi
     Y2 Y1 sub 256 div /dY ED
     X2 X1 sub 256 div /dX ED
%    dR dG dB == == ==
%startColor == endColor ==
     \ifPst at noCurrentPoint\else\pst at cp\fi       % current point?
     0 1 255 {
       /dIndex ED
%startColor ==
       X1 Y1 moveto
       X1 dX add  Y1 dY add % start for coordinate array
       gsave
       lineto
       CP /Y1 ED /X1 ED
       \pst at number\pslinewidth SLW
       startColor aload pop 3 copy setrgbcolor
       % on stack r g b
       dB add 3 1 roll    % b+dB r g
       dG add 3 1 roll    % b+dB g+dG r
       dR add 3 1 roll    % b+dB g+dG r+dR
       3 array astore /startColor ED
       stroke
       grestore
     } for
     end
   }%
   \end at OpenObj
   \ignorespaces
}

\makeatother

\begin{document}

\begin{pspicture}[linewidth=12pt,showgrid](-5,-5)(5,5)
\pscolorLine(0,-5)(0,5)
\pscolorLine[startColor=cyan,endColor=magenta](-5,0)(5,0)
\pscolorLine[startColor=blue,endColor=green]{->}(-5,5)(5,-5)
\pscolorLine[startColor=red,endColor=blue]{<->}(-5,-5)(5,5)
\end{pspicture}

\end{document}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Bildschirmfoto 2021-12-23 um 12.38.52.png
Type: image/png
Size: 100540 bytes
Desc: not available
URL: <https://tug.org/pipermail/pstricks/attachments/20211223/5f0911ba/attachment-0001.png>


More information about the PSTricks mailing list.