Fanning Software Consulting

Understanding IDL Colors

To see examples of colorful IDL plots, along with the code that creates them, please visit the Coyote Plot Gallery.

All colors in IDL are specified by means of an RGB triple. That is to say, it takes three numbers to define a color in IDL. These numbers indicate the amount of red, green, and blue to add to a color to make a particular color. Each color can be specified in 256 different "amounts", from no color (value 0) to full color (value 255). It follows, then, that the color palette we work from contains 256 times 256 times 256, or 16.7 million different colors.

There are two different ways we can obtain the RGB triple that makes up a color in IDL. Perhaps the most common way, still, even in the 21st century, is to select 256 of the possible 16.7 colors and load them into a color table. Then, we use an index into the 256 values of the color table to select a particular color. This is called an 8-bit or indexed color system or model.

The indexed color model
The indexed color model uses a color table.
 

IDL reads the color table at that index and obtains the red, green, and blue values that make up that particular color triple (red, green, blue). These values range from 0 (none of the color) to 255 (as much of that color as possible). For example, you can express a red color as the color triple (255, 0, 0). You can express a yellow color (a combination of red and green colors) as the triple (255, 255, 0).

To use a indexed color in IDL, you must first load a color at a particular color index. You do this with the TVLCT command. Suppose you wanted to load an olive green color into color index 100. An olive green color is made up of the color triple (53, 156, 83). You would type this:

  TVLCT, 53, 156, 83, 100

To use the olive green color in a plot, you could type something like this. The Device, Decomposed=0 command at the beginning of the commands below puts you into indexed color mode in IDL. The default in IDL is to be in what we call "decomposed" color mode, which you will learn about in just a moment.

   Device, Decomposed=0
   TVLCT, 53, 156, 83, 100
   cgPlot, cgDemoData(1), Color=100B
   Device, Decomposed=1

You see the results in the figure below.

A plot using indexed color mode.
A plot using indexed color mode.
 

If you took this olive green color triple and turned it into a 1-by-3 array, you could load it with TVLCT even more simply, like this:

   color = REFORM([53,156,83], 1, 3)
   TVLCT, color, 100 
   Device, Decomposed=0
   cgPlot, cgDemoData(1), Color=100B
   Device, Decomposed=1

But, with this kind of code, it is difficult, if not impossible, to tell exactly what color is being used. It would be so much easier if we could ask for the olive green color by name. What do we have to do to do that?

Well, suppose you wanted to be able to ask for five colors by name: charcoal, red, olive green, blue, and yellow. You could create a table of these colors and the color triple values associated with each, like this:

ColorRed ValueGreen ValueBlue Value
Charcoal707070
Red25500
Olive5315683
Blue00255
Yellow2552550

Reading the colors horizontally, you see the color triple that makes up each color. If you read the table vertically, however, you can see how to create three vectors: one containing the red values of the five colors, a second containing the green values, and the third containing the blue values of the five colors. These vectors can be represented in IDL like this:

   rval  = [ 70, 255,  53,   0, 255 ] 
   gval  = [ 70,   0, 186,   0, 255 ]  
   bval  = [ 70,   0,  83, 255,   0 ]

Create another vector, this time of the color names, like this:

   names = ['CHARCOAL', 'RED', 'OLIVE', 'BLUE', 'YELLOW'] 

Now you can use the Where command in IDL to look into the 'names' array for a particular color expressed as a string. The Wherecommand will return an index into the names array where the color was found. You can use this index to subscipt the color vectors for the proper color triple associated with the color you are looking for.

For example, suppose you were looking for the color 'Olive'. You could do this:

   index = Where(StrUpCase(names) EQ 'OLIVE')
   oliveColor = [rval[index], gval[index], bval[index]]  
   oliveColor = Reform(oliveColor , 1, 3) 

Now you are ready to load the color into the color table and use it, like this:

   Device, Decomposed=0
   TVLCT, oliveColor , 100 
   cgPlot, cgDemoData(1), Color=100B
   Device, Decomposed=1

-------

Specifying Colors Directly

If you are using IDL with a 24-bit graphics card, you don't need to use a color lookup table to get a specific color triple. Instead, you can specify the 24-bit color directly as a long integer. The integer is decomposed into three 8-bit portions. The low 8 bits are used for the red color, the middle 8 bits for the green color, and the highest 8 bits for the blue portion of the color triple. This method of specifying colors is called the decomposed color model.

The decomposed color model
In the decomposed color model, colors are expressed directly.
 

Because many of us don't think in bit math, it is sometimes marginally easier to use hexadecimal notation for this color. In hexadecimal notation each 8-bit portion of the number can be represented by a two-digit value. For example, the value 255 is represented at FF. So if you wanted to draw a plot in a olive color on a 24-bit display you might do something like this.

   IDL> color = [53,156,83]
   IDL> Print, color, Format='(z0)'
        35 9c 53
   IDL> Device, Decomposed=1
   IDL> cgPlot, cgDemoData(1), Color='539c35'xL

Why would anyone do such a thing!? In fact, they wouldn't.

But, if they wanted to, I have written an IDL function named cgColor24 that takes any color triple (passed to it as either a column or a row vector) and returns the equivalent long integer that expresses that color. For example, you could take the olive green color above and use it to plot in decomposed color system like this:

   olive = [53,156,83]
   Device, Decomposed=1
   cgPlot, cgDemoData(1), Color=Color24(olive)

A Program for Specifying Colors By Name

All of this is leading to a color program that can translate color names into either the appropriate indexed color or into a 24-bit color value that can be decomposed into the appropriate color. I have written such a program, named cgColor, that can be used in any IDL program to specify colors. The cgColor program has nearly 200 colors that you can specify by name. To see a list of the color names, type this.

   IDL> Print, cgColor(/Names), Format='(5(A14, 2x))'

You can see which colors are available in cgColor, and learn the color names, with the Coyote Library program cgPickColorName.

   IDL> color = cgPickColorName()

You see the available colors in the figure below.

Alternative text.
The cgPickColorName program for interactively
selecting colors.
 

The normal calling sequence for producing device-independent drawing colors with any IDL direct graphics command will look like this. Note that you no longer have to worry about what color mode, indexed or decomposed, you are using, since you will get the same results in either case.

   axisColor = cgColor("Black")
   backColor = cgColor("White")
   dataColor = cgColor("Red")
   oplotColor = cgColor("Dodger Blue")
   data = cgDemoData(17)
   Plot, data, Color=axisColor, Background=backColor, /NoData
   OPlot, data, Color=dataColor
   OPlot, cgDemoData(17), Color=oplotColor

If color decomposition is turned off, then the color is loaded at the specified index and cgColor returns the color index where the color was loaded. If color decomposition is turned on, then no color is loaded into the color table and cgColor returns a 24-bit value that can be decomposed into the right color.

The output of these commands looks like this:

The cgColor program produces the right color in either color mode.
The cgColor program produces the right color in either color mode.
 

Producing such a plot is even easier with Coyote Graphics commands, since the cgColor program is built into all Coyote Graphics commands. This means that you can simply pass the color name into these commands. The same plot above can be produced with Coyote Graphics commands like this.

   cgPlot, cgDemoData(17), Color='red'
   cgOPlot, cgDemoData(17), Color='dodger blue'

If the Triple keyword is set in the cgColor command, the program always returns a color triple. This is useful, for example, if you want to load a color in the color table yourself.

   IDL> TVLCT, cgColor('dodger blue', /Triple), 10

-------

Written 8 January 2006
Last Updated 7 February 2013