colorramp Construct a sequential or diverging color set colorramp

SAS Macro Programs: colorramp

$Version: 1.0
Michael Friendly
York University


The colorramp macro ( [download] get colorramp.sas)

Construct a sequential or diverging color set

The COLORRAMP macro constructs a set of RGB colors ranging from a starting color to an ending color, optionally going through a middle color. The result appears as the COLORVAR= variable in an OUT= output data set, and optionally as a macro variable named by the RESULT= parameter.

The ends and middle of the color scale can be specified either as 6-character RRGGBB hex strings, as 8-character HLS strings (Hhhllss), or as the predefined SAS/Graph color names, e.g., 'very light purple'. Except for RRGGBB, this requires the SAS-supplied COLORMAC macro, available with SAS V8+.

Method

START=, END= and MIDDLE= are each converted to decimal red, green, blue components, and linear interpolation is performed on each, from START= to END=, possibly through MIDDLE=. This means that if you prefer to use HLS colors, the result will not be exactly linear in HLS space, though the difference will probably be small unless very few colors are used.

Usage

The COLORRAMP macro is defined with keyword parameters. The N= parameter is required. The arguments may be listed within parentheses in any order, separated by commas. For example:

  %colorramp(n=10);
  %colorramp(start=red,mid=very light gray,end=0000FF, n=8, display=Y);
  %colorramp(start=red, end=blue, n=6, result=mycolors);
  %put Colors: &mycolors;

  %colorramp(start=H00080FF, end=H07880FF, n=10, out=mycolors);
  %genpat(n=nobs, data=mycolors);

Parameters

N=
Number of colors to be created
START=
Starting color. This should be either a 6-digit hex string (rrggbb), or an 8-char HLS string (Hhhhllss), or the name of a SAS/Graph color. [Default: START=FF0000]
MID=
Middle color. If specified, the resulting colors go from the START= color to the MIDDLE= color, then to the END= color. When N= is an even number, you can get a symmetric set of colors by specifying DUPMID=1; otherwise, there is one fewer color in the set from START= to MID= than from MID= to END=.
END=
Ending color [Default: END=0000FF]
DUPMID=
0 or 1: Duplicate middle color when N=even? [Default: DUPMID=0]
COLORVAR=
Name of color the variable in the OUT= data set. This is a SAS/Graph RGB color of the form CXrrggbb. [Default: COLORVAR=COLOR]
ORDER==
You can specify ORDER=REV to reverse the order of the colors in the OUT= data set and in the RESULT= macro variable.
OUT=
The name of the output data set [Default: OUT=COLORS]
DISPLAY=
Show a display of the colors? [Default: DISPLAY=N]
RESULT=
Name of output macro variable containing color list. If specified, a macro variable of that name is created with a list of all distinct colors in the OUT= data set.

Example

%include macros(colorramp);        *-- or include in an autocall library;

%colorramp(start=red, end=blue, n=7, display=Y, out=mycolors);
proc print data=mycolors;
	id color;
Produces:
  color      red    green    blue     rgb      nc    colornum

 CXFF0000    255      0         0    FF0000     7        1   
 CXD5002B    213      0        43    D5002B     7        2   
 CXAA0055    170      0        85    AA0055     7        3   
 CX800080    128      0       128    800080     7        4   
 CX5500AA     85      0       170    5500AA     7        5   
 CX2B00D5     43      0       213    2B00D5     7        6   
 CX0000FF      0      0       255    0000FF     7        7   

See also

brewerpal Generate Brewer color palette(s)
gensym Macro to generate SYMBOL statements
gensymMacro to generate PATTERN statements
sascolors.sas List all SAS colornames