NCL Website header
NCL Home > Documentation > Graphics > Graphical Interfaces


Chooses an RGB triplet or RGBA quadruplet for a scalar value, given a color map and a range of values.

Available in version 6.2.0 and later.


load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"

	function get_color_rgba (
		color_map  ,           
		levels [*] : numeric,  
		value  [1] : numeric   

	return_val [3] or return_val[4] 



An NCL predefined color map (like "rainbow", "BlueRed", "matlab_jet"), an RGB array, or an RGBA array.


An array of monotonically increasing numeric values of which to compare the scalar value to.


A scalar value that you want to calculate the appropriate RGB/RGBA value for, based on the given levels.

Return value

An RGB or RGBA value from the given color map.


This function uses the same spanning algorithm that NCL uses to choose colors for a filled contour or vector plot, given a particular color map and an array of levels. It uses the number of levels you have to create a "nice" span across the color map such that it starts at the very first color and ends at the very last color, using close to equal spacing. It determines which range the given scalar value falls in, and calculates the appropriate index for the RGB/A value to return.

Here's what the return RGB/A value means, given n values in levels:

RGB/A value(0) : value < levels(0)
RGB/A value(1) : levels(0) ≤ value < levels(1)
RGB/A value(2) : levels(1) ≤ value < levels(2)
RGB/A value(n-1) : levels(n-2) ≤ value < levels(n-1)
RGB/A value(n) : levels(n-1) > value

The RGB/A value returned will be based on the third or first color in the colormap, depending on whether you give it a predefined color map or an array of RGB/RGBA values. If you give it a predefined color map, then the first two colors in the color map are not considered, and the first color used will start at the third color in the color map. If you give it an RGB/RGBA array, then it assumes you want to start at the first color.

The color index values will always end at clen-1, where clen is the number of colors in the given color map.

If you want to get the full array of RGB/A values based on a set of levels, use span_color_rgba.

If you want the color index values returned rather than the RGB or RGBA value, use get_color_rgba.

See Also

span_color_indexes, span_color_rgba, get_color_index, span_named_colors


Example 1

Example 2