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

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

small bugfix

  • 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 Graphics, Widgets.
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
40; A vector to specify sticks values of the color bar. It allowes,
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; @restrictions
88; The number of colors available on the display device (not the
89; Postscript device) is used unless the NCOLORS keyword is used.
90;
91; @examples
92; To display a horizontal color bar above a contour plot, type:
93;
94; IDL> LOADCT, 5, NCOLORS=100
95; IDL> CONTOUR, DIST(31,41), POSITION=[0.15, 0.15, 0.95, 0.75], $
96; IDL> C_COLORS=INDGEN(25)*4, NLEVELS=25
97; IDL> COLORBAR, NCOLORS=100
98;
99; @history
100; Written by: David Fanning, 10 JUNE 96.
101; 10/27/96: Added the ability to send output to PostScript. DWF
102; 11/04/96: Substantially rewritten to go to screen or PostScript
103;           file without having to know much about the PostScript device
104;           or even what the current graphics device is. DWF
105; 01/27/97: Added the RIGHT and TOP keywords. Also modified the
106;           way the TITLE keyword works. DWF
107; 07/15/97: Fixed a problem some machines have with plots that have
108;           no valid data range in them. DWF
109; 03/03/98: Add DISCRETE keyword, sebastien masson (smasson\@lodyc.jussieu.fr)
110;
111; @version $Id$
112;
113;-
114PRO COLORBAR, BOTTOM=bottom, CB_CHARSIZE=cb_charsize, CB_CHARTHICK=cb_charthick $
115              , CB_COLOR=cb_color, DIVISIONS=divisions, DISCRETE=discrete $
116              , CB_LABEL = cb_label, FORMAT=format, POSITION=position $
117              , MAX = max, MIN = min, NCOLORS = ncolors, PSCOLOR=pscolor $
118              , CB_TITLE = cb_title, VERTICAL = vertical, TOP = top, RIGHT = right $
119              , CB_LOG = CB_log, NOTITLE = notitle, _extra = ex
120;
121  compile_opt idl2, strictarrsubs
122;
123; Is the PostScript device selected?
124   postScriptDevice = (!D.NAME EQ 'PS')
125
126                                ; Check and define keywords.
127
128   IF N_ELEMENTS(ncolors) EQ 0 THEN BEGIN
129
130; Most display devices to not use the 256 colors available to
131; the PostScript device. This presents a problem when writing
132; general-purpose programs that can be output to the display or
133; to the PostScript device. This problem is especially bothersome
134; if you don't specify the number of colors you are using in the
135; program. One way to work around this problem is to make the
136; default number of colors the same for the display device and for
137; the PostScript device. Then, the colors you see in PostScript are
138; identical to the colors you see on your display. Here is one way to
139; do it.
140
141      IF postScriptDevice THEN BEGIN
142         oldDevice = !D.NAME
143
144; What kind of computer are we using? SET_PLOT to appropriate
145; display device.
146
147         thisOS = !VERSION.OS_FAMILY
148         thisOS = STRMID(thisOS, 0, 3)
149         thisOS = STRUPCASE(thisOS)
150         CASE thisOS of
151            'MAC': SET_PLOT, thisOS
152            'WIN': SET_PLOT, thisOS
153            ELSE: SET_PLOT, 'X'
154         ENDCASE
155         !p.BACKGROUND=(!d.n_colors-1) < 255
156         !p.color=0
157         if !d.n_colors gt 256 then !p.background='ffffff'x
158
159; Open a window (to make sure !D.N_COLORS is accurate).
160
161         WINDOW, /FREE, /PIXMAP, XSIZE=10, YSIZE=10
162         WDELETE, !D.WINDOW
163
164; Here is how many colors we should use.
165
166         ncolors = !D.N_COLORS
167         SET_PLOT, oldDevice
168         IF oldDevice EQ 'X' OR oldDevice EQ 'MAC' OR oldDevice EQ 'WIN' then BEGIN
169            !p.BACKGROUND=(!d.n_colors-1) < 255
170            !p.color=0
171            if !d.n_colors gt 256 then !p.background='ffffff'x
172         ENDIF
173      ENDIF ELSE ncolors = !D.N_COLORS
174   ENDIF
175   IF N_ELEMENTS(bottom) EQ 0 THEN bottom = 0B
176   IF N_ELEMENTS(cb_charsize) EQ 0 THEN cb_charsize = 1.0
177   IF N_ELEMENTS(cb_charthick) EQ 0 THEN cb_charthick = 1.0
178   IF N_ELEMENTS(format) EQ 0 THEN format = '(F6.2)'
179   IF N_ELEMENTS(cb_color) EQ 0 THEN cb_color = ncolors - 1 + bottom
180   IF N_ELEMENTS(min) EQ 0 THEN min = 0.0
181   IF N_ELEMENTS(max) EQ 0 THEN max = FLOAT(ncolors) - 1
182   IF N_ELEMENTS(divisions) EQ 0 THEN divisions = 2
183   IF N_ELEMENTS(cb_title) EQ 0 THEN cb_title = ''
184   IF N_ELEMENTS(notitle) EQ 1 THEN cb_title = ''
185   pscolor = KEYWORD_SET(pscolor)
186
187   IF KEYWORD_SET(vertical) THEN BEGIN
188      IF KEYWORD_SET(discrete) THEN begin
189         facteur=256/n_elements(discrete)
190         discrete=reform(replicate(1,facteur) # discrete,facteur*n_elements(discrete), /overwrite)
191         bar = REPLICATE(1B,10) # discrete
192      endif else  bar = REPLICATE(1B,10) # BINDGEN(256)
193      IF N_ELEMENTS(position) EQ 0 THEN position = [0.88, 0.15, 0.95, 0.95]
194   ENDIF ELSE BEGIN
195      IF KEYWORD_SET(discrete) THEN begin
196         facteur=256/n_elements(discrete)
197         discrete=reform(replicate(1,facteur) # discrete,facteur*n_elements(discrete), /overwrite)
198         bar =  discrete # REPLICATE(1B,10)
199      endif else bar = BINDGEN(256) # REPLICATE(1B, 10)
200      IF N_ELEMENTS(position) EQ 0 THEN position = [0.15, 0.88, 0.95, 0.95]
201   ENDELSE
202
203; Scale the color bar.
204   IF NOT KEYWORD_SET(discrete) THEN $
205    bar = BYTSCL(bar, TOP=ncolors-1) + bottom
206
207; Get starting locations in DEVICE coordinates.
208
209   xstart = position[0] * !D.X_VSIZE
210   ystart = position[1] * !D.Y_VSIZE
211
212; Get the size of the bar in DEVICE coordinates.
213
214   xsize = (position[2] - position[0]) * !D.X_VSIZE
215   ysize = (position[3] - position[1]) * !D.Y_VSIZE
216
217; For PostScript output only, draw the annotation in !P.COLOR
218; unless "pscolor" is set. This makes better output on grayscale
219; printers.
220
221   IF postScriptDevice AND (pscolor NE 1) THEN BEGIN
222      oldcolor = cb_color
223      cb_color = !P.COLOR
224   ENDIF
225
226; Display the color bar in the window. Sizing is
227; different for PostScript and regular display.
228
229   IF postScriptDevice THEN BEGIN
230
231      TV, bar, xstart, ystart, XSIZE=xsize, YSIZE=ysize
232
233   ENDIF ELSE BEGIN
234
235     IF CEIL(xsize) LT 0 OR CEIL(ysize) LT 0 THEN return
236      bar = CONGRID(bar, CEIL(xsize), CEIL(ysize), /INTERP)
237      TV, bar, xstart, ystart
238
239   ENDELSE
240
241; Annotate the color bar.
242
243   if keyword_set(cb_label) then begin
244      divisions = n_elements(cb_label)-1
245      for i = 0,divisions DO cb_label = string(cb_label, FORMAT = format)
246      format = ''
247   ENDIF ELSE cb_label = ''
248
249
250   IF KEYWORD_SET(vertical) THEN BEGIN
251
252      IF KEYWORD_SET(right) THEN BEGIN
253
254         PLOT, [min,max], [min,max], /NODATA, XTICKS=1, $
255          YTICKS=divisions, XSTYLE=1, YSTYLE=9, $
256          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize, CHARTHICK=cb_charthick $
257          , /NOERASE, $
258          YTICKFORMAT='(A1)', XTICKFORMAT='(A1)', YTICKLEN=0.1 , $
259          YRANGE=[min, max], YTITLE=cb_title
260
261         AXIS, YAXIS=1, YRANGE=[min, max], YTICKFORMAT=format, YTICKS=divisions, $
262          YTICKLEN=0.1, YSTYLE=1, COLOR=cb_color, CHARTHICK=cb_charthick $
263          , CHARSIZE=cb_charsize, xtickname = cb_label, ylog = cb_log
264
265      ENDIF ELSE BEGIN
266
267         PLOT, [min,max], [min,max], /NODATA, XTICKS=1, $
268          YTICKS=divisions, XSTYLE=1, YSTYLE=9, $
269          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
270          , CHARTHICK=cb_charthick, /NOERASE, $
271          YTICKFORMAT=format, XTICKFORMAT='(A1)', YTICKLEN=0.1 , $
272          YRANGE=[min, max], xtickname = cb_label
273
274         AXIS, YAXIS=1, YRANGE=[min, max], YTICKFORMAT='(A1)', YTICKS=divisions, $
275          YTICKLEN=0.1, YTITLE=cb_title, YSTYLE=1, COLOR=cb_color $
276          , CHARSIZE=cb_charsize, CHARTHICK=cb_charthick, ylog = cb_log
277
278      ENDELSE
279
280   ENDIF ELSE BEGIN
281
282      IF KEYWORD_SET(top) THEN BEGIN
283
284         PLOT, [min,max], [min,max], /NODATA, XTICKS=divisions, $
285          YTICKS=1, XSTYLE=9, YSTYLE=1, $
286          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
287          , CHARTHICK=cb_charthick, /NOERASE, $
288          YTICKFORMAT='(A1)', XTICKFORMAT='(A1)', XTICKLEN=0.1, $
289          XRANGE=[min, max], XTITLE=cb_title
290
291         AXIS, XTICKS=divisions, XSTYLE=1, COLOR=cb_color $
292          , CHARSIZE=cb_charsize, CHARTHICK=cb_charthick, $
293          XTICKFORMAT=format, XTICKLEN=0.1, XRANGE=[min, max], XAXIS=1, xtickname = cb_label, xlog = cb_log
294
295      ENDIF ELSE BEGIN
296
297         PLOT, [min,max], [min,max], /NODATA, XTICKS=divisions, $
298          YTICKS=1, XSTYLE=1, YSTYLE=1, $
299          POSITION=position, COLOR=cb_color, CHARSIZE=cb_charsize $
300          , CHARTHICK=cb_charthick, /NOERASE, $
301          YTICKFORMAT='(A1)', XTICKFORMAT=format, XTICKLEN=0.1, $
302          XRANGE=[min, max], TITLE=cb_title, xtickname = cb_label, xlog = cb_log
303
304      ENDELSE
305
306   ENDELSE
307; Restore color variable if changed for PostScript.
308
309   IF postScriptDevice AND (pscolor NE 1) THEN cb_color = oldcolor
310
311   return
312END
Note: See TracBrowser for help on using the repository browser.