Specifying cgCOLORBAR Colors
QUESTION: I find your cgCOLORBAR program extremely useful. But it only works for me when I use all the colors in the color table. If I want to use specific colors in the color table, I can't get it to work. Why not?
ANSWER: Almost inevitably if people are having trouble with this, they are trying to use non-contiguous color indices for their drawing colors. I see this often, for example, when people are trying to create filled contour plots. They will load a color table and sample colors out of that color table for their contour fill colors. Their code will be some variation of this:
LoadCT, 33 ; Load a color table. step = 15 ; Step through the color table 15 indices at a time. contour_colors = step * (Indgen(16) + 1)
Now their contour colors are represented by these indices into the color table:
IDL> Print, contour_colors 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225 240
Now, they wish cgColorBar to reflect these colors, so they set it up like this:
cgColorbar, NColors=16, Divisions=16
But this doesn't work. In fact, cgColorbar uses only the first 16 indices in the currently loaded color table. How can you get cgColorbar to use the color indices you used for the Contour plot?
cgColorbar is designed to work with colors that are contiguous in the color table. (Why? Mostly because that is how I always work with colors.) So if you wanted to use these same 16 colors, and have cgColorbar reflect these colors, you would have to load them into the color table in a contiguous region of the color table. You could load them into the color table, starting at color index 1, for example, by doing something like this:
LoadCT, 33 ; Load a color table. step = 15 ; Step through the color table 15 indices at a time. contour_colors = step * (Indgen(16) + 1) ; Get the color table vectors. TVLCT, r, g, b, /Get ; Load contour colors contiguously, starting at index 1. TVLCT, r[contour_colors], g[contour_colors], b[contour_colors], 1
Now you use these colors in cgColorbar like this:
cgColorbar, NColors=16, Divisions=16, Bottom=1
The colors in the color bar now reflect the colors you are using in your contour plot.
In fact, rather than stepping through the color table as above, you can do the exact same thing with LOADCT directly:
LoadCT, 33, NColors=16, Bottom=1 cgColorbar, NColors=16, Divisions=16, Bottom=1, XTicklen=1, XMinor=0
Of course, this only works if you are using a 24-bit display, so that you can draw your contour plot with a color table that is independent of the color table used to draw the color bar. If this is not the kind of display you have, then you will have to load your colors contiguously for both your contour plot and your color bar. This is easily done just by re-defining your contour color indices to point to the contiguous color region of the color table, too.
data = cgDemoData(2) LoadCT, 33, NColors=16, Bottom=1 cgColorbar, NColors=16, Divisions=16, Bottom=1, XTicklen=1, XMinor=0, /Window cgContour, data, C_Colors=Indgen(16)+1, /Fill, NLevels=16, $ Position = [0.1, 0.1, 0.9, 0.75], /NoErase, /AddCmd
Last Updated 8 January 2006