;+ ; NAME: ; CTABLE_DISCRETE ; ; PURPOSE: ; Create a custom color table with discrete colors ; Works on IDL 8.2.1 or later version due to use of COLORTABLE function ; ; CALLING SEQUENCE: ; ct = CTABLE_DISCRETE(colors, values) ; ; INPUTS: ; colors : array including the necessary colors. ; it may contain string names or RGB values. ; example 1 : ['black', 'red', 'green', 'blue'] ; example 2 : [[0, 0, 0], [255, 0, 0], [0, 255, 0], [0, 0, 255]]) ; at least two colors should be provided, or -1 is returned ; values : optional parameter for data values for color seperation. ; number of values should be N_ELEMENTS(colors)+1 or -1 is returned ; minimum and maximum data value should be also included. ; example : [0, 0.2, 0.5, 0.6, 1] means data ranges from 0 to 1 ; if not used, intervals between each value are regarded to be even ; ; OUTPUT: ; an array of color table (byte array of 256 by 3) ; ; FUNCTIONS CALLED: ; COLORTABLE ; ; REVISION HISTORY: ; Written by Sangwoo Lee Feb, 2017 ; (Created on IDL 8.6) ; Fixed some minor problems Mar, 2023 ;- FUNCTION CTABLE_DISCRETE, colors, values IF colors EQ !null THEN colors = ['red', 'green', 'blue'] IF SIZE(colors, /N_DIM) EQ 2 THEN ncl = (SIZE(colors, /DIM))[1] ELSE $ ncl = N_ELEMENTS(colors) IF ncl LT 2 THEN RETURN, -1 ctb0 = COLORTABLE(colors, NCOLORS=ncl) & HELP, ctb0 IF values NE !null THEN BEGIN IF N_ELEMENTS(values) NE ncl+1 THEN RETURN, -1 ltot = ABS(values[N_ELEMENTS(values)-1]-values[0]) ci1 = 0 ctb = BYTARR(256, 3) FOR j = 1, N_ELEMENTS(values)-1 DO BEGIN ci2 = FIX(ABS(values[j]-values[0])/FLOAT(ltot)*255.)<255 PRINT, ci1, ci2 ctb[ci1:ci2, *] = CONGRID(ctb0[j-1, *], ci2-ci1+1, 3) ci1 = ci2+1 ENDFOR ENDIF ELSE ctb = CONGRID(ctb0, 256, 3) RETURN, ctb END