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


Given the number of desired color values, return an array of indexes that nicely span the given color map.

Available in version 6.2.0 and later.


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

	function span_color_indexes (
		color_map      ,           
		num_colors [1] : integer   

	return_val [num_colors] :  integer



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


The number of color index values you want returned.

Return value

An array of integer indexes into 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. It looks at the number of colors you want and the size of your color map, and creates a "nice" span across the color map such that it starts at the very first color and ends at the very last color.

This function can be useful, for example, if you are drawing a scatter plot, where you need to color groups of markers based on some range of data they fall in.

The index values returned will start at 2 or 0, 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 indexes, 0 and 1, are not considered, and the index values will hence start at index 2. If you give it an RGB/RGBA array, then it assumes you want to start at the first index (index 0).

To have RGB or RGBA values returned instead, use span_color_rgba.

See Also

span_color_rgba, get_color_index, get_color_rgba, span_named_colors


Example 1

Assume you are using the StepSeq25 predefined color map, which has 27 colors (this includes color indexes 0 and 1), and that you want 10 nicely spanned colors:

   icol = span_color_indexes("StepSeq25",10)                
Note that the indexes start at 2, because color indexes 0 and 1 are ignored:

Variable: icol
Type: integer
Total Size: 40 bytes
            10 values
Number of Dimensions: 1
Dimensions and sizes:   [10]
(0)     2
(1)     5
(2)     7
(3)     10
(4)     13
(5)     15
(6)     18
(7)     21
(8)     23
(9)     26
Example 2

Assume you want 20 colors from only the first half of the the amwg256 predefined color map.

First you need to read it in with read_colormap_file, and then subscript it as appropriate:

   cmap = read_colormap_file("amwg256")    ; returns 254 x 4 array
   icol = span_color_indexes(cmap(0:126,:),20)
Note that the indexes start at 0, because you are just giving it an array of RGBA values, and it makes no assumption about the first two colors.

Variable: icol
Type: integer
Total Size: 80 bytes
            20 values
Number of Dimensions: 1
Dimensions and sizes:   [20]
(0)     0
(1)     7
(2)     13
(3)     20
(4)     27
(5)     33
(6)     40
(7)     46
(8)     53
(9)     60
(10)    66
(11)    73
(12)    80
(13)    86
(14)    93
(15)    99
(16)    106
(17)    113
(18)    119
(19)    126