How IDL Selects Colors

QUESTION: Every time I start IDL I seem to have a different number of colors in my IDL session. How does IDL determine how many colors it is going to use in each IDL session?

ANSWER: This answer applies to IDL running on an 8-bit (256 color) display with the X Window Manager. I gave this answer in reply to a similar question asked on the IDL newsgroup 22 February 1997.

By default IDL requests all the colors that remain in the shared color map when IDL is started. How many colors this is depends upon what has happened before IDL starts up. The window system has always allocated some colors from the shared color map. This is why IDL will never have 256 colors on an 8-bit display if it starts in this mode. But other applications might have requested colors from the shared color map as well. Your word processor, for example, probably needs a few colors for its windows. Netscape, if it is not configured properly, can take so many colors from the shared color map that it will be difficult to get IDL to run properly at all. I'm told Netscape can be made to use fewer colors if it is invoked with the "-ncols" option, like this:

   % netscape -ncols 32 &

(The new Common Desktop Environment that we are seeing on a lot of new workstations can be a real color hog. By default it is configured to take a huge number of colors to put up an absolutely beautiful splash screen. But if you want to do some work with your computer after you oough and aaugh at the splash screen, I suggest you turn some colors off. This is a configurable item in the beautiful desktop bar at the bottom of the display.)

Programs that load color tables (LoadCT, XLoadCT) take color vectors that are 256-elements in length and automatically resample them to fit into the number of colors you are actually using in your IDL session, so that it looks like you are using the same color tables from one session to the next. (But, of course, a red pixel in today's session could indeed have a different value than the red pixel in yesterday's session, depending upon how many colors you had in your session yesterday and today. Don't allow this to effect your image interpretations!) You can determine how many colors IDL is using by examining the system variable !D.N_COLORS after you have opened an IDL graphics window.

Forcing IDL to Use a Certain Number of Colors

You can force IDL to use a certain number of colors in the IDL session. Here is how to do it.

The number of colors in the IDL session is selected when the first IDL graphics window is opened. Once the window is opened, the number of colors in that IDL session cannot be changed. Most of us open that first window in what I like to call an "unconscious" way. That is, we issue a Plot command, or some other IDL graphics command that opens a display window for us. If you open a window this way, IDL gets all of the colors that remain in the shared color table. That number is, indeed, variable.

If you open the window in a "conscious" way with the WINDOW command, you will have more control over the number of colors you have in your IDL session. For example, suppose you want to have 200 colors in your IDL session. You can open that first window like this:

   WINDOW, Colors=200

If there are 200 colors available in the shared color map, IDL will allocate 200 colors from that map. If there are not 200 colors available in the shared color map, the window manager will create a private color map for the IDL graphics window with exactly 200 colors in them. In either case, you will be guaranteed to have 200 colors in your IDL session.

If you want 256 colors in your IDL session, then you can, of course, open the first graphics window like this:

   WINDOW, Colors=256

But on 8-bit displays there are never 256 colors available in the shared color map for IDL to allocate (the window manager has already allocated a few before IDL is invoked). So this command always creates a private color map for the IDL graphics windows.

Color Flashing Problem

The problem, often, with private color maps is that they sometimes manifest themselves as a "color flashing problem". The window manager is responsible for knowing which window you are working in. (Either because your cursor is in it or because you selected it with the mouse.) It is also responsible for knowing which color table is assciated with that window. If you move your cursor from a window associated with a shared color map to one associated with a private color map, the window manager must load new colors into the hardware color table so that the colors in the new active window are correct.

If you have an IDL session with 256 colors and a private color map, then you will have to move your cursor into a graphics window (or have a graphics window as an active window) to see the colors correctly. But when you put your cursor into a graphics window quite often what will happen is that all your display windows will turn black. In fact, you may have a hard time finding them again! This is because the lower colors in most IDL color tables are dark colors. These happened to be the same colors that in the shared color map were used by the window manager for window colors. This "flashing" you see has to do with the way window managers work, not with the way IDL works.

Leaving Colors for Other Applications

Sometimes you want IDL to use the shared color table, but you also want to leave some colors in the shared color table for other applications to use. (Maybe you want to start up Netscape after you start up IDL.) You can do this with the WINDOW command by using a negative number with the Colors keyword. For example, this command will cause IDL to take all of the colors in the shared color table except for 20:

   WINDOW, Colors=-20

Twenty colors will now be available in the shared color table for other applications to use.

Google
 
Web Coyote's Guide to IDL Programming