How Do Colors Work on a 24-Bit Display

QUESTION: All my plots in IDL come out red. How can I plot with different colors on my 24-bit display?

ANSWER: Quite a lot has changed with how colors work in IDL since this article was first written. If you want to know all about it, please read the “Working with Color” chapter in my latest book, Coyote's Guide to Traditional IDL Graphics. Or, if you just want the short and sweet answer to this question, here it is: Device, Decomposed=0. The other short and sweet answer to this question is to just use Coyote Graphics commands to do your plotting instead of the normal IDL commands. Coyote Graphics commands are color mode and device independent and have as much or more functionality than the graphics commands of the very latest "new" graphics system in IDL. Once you try them, I don't think you will want to use anything else.

IDL is set up to work best on an 8-bit display (capable of displaying up to 256 colors) with what is called the indexed color model. In this model, a color is loaded in a particular location in a hardware color table and a color value is used to "index" that value. So, for example, if you are on an 8-bit display and you want to load the color yellow (which can be thought of as the RGB color triple [255, 255, 0]) into color index 100, you might type something like this:

   TVLCT, [[255], [255], [0]], 100

To use the yellow color to draw a plot, you "index" the color associated with color index 100, by doing something like this:

   PLOT, data, COLOR=100

If you load some other color into color index 100--for example, a blue color:

   TVLCT, [[0], [0], [255]], 100

then the color of the plot changes too.

Using Decomposed 24-Bit Color

On a 24-bit color display (24-bit color is sometimes also called "direct color" or "true color"), the situation is slightly more complicated. By default, on a 24-bit display IDL uses "decomposed" color. That is to say that it treats the color value not as a single index into the color table, but it tries to "decompose" the value into three separate indices into the color table. What this means is that IDL in this mode assumes that the value is a 24-bit value, and "decomposes" that value into three 8-bit values, which it uses to independently index the red, green, and blue locations in the color table. The red index is taken to be the lowest 8 bits, the green index the middle 8 bits, and the blue index is the highest 8 bits of the 24-bit value.

The command:

   PLOT, data, COLOR=100

comes out a shade of red on a 24-bit display using decomposed color, because only the lowest 8 bits of this value are set and they reference a color in the red color vector. That is, the value 100 written as a 24-bit binary number is 00000000 0000000 01100100.

So, for example, if you wanted to represent the color yellow on a 24-bit display you would load the gray-scale color table (so that the red, green, and blue color vectors all have the same values in the same index in a monotonically increasing way) and create a 24-bit number that had the lowest 8 bits and middle 8 bits all set to 1. (Another way to say this is that the lowest 8 bits represent the number or index 255 and the middle 8 bits represent the number or index 255.) That number is 65535 ( or 00000000 11111111 11111111 written in binary notation).

To draw the plot above in yellow on a 24-bit display with decomposed color, you issue these commands:

   LOADCT, 0
   PLOT, data, COLOR=65535

Now, since most of us are not fluent manipulating the bits of a 24-bit number, the number is sometimes expressed in hexadecimal notation, where two digits are enough to set 8 bits at a time (i.e., 256 or 2^8 possible values can be set by two hexadecimal digets). For example, using hexadecimal notation the plot above looks like this:

   PLOT, data, COLOR='00FFFF'x

Note that the examples above only work if the default gray-scale color table is loaded and you are using decomposed color.

If you are not using a gray-scale color table and you are using decomposed color, then the color you get will be the color that is loaded in the color table at the specific red, green, and blue color indices that result from the decomposed value. For example, suppose that I load color table 34. The color from the command above will be a purple color, since in this color table the value loaded at red index 255 is 255. The value at the green index 255 is 5. And the value at the blue index 0 is 255. The triple (255,5,255) is the color purple.

So, what IDL does--and this is not explained this way in the IDL manuals--is decompose the color value into three color indices which it then "translates" using the IDL color table into a color triple. This color triple is then expressed "directly". (This is where the term "direct color" comes from.) This means that this color is no longer tied to the specific color indices. So that if you change the values in the color table (by loading a new color table, for example) the color of the plot will not change the way it does on an 8-bit display. To display the plot in the colors of the new color table, you would have to reissue the Plot command, thereby running the color value though the the decomposition and translation steps once again.

Using Undecomposed 24-Bit Color

On a 24-bit display you do not have to use the default decomposed color model. In fact, you can choose to use undecomposed values for your color values. You select the undecomposed color model by using the Device command, like this:

   Device, Decomposed=0

If the color value is not decomposed, then the value is treated as if it were an 8-bit index into the current color table. That is to say, the same index is used to access the red, green, and blue color vectors in the color table. Thus, these commands will produce a yellow plot on a 24-bit display using undecomposed color:

   TVLCT, [[255], [255], [0]], 100
   Plot, data, Color=100

Be aware, however, that even using undecomposed color that the specific color is still expressed "directly". If you change the values loaded in the color table at index 100 the color of the plot will not change too. You will have to reissue the Plot command to see the new colors take effect.

Displaying Images on 24-Bit Displays

When you display an 8-bit image on a 24-bit display with decomposed color turned on, IDL essentially replicates the byte value of the image for all three color channels. Thus, an 8-bit image is always displayed as gray-scale on a 24-bit display with decomposed color. (This is true on PCs and Macs as of IDL 5.1. Prior to that, an 8-bit image was displayed as if color decomposition were turned off, no matter what the current decomposition setting. This behavior was considered a bug and changed in IDL 5.1 to bring the PC and Macintosh versions of IDL into compliance with the behavior of IDL on UNIX platforms.) To make 8-bit images appear with the color of the current color table, you must turn color decomposition off.

[Return to IDL Programming Tips]