[pstricks] Gouraud Shading

Eugene K. Ressler eugene.ressler at frontiernet.net
Sat Jun 18 05:35:58 CEST 2005


Hello!

> and a better one. Uses color setting of xcolor.sty
> Herbert

I'm finally getting to try Herbert's cool Gouraud triangle shading code.

It works great except that x and y in #1 were transposed.

Fixed below.  See the comment with (*) in it.

Gene

----

\documentclass[12pt]{article}
\usepackage{pstricks}
\makeatletter
\def\psGTriangle{\pst at object{psGTriangle}}
\def\psGTriangle at i(#1)(#2)(#3)#4#5#6{{%
   \def\solid at star{}
   \begin at ClosedObj
   \pst at getcoor{#1}\pst at tempa%   A: "rgb xr xg xb" or "gray xg"
   \pst at getcoor{#2}\pst at tempb%   B
   \pst at getcoor{#3}\pst at tempc%   C
   \pst at getcolor{#4}\pst at colorA
   \pst at getcolor{#5}\pst at colorB
   \pst at getcolor{#6}\pst at colorC
   \addto at pscode{%
     \pst at tempc		    % C
     \pst at tempb		    % B
     \psk at gangle             %   rotating angle
     \pst at tempa 		    % A, temporary origin
     /rgb {} def
     /gray {} def
     [ \pst at colorC ] aload length 1 eq { dup dup } if 3 array astore
%
gray -> rgb
     [ \pst at colorB ] aload length 1 eq { dup dup } if 3 array astore
     [ \pst at colorA ] aload length 1 eq { dup dup } if 3 array astore
     \tx at GTriangle % PS part
   }%
   \if at star\pspolygon(#1)(#2)(#3)\fi%	draw borderline
   \def\pst at linetype{2}%
   \end at ClosedObj}}
%
\pst at def{GTriangle}<{
   /mtrx CM def
   /colorA ED /colorB ED /colorC ED % save the colors
   /yA ED /xA ED	% save the origin (*)
   xA yA translate
   rotate			   % \psk at gangle
   /yB ED /xB ED /yC ED /xC ED      % save other coordinates
   /ds [ 			   % save data in a array
      0 0 0 colorA aload pop	   % fd x y xr xg xb
      0 xB xA sub yB yA sub colorB aload pop
      0 xC xA sub yC yA sub colorC aload pop
%     1 xC xB add yB colorA aload pop  % for use with 4 points ABCD
   ] def
   newpath
   <<
   /ShadingType 4		   % single Gouraud
   /ColorSpace [ /DeviceRGB ]
   /DataSource ds
   >>
   shfill
   closepath
   mtrx
   setmatrix
}>
\makeatother

\begin{document}

\begin{pspicture}(10,10)
   \psGTriangle(0,0)(5,10)(10,0){red}{green}{blue}
\end{pspicture}

\begin{pspicture}(10,10)
   \psGTriangle*(0,0)(9,10)(10,3){black}{white!50}{red!50!green!95}
\end{pspicture}

\begin{pspicture}(10,10)
   \psGTriangle*(0,0)(5,10)(10,0){green!16!blue!12}
                                {red!20!blue!60}
                                {red!40!green!70}
\end{pspicture}

\end{document}







More information about the PSTricks mailing list