How to specify the number of colors loaded into IDLgrPalette by LoadCT
In the IDLgrPalette object, user can specify any number of colors using the ared, ablue, agreen arguments or the respective keywords. However, when the user loads a table with the LoadCT method, user will always load all 255 colors.
It is more practical to use the feature from the "old" Loadct procedure where you can "squeeze" a colortable into a smaller number of colors. In other words, IDLgrPalette::LoadCT should accept NCOLORS keyword.
The following code provides an example of how to specify the number of colors loaded into IDLgrPalette by LoadCT.
;
; Copyright (c) 2000, Research Systems, Inc. All rights reserved.
; Unauthorized reproduction prohibited.
;
;--------------------------------------------------------------
;+
; METHODNAME:
; IDLgrPalette::LoadCT2
;
; PURPOSE:
; Loads one of the IDL predefined color tables into this palette.
; (Identical to IDLgrPalette::LoadCT, except that it also allows
; the NCOLORS keyword to be set and handled.)
;
; CALLING SEQUENCE:
; oObj->[IDLgrPalette::]LoadCT2, TableNum
;
; KEYWORD PARAMETERS:
; FILENAME: Set this keyword to the name of a colortable
; file to be used instead of the file colors1.tbl
; in the IDL distribution.
; NCOLORS: Set this keyword to the number of colors to which
; the loaded colortable should be interpolated. The
; default is 256.
;
; MODIFICATION HISTORY:
; Written by: DLD, December 2000.
;-
pro IDLgrPalette::LoadCT2, table_number, FILENAME=filename, NCOLORS=ncolors
; Default to 256 colors, but allow keyword override.
nc = 256
if (N_ELEMENTS(ncolors) ne 0) then $
nc = nc < ncolors
if (nc eq 0) then $
return
; Use the default color table file unless provided by keyword.
if (N_ELEMENTS(filename) ne 0) then $
fname = filename $
else $
fname = FILEPATH('colors1.tbl', SUBDIR=['resource','colors'])
OPENR, lun, fname, /GET_LUN
ntables = 0b
READU, lun, ntables
; Ensure the requested table number is within range.
if (table_number ge ntables) or (table_number lt 0) then $
MESSAGE, 'Table number must be from 0 to ' + STRTRIM(ntables-1, 2)
; Load the colors for the requested table.
aa = ASSOC(lun, BYTARR(256),1) ;Read 256 long ints
r = aa[table_number*3]
g = aa[table_number*3+1]
b = aa[table_number*3+2]
; Interpolate if necessary.
if nc ne 256 then begin
p = (LINDGEN(nc) * 255) / (nc-1)
r = r[p]
g = g[p]
b = b[p]
endif
FREE_LUN,lun
self->SetProperty, RED_VALUES=r, GREEN_VALUES=g, BLUE_VALUES=b
end
Review on 12/31/2013 MM