%------les macros de mise en couleur-----
%\input{/users/parall/cerin/=tex/=macros/colorrgb.tex}
\input{colorrgb.tex}
%------styles des documents-----
\documentstyle[12pt,euro]{article}
%\documentstyle[12pt,/users/parall/cerin/=tex/=macros/euro]{article}
%------Les macros locales-------
\def\CercleBidon#1#2#3#4#5#6#7#8{%
% trace un cercle de rayon #1mm et d'epaisseur du trait = #2mm,
% puis remplit ce cercle avec des hachures de largueur = #3mm inclinees
% de #4 degres. La couleur est specifiee par les parametres #5 #6 #7
% (r g b : dans cet ordre). Le parametre #8 donne l'espacement entre
% les hachures. Les angles doivent etre compris entre 0 et 180 degres.
\ifnum #4 > 180 #4 <0
    \typeout{---CercleHachure : le rayon #4 n'est pas <= 180 ni >= 0}
\else
    \special{" gsave 2.85 2.85 scale
	#5 255 div #6 255 div #7 255 div setrgbcolor
	0 0 moveto currentpoint translate newpath
	0 0 #1 0 360 arc #2 setlinewidth stroke
%	newpath 0 0 #1 0 360 arc closepath clip
	newpath
	#3 setlinewidth
	\ifdashps [ 2 2 ] 0 setdash \fi
	#4 90 ge {
		180 #4 sub cos abs #1 mul #4 sin abs #1 mul add
		180 #4 sub cos abs #1 mul #4 sin abs #1 mul sub
		translate #4 rotate
		0 #8 #1 2 mul {
			pop 0 0 moveto
			#1 2 mul 0 lineto
			0 #8 moveto currentpoint translate
		} for
	} {
		180 #4 sub cos abs #1 mul #4 sin abs #1 mul sub
		180 #4 sub cos abs #1 mul #4 sin abs #1 mul add
		translate #4 rotate
		0 #8 #1 2 mul {
			pop 0 0 moveto
			#1 2 mul neg 0 lineto
			0 #8 neg moveto currentpoint translate
		} for
	} ifelse
	stroke}
\fi}
\def\ggf{\raise.4ex\hbox{%
	$\scriptscriptstyle\langle\!\langle\;$}}
\def\gdf{\raise.4ex\hbox{%
	$\scriptscriptstyle\;\rangle\!\rangle\;$}}
\setlength{\topmargin}{-10mm}
%------debut du document------
\begin{document}

\title{A step by step approach to putting colour in \TeX\ documents}
\author{Christophe C\'erin}
\affiliation{Universit\'e de Paris Sud\\
LRI bat 490\\
91405 orsay\\
email : cerin\@ lri.lri.fr}

\maketitle

We have developed sets of macros to handle colour in \TeX\ documents
for one year. First of all "Vers l'introduction de la couleur dans \TeX" \cite{cc}
allows the marking out of text to
change colour, layouts of rectangular boxes in colour, different
kinds of shading on strings\ldots When we change from one colour to another,
we implement these tasks with PostScript operators. Hence our macros are
dependant on translators \verb+dvi+ to PostScript. We use, for this,
\verb+dvips+ from T.Rokicki.

The new set --version 2.3-- we put forward for the meeting allows for
rectangular boxes with uniform colour background, rectangular boxes
with gradually shaded backgrounds and efficient graphics primitives as Bezier
curves in colour and hachure circles for \LaTeX\ picture environment.

On this occasion, we'd like to comment on our general rules for
the design of colour macros :

\begin{itemize}
\item each time that we need to superimpose \TeX\ boxes containing
graphic objects or text ; each time we need to know dimensions of
these \hbox{boxes :} do it with \TeX\ ;
\item Moreover, each time we have to draw lines in colour, to put circles
at a precise position on the page layer : do it with PostScript (a notion
of current point is needed) ;
\end{itemize}

At last, we show that a strict discipline of specification and the use
of programming concepts from classical languages leads to a good
implementation in time and in disk space.

\section{A macro without notion of current point}

We would like a macro called \verb+echellon+ realizing shading on strings.
The first and the last letter are printed with a value of colour given
by parameters \verb+begin-color+ and \verb+end-color+. The main difficulty lies
in the distribution of colour along the string uniformly.

In an informal way, the problem can be enounced like this : to put a
string in colour, print the first letter of the string with the value
of \verb+begin-color+, increase the value of the colour and repeat this
same process on the string without the first letter.

An intermediate code is (cf \cite{cc}) :

\vspace{1cm}

\hrule width 8cm height3pt \hfill
\begin{tabbing}
{\bf dec} \= echelon:chaine $\rightarrow$ liste d'instructions \\
    \> echelon(c)$\leftarrow$ $l$; \\
    \> ~~~\= {\bf where} $l$ = \\
    \>    \>~~~ \= {\bf if} $l$ =  $\emptyset$ {\bf then $\leftarrow \emptyset$};\\
    \>    \>    \> {\bf else} \\
    \>    \>    \>~~~\= {\bf let} $l_1$ = ({\tt $\backslash$begincolor\{$couleur$\}} ({\bf car} c) {\tt $\backslash$endcolor}) ; \\
    \>    \>    \>   \> $couleur \leftarrow couleur + increment$ ;\\
    \>    \>    \>   \> {\bf if} $couleur > couleurmax$ {\bf then}\\
    \>    \>    \>   \>~~~\= $couleur \leftarrow couleurmin$ ;\\
    \>    \>    \>   \> {\bf endif} ;\\
    \>    \>    \>   \> $\leftarrow$ ({\bf cons} $l_1$ {\tt echelon} ({\bf cdr} c)) ;\\
    \>    \>    \> {\bf endif}\\
    \> ;;\\

\end{tabbing}
\hrule width 8cm height1pt

\vspace{1cm}

We explain the \TeX\ code in \cite{cc}. We only use PostScript to change the
current colour. In order to produce a code for the macro \verb+echellon+, we
follow exactly the recursive definition in \TeX\ language.

An example of a call to the macro is :

\begin{verbatim}
{% debut de groupe
% rouge = bleu = vert = 20
% maxrouge = maxvert = maxbleu = 210
% chaque caractere change de couleur
\Large\bf
\initcolor{20}{20}{20}{210}{210}{210}{1}
% increrouge = increvert = increbleu = 40
\initinc{40}40{40}
\echelon{exemple}
}% fin du groupe
\end{verbatim}

which leads to :  {% debut de groupe
% rouge = bleu = vert = 20
% maxrouge = maxvert = maxbleu = 210
% chaque caractere change de couleur
\Large\bf\initcolor{20}{20}{20}{210}{210}{210}{1}
% increrouge = increvert = increbleu = 40
\initinc{40}{40}{40}\echelon{exemple}}% fin du groupe

\section{A \TeX\ macro color implemented almost all in PostScript language}

To draw a Bezier curve in a picture environment, we propose a PostScript implementation
with \verb+curveto+ operator. For this purpose, we need 4 points
$(x_0,y_0),(x_1,y_1),(x_2,y_2),(x_3,y_3)$  which define the tangents of the curve of
figure \ref{figone}.

\begin{figure}[th]
\begin{center}
\unitlength=1.00mm
\linethickness{0.4pt}
\begin{picture}(60,50)
\put(0.00,0.00){\framebox(60,50)[t]{}}
% debut de la premiere courbe de bezier (celle de gauche)
\put(0,0){\bezierps [ [ 10 10 15 25 45 45 50 25 ]
 		] \endbezierps{.4}{3}{2}{255}{0}{0}}
\put(10,5){\makebox(0,0)[cc]{$(x_0,y_0)$}}
\put(7,25){\makebox(0,0)[cc]{$(x_1,y_1)$}}
\put(36,45){\makebox(0,0)[cc]{$(x_2,y_2)$}}
\put(50,22){\makebox(0,0)[cc]{$(x_3,y_3)$}}
\put(10,10){\makebox(0,0)[cc]{$\bullet$}}
\put(15,25){\makebox(0,0)[cc]{$\bullet$}}
\put(45,45){\makebox(0,0)[cc]{$\bullet$}}
\put(50,25){\makebox(0,0)[cc]{$\bullet$}}
\put(10,10){\line(1,3){5}}
\put(45,45){\line(1,-4){5}}
\end{picture}
\caption{{\it B\'ezier curve}}
\label{figone}
\end{center}
\end{figure}

Then, a polynomial interpolation is utilized to draw the ``best'' curve between
points $(x_0,y_0),(x_3,y_3)$. The macro is called \verb+Bezierps+ and admits as
parameters a list of points. For \TeX, this list ends when the syntatic
analysor encounters the key word \verb+\endbezierps+ which constitutes the
first parameter of the macro (for explanations about macros with mark out
arguments see \cite{seroul} page 201).

The body of the macro is built with a PostScript abstraction on lists -- the
\verb+forall+ operator-- corresponding to the Lisp function \verb+map+.

In our case, we apply \verb+curveto+ to a finite list of 4 points. This
allows the use of only one \verb+curveto+ order. This construction is more
efficient than a construction with \verb+curveto+ operators for each curve.
The gain in disk space is obvious in the case of multiple curves in the
application.

The drawing of figure \ref{figone} has been realized under \TeX cad on a PC
machine. This software from the EM\TeX\ package doesn't allow for the drawing of
Bezier curves ; it constrains us to mark explicitly the points
$(x_0, y_0)\cdots (x_3, y_3)$, then edit the file and insert coordinantes
as parameters of the macro \verb+Bezierps+. We code:

\begin{verbatim}
\begin{figure}[th]
\begin{center}
\unitlength=1.00mm
\linethickness{0.4pt}
\begin{picture}(60,50)
\put(0.00,0.00){\framebox(60,50)[t]{}}
% debut de la courbe de bezier
\put(0,0){\bezierps [ [ 10 10 15 25 45 45 50 25 ]
 		] \endbezierps{.4}{3}{2}{255}{0}{0}}
\put(10,5){\makebox(0,0)[cc]{$(x_0,y_0)$}}
\put(7,25){\makebox(0,0)[cc]{$(x_1,y_1)$}}
\put(36,45){\makebox(0,0)[cc]{$(x_2,y_2)$}}
\put(50,22){\makebox(0,0)[cc]{$(x_3,y_3)$}}
\put(10,10){\makebox(0,0)[cc]{$\bullet$}}
\put(15,25){\makebox(0,0)[cc]{$\bullet$}}
\put(45,45){\makebox(0,0)[cc]{$\bullet$}}
\put(50,25){\makebox(0,0)[cc]{$\bullet$}}
\put(10,10){\line(1,3){5}}
\put(45,45){\line(1,4){5}}
\end{picture}
\caption{{\it B\'ezier curve}}
\label{figone}
\end{center}
\end{figure}
\end{verbatim}

\section{A colour macro half implemented in \TeX, half in PostScript}

We would like a macro to put text under a vertically or horizontally shaded
background. The text is contained in a \TeX\ box and we use the width and the
height of the box to obtain the required result. For the drawing of coloured
stripes in figure \ref{figtwo}, we need to translate from the rectangular
box center to the point $A$ and we start to draw from this point. We work
with \TeX\ \verb+\special+ order. We also need the current coordinate
system (axes, scale factor\ldots ) in a \verb+\special+ order to change
and restore them to thier appropriate values eventually.

After this series of operations, it is straightforward to put on the
layout the \TeX\ box containing the text under the shaded backgroung



\setbox101=\vbox{\hsize=0.5\textwidth\bf\noindent%
\begincolor{255}{255}{255}%
Dans un article r\'ecent, nous pr\'e\-sen\-tions
les avan\-tages d'utilisation de la couleur dans le
traitement de texte \TeX\  au travers de deux jeux
de macros : Color\TeX rgb et colour\TeX cmyk
\ec
\par}

\horizontaltrue

\begin{figure}[th]
\begin{center}
\unitlength=1.00mm
\linethickness{0.4pt}
\begin{picture}(100,50)
\put(0,0){\framebox(100,50)[t]{}}
\put(50,25){\makebox(0,0)[cc]{
\RectangleDegrade{\copy101}{200}{200}{0}{0}{0}{0}{3}}}
\put(13,43){\makebox(0,0)[cc]{$A$}}
\put(13,15){\makebox(0,0)[cc]{\hbox{$\left\{\vbox to 6mm{}\right.$}}}
\end{picture}
\caption{{\it shaded background}}
\label{figtwo}
\end{center}
\end{figure}

The \TeX\ code is:

\begin{verbatim}
\setbox101=\vbox{\hsize=0.5\textwidth\bf\noindent%
\begincolor{255}{255}{255}%
Dans un article r\'ecent, nous pr\'e\-sen\-tions
les avan\-tages d'utilisation de la couleur dans le
traitement de texte \TeX\  au travers de deux jeux
de macros : Color\TeX rgb et colour\TeX cmyk
\ec
\par}

\horizontaltrue% degrade horizontal

\begin{figure}[th]
\begin{center}
\unitlength=1.00mm
\linethickness{0.4pt}
\begin{picture}(100,50)
\put(0,0){\framebox(100,50)[t]{}}
\put(15,40){\makebox(0,0)[cc]{$A$}}
\put(15,15){\makebox(0,0)[cc]{
   \hbox{$\left\{\vbox to 7mm{}\right.$}}}
\put(50,25){\makebox(0,0)[cc]{
   \RectangleDegrade{\copy101}{200}{200}{0}{0}{0}{0}{3}
}}
\end{picture}
\caption{{\it shaded background}}
\label{figtwo}
\end{center}
\end{figure}
\end{verbatim}

\section{How to draw hachure circles with ``window printing'' ?}

The macro we introduce now realizes the printing of an hachure circle. We have
the possibility to choose the distance between hachures, angle and
shape of hachures (plain stroke or not).

We use the PostScript mechanism of ``clipping'' which consists in the definition
of a closing outline. Thus, we only observe objects belonging to the
interior of the outline. To obtain the required work, we proceed as follow
(see figure \ref{figthree})\ :

\begin{figure}[th]
\begin{center}
\unitlength=1.00mm
\linethickness{0.4pt}
\begin{picture}(74.00,61.00)
\put(0.00,0.00){\framebox(74.00,61.00)[t]{}}
\put(3.97,10.00){\vector(1,0){28.10}}
\put(39.05,10.00){\vector(1,0){30.00}}
\put(6.03,7.94){\vector(0,1){39.05}}
\put(42.06,7.94){\vector(0,1){39.05}}
% debut de la premiere courbe de bezier (celle de gauche)
\put(0,0){\bezierps [ [ 2.06 36.98 6.03 39.00 10.95 39.00
 		14.03 29.02 ] [ 14.03 29.02 19.05 16.06 23.02 16.98 30.00 13.97 ] ]
 		\endbezierps{.4}{3}{2}{255}{0}{0}}
% fin de la premiere courbe de bezier (celle de gauche)
% debut de la 2 courbe de bezier (celle de gauche)
\put(0,0){\bezierps [ [ 33.97 39.00 37.94 39.00 40.00 37.94
		42.06 34.92 ] [ 42.06 34.92 44.92 26.98 50.95 16.98 66.98 12.06 ] ]
		\endbezierps{.4}{3}{2}{255}{0}{0}}
%\put(0,0){\bezierps [ [ 38.06 36.98 42.03 39.00 46.95 39.00
% 		50.03 29.02 ] [ 50.03 29.02 55.05 16.06 59.02 16.98 66.00 13.97 ] ]
% 		\endbezierps{.4}{3}{2}{255}{0}{0}}
% fin de la 2 courbe de bezier (celle de gauche)
\put(42.06,34.92){\circle*{1.90}}
% je place un cercle hachure
\put(6,38){\CercleBidon{4.5}{.5}{.1}{63}{255}{60}{255}{1}}
\put(42,35){\CercleHachure{4.5}{.5}{.1}{63}{255}{60}{255}{1}}
\put(30,6){\makebox(0,0)[cc]{$x$}}
\put(61,6){\makebox(0,0)[cc]{$x$}}
\put(6.03,54){\makebox(0,0)[cc]{$f(x)$}}
\put(42,54){\makebox(0,0)[cc]{$f(x)$}}
\put(10,28.5){\makebox(0,0)[cc]{$B$}}
\put(8,32){\circle*{1.90}}
\put(8,32){\line(1,0){10}}
\put(8,32){\line(1,2){10}}
\put(16,35.5){\makebox(0,0)[cc]{$\alpha$}}
\put(6.03,38.00){\makebox(0,0)[cc]{\bf A}}
\end{picture}
\caption{{\it hachure circle}}
\label{figthree}
\end{center}
\end{figure}

\begin{enumerate}
\item we move to point $B$ from point $A$ with a calculus between cosinus and
sinus of $\alpha$ angle and raduis of circle. Then we change the coordinate
system and we draw lines ;
\item we move to point $A$ and we now draw the circle required and we consider
it as a closing outline ;
\end{enumerate}

For ending this section, we notice we draw smallest square shape inside the
circle and we use the clipping facility of PostScript language. The drawing of
hachures at their exact dimensions is too difficult a task for the
\hbox{programmer !}

\section{Recovery, treatment from a current point}

\TeX\ \verb+\special+ order allows the inclusion of PostScript text. The
syntax \verb+\put(0,0){\special{"+ for \verb+dvips+ driver move to the
left corner of figure \ref{figfour} with ``point'' as a scale unit. If we want
to work in millimeters we have to apply a correct scale factor :

\setbox100=\vbox{\bf\begincolor{255}{255}{255}
\halign{\quad#&\quad#&\quad#\cr
& LRI& \cr
LRI& LRI& LRI\cr
& LRI& \cr}
\endcolor}
% rouge=(255,0,0)  noir=(0,0,0)   nb-nuances=10
\xtrans=30pt% on decale le centre de 50pt vers la droite
\ytrans=30pt% et 50pt vers le haut

\begin{figure}[th]
\begin{center}
\unitlength=1.00mm
\linethickness{0.4pt}
\begin{picture}(80,50)
\put(0,0){\framebox(80,50)[t]{}}
\put(40,25){\makebox(0,0)[cc]{
\CercleGeneral{\copy100}{255}{0}{0}{0}{0}{0}{10}
}}
\end{picture}
\caption{{\it shaded background}}
\label{figfour}
\end{center}
\end{figure}

\begin{verbatim}
% la constante 2.85 = 72 / 25.6
%    ==> pour travailler en mm
\special{" gsave 2.85 2.85 scale
.
% votre code -- unite en mm
.
grestore}
\end{verbatim}

You can now examine the \TeX\ code in more detail. The drawing of a circle
is realized with a PostScript \verb+clip+ operator and we decompose the
problem as follow:

\begin{enumerate}
\item the \TeX\ box number 1 contains the text. Let $r=\max\{ \backslash ht1,
\backslash wd1\}$ ; let $xtrans,ytrans$ be two variables which allow you to shift the
center and let $A(x,y)$ be the point of the center ;
\item we move to point $A'=(x+xtrans,y+ytrans)$ we compute $r'=r+
\sqrt{xtrans^2+ytrans^2}$ ;
\item we draw a circle of raduis $r'$ from point $A'$ and we consider point $A$ as
a closing outline. We reiterate drawings of these smaller and smaller circles
from point $A'$ ;
\end{enumerate}

\begin{verbatim}
\setbox100=\vbox{\bf\begincolor{255}{255}{255}
\halign{\quad#&\quad#&\quad#\cr
& LRI& \cr
LRI& LRI& LRI\cr
& LRI& \cr}
\endcolor}
% rouge=(255,0,0)  noir=(0,0,0)   nb-nuances=10
\xtrans=30pt% on decale le centre de 50pt vers la droite
\ytrans=30pt% et 50pt vers le haut
\begin{figure}[th]
\begin{center}
\unitlength=1.00mm
\linethickness{0.4pt}
\begin{picture}(80,50)
\put(0,0){\framebox(80,50)[t]{}}
\put(40,25){\makebox(0,0)[cc]{% il y a 10 nuances
\CercleGeneral{\copy100}{255}{0}{0}{0}{0}{0}{10}}}
\end{picture}
\caption{{\it shaded background}}
\label{figfour}
\end{center}
\end{figure}
\end{verbatim}

\section{Conclusion}

A programming methodology by means of decomposition in sub-tasks and the use
of PostScript operators offers us a high degree of abstraction and makes up the
basis of our approach handling colour in \TeX\ documents.

This way leads to \TeX\ and PostScript structured programming styles. We
discover on this occasion, dark areas in some PostScript order definitions.
For example, we easily imagine what is the outline of a vectorial letter
--the ``sum'' of the vectors which define it-- but what is the outline of
a bitmap letter ? That is to say, is it possible to recover, in PostScript
language, the outline of such letters ?

We have also given a commentary on operations for allowing the interface
between \TeX\ and PostScript with \verb+\special+ order.

The set of macros and a user guide can be obtained by writting to the author.

\begin{thebibliography}{99}

\bibitem{cc} Christophe C\'erin et Beno\^\i t Lemaire
{\it Vers l'introduction de la couleur dans \TeX},
Cahiers GUTenberg num\'ero 5 -- pages 8-15.

\bibitem{seroul} Raymond Seroul.
{\it Le petit livre de \TeX}, InterEdition -- Mai 1989

\bibitem{ps3}Adobe Systems Incorporated.
{\it PostScript Language Reference Manual}, Addison-Wesley. October 1986.

\bibitem{ps2}Adobe Systems Incorporated.
{\it PostScript Language Tutorial and Cookbook}, Addison-Wesley. March 1987.

\bibitem{ps1}Adobe Systems Incorporated.
{\it PostScript Language Program Design}, Addison-Wesley. February 1988.

\bibitem{mt} Donald E.Knuth. {\it The \TeX book}, Addison-Wesley. 1986.

\bibitem{latex} Leslie Lamport.
{\it \LaTeX\ user's guide \& reference Manual}, Addison-Wesley. 1986

\end{thebibliography}

\end{document}
