Fanning Software Consulting

Graphics Windows With White Backgrounds

QUESTION: I notice that most IDL graphics windows have black backgrounds with the graphics draw in white. This makes it extremely difficult to write IDL programs that work both on the display and and in a PostScript device. What is the best way to reverse these colors, so that I draw in black on a white background?

ANSWER: The short answer to this question is to simply use Coyote Graphics routines to draw your graphics output. These routines have many advantages, but one of them is that they draw their graphics in windows with white backgrounds. For example, if you want white background windows, create your graphics windows with cgDisplay, rather than with the Window command, and erase graphics windows with cgErase, rather than with the Erase command.

Failing that, there are a couple of things you definitely should not do to draw black graphics output in windows with white backgrounds. Don't, for example, simply reverse the color table.

   TVLCT, rgb, /Get
   TVLCT, Reverse(rgb,1)

And don't reverse the system variables !P.Color and !P.Background.

   temp = !P.Color
   !P.Color = !P.Background
   !P.Background = temp

Both of these methods will work for the display, but will not work in a PostScript file. (Well, they might work for the display, depending on whether you are using indexed color or decomposed color in your IDL session.) In a PostScript file, with either of these methods, you will be drawing white graphics on a white background. It will be pretty hard to see!

If you want to use drawing colors that work both on the display and in a PostScript file, do not load those drawing colors at either the top or bottom of the color table. In other words, don't load them in the positions pointed to by the typical values of !P.Color and !P.Background if you are using indexed color mode.

If you are using indexed color, you might try doing something like this.

   TVLCT, 255, 255, 255, 254 ; White color
   TVLCT, 0, 0, 0, 253       ; Black color
   !P.Color = 253
   !P.Background = 254

Now your graphics will be displayed in black on white backgrounds in any device that uses indexed color mode.

If you are using decomposed color mode, then you can do this.

   !P.Color = '000000'xL
   !P.Background = 'FFFFFF'xL

And you will be golden as long as you use decomposed color mode in your output devices. (The PostScript device will have to be configured to use decomposed color, and this is available only in IDL 7.1.1 and later.)

If you have no idea what color mode you are using (and don't care to learn anything more about it!), then you should use cgColor to set your graphics colors, like this.

   Plot, Indgen(11), Color=cgColor('black'), Background=cgColor('white')

This will work both on your display and in your PostScript file, whether you are set up to work in indexed or decomposed color mode. This is essentially how Coyote Graphics routines work, although since these are the default colors, you don't have to specify them.

   cgPlot, Indgen(11), /Window

The cgColor program does not load colors into the color table unless it has to, and it never loads colors at color index 0 or 255. To learn what color names you can use with cgColor, you can use the interactive Coyote Library program, cgPickColorName.

   color = cgPickColorName()

White Backgrounds on Map Projections

If you want a white background for a map projection and you are using Map_Set as your first graphics command, then you have to do a little more singing and dancing, since the Map_Set command does not have a Background keyword. In this case, you want to erase the window with a white color, then use the NoErase keyword on the Map_Set command. Make sure, however, that you use a black drawing color with Map_Set, or it will draw white figures on your white background!

   Erase, Color=cgColor('white')
   Map_Set, /Cylindrical, /Continents, Color=cgColor('black'), /Countries, /NoErase
   Map_Grid, LineStyle=2, Color=cgColor('black')

You can also use Coyote Graphics map projection routines to set up your map projection space. These routines work exactly like other Coyote Graphics routines.

   cgErase
   cgMap_Set, /Cylindrical, /Continents, /Countries, /NoErase
   cgMap_Grid, LineStyle=2

Version of IDL used to prepare this article: IDL 8.1.

Written: 5 October 2011