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

Last change on this file since 163 was 163, checked in by navarro, 18 years ago

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

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