CERN Accelerating science

This website is no longer maintained. Its content may be obsolete. Please visit http://home.cern/ for current CERN information.


next up previous
Next: Questions and Answers Up: Text Processing Previous:

LaTeXe's Colour Extensions

  Michel Goossens CN/ASD

Colour systems

Several representations are possible to technically describe colours and one can choose one or the other representation depending on the targetted application domain, for instance additive or substractive colour mixing, colour perception theory, television, artwork, dyes, grey levels. In particular, PS subdivides colour spaces into three categories:

The Additive RGB Colour Space

In RGB space all colours are obtained by the superposition (addition) of three primary components, defined by the CIE as red (700 nm), green (546.1 nm), and blue (435.8 nm). This model is the one of colour computer display screens, where the electrons of the cathode ray tube hit phosphorous elements emitting light of the right wavelength, that combine (add) to give the correct colour

PostScript version

The substractive CMYK Colour Space

The printing industry does not use the RGB primary colours, but rather their complements: cyan, magenta and yellow. This is because inks ``subtract'' their supplementary colours from the white light which falls on the surface, eg. cyan ink absorbs the red component of white light, and thus, in terms of the additive primaries, cyan is white minus red, ie. blue plus green. Similarly, magenta absorbs the green component and corresponds to red plus blue, while yellow, which absorbs blue, is red plus green. In fact, for practical purposes in the printing industry, a process called ``undercolour removal'' takes places. In this procedure a fourth ``colour'', black, is added to the printing process, with an concentration equal to the equal amounts of cyan, magenta and yellow present in the sample. This way one creates a darker black than is possible by mixing the three coloured inks. This colour model is called the CMYK model, where the final ``K'' stands for the black component, whose value is calculated as follows:

As an example how colour printing using the CMYK model works one should consider the following figure.

PostScript version

It shows how adding the various coloured inks allows one to obtain the five Olympic rings at the top and a multi-colour ellipse at the bottom of the colour picture. One starts with applying the cyan ink (top left), and then add the magenta (top right), yellow (bottom left), and finally the black inks (bottom right), to obtain the picture in full colour. The process is shown with the four separate stages, and the cumulative effect.

Symbolic Values of Colour

The significance of colour, like symbols, varies greatly across cultures. The following table shows cultural connotational differences for various often-used colours.

PostScript version

For example, red means danger in the United States, as in most Western cultures, but it is a symbol for life and creativity in India. It is thus important to be aware of these differences when designing a poster, cover of a book or a human interface for a computer program.

To minimize ambiguity, international standard bodies have chosen colours that carry symbolism that are generally understood. In the field of traffic signs, for instance, red is a sign for danger, while green is for health services, or for telling that the road is clear. Blue, white and black are only secondary colours in that context, used for guiding on road surfaces, and for indicating rest areas, etc. Similar standards have been developed for colour codes for electric wires and pipes in plumbing.

When a colour has a generally-recognized meaning it can help to reinforce an idea, and can play an important phychological role in creating an atmosphere or as a cultural, religious, or political message. It should be noted, however, that every human activity, profession, and interest group, even within a same culture, has its own ``colour'' jargon. One should thus always be aware of possible side-effects of using a colour in any given situation.

Colour and Readability

The readability of a message or sign is closely linked to how our visual system processes the information presented to it. Factors which influence the visibility of colours are:

(a)
intensity: pure colours of the spectrum have the highest intensity;
(b)
contrast: between the different colours;
(c)
purity: pure colours are more visible than graded variants, where white is added, making them fainter, or black, making them darker.

The figure below shows some of the most effective colour contrasts, which can be used for maximum readability or visibility, for instance on slides, road signs, or publicity leaflets.

PostScript version

LaTeXe and Colour

LaTeXe provides a generalized driver-independent interface for the definition and use of colour. However, as features are not in the LaTeXe kernel, one has to load the standard supported color extension package to get access to the basic colour functions. The colour models supported by a driver may vary, but typically include:

rgb
Red Green Blue. A comma separated list of three real numbers between 0. and 1., giving the components of the colour.
cmyk
Cyan Magenta Yellow [K]Black. A comma separated list of four real numbers between 0. and 1., giving the components of the colour according to the additive model used in most printers.
gray
Grey scale. A single real number between 0. (black) and 1. (grey).
named
Not really a colour model, but a list of predefined colours that can be accessed by name.

The named colour model is not supported by all drivers. When the model is available then the driver must define the colour equivalent for each name. Users can add their own definitions (with the \definecolor command, described below). In fact, for the dvips driver 64 ``crayola'' colours, as originally proposed by Jim Hafner in his colordvi and foiltex packages, are predefined. Their names are found below.

PostScript version

Note that the named model is merely an example of a colour model which takes names rather than numeric values. Other colour models can be easily defined (like Pantone, X11, HSB, etc.). In each case the relevant code dealing with the new models has to be entered in the driver files.

Supported options

As pointed out above it is the output-device driver's task to paint the colours on the output medium. Therefore the driver must be declared as an option on the \documentclass or \usepackage commands, or specified in the configuration file color.cfg using the command:

 \ExecuteOptions{dvips}

It is possible to turn off the effect of all colour commands by specifying the monochrome option. This can be of interest when previewing.

Three options control the behavior of the named model. By default the named colour model has no pre-declared names. The dvipsnames option predeclares all the names in the colour prologue of dvips. The dvips option automatically implies dvipsnames, unless one explicitly overrules this choice by specifying the nodvipsnames option. Conversely, if with another driver, eg. textures, one wants to use these names then one has to specify the dvipsnames option. Finally, the usenames option makes all names of the named colour model available, as described in section gif.

Defining Colours

The colours black, white, red, green, blue, cyan, magenta, yellow should be predefined by any driver. Packages can define other colours. If you want to define colours yourself, you can use the \definecolor command.

\definecolor{name}{model}{colour specification}

This defines name as a colour which can be used in later colour commands. For example

\DefineColor{Myorange}{cmyk}{0,0.42,1,0}
\definecolor{Myblue}{rgb}{0.8,0.85,1}
\definecolor{Mygrey}{gray}{0.75}
\definecolor{Mygreen}{named}{SpringGreen}
After these definitions, Myorange, Myblue, and Mygrey can now be used in addition to the already predefined colours, while Mygreen is an alias for the named colour SpringGreen.

Using Colours

 

The syntax for colour changes is similar to that of font changes, and comes in two forms, one declarative, and the other with arguments for local changes.

\color[model]{specification}
\textcolor[model]{specification}{text}{text}

Without the optional argument [model] predefined colour names can be used as specification.

Note that \textcolor{name}{text} is equivalent to {\color{name}text}.

For instance, an example using predefined colours is:

{\color{green} Text starts off in green
\textcolor{red}{ a little red}
{\color{blue}nested blue text}
returning to green}

PostScript version

This is equivalent to directly specifying the colour in, e.g., the RBG model, as follows:

{\color[rgb]{0,1,0} Text starts off in green
\textcolor[rgb]{1,0,0}{ a little red}
{\color[rgb]{0,0,1}nested blue text}

PostScript version

Another example, mixing predefined and local colour specifications, is the following list:

\begin{enumerate}
\item \textcolor[rgb]{0, 1, 0}{green rgb} black
\item \color[gray]{0.5}
  \textcolor{blue}{predefined blue} grey text
\end{enumerate}

PostScript version

A more complex example, using the CMYK model to define a colour Ora, for Orange, also shows that the colour is defined when the box is filled with the \sbox command, not when it is typeset (i.e., the colour characteristics are stored with the box, and the surrounding colour does not influence that colour).

\definecolor{Ora}{cmyk}{0, 0.6, 0.8, 0}
\newsavebox{\X}
\sbox{\X}{[framed black text]
 \color{Ora}[framed orange text]}
1. We \usebox{\X} return to black\\ 
2. {\color{Ora} it's orange
\usebox{\X} and once again orange}

PostScript version

Page Colour

\pagecolor{name}

The background colour of the whole page can be set using \pagecolor. This takes the same argument forms as \color but sets the background colour for the current and all subsequent pages. As it is a global declaration, you must use \pagecolor{white} to return to a white background (the default setting).

Coloured box backgrounds

Two commands similar to Lcsframebox produce boxes with the backgrounds shaded with a given colour.

\colorbox{background colour}{text}
\fcolorbox{frame colour}{background colour}{text}

The examples below show how these commands are used.

\colorbox{red}{Black text on red background}

\fcolorbox{blue}{red}%
  {Black text, red background, blue frame}

\fcolorbox{red}{blue}%
  {\color{white} White text,
   blue background, red frame}

PostScript version

A combination of colour definitions and colour boxes can be used as follows:

\definecolor{Brown}{cmyk}{0, 0.8, 1, 0.6}
\definecolor{Yellow}{rgb}{1, 1, 0}
\definecolor{Light}{gray}{.80}
\definecolor{Dark}{gray}{.20}
\colorbox{Brown}{%
  \textcolor{white}{White background}}\qquad
\colorbox{Yellow}{%
  \textcolor{red}{Red background}}\\ 
\colorbox{Light}{%
  \textcolor{Dark}{Light background}}\qquad
\colorbox{Dark}{%
  \textcolor{white}{Dark background}}\\

PostScript version

A few final examples show you how the exact form of the box can be controlled.

\setlength{\fboxrule}{6pt}%
\colorbox{yellow}{Fun with colour}\quad
\fcolorbox{red}{yellow}{Fun with colour} 

\colorbox{green}{Fun with colour}\quad
\setlength{\fboxrule}{0pt}%
\setlength{\fboxsep}{10pt}%
\colorbox{green}{Fun with colour}

PostScript version

A more complex example---a coloured list

Combining the use of PostScript fonts and colour one can construct lists wil colourful elements:

\newenvironment{coldinglist}[2]%
{\begin{list}{\textcolor{#2}{\ding{#1}}}{}}%
{\end{list}}%

\begin{coldinglist}{113}{red}
  \item Farallon's \textcolor{blue}{Replica}
  \begin{coldinglist}{42}{green}
    \item runs only on Windows;
    \item transforms PostScript type 1 fonts
          into bitmaps;
    \item well integrated in the environment.
  \end{coldinglist}
  \item \textcolor{blue}{Common Ground} of No
   Hands Software
   ....

PostScript version

More complicated colour support can be obtained in the framework of the pstricks package, which allows you to produce for instance coloured tables.

Shades of colours

In this section we show some of the subtler aspects. One can trivially make shades of a colour, for instance by varying the red, green, or blue components between 0 and 90%.

%%% Examples need LaTeX's ifthen.sty package
\newcounter{Col}
\setlength{\fboxsep}{2mm}
\newcommand{\CBox}[1]{% vary red component
    \colorbox[rgb]{.#1,0.,0.}{.#1}}
\begin{flushleft}
\makebox[15mm][l]{Red:}%
\whiledo{\value{Col}<10}{\CBox{\theCol}%
                           \stepcounter{Col}}\\ 
\renewcommand{\CBox}[1]{% vary green component
    \colorbox[rgb]{0.,.#1,0.}{.#1}}%
\setcounter{Col}{0}\makebox[15mm][l]{Green:}%
\whiledo{\value{Col}<10}{\CBox{\theCol}%
                           \stepcounter{Col}}\\ 
\renewcommand{\CBox}[1]{% vary blue component
    \colorbox[rgb]{0.,0.,.#1}{.#1}}%
\setcounter{Col}{0}\makebox[15mm][l]{Blue:}%
\whiledo{\value{Col}<10}{\CBox{\theCol}%
                           \stepcounter{Col}}
\end{flushleft}

PostScript version

Adding colour models

 

As LaTeX does not itself know about colour and colour models, one can use any of the supported colour models supported by a given driver. For instance, in the case of dvips, which generates PostScript output, one can specify colours using the HSB (Hue, Saturation, Brightness) model, which is better suited for graphic artists, since colours are given by one number (the hue), while the saturation and brightness components can also easily be varied, independently of the colour. Of course, the driver file (dvips.def, in our case) must contain code to translate from the LaTeX command to the corresponding PostScript.

\begin{flushleft}
\setlength{\fboxsep}{2mm}
\renewcommand{\CBox}[1]{% hue component
    \colorbox[hsb]{.#1,1.,1.}{.#1}}%
\setcounter{Col}{0}\makebox[18mm][l]{Hue:}%
\whiledo{\value{Col}<10}{\CBox{\theCol}%
                               \stepcounter{Col}}\\ 
\renewcommand{\CBox}[1]{% saturation component
    \colorbox[hsb]{.1,.#1,1.}{.#1}}%
\setcounter{Col}{0}\makebox[18mm][l]{Saturation:}%
\whiledo{\value{Col}<10}{\CBox{\theCol}
                               \stepcounter{Col}}\\ 
\renewcommand{\CBox}[1]{% brightness component
    \colorbox[hsb]{1.,1.,.#1}{.#1}}%
\setcounter{Col}{0}\makebox[18mm][l]{Brightness:}%
\whiledo{\value{Col}<10}{\CBox{\theCol}
                               \stepcounter{Col}}
\end{flushleft}


PostScript version


next up previous
Next: Questions and Answers Up: Text Processing Previous:



Michel Goossens
CN Division
Tel. 3363
Tue Nov 28 18:14:41 MET 1995