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

Last change on this file since 325 was 325, checked in by pinsard, 16 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

  • 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;-
119PRO colorbar, BOTTOM=bottom, CB_CHARSIZE=cb_charsize, CB_CHARTHICK=cb_charthick $
120              , CB_COLOR=cb_color, DIVISIONS=divisions, DISCRETE=discrete $
121              , CB_LABEL = cb_label, FORMAT=format, POSITION=position $
122              , MAX = max, MIN = min, NCOLORS = ncolors, PSCOLOR=pscolor $
123              , CB_TITLE = cb_title, VERTICAL = vertical, TOP = top, RIGHT = right $
124              , CB_LOG = CB_log, NOTITLE = notitle, _EXTRA = ex
125;
126  compile_opt idl2, strictarrsubs
127;
128; Is the PostScript device selected?
129   postScriptDevice = (!D.NAME EQ 'PS')
130
131                                ; Check and define keywords.
132
133   IF N_ELEMENTS(ncolors) EQ 0 THEN BEGIN
134
135; Most display devices to not use the 256 colors available to
136; the PostScript device. This presents a problem when writing
137; general-purpose programs that can be output to the display or
138; to the PostScript device. This problem is especially bothersome
139; if you don't specify the number of colors you are using in the
140; program. One way to work around this problem is to make the
141; default number of colors the same for the display device and for
142; the PostScript device. Then, the colors you see in PostScript are
143; identical to the colors you see on your display. Here is one way to
144; do it.
145
146      IF postScriptDevice THEN BEGIN
147         oldDevice = !D.NAME
148
149; What kind of computer are we using? SET_PLOT to appropriate
150; display device.
151
152         thisOS = !VERSION.OS_FAMILY
153         thisOS = STRMID(thisOS, 0, 3)
154         thisOS = STRUPCASE(thisOS)
155         CASE thisOS of
156            'MAC': SET_PLOT, thisOS
157            'WIN': SET_PLOT, thisOS
158            ELSE: SET_PLOT, 'X'
159         ENDCASE
160         !p.BACKGROUND=(!d.n_colors-1) < 255
161         !p.color=0
162         if !d.n_colors gt 256 then !p.background='ffffff'x
163
164; Open a window (to make sure !D.N_COLORS is accurate).
165
166         WINDOW, /FREE, /PIXMAP, XSIZE=10, YSIZE=10
167         WDELETE, !D.WINDOW
168
169; Here is how many colors we should use.
170
171         ncolors = !D.N_COLORS
172         SET_PLOT, oldDevice
173         IF oldDevice EQ 'X' OR oldDevice EQ 'MAC' OR oldDevice EQ 'WIN' then BEGIN
174            !p.BACKGROUND=(!d.n_colors-1) < 255
175            !p.color=0
176            if !d.n_colors gt 256 then !p.background='ffffff'x
177         ENDIF
178      ENDIF ELSE ncolors = !D.N_COLORS
179   ENDIF
180   IF N_ELEMENTS(bottom) EQ 0 THEN bottom = 0B
181   IF N_ELEMENTS(cb_charsize) EQ 0 THEN cb_charsize = 1.0
182   IF N_ELEMENTS(cb_charthick) EQ 0 THEN cb_charthick = 1.0
183   IF N_ELEMENTS(format) EQ 0 THEN format = '(F6.2)'
184   IF N_ELEMENTS(cb_color) EQ 0 THEN cb_color = ncolors - 1 + bottom
185   IF N_ELEMENTS(min) EQ 0 THEN min = 0.0
186   IF N_ELEMENTS(max) EQ 0 THEN max = FLOAT(ncolors) - 1
187   IF N_ELEMENTS(divisions) EQ 0 THEN divisions = 2
188   IF N_ELEMENTS(cb_title) EQ 0 THEN cb_title = ''
189   IF N_ELEMENTS(notitle) EQ 1 THEN cb_title = ''
190   pscolor = KEYWORD_SET(pscolor)
191
192   IF KEYWORD_SET(vertical) THEN BEGIN
193      IF KEYWORD_SET(discrete) THEN begin
194         facteur=256/n_elements(discrete)
195         discrete=reform(replicate(1,facteur) # discrete,facteur*n_elements(discrete), /overwrite)
196         bar = REPLICATE(1B,10) # discrete
197      endif else  bar = REPLICATE(1B,10) # BINDGEN(256)
198      IF N_ELEMENTS(position) EQ 0 THEN position = [0.88, 0.15, 0.95, 0.95]
199   ENDIF ELSE BEGIN
200      IF KEYWORD_SET(discrete) THEN begin
201         facteur=256/n_elements(discrete)
202         discrete=reform(replicate(1,facteur) # discrete,facteur*n_elements(discrete), /overwrite)
203         bar =  discrete # REPLICATE(1B,10)
204      endif else bar = BINDGEN(256) # REPLICATE(1B, 10)
205      IF N_ELEMENTS(position) EQ 0 THEN position = [0.15, 0.88, 0.95, 0.95]
206   ENDELSE
207
208; Scale the color bar.
209   IF NOT KEYWORD_SET(discrete) THEN $
210    bar = BYTSCL(bar, TOP=ncolors-1) + bottom
211
212; Get starting locations in DEVICE coordinates.
213
214   xstart = position[0] * !D.X_VSIZE
215   ystart = position[1] * !D.Y_VSIZE
216
217; Get the size of the bar in DEVICE coordinates.
218
219   xsize = (position[2] - position[0]) * !D.X_VSIZE
220   ysize = (position[3] - position[1]) * !D.Y_VSIZE
221
222; For PostScript output only, draw the annotation in !P.COLOR
223; unless "pscolor" is set. This makes better output on grayscale
224; printers.
225
226   IF postScriptDevice AND (pscolor NE 1) THEN BEGIN
227      oldcolor = cb_color
228      cb_color = !P.COLOR
229   ENDIF
230
231; Display the color bar in the window. Sizing is
232; different for PostScript and regular display.
233
234   IF postScriptDevice THEN BEGIN
235
236      TV, bar, xstart, ystart, XSIZE=xsize, YSIZE=ysize
237
238   ENDIF ELSE BEGIN
239
240     IF CEIL(xsize) LT 0 OR CEIL(ysize) LT 0 THEN return
241      bar = CONGRID(bar, CEIL(xsize), CEIL(ysize), /INTERP)
242      TV, bar, xstart, ystart
243
244   ENDELSE
245
246; Annotate the color bar.
247
248   if keyword_set(cb_label) then begin
249      divisions = n_elements(cb_label)-1
250      for i = 0,divisions DO cb_label = string(cb_label, FORMAT = format)
251      format = ''
252   ENDIF ELSE cb_label = ''
253
254
255   IF KEYWORD_SET(vertical) THEN BEGIN
256
257      IF KEYWORD_SET(right) THEN BEGIN
258
259         PLOT, [min,max], [min,max], /NODATA, XTICKS=1, $
260          YTICKS=divisions, XSTYLE=1, YSTYLE=9, $
261          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize, CHARTHICK=cb_charthick $
262          , /NOERASE, $
263          YTICKFORMAT='(A1)', XTICKFORMAT='(A1)', YTICKLEN=0.1 , $
264          YRANGE=[min, max], YTITLE=cb_title
265
266         AXIS, YAXIS=1, YRANGE=[min, max], YTICKFORMAT=format, YTICKS=divisions, $
267          YTICKLEN=0.1, YSTYLE=1, COLOR=cb_color, CHARTHICK=cb_charthick $
268          , CHARSIZE=cb_charsize, xtickname = cb_label, ylog = cb_log
269
270      ENDIF ELSE BEGIN
271
272         PLOT, [min,max], [min,max], /NODATA, XTICKS=1, $
273          YTICKS=divisions, XSTYLE=1, YSTYLE=9, $
274          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
275          , CHARTHICK=cb_charthick, /NOERASE, $
276          YTICKFORMAT=format, XTICKFORMAT='(A1)', YTICKLEN=0.1 , $
277          YRANGE=[min, max], xtickname = cb_label
278
279         AXIS, YAXIS=1, YRANGE=[min, max], YTICKFORMAT='(A1)', YTICKS=divisions, $
280          YTICKLEN=0.1, YTITLE=cb_title, YSTYLE=1, COLOR=cb_color $
281          , CHARSIZE=cb_charsize, CHARTHICK=cb_charthick, ylog = cb_log
282
283      ENDELSE
284
285   ENDIF ELSE BEGIN
286
287      IF KEYWORD_SET(top) THEN BEGIN
288
289         PLOT, [min,max], [min,max], /NODATA, XTICKS=divisions, $
290          YTICKS=1, XSTYLE=9, YSTYLE=1, $
291          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
292          , CHARTHICK=cb_charthick, /NOERASE, $
293          YTICKFORMAT='(A1)', XTICKFORMAT='(A1)', XTICKLEN=0.1, $
294          XRANGE=[min, max], XTITLE=cb_title
295
296         AXIS, XTICKS=divisions, XSTYLE=1, COLOR=cb_color $
297          , CHARSIZE=cb_charsize, CHARTHICK=cb_charthick, $
298          XTICKFORMAT=format, XTICKLEN=0.1, XRANGE=[min, max], XAXIS=1, xtickname = cb_label, xlog = cb_log
299
300      ENDIF ELSE BEGIN
301
302         PLOT, [min,max], [min,max], /NODATA, XTICKS=divisions, $
303          YTICKS=1, XSTYLE=1, YSTYLE=1, $
304          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
305          , CHARTHICK=cb_charthick, /NOERASE, $
306          YTICKFORMAT='(A1)', XTICKFORMAT=format, XTICKLEN=0.1, $
307          XRANGE=[min, max], TITLE=cb_title, xtickname = cb_label, xlog = cb_log
308
309      ENDELSE
310
311   ENDELSE
312; Restore color variable if changed for PostScript.
313
314   IF postScriptDevice AND (pscolor NE 1) THEN cb_color = oldcolor
315
316   return
317END
Note: See TracBrowser for help on using the repository browser.