Discrete Color Bar

QUESTION: I've used your cgColorbar program in the past, and I like it. But it seems more useful for images that have a continuous color range. The images I'm working with now only use a handful of colors to represent various properties in the image. The colors I use are often significantly different from each other. Do you have a good way to represent these colors in a color bar? I'm particularly interested in being able to center the labels either under the color representation or adjacent to it.

ANSWER: I have written an IDL program, named cgDCBar, for "discrete color bar", that does exactly this. It is to be used with a handful, say 5 to 20, colors and allows you to label each color individually. You can display this discrete color bar horizontally (the default) or vertically. Keywords allow you rotate the labels, if needed, to keep them from crowding up on one another.

The cgDCBar program is part of a suite of programs for working with color in IDL. And to some extent, the cgDCBar program depends on these other color programs. If you haven't yet done so, it would be a good idea to download and install the entire Coyote Library. In general, it is best to work in 24-bit color with these tools, since then they don't have to load colors in the system color table and possibly leave you wondering why your color table changed. But, if necessary, you can work with them in an 8-bit environment, too, and this includes the PostScript device. You should see identical colors on all devices, no matter that color state your graphics device is in. (That is to say, whether or not you have turned color decomposition on or off with the Decomposed keyword to the Device command.)

There are a number of keywords that can be used to control properties of the color bar, but I'll give you some examples here so you can see how it works. You can learn more by reading the documentation header in the program file itself.

Let's start by opening a window, so we have something to draw in.

   IDL> cgDisplay, 500, 350    IDL> SetDecomposedState, 1
   IDL> cgErase, 'ivory' 

It is possible to specify the colors to be used in the color bar in several different ways with the program. One way they can be specified is in the identical way they are specified with cgColorbar, by loading the colors in a contiguous region of the color table, and telling the program where to find them with the combination of NColors and Bottom keywords.

   IDL> cgLoadCt, 25, /BREWER, NCOLORS=12, BOTTOM=1
   IDL> cgDCBar, NCOLORS=12, BOTTOM=1, COLOR='navy', $
            LABELS=cgMonths(/Abbreviation) 
A discrete color bar.
A discrete color bar, specifying colors in the manner of cgColorbar.
 

If you like, you can use longer labels and rotate them so they fit on the display.

    IDL> cgLoadCt, 25, /BREWER, NCOLORS=12, BOTTOM=1
   IDL> cgDCBar, NCOLORS=12, BOTTOM=1, COLOR='navy', $
            LABELS=theMonths(), ROTATE=45 
A discrete color bar.
A discrete color bar with rotated labels.
 

Another way to specify colors is to pass a vector of color names to the program.

   IDL> colors = ['indian red', 'dodger blue', 'tan', 'purple', 'forest green']
   IDL> labels = ['Snow', 'Water', 'Desert', 'Steepe', 'Forest']
   IDL> cgDCBar, colors, COLOR='navy', LABELS=labels, TITLE='Land Key' 
A discrete color bar.
A discrete color bar with colors specified as color names.
 

You can also specify colors as color indices into the current color table.

    IDL> LoadCT, 5    IDL> colors = [40, 77, 115, 176, 210]
   IDL> labels = ['Snow', 'Water', 'Desert', 'Steepe', 'Forest']
   IDL> cgDCBar, colors, COLOR='navy', LABELS=labels, TITLE='Land Key' 
A discrete color bar.
A discrete color bar with colors specified as color indices.
 

Or, you can specify the colors as 24-bit values that can be decomposed into the proper RGB triple.

   IDL> colors = ['B48246'xL, '7280FA'xL, '578B2E'xL, '13458B'xL, 'A3526A'xL]
   IDL> labels = ['Snow', 'Water', 'Desert', 'Steepe', 'Forest']
   IDL> cgDCBar, colors, COLOR='navy', LABELS=labels, TITLE='Land Key' 
A discrete color bar.
A discrete color bar with colors specified as 24-bit color integers.
 

It is also possible to create vertical color bars.

   IDL> colors = ['indian red', 'dodger blue', 'tan', 'purple', 'forest green']
   IDL> labels = ['Snow', 'Water', 'Desert', 'Steepe', 'Forest']
   IDL> cgDCBar, colors, COLOR='navy', LABELS=labels, TITLE='Land Key', $
	   ROTATE=-45, /VERTICAL
   IDL> cgDCBar, colors, COLOR='navy', LABELS=labels, TITLE='Land Key', $
           ROTATE=45, /VERTICAL, POSITION=[0.15, 0.1, 0.20, 0.9]  
A discrete color bar.
Two discrete color bars, positioned vertically.
 

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

Google
 
Web Coyote's Guide to IDL Programming