FUNCTION rgb,r1,g1,b1, $ PALETTE=palette, $ CLEAR=clear, $ VERBOSE=verbose @compile_opt COMMON rgb_data,palette_alloc,palette_rgb IF N_ELEMENTS(verbose) EQ 0 THEN verbose=1 IF N_ELEMENTS(r1) EQ 0 THEN r=0 ELSE r=r1 IF N_ELEMENTS(g1) EQ 0 THEN g=r ELSE g=g1 IF N_ELEMENTS(b1) EQ 0 THEN b=g ELSE b=b1 ; decompose ARRAY[3,*] into 3*ARRAY[*] IF size(r,/N_DIMENSIONS) EQ 2 THEN BEGIN x=size(r,/DIMENSIONS) IF x[0] EQ 3 THEN BEGIN b=r[2,*] g=r[1,*] r=r[0,*] ENDIF ELSE BEGIN PRINT,' [RGB] ERROR: require ARRAY[3,*]' RETURN,-1 ENDELSE ENDIF ; this section transforms float in the range 0...1 to ints in the ; range 0...255 x=SIZE(r) type=x[x[0]+1] IF (type EQ 4) OR (type EQ 5) THEN r=clamp(DOUBLE(r),0.0D0,1.0D0)*255D0 x=SIZE(g) type=x[x[0]+1] IF (type EQ 4) OR (type EQ 5) THEN g=clamp(DOUBLE(g),0.0D0,1.0D0)*255D0 x=SIZE(b) type=x[x[0]+1] IF (type EQ 4) OR (type EQ 5) THEN b=clamp(DOUBLE(b),0.0D0,1.0D0)*255D0 r=clamp(LONG(r),0L,255L) g=clamp(LONG(g),0L,255L) b=clamp(LONG(b),0L,255L) ncolors=!D.N_COLORS IF (!D.NAME EQ 'X') OR (!D.NAME EQ 'WIN') THEN BEGIN DEVICE,GET_DECOMPOSED=decomposed ; IF decomposed EQ 0 THEN ncolors=256 ENDIF IF (NCOLORS LE 256) THEN BEGIN IF N_ELEMENTS(palette_alloc) EQ 0 THEN clear=1 IF N_ELEMENTS(clear) EQ 0 THEN clear=0 IF clear THEN BEGIN palette_alloc=REPLICATE(0b,NCOLORS) palette_rgb=REPLICATE(-1L,NCOLORS) palette_alloc[!P.COLOR<255]=1 palette_alloc[!P.BACKGROUND<255]=1 rx=0 gx=0 bx=0 TVLCT,xr,xg,xb,!P.COLOR,/GET palette_rgb[!P.COLOR<255]=2L^16*rx+2L^8*gx+bx TVLCT,xr,xg,xb,!P.BACKGROUND,/GET palette_rgb[!P.BACKGROUND<255]=2L^16*rx+2L^8*gx+bx IF N_PARAMS() EQ 0 THEN RETURN,-1 ENDIF IF N_ELEMENTS(palette) EQ 0 THEN palette=-1 ipalette=palette npalette=N_ELEMENTS(ipalette) IF ipalette[0] EQ -1 THEN BEGIN npalette=N_ELEMENTS(r) maxcolors=NCOLORS-2 IF npalette GT maxcolors THEN BEGIN IF verbose GT 0 THEN BEGIN PRINT,'[RGB] WARNING: can allocate only '+$ STRTRIM(STRING(maxcolors),2)+$ ' color; will disscard remaining '+$ STRTRIM(STRING(npalette-maxcolors),2)+$ ' colors.' ENDIF npalette=npalette0 npal_new=npal_need-npal_recyc FOR i=0,npal_new-1 DO BEGIN IF (ipalette[i] EQ -1) THEN BEGIN ipalette[i]=x[N_ELEMENTS(x)-1-i] ENDIF ENDFOR FOR i=0,npal_recyc-1 DO BEGIN IF (ipalette[i] EQ -1) THEN BEGIN ; OK, this might ovwewrite something... ipalette[i+npal_new]=maxcolors-i ENDIF ENDFOR IF verbose GT 0 THEN BEGIN IF npal_recyc GT 0 THEN BEGIN PRINT,'[RGB] WRANING recycling colors '+$ STRTRIM(STRING(NCOLORS-1-npal_recyc),2)+ $ ' through '+ $ STRTRIM(STRING(NCOLORS-2),2) ENDIF ENDIF ENDIF IF N_ELEMENTS(ipalette) NE N_ELEMENTS(r) THEN BEGIN npalette=N_ELEMENTS(r) ipalette=ipalette[0]+INDGEN(npalette) ENDIF FOR i=0,npalette-1 DO BEGIN tvlct,r[i],g[i],b[i],ipalette[i] palette_alloc[ipalette[i]]=1 palette_rgb[ipalette[i]]=2L^16*r[i]+2L^8*g[i]+b[i] ENDFOR x=ipalette ENDIF ELSE IF NCOLORS EQ 262144L THEN BEGIN x=(((b/8)*256L)+(g/4))*256L+(r/8) ENDIF ELSE BEGIN x=((b*256L)+g)*256L+r ENDELSE IF N_ELEMENTS(x) EQ 1 THEN x=x[0] RETURN,x END