## Color Tables

There are numerous systems for the measuring and specification of color. Most systems are three-dimensional in nature. For a complete discussion of color systems, refer to Foley and Van Dam (1982, Chapter 17). Parts of this discussion are taken from that chapter.

Most devices capable of displaying color use the RGB (red, green, and blue) color system. Other common color systems include the Munsell, HSV (hue, saturation, and value), HLS (hue, lightness, and saturation), and CMY (cyan, magenta, and yellow) color systems. Algorithms exist to convert colors from one system to another. IDL accepts color specifications in the RGB, HLS, or HSV color systems.

The RGB color system, as implemented in IDL, uses a three-dimensional Cartesian coordinate system with the value of each color ranging from 0 to 255. Each displayable color is a point within this cube. The origin, (0, 0, 0), where each color coordinate is 0, is black. The point at (255, 255, 255) is white and represents an additive mixture of the full intensity of each of the three colors. Points along the main diagonal-where the intensities of each of the three primary colors are equal-are shades of gray. The color yellow is represented by the coordinate (255, 255, 0), or a mixture of 100% red, plus 100% green, and no blue.

Typically, digital display devices represent each component of an RGB color coordinate as an n-bit integer in the range of 0 to 2n -1. Each displayable color is an RGB coordinate triple of n-bit numbers yielding a palette containing 23n total colors. Therefore, for 8-bit colors, each color coordinate can range from 0 to 255, and the total palette contains 224 or 16,777,216 colors.

A display with an m-bit pixel can represent 2m colors simultaneously, given enough pixels. In the case of 8-bit colors, 24-bit pixels are required to represent all colors. The more common case is a display with 8 bits per pixel which allows the display of 28 = 256 colors selected from the much larger palette.

If there are not enough bits in a pixel to represent all colors, m < 23n, a color translation table is used to associate the value of a pixel with a color triple. This table is an array of color triples with an element for each possible pixel value. Given 8-bit pixels, a color table containing 28 = 256 elements is required. The color table element with an index of i specifies the color for pixels with a value of i.

To summarize, given a display with an n-bit color representation and an m-bit pixel, the color translation table, C, is a 2m long array of RGB triples:

Ci = {ri, gi, bi},   0 £ i < 2m

0 £ ri, gi, bi < 2n

Objects containing a value, or color index, of i are displayed with a color of Ci.

The IDL COLOR_CONVERT procedure can be used to convert color triples to and from the RGB color system and the HLS and HSV systems.

You can display TrueColor images on pseudo-color displays by using the COLOR_QUAN function. This function creates a pseudo-color palette for displaying the TrueColor image and then maps the TrueColor image to the new palette. See COLOR_QUAN for more information.

IDL maintains its own internal color table which is read and written by the TVLCT procedure. When this table is modified, it is loaded into the currently selected graphics output device. A call to this procedure has the form:

TVLCT, V1, V2, V3 [, Start]

where the arguments and keywords are as follows:

#### V1, V2, and V3

The vectors containing the intensity or value of each color for each index in the RGB, HLS, or HSV color systems. Standard devices have an 8-bit color representation so the color values should range from 0 to 255. These vectors can contain up to 2m elements (usually 256), assuming the display contains m bit pixels.

#### Start

The starting index in the color translation table into which V1, V2, and V3 are loaded. If not specified, a value of 0 is used, causing the tables to be loaded starting at the first element of the translation vectors. The Start argument can be used to change only part of the color table.

In addition, the following keyword parameters can also be present:

#### GET

Returns the RGB values from the internal color table into the three variables.

#### HLS

Indicates that the parameters specify color using the HLS color system. The plain argument parameters are in the order H-L-S. Hue is expressed in degrees, and the lightness and saturation range from 0 to 1.

#### HSV

Indicates that the parameters specify color using the HSV color system. The plain argument parameters are in the order H-S-V. As above, hue is in degrees, and the saturation and value range from 0 to 1.

### Example

This example creates a graph with the axes drawn in white, then successively adds red, green, blue, and yellow lines. As there are five distinct colors, plus one color for the background, a six-element color table is created. Usually, color index 0 represents black (0, 0, 0). We arbitrarily choose color index 1 to be white (1, 1, 1), 2 as red (1, 0, 0), 3 as green (0, 1, 0), 4 as blue (0, 0, 1), and 5 as yellow (1, 1, 0). The display must have at least 3 bits per pixel to represent six colors simultaneously, and an 8-bit color table is assumed.

```;Specify the red component of each color:
RED = [0, 1, 1, 0, 0, 1]
;Specify the green component of each color:
GREEN = [0, 1, 0, 1, 0, 1]
;Specify the blue component of each color:
BLUE = [0, 1, 0, 0, 1, 0]
;Load the first six elements of the color table:
TVLCT, 255 * RED, 255 * GREEN, 255 * BLUE
;Draw the axes in white, color index 1:
PLOT, COLOR = 1, /NODATA,...
;Draw in red:
OPLOT, COLOR = 2, ...
;Draw in green:
OPLOT, COLOR = 3, ...
;Draw in blue.
OPLOT, COLOR = 4, ...
;Draw in yellow:
OPLOT, COLOR = 5, ...  ...
```

The INDGEN function is handy when creating larger color tables in which each color's intensity can be expressed as a function of its index:

```; Straight line, A[I] = I:
A = INDGEN(256)
; Display image with a linear red scale, disable green and blue:
TVLCT, A, A * 0, A * 0
; Display with linear black and white scale:
TVLCT, A, A, A
; Warm body temperature scale. Red is linear,
; green starts at 128, and blue starts at 192:
TVLCT, A, 2 * (A - 128) > 64, 4 * (A - 192) > 0
```

### Color Table Procedures

The following IDL procedures are used to manipulate color tables:

Load predefined color tables. LOADCT has one parameter: the index of the predefined color table to be loaded. There are 40 pre-defined color tables in the file colors1.tbl, which is supplied with IDL. To obtain a menu listing the available color tables, call LOADCT with no parameters. Standard tables are listed below.

Number
Name
Number
Name
0
Black & White Linear
21
Hue Sat Value 1
1
Blue/White Linear
22
Hue Sat Value 2
2
Green-Red-Blue-White
23
Purple-Red + Stripes
3
Red Temperature
24
Beach
4
Blue-Green-Red-Yellow
25
Mac Style
5
Standard Gamma-II
26
Eos A
6
Prism
27
Eos B
7
Red-Purple
28
Hardcandy
8
Green/White Linear
29
Nature
9
Green/White Exponential
30
Ocean
10
Green-Pink
31
Peppermint
11
Blue-Red
32
Plasma
12
16 Level
33
Blue-Red 2
13
Rainbow
34
Rainbow 2
14
Steps
35
Blue Waves
15
Stern Special
36
Volcano
16
Haze
37
Waves
17
Blue-Pastel-Red
38
Rainbow18
18
Pastels
39
Rainbow + white
19
Hue Sat Lightness 1
40
Rainbow + black
20
Hue Sat Lightness 2

This procedure provides a widget interface to LOADCT. Pre-defined color tables can be loaded and manipulated using this tool. Tables can be stretched and Gamma corrected interactively using this procedure.

#### XPALETTE

This widget procedure allows you to create your own color tables using a set of three sliders. This procedure can interpolate the space between color indices (to create smooth color transitions) or edit individual colors.

#### MODIFYCT

Saves color tables for later use by LOADCT.

#### HSV

Makes and loads color tables based on the HSV color system. A spiral through the single-ended HSV cone is traced. The color representation of pixel values is linearly interpolated from beginning and ending values of hue, saturation, and value.

#### HLS

Makes and loads color tables based on the HLS color system which is based on the Otswald color system. As with the HSV procedure, spirals are interpolated in the three-dimensional color space.

#### PSEUDO

Generates and loads a pseudo-color table based on the LHB (lightness, hue, and brightness) system.

#### STRETCH

Linearly expands the entire range of the last color table loaded to cover a given range of pixel values. STRETCH has two parameters: the pixel value to be displayed with color index 0 and the pixel value to be displayed with the maximum color index:

```STRETCH, LOW, HIGH
```

#### Example

```; Expand the color tables so that pixels in
; the range of 100 to 150 fill the entire color range:
STRETCH, 100, 150
```

To revert to a normal color table, call STRETCH with no parameters.

 Note
The window-oriented procedures will not work without a window system.

### Obtaining the Color Tables

All of the IDL color-table procedures maintain the current color table in a common block called COLORS, defined as follows:

```COMMON COLORS, R_orig, G_orig, B_orig, R_curr, G_curr, B_curr
```

The variables are integer vectors of length equal to the number of color indices. Your program can access these variables by defining the common block. The convention is that routines that modify the current color table should read it from R_orig, G_orig, and B_orig, then load the color table using TVLCT and leave the resulting color table in R_curr, G_curr, and B_curr.

### Color Tables-Switching Between Devices

Use the SET_PLOT procedure to direct the graphics output to different devices. Because devices have differing capabilities and not all are capable of representing the same number of colors, the treatment of color tables when switching devices is somewhat tricky.

After selecting a new graphics output device, SET_PLOT will perform one of the following color-table actions depending upon which keyword parameters are specified:

• The default is to do nothing. The problem with this treatment is that the internal color tables incorrectly reflect the state of the device's color tables until TVLCT is called (usually via LOADCT).
• If the COPY keyword parameter is set, the internal color tables are copied into the device. This is straightforward if both devices have the same number of color indices. If the new device has more colors than the old device, some color indices will be invalid. If the new device has less colors than the old, not all the colors are saved. This is the preferred method if you are displaying graphics and each color index is explicitly loaded.
• When the INTERPOLATE keyword is set, the new device's table is loaded by interpolating the old color table to span the new number of color indices. This method works best when displaying images with continuous color ranges.