source: trunk/SRC/Colors/colorbar.pro @ 318

Last change on this file since 318 was 318, checked in by smasson, 16 years ago

header cleaning

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.1 KB
Line 
1;+
2;
3; @file_comments
4; The purpose of this routine is to add a color bar to the current
5; graphics window.
6;
7; @categories
8; Graphics
9;
10; @keyword BOTTOM {default=0B}
11; The lowest color index of the colors to be loaded in the bar.
12;
13; @keyword CB_CHARSIZE {default=1.0}
14; The character size of the color bar annotations.
15;
16; @keyword CB_CHARTHICK {default=1.0}
17; The character thick of the color bar annotations.
18;
19; @keyword CB_COLOR {default=ncolors - 1 + bottom}
20; The color index of the bar outline and characters.
21;
22; @keyword CB_LOG
23; to get logarithmic scale for the colorbar
24;
25; @keyword CB_TITLE {default=''}
26; This is title for the color bar.
27;
28; @keyword DISCRETE
29; Vector which contain color's indexes to trace in a color bar. Therefore
30; we obtain a discrete colorbar which contains only the specified colors
31; in the order they appear in the vector
32;
33; @keyword DIVISIONS {default=2}
34; The number of divisions to divide the bar into.
35; There will be (divisions + 1) annotations.
36;
37; @keyword FORMAT {default='(F6.2)'}
38; The format of the bar annotations.
39;
40; @keyword CB_LABEL {type=vector}
41; A vector to specify sticks values of the color bar. It allows,
42; when we use DISCRETE, to have colors that don't increase by
43; increments in a regular way.
44;
45; @keyword MAX {default=NCOLORS - 1}
46; The maximum data value for the bar annotation.
47;
48; @keyword MIN {default=0}
49; The minimum data value for the bar annotation.
50;
51; @keyword NCOLORS {default=!D.N_COLORS}
52; This is the number of colors in the color bar.
53;
54; @keyword NOTITLE
55; Force to don't write title even if CB_TITLE is declared.
56;
57; @keyword POSITION {default=see bellow}
58; A four-element array of normalized coordinates in the same
59; form as the POSITION keyword on a plot. Default is
60; [0.88, 0.15, 0.95, 0.95] for a vertical bar and
61; [0.15, 0.88, 0.95, 0.95] for a horizontal bar.
62;
63; @keyword PSCOLOR
64; This keyword is only applied if the output is being sent to
65; a Postscript file. It indicates that the Postscript device
66; is configured for color output. If this keyword is set, then
67; the annotation is drawn in the color specified by the COLOR
68; keyword. If the keyword is not set, the annotation is drawn
69; in the color specified by the !P.COLOR system variable
70; (usually this will be the color black). In general, this
71; gives better looking output on non-color or gray-scale
72; printers. If you are not specifically setting the annotation
73; color (with the COLOR keyword), it will probably
74; be better NOT to set this keyword either, even if you
75; are outputting to a color Postscript printer.
76;
77; @keyword RIGHT
78; This puts the labels on the right-hand side of a vertical
79; color bar. It applies only to vertical color bars.
80;
81; @keyword TOP
82; This puts the labels on top of the bar rather than under it.
83; The keyword only applies if a horizontal color bar is rendered.
84;
85; @keyword VERTICAL {default=0 -> horizontal color bar}
86; Setting this keyword give a vertical color bar.
87;
88; @keyword _EXTRA
89; pass any keyword to colorbar
90;
91; @restrictions
92; The number of colors available on the display device (not the
93; Postscript device) is used unless the NCOLORS keyword is used.
94;
95; @examples
96; To display a horizontal color bar above a contour plot, type:
97;
98; IDL> LOADCT, 5, NCOLORS=100
99; IDL> CONTOUR, DIST(31,41), POSITION=[0.15, 0.15, 0.95, 0.75], $
100; IDL> C_COLORS=INDGEN(25)*4, NLEVELS=25
101; IDL> COLORBAR, NCOLORS=100
102;
103; @history
104; Written by: David Fanning, 10 JUNE 96.
105; 10/27/96: Added the ability to send output to PostScript. DWF
106; 11/04/96: Substantially rewritten to go to screen or PostScript
107;           file without having to know much about the PostScript device
108;           or even what the current graphics device is. DWF
109; 01/27/97: Added the RIGHT and TOP keywords. Also modified the
110;           way the TITLE keyword works. DWF
111; 07/15/97: Fixed a problem some machines have with plots that have
112;           no valid data range in them. DWF
113; 03/03/98: Add DISCRETE keyword, sebastien masson (smasson\@lodyc.jussieu.fr)
114;
115; @version
116; $Id$
117;
118;-
119;
120PRO colorbar, BOTTOM=bottom, CB_CHARSIZE=cb_charsize, CB_CHARTHICK=cb_charthick $
121              , CB_COLOR=cb_color, DIVISIONS=divisions, DISCRETE=discrete $
122              , CB_LABEL = cb_label, FORMAT=format, POSITION=position $
123              , MAX = max, MIN = min, NCOLORS = ncolors, PSCOLOR=pscolor $
124              , CB_TITLE = cb_title, VERTICAL = vertical, TOP = top, RIGHT = right $
125              , CB_LOG = CB_log, NOTITLE = notitle, _EXTRA = ex
126;
127  compile_opt idl2, strictarrsubs
128;
129; Is the PostScript device selected?
130   postScriptDevice = (!D.NAME EQ 'PS')
131
132                                ; Check and define keywords.
133
134   IF N_ELEMENTS(ncolors) EQ 0 THEN BEGIN
135
136; Most display devices to not use the 256 colors available to
137; the PostScript device. This presents a problem when writing
138; general-purpose programs that can be output to the display or
139; to the PostScript device. This problem is especially bothersome
140; if you don't specify the number of colors you are using in the
141; program. One way to work around this problem is to make the
142; default number of colors the same for the display device and for
143; the PostScript device. Then, the colors you see in PostScript are
144; identical to the colors you see on your display. Here is one way to
145; do it.
146
147      IF postScriptDevice THEN BEGIN
148         oldDevice = !D.NAME
149
150; What kind of computer are we using? SET_PLOT to appropriate
151; display device.
152
153         thisOS = !VERSION.OS_FAMILY
154         thisOS = STRMID(thisOS, 0, 3)
155         thisOS = STRUPCASE(thisOS)
156         CASE thisOS of
157            'MAC': SET_PLOT, thisOS
158            'WIN': SET_PLOT, thisOS
159            ELSE: SET_PLOT, 'X'
160         ENDCASE
161         !p.BACKGROUND=(!d.n_colors-1) < 255
162         !p.color=0
163         if !d.n_colors gt 256 then !p.background='ffffff'x
164
165; Open a window (to make sure !D.N_COLORS is accurate).
166
167         WINDOW, /FREE, /PIXMAP, XSIZE=10, YSIZE=10
168         WDELETE, !D.WINDOW
169
170; Here is how many colors we should use.
171
172         ncolors = !D.N_COLORS
173         SET_PLOT, oldDevice
174         IF oldDevice EQ 'X' OR oldDevice EQ 'MAC' OR oldDevice EQ 'WIN' then BEGIN
175            !p.BACKGROUND=(!d.n_colors-1) < 255
176            !p.color=0
177            if !d.n_colors gt 256 then !p.background='ffffff'x
178         ENDIF
179      ENDIF ELSE ncolors = !D.N_COLORS
180   ENDIF
181   IF N_ELEMENTS(bottom) EQ 0 THEN bottom = 0B
182   IF N_ELEMENTS(cb_charsize) EQ 0 THEN cb_charsize = 1.0
183   IF N_ELEMENTS(cb_charthick) EQ 0 THEN cb_charthick = 1.0
184   IF N_ELEMENTS(format) EQ 0 THEN format = '(F6.2)'
185   IF N_ELEMENTS(cb_color) EQ 0 THEN cb_color = ncolors - 1 + bottom
186   IF N_ELEMENTS(min) EQ 0 THEN min = 0.0
187   IF N_ELEMENTS(max) EQ 0 THEN max = FLOAT(ncolors) - 1
188   IF N_ELEMENTS(divisions) EQ 0 THEN divisions = 2
189   IF N_ELEMENTS(cb_title) EQ 0 THEN cb_title = ''
190   IF N_ELEMENTS(notitle) EQ 1 THEN cb_title = ''
191   pscolor = KEYWORD_SET(pscolor)
192
193   IF KEYWORD_SET(vertical) THEN BEGIN
194      IF KEYWORD_SET(discrete) THEN begin
195         facteur=256/n_elements(discrete)
196         discrete=reform(replicate(1,facteur) # discrete,facteur*n_elements(discrete), /overwrite)
197         bar = REPLICATE(1B,10) # discrete
198      endif else  bar = REPLICATE(1B,10) # BINDGEN(256)
199      IF N_ELEMENTS(position) EQ 0 THEN position = [0.88, 0.15, 0.95, 0.95]
200   ENDIF ELSE BEGIN
201      IF KEYWORD_SET(discrete) THEN begin
202         facteur=256/n_elements(discrete)
203         discrete=reform(replicate(1,facteur) # discrete,facteur*n_elements(discrete), /overwrite)
204         bar =  discrete # REPLICATE(1B,10)
205      endif else bar = BINDGEN(256) # REPLICATE(1B, 10)
206      IF N_ELEMENTS(position) EQ 0 THEN position = [0.15, 0.88, 0.95, 0.95]
207   ENDELSE
208
209; Scale the color bar.
210   IF NOT KEYWORD_SET(discrete) THEN $
211    bar = BYTSCL(bar, TOP=ncolors-1) + bottom
212
213; Get starting locations in DEVICE coordinates.
214
215   xstart = position[0] * !D.X_VSIZE
216   ystart = position[1] * !D.Y_VSIZE
217
218; Get the size of the bar in DEVICE coordinates.
219
220   xsize = (position[2] - position[0]) * !D.X_VSIZE
221   ysize = (position[3] - position[1]) * !D.Y_VSIZE
222
223; For PostScript output only, draw the annotation in !P.COLOR
224; unless "pscolor" is set. This makes better output on grayscale
225; printers.
226
227   IF postScriptDevice AND (pscolor NE 1) THEN BEGIN
228      oldcolor = cb_color
229      cb_color = !P.COLOR
230   ENDIF
231
232; Display the color bar in the window. Sizing is
233; different for PostScript and regular display.
234
235   IF postScriptDevice THEN BEGIN
236
237      TV, bar, xstart, ystart, XSIZE=xsize, YSIZE=ysize
238
239   ENDIF ELSE BEGIN
240
241     IF CEIL(xsize) LT 0 OR CEIL(ysize) LT 0 THEN return
242      bar = CONGRID(bar, CEIL(xsize), CEIL(ysize), /INTERP)
243      TV, bar, xstart, ystart
244
245   ENDELSE
246
247; Annotate the color bar.
248
249   if keyword_set(cb_label) then begin
250      divisions = n_elements(cb_label)-1
251      for i = 0,divisions DO cb_label = string(cb_label, FORMAT = format)
252      format = ''
253   ENDIF ELSE cb_label = ''
254
255
256   IF KEYWORD_SET(vertical) THEN BEGIN
257
258      IF KEYWORD_SET(right) THEN BEGIN
259
260         PLOT, [min,max], [min,max], /NODATA, XTICKS=1, $
261          YTICKS=divisions, XSTYLE=1, YSTYLE=9, $
262          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize, CHARTHICK=cb_charthick $
263          , /NOERASE, $
264          YTICKFORMAT='(A1)', XTICKFORMAT='(A1)', YTICKLEN=0.1 , $
265          YRANGE=[min, max], YTITLE=cb_title
266
267         AXIS, YAXIS=1, YRANGE=[min, max], YTICKFORMAT=format, YTICKS=divisions, $
268          YTICKLEN=0.1, YSTYLE=1, COLOR=cb_color, CHARTHICK=cb_charthick $
269          , CHARSIZE=cb_charsize, xtickname = cb_label, ylog = cb_log
270
271      ENDIF ELSE BEGIN
272
273         PLOT, [min,max], [min,max], /NODATA, XTICKS=1, $
274          YTICKS=divisions, XSTYLE=1, YSTYLE=9, $
275          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
276          , CHARTHICK=cb_charthick, /NOERASE, $
277          YTICKFORMAT=format, XTICKFORMAT='(A1)', YTICKLEN=0.1 , $
278          YRANGE=[min, max], xtickname = cb_label
279
280         AXIS, YAXIS=1, YRANGE=[min, max], YTICKFORMAT='(A1)', YTICKS=divisions, $
281          YTICKLEN=0.1, YTITLE=cb_title, YSTYLE=1, COLOR=cb_color $
282          , CHARSIZE=cb_charsize, CHARTHICK=cb_charthick, ylog = cb_log
283
284      ENDELSE
285
286   ENDIF ELSE BEGIN
287
288      IF KEYWORD_SET(top) THEN BEGIN
289
290         PLOT, [min,max], [min,max], /NODATA, XTICKS=divisions, $
291          YTICKS=1, XSTYLE=9, YSTYLE=1, $
292          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
293          , CHARTHICK=cb_charthick, /NOERASE, $
294          YTICKFORMAT='(A1)', XTICKFORMAT='(A1)', XTICKLEN=0.1, $
295          XRANGE=[min, max], XTITLE=cb_title
296
297         AXIS, XTICKS=divisions, XSTYLE=1, COLOR=cb_color $
298          , CHARSIZE=cb_charsize, CHARTHICK=cb_charthick, $
299          XTICKFORMAT=format, XTICKLEN=0.1, XRANGE=[min, max], XAXIS=1, xtickname = cb_label, xlog = cb_log
300
301      ENDIF ELSE BEGIN
302
303         PLOT, [min,max], [min,max], /NODATA, XTICKS=divisions, $
304          YTICKS=1, XSTYLE=1, YSTYLE=1, $
305          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
306          , CHARTHICK=cb_charthick, /NOERASE, $
307          YTICKFORMAT='(A1)', XTICKFORMAT=format, XTICKLEN=0.1, $
308          XRANGE=[min, max], TITLE=cb_title, xtickname = cb_label, xlog = cb_log
309
310      ENDELSE
311
312   ENDELSE
313; Restore color variable if changed for PostScript.
314
315   IF postScriptDevice AND (pscolor NE 1) THEN cb_color = oldcolor
316
317   return
318END
Note: See TracBrowser for help on using the repository browser.