pro setplotcolors, ncolors=ncolors
;+
; NAME:
;  setplotcolors
;
; PURPOSE:
;  Modify the color table to define a set of colors for plotting at
;  the end. The defined colors are:
;    ncolors  system variable  rgb code 
;       1     !white           "ffffff"
;       2     !black           "000000"
;       3     !red             "ff0000"
;       4     !green           "00ff00"
;       5     !blue            "0000ff"
;       6     !yellow          "ffff00"
;       7     !pink            "ff00ff"
;       8     !cyan            "00ffff"
;       9     !orange          "ff8040"
;      10     !violet          "8d38c9"
;      11     !maroon          "810541"
;      12     !plum            "b93b8f"
;      13     !sblue           "6698ff"
;      14     !khaki           "ada96e"
;      15     !coral           "e77471"
;      16     !gray            "657383"
;   
;  The original color table is compressed between the remaining
;  colors. Additionally, three system variables are defined:
;  !nbplotcolors - the number of colors reserved for plotting, equal
;                  to 16 or the value of the ncolors keyword.
;  !nbdrawcolors - the number of colors available for drawing
;                  (e. g. tv). It is equal !d.table_size - !nbplotcolors
;  !topcolor     - the last color of the colortable. This system
;                  variable is usuful for the scaling operation of
;                  images. For example, to display an image properly
;                  using the remaining colors, use :
;                  tv, bytscl(ima, top=!topcolor)  
;
; CATEGORY:
;  Graphics
;
; CALLING SEQUENCE:
;  setplotcolors, ncolors=ncolors
;
; INPUTS:
;  None
;
; OPTIONAL INPUTS:
;  None
;
; KEYWORD PARAMETERS:
;  ncolors: integer - number of colors to define, between 2 and 16,
;                     defaulted to 16
;
; OUTPUTS:
;  None
;
; OPTIONAL OUTPUTS:
;  None
;
; COMMON BLOCKS:
;  None
;
; SIDE EFFECTS:
;  Define some new system variables
;
; RESTRICTIONS:
;  None
;
; PROCEDURE:
;  resample the colortable in the remaining colors and add the new
;  defined ones at the end.
;
; EXAMPLE:
;  setplotcolors, ncolors=10
;
; MODIFICATION HISTORY:
;  01/12/06: Herve Aussel
;-


if n_elements(ncolors) eq 0 then ncolors=16

if ncolors LT 1 then return
if ncolors GT 16 then ncolors=16
if ncolors GT !d.table_size then begin
    message, /info, $
    "Requested number of colors is larger than the device capabilities"
    return
endif 

ntablecols = !d.table_size - ncolors

; get the current color table:
tvlct, rvect, gvect, bvect, /get

; interpolate the table
xnew = round(findgen(ntablecols)/float(ntablecols)*float(!d.table_size))


newr = rvect[xnew]
newg = gvect[xnew]
newb = bvect[xnew]

rtiny = ['FF'X, '00'X, 'FF'X, '00'X, '00'X, 'FF'X, 'FF'X, '00'X, $
         'FF'X, '8D'X, '81'X, 'B9'X, '66'X, 'AD'X, 'E7'X, '65'X ]
gtiny = ['FF'X, '00'X, '00'X, 'FF'X, '00'X, 'FF'X, '00'X, 'FF'X, $
         '80'X, '38'X, '05'X, '3B'X, '98'X, 'A9'X, '74'X, '73'X ]
btiny = ['FF'X, '00'X, '00'X, '00'X, 'FF'X, '00'X, 'FF'X, 'FF'X, $
         '40'X, 'C9'X, '41'X, '8F'X, 'FF'X, '6E'X, '71'X, '83'X ]


icol = ncolors-1
r = [newr, reverse(rtiny[0:icol])]
g = [newg, reverse(gtiny[0:icol])]
b = [newb, reverse(btiny[0:icol])]

tvlct, r, g, b

defsysv, '!nbplotcolors', ncolors
defsysv, '!nbdrawcolors', ntablecols
defsysv, '!topcolor', !d.table_size-ncolors-1

varnames = ['!white', '!black', '!red', '!green', '!blue', '!yellow', $
            '!pink', '!cyan', '!orange', '!violet', '!maroon', '!plum', $
            '!sblue', '!khaki', '!coral', '!gray']

for i=0, icol do call_procedure, 'defsysv' ,  varnames[i],  !d.table_size-1-i

return

end

