source: trunk/SRC/Colors/colorbar.pro

Last change on this file was 495, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo; dupe empty lines; trailing blanks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.5 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 below}
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;-
119PRO colorbar, BOTTOM=bottom, CB_CHARSIZE=cb_charsize $
120            , 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 ncolors = !D.N_COLORS < 256
135   IF N_ELEMENTS(bottom) EQ 0 THEN bottom = 0B
136   IF N_ELEMENTS(cb_charsize) EQ 0 THEN cb_charsize = 1.0
137   IF N_ELEMENTS(cb_charthick) EQ 0 THEN cb_charthick = 1.0
138   IF N_ELEMENTS(format) EQ 0 THEN format = '(F6.2)'
139   IF N_ELEMENTS(cb_color) EQ 0 THEN cb_color = ncolors - 1 + bottom
140   IF N_ELEMENTS(min) EQ 0 THEN min = 0.0
141   IF N_ELEMENTS(max) EQ 0 THEN max = FLOAT(ncolors) - 1
142   IF N_ELEMENTS(divisions) EQ 0 THEN divisions = 2
143   IF N_ELEMENTS(cb_title) EQ 0 THEN cb_title = ''
144   IF N_ELEMENTS(notitle) EQ 1 THEN cb_title = ''
145   pscolor = KEYWORD_SET(pscolor)
146
147   IF KEYWORD_SET(vertical) THEN BEGIN
148      IF KEYWORD_SET(discrete) THEN begin
149         facteur=256/n_elements(discrete)
150         discrete=reform(replicate(1,facteur) # discrete,facteur*n_elements(discrete), /overwrite)
151         bar = REPLICATE(1B,10) # discrete
152      endif else  bar = REPLICATE(1B,10) # BINDGEN(256)
153      IF N_ELEMENTS(position) EQ 0 THEN position = [0.88, 0.15, 0.95, 0.95]
154   ENDIF ELSE BEGIN
155      IF KEYWORD_SET(discrete) THEN begin
156         facteur=256/n_elements(discrete)
157         discrete=reform(replicate(1,facteur) # discrete,facteur*n_elements(discrete), /overwrite)
158         bar =  discrete # REPLICATE(1B,10)
159      endif else bar = BINDGEN(256) # REPLICATE(1B, 10)
160      IF N_ELEMENTS(position) EQ 0 THEN position = [0.15, 0.88, 0.95, 0.95]
161   ENDELSE
162
163; Scale the color bar.
164   IF NOT KEYWORD_SET(discrete) THEN $
165    bar = BYTSCL(bar, TOP=ncolors-1) + bottom
166
167; Get starting locations in DEVICE coordinates.
168
169   xstart = position[0] * !D.X_VSIZE
170   ystart = position[1] * !D.Y_VSIZE
171
172; Get the size of the bar in DEVICE coordinates.
173
174   xsize = (position[2] - position[0]) * !D.X_VSIZE
175   ysize = (position[3] - position[1]) * !D.Y_VSIZE
176
177; For PostScript output only, draw the annotation in !P.COLOR
178; unless "pscolor" is set. This makes better output on grayscale
179; printers.
180
181   IF postScriptDevice AND (pscolor NE 1) THEN BEGIN
182      oldcolor = cb_color
183      cb_color = !P.COLOR
184   ENDIF
185
186; Display the color bar in the window. Sizing is
187; different for PostScript and regular display.
188
189   IF postScriptDevice THEN BEGIN
190
191      TV, bar, xstart, ystart, XSIZE=xsize, YSIZE=ysize
192
193   ENDIF ELSE BEGIN
194
195     IF CEIL(xsize) LT 0 OR CEIL(ysize) LT 0 THEN return
196      bar = CONGRID(bar, CEIL(xsize), CEIL(ysize), /INTERP)
197      TV, bar, xstart, ystart
198
199   ENDELSE
200
201; Annotate the color bar.
202
203   if keyword_set(cb_label) then begin
204      divisions = n_elements(cb_label)-1
205      for i = 0,divisions DO cb_label = string(cb_label, FORMAT = format)
206      format = ''
207   ENDIF ELSE cb_label = ''
208
209   IF KEYWORD_SET(vertical) THEN BEGIN
210
211      IF KEYWORD_SET(right) THEN BEGIN
212
213         PLOT, [min,max], [min,max], /NODATA, XTICKS=1, $
214          YTICKS=divisions, XSTYLE=1, YSTYLE=9, $
215          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize, CHARTHICK=cb_charthick $
216          , /NOERASE, $
217          YTICKFORMAT='(A1)', XTICKFORMAT='(A1)', YTICKLEN=0.1 , $
218          YRANGE=[min, max], YTITLE=cb_title
219
220         AXIS, YAXIS=1, YRANGE=[min, max], YTICKFORMAT=format, YTICKS=divisions, $
221          YTICKLEN=0.1, YSTYLE=1, COLOR=cb_color, CHARTHICK=cb_charthick $
222          , CHARSIZE=cb_charsize, ytickname = cb_label, ylog = cb_log
223
224      ENDIF ELSE BEGIN
225
226         PLOT, [min,max], [min,max], /NODATA, XTICKS=1, $
227          YTICKS=divisions, XSTYLE=1, YSTYLE=9, $
228          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
229          , CHARTHICK=cb_charthick, /NOERASE, $
230          YTICKFORMAT=format, XTICKFORMAT='(A1)', YTICKLEN=0.1 , $
231          YRANGE=[min, max], ytickname = cb_label
232
233         AXIS, YAXIS=1, YRANGE=[min, max], YTICKFORMAT='(A1)', YTICKS=divisions, $
234          YTICKLEN=0.1, YTITLE=cb_title, YSTYLE=1, COLOR=cb_color $
235          , CHARSIZE=cb_charsize, CHARTHICK=cb_charthick, ylog = cb_log
236
237      ENDELSE
238
239   ENDIF ELSE BEGIN
240
241      IF KEYWORD_SET(top) THEN BEGIN
242
243         PLOT, [min,max], [min,max], /NODATA, XTICKS=divisions, $
244          YTICKS=1, XSTYLE=9, YSTYLE=1, $
245          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
246          , CHARTHICK=cb_charthick, /NOERASE, $
247          YTICKFORMAT='(A1)', XTICKFORMAT='(A1)', XTICKLEN=0.1, $
248          XRANGE=[min, max], XTITLE=cb_title
249
250         AXIS, XTICKS=divisions, XSTYLE=1, COLOR=cb_color $
251          , CHARSIZE=cb_charsize, CHARTHICK=cb_charthick, $
252          XTICKFORMAT=format, XTICKLEN=0.1, XRANGE=[min, max], XAXIS=1, xtickname = cb_label, xlog = cb_log
253
254      ENDIF ELSE BEGIN
255
256         PLOT, [min,max], [min,max], /NODATA, XTICKS=divisions, $
257          YTICKS=1, XSTYLE=1, YSTYLE=1, $
258          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
259          , CHARTHICK=cb_charthick, /NOERASE, $
260          YTICKFORMAT='(A1)', XTICKFORMAT=format, XTICKLEN=0.1, $
261          XRANGE=[min, max], TITLE=cb_title, xtickname = cb_label, xlog = cb_log
262
263      ENDELSE
264
265   ENDELSE
266; Restore color variable if changed for PostScript.
267
268   IF postScriptDevice AND (pscolor NE 1) THEN cb_color = oldcolor
269
270   return
271END
Note: See TracBrowser for help on using the repository browser.