source: trunk/SRC/Colors/getcolor.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: 12.3 KB
Line 
1;+
2;
3; @file_comments
4; The original purpose of this function was to enable the
5; user to specify one of the 16 colors supported by the
6; McIDAS color map by name. Over time, however, the function
7; has become a general purpose function for handling and
8; supporting drawing colors in a device-independent way.
9; In particular, I have been looking for ways to write color
10; handling code that will work transparently on both 8-bit and
11; 24-bit machines. On 24-bit machines, the code should work the
12; same where color decomposition is turned on or off.
13;
14; (The 16 supported colors in GETCOLOR come from the McIDAS color
15; table offered on the IDL newsgroup by Liam Gumley.)
16;
17; @categories Graphics, Color
18;
19; @param thisColor {in}{optional}
20; A string with the "name" of the color. Valid names are:
21;           black
22;           magenta
23;           cyan
24;           yellow
25;           green
26;           red
27;           blue
28;           navy
29;           gold
30;           pink
31;           aqua
32;           orchid
33;           gray
34;           sky
35;           beige
36;           white
37;
38; The color YELLOW is returned if the color name can't be resolved.
39; Case is unimportant.
40;
41;
42; @param index {in}{optional}
43; The color table index where the specified color should be loaded.
44;
45; @returns
46; If no positional parameter is present, then the return value is either a 16-by-3
47; byte array containing the RGB values of all 16 colors or it is a 16-element
48; long integer array containing color values that can be decomposed into colors.
49; The 16-by-3 array is appropriate for loading color tables with the TVLCT command:
50;
51;           Device, Decomposed=0
52;           colors = GetColor()
53;           TVLCT, colors, 100
54;
55; If  function is called with just this single input parameter,
56; the return value is either a 1-by-3 array containing the RGB values of
57; that particular color, or a 24-bit integer that can be "decomposed" into
58; that particular color, depending upon the state of the TRUE keyword and
59; upon whether color decomposition is turned on or off. The state of color
60; decomposition can ONLY be determined if the program is being run in
61; IDL 5.2 or higher.
62;
63; If index parameter is passed, then the return value of the function is the
64; index number and not the color triple. (If color decomposition is turned
65; on AND the user specifies an index parameter, the color is loaded in the
66; color table at the proper index, but a 24-bit value is returned to the
67; user in IDL 5.2 and higher.)
68;
69;
70; @keyword NAMES
71; If this keyword is set, the return value of the function is
72; a 16-element string array containing the names of the colors.
73; These names would be appropriate, for example, in building
74; a list widget with the names of the colors. If the NAMES
75; keyword is set, the COLOR and INDEX parameters are ignored.
76;
77; listID = Widget_List(baseID, Value=GetColor(/Names), YSize=16)
78;
79; @keyword LOAD 
80; If this keyword is set, all 16 colors are automatically loaded
81; starting at the color index specified by the START keyword.
82; Note that setting this keyword means that the return value of the
83; function will be a structure, with each field of the structure
84; corresponding to a color name. The value of each field will be
85; an index number (set by the START keyword) corresponding to the
86; associated color, or a 24-bit long integer value that creates the
87; color on a true-color device. What you have as the field values is
88; determined by the TRUE keyword or whether color decomposition is on
89; or off in the absence of the TRUE keyword. It will either be a 1-by-3
90; byte array or a long integer value.
91;
92; @keyword START {default=!D.TABLE_SIZE - 17}
93; The starting color index number if the LOAD keyword is set. This keyword
94; value is ignored unless the LOAD keyword is also set. The keyword is also
95; ignored if the TRUE keyword is set or if color decomposition in on in
96; IDL 5.2 and higher.
97;
98; @keyword TRUE 
99; If this keyword is set, the specified color triple is returned
100; as a 24-bit integer equivalent. The lowest 8 bits correspond to
101; the red value; the middle 8 bits to the green value; and the
102; highest 8 bits correspond to the blue value. In IDL 5.2 and higher,
103; if color decomposition is turned on, it is as though this keyword
104; were set.
105;
106; @restrictions
107; The TRUE keyword causes the START keyword to be ignored.
108; The NAMES keyword causes the COLOR, INDEX, START, and TRUE parameters to be
109; ignored.
110; The COLOR parameter is ignored if the LOAD keyword is used.
111; On systems where it is possible to tell the state of color decomposition
112; (i.e., IDL 5.2 and higher), a 24-bit value (or values) is automatically
113; returned if color decomposition is ON.
114;
115; @examples
116; To load a yellow color in color index 100 and plot in yellow, type:
117;
118;      IDL> yellow = GETCOLOR('yellow', 100)
119;      IDL> PLOT, data, COLOR=yellow
120;
121; or,
122;
123;      IDL> PLOT, data, COLOR=GETCOLOR('yellow', 100)
124;
125; To do the same thing on a 24-bit color system with decomposed color on, type:
126;
127;      IDL> PLOT, data, COLOR=GETCOLOR('yellow', /TRUE)
128;
129; or in IDL 5.2 and higher,
130;
131;      IDL> DEVICE, Decomposed=1
132;      IDL> PLOT, data, COLOR=GETCOLOR('yellow')
133;
134; To load all 16 colors into the current color table, starting at
135; color index 200, type:
136;
137;      IDL> TVLCT, GETCOLOR(), 200
138;
139; To add the color names to a list widget:
140;
141;      IDL> listID = Widget_List(baseID, Value=GetColor(/Names), YSize=16)
142;
143; To load all 16 colors and have the color indices returned in a structure:
144;
145;      IDL> DEVICE, Decomposed=0
146;      IDL> colors = GetColor(/Load, Start=1)
147;      IDL> HELP, colors, /Structure
148;      PLOT, data, COLOR=colors.yellow
149;
150; To get the direct color values as 24-bit integers in color structure fields:
151;
152;      IDL> DEVICE, Decomposed=1
153;      IDL> colors = GetColor(/Load)
154;      IDL> PLOT, data, COLOR=colors.yellow
155;
156; Note that the START keyword value is ignored if on a 24-bit device,
157; so it is possible to write completely device-independent code by
158; writing code like this:
159;
160;      IDL> colors = GetColor(/Load)
161;      IDL> PLOT, data, Color=colors.yellow;           IDL> DEVICE, Decomposed=0
162;      IDL> colors = GetColor(/Load, Start=1)
163;      IDL> HELP, colors, /Structure
164;      PLOT, data, COLOR=colors.yellow
165;
166; To get the direct color values as 24-bit integers in color structure fields:
167;
168;      IDL> DEVICE, Decomposed=1
169;      IDL> colors = GetColor(/Load)
170;      IDL> PLOT, data, COLOR=colors.yellow
171;
172; Note that the START keyword value is ignored if on a 24-bit device,
173; so it is possible to write completely device-independent code by
174; writing code like this:
175;
176;      IDL> colors = GetColor(/Load)
177;      IDL> PLOT, data, Color=colors.yellow
178;
179; @history
180; Written by: David Fanning, 10 February 96.
181; Fixed a bug in which N_ELEMENTS was spelled wrong. 7 Dec 96. DWF
182; Added the McIDAS colors to the program. 24 Feb 99. DWF
183; Added the INDEX parameter to the program 8 Mar 99. DWF
184; Added the NAMES keyword at insistence of Martin Schultz. 10 Mar 99. DWF
185; Reordered the colors so black is first and white is last. 7 June 99. DWF
186; Added automatic recognition of DECOMPOSED=1 state. 7 June 99. DWF
187; Added LOAD AND START keywords. 7 June 99. DWF.
188;
189; @version $Id$
190;-
191
192FUNCTION getcolor, thisColor, index, TRUE=truecolor, $
193   NAMES=colornames, LOAD=load, START=start
194;
195  compile_opt idl2, strictarrsubs
196;
197
198   ; Set up the color vectors.
199
200names  = ['Black', 'Magenta', 'Cyan', 'Yellow', 'Green']
201rvalue = [  0,        255,       0,      255,       0  ]
202gvalue = [  0,          0,     255,      255,     255  ]
203bvalue = [  0,        255,     255,        0,       0  ]
204names  = [names,  'Red', 'Blue', 'Navy', 'Gold', 'Pink']
205rvalue = [rvalue,  255,     0,      0,    255,    255  ]
206gvalue = [gvalue,    0,     0,      0,    187,    127  ]
207bvalue = [bvalue,    0,   255,    115,      0,    127  ]
208names  = [names,  'Aqua', 'Orchid', 'Gray', 'Sky', 'Beige', 'White']
209rvalue = [rvalue,   112,     219,     127,    0,     255,     255  ]
210gvalue = [gvalue,   219,     112,     127,  163,     171,     255  ]
211bvalue = [bvalue,   147,     219,     127,  255,     127,     255  ]
212
213   ; Did the user ask for a specific color? If not, return
214   ; all the colors. If the user asked for a specific color,
215   ; find out if a 24-bit value is required. Return to main
216   ; IDL level if an error occurs.
217
218ON_Error, 1
219np = N_Params()
220IF Keyword_Set(start) EQ 0 THEN start = !D.TABLE_SIZE - 17
221
222   ; User ask for the color names?
223
224IF Keyword_Set(colornames) THEN RETURN, names ELSE names = StrUpCase(names)
225
226   ; If no positional parameter, return all colors.
227
228IF np EQ 0 THEN BEGIN
229
230   ; Did the user want a 24-bit value? If so, call COLOR24.
231
232   IF Keyword_Set(trueColor) THEN BEGIN
233      returnColor = LonArr(16)
234      FOR j=0,15 DO returnColor[j] = Color24([rvalue[j], gvalue[j], bvalue[j]])
235
236         ; If LOAD keyword set, return a color structure.
237
238      IF Keyword_Set(load) THEN BEGIN
239         returnValue = Create_Struct('black', returnColor[0])
240         FOR j=1,15 DO returnValue = Create_Struct(returnValue, names[j], returnColor[j])
241         returnColor = returnValue
242      ENDIF
243
244      RETURN, returnColor
245   ENDIF
246
247   ; If color decomposition is ON, return 24-bit values.
248
249   IF Float(!Version.Release) GE 5.2 THEN BEGIN
250      IF (!D.Name EQ 'X' OR !D.Name EQ 'WIN' OR !D.Name EQ 'MAC') THEN BEGIN
251         Device, Get_Decomposed=decomposedState
252      ENDIF ELSE decomposedState = 0
253      IF decomposedState EQ 1 THEN BEGIN
254         returnColor = LonArr(16)
255         FOR j=0,15 DO returnColor[j] = Color24([rvalue[j], gvalue[j], bvalue[j]])
256         IF Keyword_Set(load) THEN BEGIN
257            returnValue = Create_Struct('black', returnColor[0])
258            FOR j=1,15 DO returnValue = Create_Struct(returnValue, names[j], returnColor[j])
259            RETURN, returnValue
260         ENDIF
261         RETURN, returnColor
262      ENDIF
263
264      IF Keyword_Set(load) THEN BEGIN
265         TVLCT, Reform([rvalue, gvalue, bvalue], 16, 3), start
266         returnValue = Create_Struct('black', start)
267         FOR j=1,15 DO returnValue = Create_Struct(returnValue, names[j], start+j)
268         RETURN, returnValue
269      ENDIF
270
271      returnColor = REFORM([rvalue, gvalue, bvalue], 16, 3)
272      RETURN, returnColor
273
274   ENDIF
275
276   IF Keyword_Set(load) THEN BEGIN
277      TVLCT, Reform([rvalue, gvalue, bvalue], 16, 3), start
278      returnValue = Create_Struct('black', start)
279      FOR j=1,15 DO returnValue = Create_Struct(returnValue, names[j], start+j)
280      RETURN, returnValue
281   ENDIF
282
283   returnColor = REFORM([rvalue, gvalue, bvalue], 16, 3)
284   RETURN, returnColor
285
286ENDIF
287
288   ; Check synonyms of colors.
289
290IF StrUpCase(thisColor) EQ 'GREY' THEN thisColor = 'GRAY'
291IF StrUpCase(thisColor) EQ 'CHARCOAL' THEN thisColor = 'GRAY'
292IF StrUpCase(thisColor) EQ 'AQUAMARINE' THEN thisColor = 'AQUA'
293IF StrUpCase(thisColor) EQ 'SKYBLUE' THEN thisColor = 'SKY'
294
295   ; Make sure the parameter is an uppercase string.
296
297varInfo = SIZE(thisColor)
298IF varInfo[varInfo[0] + 1] NE 7 THEN $
299   MESSAGE, 'The color name must be a string.'
300thisColor = STRUPCASE(thisColor)
301
302   ; Get the color triple for this color.
303
304colorIndex = WHERE(names EQ thisColor)
305
306   ; If you can't find it. Issue an informational message,
307   ; set the index to a YELLOW color, and continue.
308
309IF colorIndex[0] LT 0 THEN BEGIN
310   MESSAGE, "Can't find color. Returning YELLOW.", /INFORMATIONAL
311   colorIndex = 3
312ENDIF
313
314   ; Get the color triple.
315
316r = rvalue[colorIndex]
317g = gvalue[colorIndex]
318b = bvalue[colorIndex]
319returnColor = REFORM([r, g, b], 1, 3)
320
321   ; Did the user want a 24-bit value? If so, call COLOR24.
322
323IF KEYWORD_SET(trueColor) THEN BEGIN
324   returnColor = COLOR24(returnColor)
325   RETURN, returnColor
326ENDIF
327
328   ; If color decomposition is ON, return 24-bit value.
329
330IF Float(!Version.Release) GE 5.2 THEN BEGIN
331
332      IF (!D.Name EQ 'X' OR !D.Name EQ 'WIN' OR !D.Name EQ 'MAC') THEN BEGIN
333         Device, Get_Decomposed=decomposedState
334      ENDIF ELSE decomposedState = 0
335
336   IF decomposedState EQ 1 THEN BEGIN
337
338         ; Before you change return color, load index if requested.
339
340      IF N_Elements(index) NE 0 THEN BEGIN
341         index = 0 > index < (!D.Table_Size-1)
342         TVLCT, returnColor, index
343      ENDIF
344
345      returnColor = COLOR24(returnColor)
346      RETURN, returnColor
347   ENDIF
348ENDIF
349
350   ; Did the user specify a color index? If so, load it.
351
352IF N_Elements(index) NE 0 THEN BEGIN
353   index = 0 > index < (!D.Table_Size-1)
354   TVLCT, returnColor, index
355   returnColor = index
356ENDIF
357
358RETURN, returnColor
359END
Note: See TracBrowser for help on using the repository browser.