;+ ; MKCOLORJMA generates four new color tables:
; 40: Black (0) + RGYBMC (123456) + Orange (7) + Light green (8) + Purple (9) + 
;     Dark green (10) + Dark red (11) + Brown (12) + Light orange (13) + 
;     Medium gray (14) + Greenish gray (15) +  Dark blue (16) + Turquoise (17) +
;     Olive green (18) + Pink (19) + Bluish green (20) + Gray scale (25 to 255)
; 39: Heat.
; 38: Hue- + brightness- selected colors with maximum saturation.
; 37: EOS B improved + Black + gray scale + White. 
; See the full procedure
; @version v. 3.1, 11 May 2013.
; @history
                                                        
;
;  v0.1: X color eliminated.  
;        Color table 38 added. 
;  v1.0: Color table 37 added. 
;        General clean up.     
;  v1.1: Better treatment of the directory structure and OS. 
;  v2.0: English version.      
;  v2.1: IDLdoc documentation.
;  v2.2: Upgrade to IDLdoc documentation. 
;  v2.3: Support for Windows introduced. 
;  v2.4: Small changes for unix compatibility.
;  v2.5: New colors added to table 40.
;        Version system update. 
;  v3.0: General adaptation to new version.
;        Use of jmaplot_path and reubication of colorsjma.tbl there.
;        ONLY_LOAD, CTABLE, RGB added.                                            
;        Table 39 is now Heat (old: EOS B + Black + White).
;        Postscript sample output added.
;        HTML pages moved to local directory.       
;  v3.1: Only version change. 
; @author Jesús Maíz Apellániz, IAA
; @keyword ONLY_LOAD {in}{optional}{type=boolean}{default=FALSE} Flag to avoid generation of ; new files and instead just load the color table.
; @keyword CTABLE {in}{optional}{type=byte}{default=40} Color table to load.
; @keyword RGB {out}{optional}{type=byte array} 256x3 array with the RGB information ; of the loaded color table.
; @keyword QUIET {in}{optional}{type=boolean}{default=FALSE} Flag to supress informational messages.
;- ;
PRO MKCOLORJMA, ONLY_LOAD=only_load, CTABLE=ctable, RGB=rgb, QUIET=quiet
ON_ERROR, 1
COMMON colors, r_orig, g_orig, b_orig, r_curr, g_curr, b_curr
jmaplot_path = JMAPLOTPATH(JMAPLOT_VERSION=jmaplot_version)
;
; Copiamos (en unix) el fichero colors1.tbl a colorjma.tbl
;
OPENR, unit, !DIR + '/resource/colors/colors1.tbl', /GET_LUN, ERROR=err
IF err NE 0 AND KEYWORD_SET(ONLY_LOAD) EQ 0 THEN BEGIN
 PRINT, 'mkcolorja failed!!!' 
 PRINT, 'This procedure may not work under an OS different from Unix'
ENDIF ELSE IF   KEYWORD_SET(ONLY_LOAD) EQ 0 THEN BEGIN
 CLOSE, unit 
 FREE_LUN, unit
 IF KEYWORD_SET(QUIET) EQ 0 THEN BEGIN
  PRINT, 'This procedure may not work if you do not have the right permissions.'
  PRINT, 'Try the ONLY_LOAD flag if that happens and you do need to load a table.'
 ENDIF
 IF !VERSION.OS_FAMILY NE 'Windows' THEN $
  SPAWN, ['/bin/cp', !DIR + '/resource/colors/colors1.tbl',   jmaplot_path + 'colorjma.tbl'], /NOSHELL ELSE $
  SPAWN, 'copy '   + !DIR + '\resource\colors\colors1.tbl ' + jmaplot_path + 'colorjma.tbl'
;
; Creamos el fichero PS fantasma
;
 mydevice = !D.NAME
 SET_PLOT, 'PS'
 DEVICE, FILENAME='kk.ps', /COLOR, BITS = 8
;
; Primera tabla
;
; Creamos la escala de grises modificada en la paleta 40
; Escala de  0 (negro) a 255 (blanco) + 
;            1,  2,  4       (RGB) + 
;            3,  5,  6       (YMC) + 
;            7,  8,  9,      (Naranja, Verde claro, Morado) +
;           10, 11, 12,      (Verde oscuro, Rojo oscuro, Marrón) +
;           13, 14, 15,      (Naranja claro, Gris, Gris verdoso) +
;           16, 17, 18,      (Azul oscuro, Turquesa, Verde oliva) +
;           19, 20           (Rosa, Verde azulado)
 r = INDGEN(256) & g = INDGEN(256) & b = INDGEN(256) & name = STRARR(256)
                                            & name[0]  = 'black'
 r[1]   = 255 & g[1]   =   0 & b[1]   =   0 & name[1]  = 'red'
 r[2]   =   0 & g[2]   = 255 & b[2]   =   0 & name[2]  = 'green'
 r[3]   = 255 & g[3]   = 255 & b[3]   =   0 & name[3]  = 'yellow'
 r[4]   =   0 & g[4]   =   0 & b[4]   = 255 & name[4]  = 'blue'
 r[5]   = 255 & g[5]   =   0 & b[5]   = 255 & name[5]  = 'magenta'
 r[6]   =   0 & g[6]   = 255 & b[6]   = 255 & name[6]  = 'cyan'
 r[7]   = 255 & g[7]   = 150 & b[7]   =   0 & name[7]  = 'orange'
 r[8]   = 127 & g[8]   = 255 & b[8]   =   0 & name[8]  = 'light green'
 r[9]   = 127 & g[9]   =   0 & b[9]   = 255 & name[9]  = 'purple'
 r[10]  =   0 & g[10]  = 127 & b[10]  =   0 & name[10] = 'dark green'
 r[11]  = 150 & g[11]  =   0 & b[11]  =   0 & name[11] = 'dark red'
 r[12]  = 139 & g[12]  =  69 & b[12]  =  19 & name[12] = 'brown'
 r[13]  = 255 & g[13]  = 200 & b[13]  =   0 & name[13] = 'light orange'
 r[14]  = 127 & g[14]  = 127 & b[14]  = 127 & name[14] = 'gray'
 r[15]  =  47 & g[15]  =  79 & b[15]  =  79 & name[15] = 'greenish gray'
 r[16]  =   0 & g[16]  =   0 & b[16]  = 127 & name[16] = 'dark blue'
 r[17]  =  64 & g[17]  = 224 & b[17]  = 208 & name[17] = 'turquoise'
 r[18]  = 107 & g[18]  = 142 & b[18]  =  35 & name[18] = 'olive green'
 r[19]  = 255 & g[19]  =  20 & b[19]  = 147 & name[19] = 'pink'
 r[20]  =  32 & g[20]  = 178 & b[20]  = 170 & name[20] = 'bluish green'
 v40 = [[r],[g],[b]]
 MODIFYCT, 40, 'BRGYBMC + others + gray scale', r, g, b, $
             FILE = jmaplot_path + 'colorjma.tbl'
;
; Segunda y quinta tablas 
;
; Creamos la escala Heat en la paleta 39
 r = INTARR(256) & g = INTARR(256) & b = INTARR(256)
 r[  0:104] = ROUND(255.*INDGEN(105)/104)
 r[105:255] =       255
 g[ 65:229] = ROUND(255.*INDGEN(165)/164)
 g[230:255] =       255
 b[190:255] = ROUND(255.*INDGEN( 66)/65 )
 MODIFYCT, 39, 'Heat', r, g, b, FILE = jmaplot_path + 'colorjma.tbl'
 v39 = [[r],[g],[b]]
; Añadimos el verde para la paleta 36
 r[255] = 0 & g[255] = 255 & b[255] = 0
 MODIFYCT, 36, 'Heat + green', r, g, b, FILE = jmaplot_path + 'colorjma.tbl'
;
; Tercera tabla
;
; Barrido en el espacio de "Hue" con saturación de 1 y brillo variable
; + colores especiales: 0 blanco (para transparente), 253 negro, 254 casi blanco
; 255 blanco
;
 h = FLTARR(256) & s = FLTARR(256) & l = FLTARR(256)
 s[1:252] = 1.0
;     haux = 10*FINDGEN(36)
 haux = [  0, 18, 24, 30, 36, 42, 48, 54, 60, 70, 80, 95,120, $
         145,160,170,180,185,190,195,200,205,210,220,240,260,270,280, $
         290,295,300,305,310,320,330,345]
 FOR i=0,6 DO h[36*i+1:36*i+36] = haux
 l[1:252] = 0.50+0.06*FIX(INDGEN(252)/36)
 COLOR_CONVERT, h, l, s, r, g, b, /HLS_RGB
 r[  0] = 255 & g[  0] = 255 & b[  0] = 255
 r[253] =   0 & g[253] =   0 & b[253] =   0
 r[254] = 240 & g[254] = 240 & b[254] = 240
 r[255] = 255 & g[255] = 255 & b[255] = 255
 MODIFYCT, 38, 'Sweeping colors', r, g, b, $
             FILE = jmaplot_path + 'colorjma.tbl'
 v38 = [[r],[g],[b]]
;
; Cuarta tabla: EOS-B mejorado
;
 r = INTARR(256) & g = INTARR(256) & b = INTARR(256)
; r[  0:30 ] = REPLICATE(  0,31)
; g[  0:30 ] = REPLICATE(  0,31)
; b[  0:30 ] =  20 + ROUND(235*(INDGEN(31)/30.)^6.0)
; b[  0    ] =   0
; r[ 30:80 ] = REPLICATE(  0,51)
; g[ 30:80 ] =       ROUND(150*(INDGEN(51)/50.)^0.2)
; b[ 30:80 ] = 255 - ROUND(255*(INDGEN(51)/50.)^2.0)
; r[ 50:80 ] = REPLICATE(  0,31)
; g[ 50:80 ] =       ROUND(150*(INDGEN(31)/30.)^0.2)
; b[ 50:80 ] = 255 - ROUND(255*(INDGEN(31)/30.)^2.0)
 r[  0:25 ] = 128 - ROUND(128*(INDGEN(26)/25.)^1.2)
 g[  0:25 ] = REPLICATE(  0,26)
 b[  0:25 ] = 255 - ROUND(105*(INDGEN(26)/25.)^0.8)
 r[  0    ] = 0
 b[  0    ] = 0
 r[ 25:45 ] = REPLICATE(  0,21)
 g[ 25:45 ] = REPLICATE(  0,21)
 b[ 25:45 ] = 150 + ROUND(105*(INDGEN(21)/20.)^1.0)
 r[ 45:80 ] = REPLICATE(  0,36)
 g[ 45:80 ] =       ROUND(150*(INDGEN(36)/35.)^0.5)
 b[ 45:80 ] = 255 - ROUND(255*(INDGEN(36)/35.)^2.0)
 r[ 80:130] =       ROUND(255*(INDGEN(51)/50.)^0.8)
 g[ 80:130] = 150 + ROUND(105*(INDGEN(51)/50.)^0.8)
 b[ 80:130] = REPLICATE(  0,51)
 r[130:190] = REPLICATE(255,61)
 g[130:190] = 255 - ROUND(255*INDGEN(61)/60.)
 b[130:190] = REPLICATE(  0,61)
 r[190:250] = 255 - ROUND(255*INDGEN(61)/60.)
 g[190:250] = REPLICATE(  0,61)
 b[190:250] = REPLICATE(  0,61)
 r[251:255] = ROUND(255*INDGEN(5)/4.)
 g[251:255] = ROUND(255*INDGEN(5)/4.)
 b[251:255] = ROUND(255*INDGEN(5)/4.)
 MODIFYCT, 37, 'EOS-B improved + Bl-Grays-Wh', r, g, b, FILE = jmaplot_path + 'colorjma.tbl'
 v37 = [[r],[g],[b]]
;
; Cerramos el fichero PostScript fantasma y lo borramos (desde Unix)
;
 DEVICE, /CLOSE
 SET_PLOT, mydevice
 SPAWN, ['/bin/rm', 'kk.ps'], /NOSHELL
;
; Creamos el fichero PS de demostración
;
 v = BYTARR(2760,2760,3) + 255
 FOR j=101,199     DO v[100:2659,j      ,0] = v37[INDGEN(2560)/10,0]
 FOR j=101,199     DO v[100:2659,j      ,1] = v37[INDGEN(2560)/10,1]
 FOR j=101,199     DO v[100:2659,j      ,2] = v37[INDGEN(2560)/10,2]
 FOR j=301,399     DO v[100:2659,j      ,0] = v38[INDGEN(2560)/10,0]
 FOR j=301,399     DO v[100:2659,j      ,1] = v38[INDGEN(2560)/10,1]
 FOR j=301,399     DO v[100:2659,j      ,2] = v38[INDGEN(2560)/10,2]
 FOR j=501,599     DO v[100:2659,j      ,0] = v39[INDGEN(2560)/10,0]
 FOR j=501,599     DO v[100:2659,j      ,1] = v39[INDGEN(2560)/10,1]
 FOR j=501,599     DO v[100:2659,j      ,2] = v39[INDGEN(2560)/10,2]
 FOR j=701,799     DO v[100:2659,j      ,0] = v40[INDGEN(2560)/10,0]
 FOR j=701,799     DO v[100:2659,j      ,1] = v40[INDGEN(2560)/10,1]
 FOR j=701,799     DO v[100:2659,j      ,2] = v40[INDGEN(2560)/10,2]
 FOR j=100,800,100 DO v[100:2659,j      ,*] = 0
 FOR j=100,700,200 DO v[ 99     ,j:j+100,*] = 0
 FOR j=100,700,200 DO v[    2660,j:j+100,*] = 0
 xs = (100+850*INDGEN(3)) # (1+INTARR(7))
 ys = (1+INTARR(3))       # (2500-250*INDGEN(7))
 FOR i=0,2 DO FOR j=0,6 DO v[xs[i,j]  :xs[i,j]+99 ,ys[i,j]  :ys[i,j]+99 ,0] = v40[i+3*j,0]
 FOR i=0,2 DO FOR j=0,6 DO v[xs[i,j]  :xs[i,j]+99 ,ys[i,j]  :ys[i,j]+99 ,1] = v40[i+3*j,1]
 FOR i=0,2 DO FOR j=0,6 DO v[xs[i,j]  :xs[i,j]+99 ,ys[i,j]  :ys[i,j]+99 ,2] = v40[i+3*j,2]
 FOR i=0,2 DO FOR j=0,6 DO v[xs[i,j]-1:xs[i,j]+100,ys[i,j]-1            ,*] = 0
 FOR i=0,2 DO FOR j=0,6 DO v[xs[i,j]-1:xs[i,j]+100,          ys[i,j]+100,*] = 0
 FOR i=0,2 DO FOR j=0,6 DO v[xs[i,j]-1            ,ys[i,j]-1:ys[i,j]+100,*] = 0
 FOR i=0,2 DO FOR j=0,6 DO v[          xs[i,j]+100,ys[i,j]-1:ys[i,j]+100,*] = 0
 title    = 'colorjma.tbl'
 x        = xs[INDGEN(21)] + 125
 y        = ys[INDGEN(21)] +  20
 txt      = STRING(INDGEN(21),FORMAT='(I2)') + ': ' + name
 txm      = 'ALIGNMENT=0.0, CHARSIZE=1.3, FONT=0, /DATA'
 x2       = 1380.
 y2       = 220. + 200*INDGEN(4)
 txt2     = STRTRIM(37 + INDGEN(4),2) + ': ' + $
	    ['EOS-B improved + Black-Grays-White', 'Sweeping colors','Heat','BRGYBMC + others + gray scale']
 txm2     = 'ALIGNMENT=0.5, CHARSIZE=1.0, FONT=0, /DATA'
 SINGLEPLOT, jmaplot_path + "colorjma.ps", $
	     BACKGROUND=v,                 $
             {x:x,  y:y,  txt:txt,  txm:txm },  $
             {x:x2, y:y2, txt:txt2, txm:txm2},  $
	     PLOT={title:title},            $
             XAXISL={style:-2, range:[-0.5,2759.5]}, YAXISL={style:-2, range:[-0.5,2759.5]}
ENDIF ELSE BEGIN
 CLOSE, unit 
 FREE_LUN, unit
ENDELSE
OPENR, unit, jmaplot_path + 'colorjma.tbl', /GET_LUN, ERROR=err
IF err NE 0 THEN BEGIN
 PRINT, 'colorjma.tbl' + ' not found in ' + jmaplot_path
ENDIF ELSE BEGIN
 CLOSE, unit 
 FREE_LUN, unit
 IF N_ELEMENTS(CTABLE) EQ 0 THEN ctable = 40
 LOADCT, ctable, FILE= jmaplot_path + 'colorjma.tbl', SILENT=quiet
 DEVICE, DECOMPOSED=0
 rgb = [[r_orig],[g_orig],[b_orig]]
ENDELSE
END
;