source: trunk/SRC/ToBeReviewed/COULEURS/colorbar.pro @ 114

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 12.4 KB
Line 
1;+
2; NAME:
3;   COLORBAR
4;
5; PURPOSE:
6;       The purpose of this routine is to add a color bar to the current
7;       graphics window.
8;
9; CATEGORY:
10;       Graphics, Widgets.
11;
12; CALLING SEQUENCE:
13;       COLORBAR
14;
15; INPUTS:
16;       None.
17;
18; KEYWORD PARAMETERS:
19;
20;       BOTTOM: The lowest color index of the colors to be loaded in
21;                 the bar.
22;
23;       CB_CHARSIZE: The character size of the color bar annotations. Default is 1.0.
24;
25;       CB_CHARTICK: The character thick of the color bar annotations. Default is 1.0.
26;
27;       CB_COLOR:    The color index of the bar outline and characters. Default
28;                 is ncolors - 1 + bottom.
29;
30;       CB_LOG: to get logarithmic scale for the colorbar
31;
32;       CB_TITLE:    This is title for the color bar. The default is to have
33;                 no title.
34;
35;       DISCRET:  Vecteur contenant les incices des couleurs a tracer en barre
36; de couleur. On obtient ainsi une barre de couleur discrete ne comportant que
37; les couleurs specifiees ds l'ordre ou elles apparaissent ds le vecteur.
38;
39;       DIVISIONS: The number of divisions to divide the bar into. There will
40;                 be (divisions + 1) annotations. The default is 2.
41;
42;       FORMAT:   The format of the bar annotations. Default is '(F6.2)'.
43;
44;       CB_LABEL:    C''est un vecteur qui specifie la valeur des sticks
45;       presents dans la barre de couleur. Il permet qd on utilise
46;       DISCRET d''avoir des couleurs qui ne s''incrementent pas de
47;       facon regulieres.
48;
49;       MAX:      The maximum data value for the bar annotation. Default is
50;                 NCOLORS-1.
51;
52;       MIN:      The minimum data value for the bar annotation. Default is 0.
53;
54;       NCOLORS:  This is the number of colors in the color bar.
55;
56;       NOTITLE:  oblige a ne pas mettre de titre meme si cb_title est declare
57;
58;       POSITION: 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;       PSCOLOR:  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;       RIGHT:    This puts the labels on the right-hand side of a vertical
77;                 color bar. It applies only to vertical color bars.
78;
79;       TOP:      This puts the labels on top of the bar rather than under it.
80;                 The keyword only applies if a horizontal color bar is rendered.
81;
82;       VERTICAL: Setting this keyword give a vertical color bar. The default
83;                 is a horizontal color bar.
84;
85; COMMON BLOCKS:
86;       None.
87;
88; SIDE EFFECTS:
89;       Color bar is drawn in the current graphics window.
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; EXAMPLE:
96;       To display a horizontal color bar above a contour plot, type:
97;
98;       LOADCT, 5, NCOLORS=100
99;       CONTOUR, DIST(31,41), POSITION=[0.15, 0.15, 0.95, 0.75], $
100;          C_COLORS=INDGEN(25)*4, NLEVELS=25
101;       COLORBAR, NCOLORS=100
102;
103; MODIFICATION HISTORY:
104;       Written by: David Fanning, 10 JUNE 96.
105;       10/27/96: Added the ability to send output to PostScript. DWF
106;       11/4/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;       1/27/97: Added the RIGHT and TOP keywords. Also modified the
110;            way the TITLE keyword works. DWF
111;       7/15/97: Fixed a problem some machines have with plots that have
112;            no valid data range in them. DWF
113;       3/3/98:  ajout du keyword discret par
114;                sebastien (smasson@lodyc.jussieu.fr)
115;-
116
117PRO COLORBAR, BOTTOM=bottom, CB_CHARSIZE=cb_charsize, CB_CHARTHICK=cb_charthick $
118              , CB_COLOR=cb_color, $
119              DIVISIONS=divisions, DISCRET=discret,CB_LABEL = cb_label, $
120              FORMAT=format, POSITION=position, MAX=max, MIN=min, NCOLORS=ncolors, $
121              PSCOLOR=pscolor, CB_TITLE=cb_title, VERTICAL=vertical, TOP=top, RIGHT=right, CB_LOG = CB_log, _extra = ex
122                                ; Is the PostScript device selected?
123;
124  compile_opt idl2, strictarrsubs
125;
126
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(discret) THEN begin
192         facteur=256/n_elements(discret)
193         discret=reform(replicate(1,facteur) # discret,facteur*n_elements(discret), /overwrite)
194         bar = REPLICATE(1B,10) # discret
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(discret) THEN begin
199         facteur=256/n_elements(discret)
200         discret=reform(replicate(1,facteur) # discret,facteur*n_elements(discret), /overwrite)
201         bar =  discret # 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(discret) 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      bar = CONGRID(bar, CEIL(xsize), CEIL(ysize), /INTERP)
239      TV, bar, xstart, ystart
240
241   ENDELSE
242
243                                ; Annotate the color bar.
244
245   if keyword_set(cb_label) then begin
246      divisions = n_elements(cb_label)-1
247      for i = 0,divisions DO cb_label = string(cb_label, FORMAT = format)
248      format = ''
249   ENDIF ELSE cb_label = ''
250
251
252   IF KEYWORD_SET(vertical) THEN BEGIN
253
254      IF KEYWORD_SET(right) THEN BEGIN
255
256         PLOT, [min,max], [min,max], /NODATA, XTICKS=1, $
257          YTICKS=divisions, XSTYLE=1, YSTYLE=9, $
258          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize, CHARTHICK=cb_charthick $
259          , /NOERASE, $
260          YTICKFORMAT='(A1)', XTICKFORMAT='(A1)', YTICKLEN=0.1 , $
261          YRANGE=[min, max], YTITLE=cb_title
262
263         AXIS, YAXIS=1, YRANGE=[min, max], YTICKFORMAT=format, YTICKS=divisions, $
264          YTICKLEN=0.1, YSTYLE=1, COLOR=cb_color, CHARTHICK=cb_charthick $
265          , CHARSIZE=cb_charsize, xtickname = cb_label, ylog = cb_log
266
267      ENDIF ELSE BEGIN
268
269         PLOT, [min,max], [min,max], /NODATA, XTICKS=1, $
270          YTICKS=divisions, XSTYLE=1, YSTYLE=9, $
271          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
272          , CHARTHICK=cb_charthick, /NOERASE, $
273          YTICKFORMAT=format, XTICKFORMAT='(A1)', YTICKLEN=0.1 , $
274          YRANGE=[min, max], xtickname = cb_label
275
276         AXIS, YAXIS=1, YRANGE=[min, max], YTICKFORMAT='(A1)', YTICKS=divisions, $
277          YTICKLEN=0.1, YTITLE=cb_title, YSTYLE=1, COLOR=cb_color $
278          , CHARSIZE=cb_charsize, CHARTHICK=cb_charthick, ylog = cb_log
279
280      ENDELSE
281
282   ENDIF ELSE BEGIN
283
284      IF KEYWORD_SET(top) THEN BEGIN
285
286         PLOT, [min,max], [min,max], /NODATA, XTICKS=divisions, $
287          YTICKS=1, XSTYLE=9, YSTYLE=1, $
288          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
289          , CHARTHICK=cb_charthick, /NOERASE, $
290          YTICKFORMAT='(A1)', XTICKFORMAT='(A1)', XTICKLEN=0.1, $
291          XRANGE=[min, max], XTITLE=cb_title
292
293         AXIS, XTICKS=divisions, XSTYLE=1, COLOR=cb_color $
294          , CHARSIZE=cb_charsize, CHARTHICK=cb_charthick, $
295          XTICKFORMAT=format, XTICKLEN=0.1, XRANGE=[min, max], XAXIS=1, xtickname = cb_label, xlog = cb_log
296
297      ENDIF ELSE BEGIN
298
299         PLOT, [min,max], [min,max], /NODATA, XTICKS=divisions, $
300          YTICKS=1, XSTYLE=1, YSTYLE=1, $
301          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
302          , CHARTHICK=cb_charthick, /NOERASE, $
303          YTICKFORMAT='(A1)', XTICKFORMAT=format, XTICKLEN=0.1, $
304          XRANGE=[min, max], TITLE=cb_title, xtickname = cb_label, xlog = cb_log
305
306      ENDELSE
307
308   ENDELSE
309                                ; Restore color variable if changed for PostScript.
310
311   IF postScriptDevice AND (pscolor NE 1) THEN cb_color = oldcolor
312
313   return
314END
Note: See TracBrowser for help on using the repository browser.