%
% \iffalse
%
%% color.dtx Copyright (C) 1994 David Carlisle
%
%<package>\NeedsTeXFormat{LaTeX2e}[1994/05/13]
%<package>\ProvidesPackage{color}
%<package>         [1994/05/14 v0.2e Standard LaTeX Color (DPC)]
%
%<*driver>
\documentclass{ltxdoc}
\usepackage[dvips]{color}
\GetFileInfo{color.sty}
\begin{document}
 \title{The \textcolor{red}{color} package\thanks{This file
        has version number \fileversion, last
        revised \filedate.}}
 \author{D. P. Carlisle}
 \date{\filedate}
 \maketitle
 \DocInput{color.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{430}
%
% \definecolor{myred}{named}{Red}
% \definecolor{myblue}{rgb}{0, 0, 1}
%
% \makeatletter
% ^^A In poor taste, but it's only a demo!
% \renewcommand\section{%
%    \@startsection {section}{1}{\z@}{-3.5ex \@plus -1ex \@minus -.2ex}%
%           {2.3ex \@plus.2ex}{\reset@font\Large\bfseries\color{myred}}}
% \makeatother
%
% \title{\color{myred}The implementation of the 
%                 \textcolor{myblue}{\LaTeX} Colour interface.}
%
% \StopEventually{}
%
%
% \section{Options}
%
% \changes{v0.2a}{1994/04/13}
%     {TEMP KLUDGE for Feb 94 release of 2e: 
%      Add extra groups}
% \changes{v0.2d}{1994/05/13}
%     {All support for Feb 1994 release removed (so used optional arg
%     of \cs{NeedsTeXFormat}).}
%
% Initially most of the options just access the `reference' driver
% files. The |dvips| option is more or less like |ref2| except that all
% of the colours defined in |color.pro| are declared. The |monochrome|
% option is more or less |ref0| but also sets the flag |\ifcolors@| to
% false. The |genericPS| option is based on |ref3|, but modifies some
% standard \LaTeX\ commands to compensate for some of the deficiencies
% of the |ref3| driver file.
%    \begin{macrocode}
%<*package>
\DeclareOption{ref0}{\input{ref0.col}}
\DeclareOption{ref1}{\input{ref1.col}}
\DeclareOption{ref2}{\input{ref2.col}}
\DeclareOption{ref2b}{\input{ref2b.col}}
\DeclareOption{ref3}{\input{ref3.col}}
\DeclareOption{monochrome}{\input{ref0.col}\colors@false}
\DeclareOption{literalPS}{%
  \input{ref3.col}%
  \let\@ldc@l@r\color
  \def\color{\if@inlabel\leavevmode\fi\@ldc@l@r}%
  \let\@ldtextc@l@r\textcolor
  \def\textcolor{\if@inlabel\leavevmode\fi\@ldtextc@l@r}%
  \let\@lduseb@x\usebox
  \def\usebox##1{\@lduseb@x{##1}\set@color}}
%    \end{macrocode}
%
% Now the options for supported drivers.
%    \begin{macrocode}
\DeclareOption{dvips}{\input{dvips.def}}
\DeclareOption{dvgt}{\input{dvgt.def}}
\DeclareOption{dvi2ps}{\input{dvi2ps.def}}
\DeclareOption{dvialw}{\input{dvialw.def}}
\DeclareOption{dvilaser}{\input{dvilaser.def}}
\DeclareOption{dvipsone}{\input{dvipsone.def}}
\DeclareOption{dvitops}{\input{dvitops.def}}
\DeclareOption{dviwindo}{\input{dviwindo.def}}
\DeclareOption{dvitps}{\input{dvitps.def}}
\DeclareOption{emtex}{\input{emtex.def}}
\DeclareOption{dviwin}{\input{dviwin.def}}
\DeclareOption{oztex}{\input{oztex.def}}
\DeclareOption{psprint}{\input{psprint.def}}
\DeclareOption{pubps}{\input{pubps.def}}
\DeclareOption{textures}{\input{textures.def}}
%    \end{macrocode}
%
% A local configuration file may declare more options.
%    \begin{macrocode}
\InputIfFileExists{color.cfg}{}{}
%    \end{macrocode}
%
% \section{Using Colours}
%
% \subsection{Declarative form}
%
%  \begin{macro}{\color}
% |\color{|\emph{declared-colour}|}| switches to the colour
% \emph{declared-colour}, which must previously have been defined
% using |\definecolor|. This colour will stay in effect until the end of
% the current \TeX\ group.
%
% |\color[|\emph{model}|]{|\emph{colour-specification}|}| is similar
% to the above, but uses a colour not declared by |\definecolor|. The
% allowed \emph{model}'s, vary depending on the driver. The syntax of
% the \emph{colour-specification} argument depends on the model.
%
%    \begin{macrocode}
\def\color{%
  \@ifnextchar[\@undeclaredcolor\@declaredcolor}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@undeclaredcolor}
% \changes{v0.2c}{1994/05/09}
%     {Improve error messages}
%    \begin{macrocode}
\def\@undeclaredcolor[#1]#2{%
  \expandafter\ifx\csname color@#1\endcsname\relax
    \@latexerr{Undefined color model. `#1'}\@ehd
  \else
    \csname color@#1\endcsname{#2}%
  \fi
  \ignorespaces}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@declaredcolor}%
% \changes{v0.1c}{1994/04/01}
%     {Add \cs{ignorespaces}}
% \changes{v0.1e}{1994/05/14}
%     {Minor change to error message}
%    \begin{macrocode}
\def\@declaredcolor#1{%
  \@ifundefined{\string\color @#1}%
    {\@latexerr{Undefined color `#1'}\@ehd}%
    {\csname\string\color @#1\endcsname}%
  \ignorespaces}
%    \end{macrocode}
%  \end{macro}
%
% \subsection{Command (Argument) Form}
%
%  \begin{macro}{\textcolor}
% |\textcolor{|\emph{declared-colour}|}{|\emph{text}|}| and
% |\textcolor[|\emph{model}|]{|\emph{colour-spec}|}{|\emph{text}|}|
% are just alternative syntax for |\color|, in
% which the groups are added implicitly. Thus \emph{text} appears in the
% specified colour, but then the colour reverts to its previous value.
% The naming is by analogy with |\textrm| (as opposed to |\rm| and
% |\rmfamily|) although it is slightly a misnomer as the command also
% works in math-mode.
%
%    \begin{macrocode}
\def\textcolor{%
  \@ifnextchar[\text@ud@color\text@d@color}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\text@ud@color}
%  \begin{macro}{\text@d@color}
%    \begin{macrocode}
\long\def\text@ud@color[#1]#2#3{{\@undeclaredcolor[#1]{#2}#3}}
\long\def\text@d@color#1#2{{\@declaredcolor{#1}#2}}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
% \subsection{Background (Page) Colour}
%
% \begin{macro}{\pagecolor}
% |\pagecolor|, which has the same argument forms as |\color|, specifies
% the background colour for the current, and all following, pages. It is
% a global declaration which does not respect \TeX\ groups.
%
% \changes{v0.1c}{1994/04/01}
%     {end the group even if the colour command generates an error}
%    \begin{macrocode}
\def\pagecolor{%
  \begingroup
      \let\ignorespaces\endgroup
      \def\set@color{\set@page@color}%
      \@ifnextchar[\@undeclaredcolor\@declaredcolor}
%    \end{macrocode}
%  \end{macro}
%
% \section{Defining Colours}
%
%  \begin{macro}{\definecolor}
% |\definecolor{|\emph{name}|}{|\emph{model}|}{|\emph{colour-spec}|}|
% defines the color \emph{name}, which may then be used in subsequent
% |\color| or |\textcolor| commands to refer to a colour specified by
% \emph{colour-spec} in the colour model \emph{model}.
%
% Depending on the driver it may be implemented in two ways:
% \begin{itemize}
% \item |\definecolor| may call a |\special| which \emph{defines} a
% color name. Then |\color{|\emph{name}|}| access a named colour model.
% \item |\definecolor| may associate the \emph{name} the  colour
% \emph{model} So |\color{|\emph{name}|}| would check \emph{name} but
% then issue a |\special| for the colour model \emph{model}.
% \end{itemize}
% The first alternative may lead to problems with a PostScript
% Dictionary overflow, and is not used in any of the driver files
% supplied with this package.
%
% |\definecolor| just calls an internal macro that defines the colour
% for a particular model. This macro must have been defined by the
% driver file that supports the requested \emph{model}.
%
% \changes{v0.1b}{1994/03/15}
%     {change name from \cmd\newcolor to \cmd\definecolor}
% \changes{v0.1e}{1994/05/14}
%     {Minor change to error message}
%    \begin{macrocode}
\def\definecolor#1#2#3{%
  \@ifundefined{definecolor@#2}%
    {\@latexerr{Undefined color model `#2'}\@ehd}%
    {\csname definecolor@#2\endcsname{#1}{#3}}}
%    \end{macrocode}
%  \end{macro}
%
%
% \section{Colour Switch}
%
%  \begin{macro}{\ifcolor@}
% This boolean can be tested by higher level macros that may want to
% alter their behaviour if a monochrome driver is being used.
%    \begin{macrocode}
\newif\ifcolors@
\colors@true
%    \end{macrocode}
%  \end{macro}

%
% \section{Processing Options}
%
%    \begin{macrocode}
\ProcessOptions
%    \end{macrocode}
%
% \section{Default Colour}
%
% \begin{macro}{\normalcolor}
% Early versions of this style redefined |\reset@font| to reset the
% color as well. Current versions do not do this (there are too many
% |\reset@font| commands hidden in strange places) and so defines a
% separte command, |\normalcolor| to reset the colour to the colour in
% effect at the start of the document.
%
% |\normalcolor| is defined (to |\relax|) in the \LaTeX\ kernel, so it
% is safe to use this in macros that may possibly be used in
% conjunction with colour. It will have no effect until the |color|
% package is also loaded.
% \changes{v0.2b}{1994/04/18}
%     {Only reset the colour with \cmd{\reset@font} not 
%      \cmd{\normalfont}}
% \changes{v0.2d}{1994/05/13}
%     {Stop changing \cs{reset@font}. Use the new \cs{normalfont}
%     instead.}
%    \begin{macrocode}
\def\normalcolor{\let\current@color\default@color\set@color}
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\default@color}
% Internal macro to store the `default' colour used by |\normalcolor|.
%    \begin{macrocode}
\AtBeginDocument{\let\default@color\current@color}
%    \end{macrocode}
% \end{macro}
%
% |\current@color| contains an internal representation of the colour
% at this point in the document. (This can only be an approximation to
% the truth as the `macro layer' of \TeX\ does not know where the output
% routine is going to re-insert floats. This is why drivers must
% maintain their own stack of colours in order to fully support these
% commands. 
%
% For dvips, the |\current@color| it is something like `|Black|' or
% `|rgb 0 1 0|', but other packages should not rely on any particular
% format for this macro.
%
% The driver file \emph{must} initialise |\current@color| to a
% specification for Black. This initialisation can not occur here, as
% the possible colour models (and thus the syntax for `black')
% are not known at this point.
%
% \section{Higher Level Commands}
% 
% With the basic colour primitives specified above we may define a few
% higer level commands for coloured boxes etc. This is still quite a low
% level and presumably packages and classes making use of colour will
% define more appropriate document-level commands.
%
% \subsection{Colour Block}
%
% \begin{macro}{\color@block}
% |\color@block{|\emph{width}|}{|\emph{height}|}{|\emph{depth}|}|\\
% Should take up no space for \TeX, but produce a block in the current
% colour of the specified size. It is mainly used for producing box
% backgrounds. 
%
% The definition here works by selecting a colour, and then drawing a
% \TeX\ rule (unless |\ifcolors@false|). This allows the `driver
% independent' colour specials to be used. However it is defined using
% |\providecommand|, so that this file will not over-write any other
% definition of this command. A graphics package may want to define it
% using a special to produce (for example) a PostScript line. Producing
% the line in the |\special| has the advantage that on a preview that
% does not understand |\special|s, the line is automatically omited,
% without needing to modify the source of the document (for instance by
% adding the |monochrome| option).
%
%    \begin{macrocode}
\def\color@block#1#2#3{%
  {\set@color\rlap{\ifcolors@\vrule\@width#1\@height#2\@depth#3\fi}}}
%    \end{macrocode}
% \end{macro}
%
% \subsection[Coloured Boxes]{Coloured Boxes\footnote
% {The four commands defined here ought to share code, but not today!}}
%
% \begin{macro}{\colorbox}
% |\colorbox| takes the same argument forms as |\textcolor|, but the
% colour specifies the \emph{background} colour of the box.
%
%    \begin{macrocode}
\def\colorbox{\leavevmode
  \@ifnextchar[\box@ud@color\box@d@color}
%    \end{macrocode}
%
%    \begin{macrocode}
\long\def\box@ud@color[#1]#2#3{%
 \setbox\z@\hbox{\kern\fboxsep{#3}\kern\fboxsep}%
 \dimen@\ht\z@\advance\dimen@\fboxsep\ht\z@\dimen@
 \dimen@\dp\z@\advance\dimen@\fboxsep\dp\z@\dimen@
 {\@undeclaredcolor[#1]{#2}\color@block{\wd\z@}{\ht\z@}{\dp\z@}}%
 \box\z@}
%    \end{macrocode}
%
%    \begin{macrocode}
\long\def\box@d@color#1#2{%
 \setbox\z@\hbox{\kern\fboxsep{#2}\kern\fboxsep}%
 \dimen@\ht\z@\advance\dimen@\fboxsep\ht\z@\dimen@
 \dimen@\dp\z@\advance\dimen@\fboxsep\dp\z@\dimen@
 {\@declaredcolor{#1}\color@block{\wd\z@}{\ht\z@}{\dp\z@}}%
 \box\z@}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fcolorbox}
% |\fcolorbox| has an extra \emph{colour-spec} argument, and puts a
% frame of the first colour around a box with a background specified by
% the second colour. If an optional argument is given, it specifies the
% colour model for both colours.
%
%    \begin{macrocode}
\def\fcolorbox{\leavevmode
  \@ifnextchar[\fbox@ud@color\fbox@d@color}
%    \end{macrocode}
%
% \changes{v0.1c}{1994/04/01}
%     {Move \cs{color@block} inside the inner box}
%    \begin{macrocode}
\long\def\fbox@ud@color[#1]#2#3#4{%
 \setbox\z@\hbox{\kern\fboxsep{\set@color#4}\kern\fboxsep}%
 \dimen@\ht\z@\advance\dimen@\fboxsep\ht\z@\dimen@
 \dimen@\dp\z@\advance\dimen@\fboxsep\dp\z@\dimen@
 {\fboxsep\z@\@undeclaredcolor[#1]{#2}%
    \fbox{%
      {\@undeclaredcolor[#1]{#3}\color@block{\wd\z@}{\ht\z@}{\dp\z@}}%
      \box\z@}}}
%    \end{macrocode}
%
% \changes{v0.1c}{1994/04/01}
%     {Move \cs{color@block} inside the inner box}
%    \begin{macrocode}
\long\def\fbox@d@color#1#2#3{%
 \setbox\z@\hbox{\kern\fboxsep{\set@color#3}\kern\fboxsep}%
 \dimen@\ht\z@\advance\dimen@\fboxsep\ht\z@\dimen@
 \dimen@\dp\z@\advance\dimen@\fboxsep\dp\z@\dimen@
 {\fboxsep\z@\@declaredcolor{#1}%
    \fbox{%
      {\@declaredcolor{#2}\color@block{\wd\z@}{\ht\z@}{\dp\z@}}%
      \box\z@}}}
%    \end{macrocode}
% \end{macro}
%
% \section{Extra Groups}
% Turning on extra groups in the standard \LaTeX\ commands, so that
% colour commands are scoped corectly.
%
% Like |\normalcolor|, the following three commands are defined in the
% kernel, with empty definitions (|\relax|). This means that they can
% be used to make macros in packages `colour safe'. The commands
% will not have any effect unless a user also uses this colour
% package, when the `active definitions' here will take effect. and
% keep colour commands correctly scoped.
%
% \begin{macro}{\color@setgroup}
% This is to be used in contexts (eg `lrbox') where text is to be
% saved and used after some other, unknown, text that may contain
% colour commands. A matching |\color@endgroup| should be used at the
% end of the text.
% \changes{v0.2d}{1994/05/13}
%     {Macro added}
%    \begin{macrocode}
\def\color@setgroup{\begingroup\set@color}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\color@begingroup}
% This is to be used at the start of contexts that may contain colour
% commands, but where it is not necessary to save the current colour.
% Examples of this are in the box commands of |ltboxes.dtx| where
% user-supplied text is saved internally in a box between
% |\color@begingroup|, |\color@endgroup|, but the box is used before
% any other colour commands could intervene. A matching
% |\color@endgroup| should be used at the  end of the text.
% \changes{v0.2d}{1994/05/13}
%     {New definition, was previously like \cs{color@setgroup}}
%    \begin{macrocode}
\let\color@begingroup\begingroup
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\color@endgroup}
% To be used to close the `group' started by one of the above two
% commands. The |\endgraf| in its definition is required in the case
% of groups of text in vertical `par' mode, but doesn't do any harm in
% horizontal `LR' contexts.
% \changes{v0.2d}{1994/05/13}
%     {New definition, was previously just \cs{endroup}}
%    \begin{macrocode}
\def\color@endgroup{\endgraf\endgroup}
%    \end{macrocode}
%  \end{macro}
%
% \section{Remaining changes}
%
% \changes{v0.2d}{1994/05/13}
%     {tabbing support removed (now in kernel)}
%
% \begin{macro}{\underbar}
%    \begin{macrocode}
\def\underbar#1{%
  $\setbox\z@\hbox{\color@begingroup#1\color@endgroup}%
  \dp\z@\z@\m@th\underline{\box\z@}$}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\makeph@nt}
%    \begin{macrocode}
\def\makeph@nt#1{%
  \setbox\z@\hbox{\color@begingroup#1\color@endgroup}\finph@nt}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\makepsm@sh}
%    \begin{macrocode}
\def\makesm@sh#1{%
  \setbox\z@\hbox{\color@begingroup#1\color@endgroup}\finsm@sh}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\showhyphens}
%    \begin{macrocode}
\def\showhyphens#1{\setbox0\vbox{%
  \color@begingroup
  \parfillskip\z@skip\hsize\maxdimen
  \reset@font
  \pretolerance\m@ne\tolerance\m@ne\hbadness0\showboxdepth0\ %
    #1%
  \color@endgroup}}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\@savemarparbox}
% \changes{v0.2b}{1994/04/18}
%     {Keep the \cmd{\special} out of the \cmd{\vbox}}
%    \begin{macrocode}
\long\def\@savemarbox#1#2{%
  \global\sbox#1{\vtop{\hsize\marginparwidth
   \@parboxrestore #2}}}
%    \end{macrocode}
% \end{macro}
%
% \section{Predefining Colours}
%
% As long as the driver file has defined sufficient colour models, we
% define a few colours, just to get people started.
%
% \begin{macro}{black}
% \begin{macro}{white}
% Black and white `colours'.
%    \begin{macrocode}
\ifx\definecolor@gray\@undefined
  \ifx\definecolor@rgb\@undefined
  \else
    \definecolor@rgb{black}{0,0,0}
    \definecolor@rgb{white}{1,1,1}
  \fi
\else
  \definecolor@gray{black}{0}
  \definecolor@gray{white}{1}
\fi
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{red}
% \begin{macro}{green}
% \begin{macro}{blue}
% Additive primaries.
%    \begin{macrocode}
\ifx\definecolor@rgb\@undefined\else
  \definecolor@rgb{red}{1,0,0}
  \definecolor@rgb{green}{0,1,0}
  \definecolor@rgb{blue}{0,0,1}
\fi
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{cyan}
% \begin{macro}{magenta}
% \begin{macro}{yellow}
% Subtractive primaries.
%    \begin{macrocode}
\ifx\definecolor@cmyk\@undefined\else
  \definecolor@cmyk{cyan}{1,0,0,0}
  \definecolor@cmyk{magenta}{0,1,0,0}
  \definecolor@cmyk{yellow}{0,0,1,0}
\fi
%</package>
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \Finale
%
