NCL Home > Documentation > Graphics

Create your own color table file

If none of the predefined color tables are suitable for your purpose, then you can create your own color table and place it where NCL can load it directly, or keep it in your own personal directory.

Color tables in NCL can only have up to 256 colors. The first two colors are the background and foreground colors respectively. When you create a color table, the foreground and background colors will automatically be added and set to white and black respectively (unless you set the wkForegroundColor and wkBackgroundColor resources in your script or resource file).

To create your own color table, follow these steps:

  1. Generate RGB triplets
  2. Create file with triplets
  3. Move file to appropriate directory
  4. Test your new color table

Generate RGB triplets

First, you need to generate the red/green/blue (RGB) triplets that represent each color in your color table. Do not generate triplets for the foreground and background colors as NCL will automatically set these for you.

RGB triplets in this context are integers from 0 to 255, where the greater the number, the higher concentration of that color. Here are some sample RGB triplets and their corresponding colors:

Red Green Blue Resultant color Red Green Blue Resultant color
0 0 0   0 255 0  
255 255 255   0 0 255  
86 86 86   255 255 0  
255 0 0   255 128 65  

RGB triplets with the same values create varying shades of gray.

Many people find that it is easier to select colors using the HSV (Hue, Saturation, Value) color space rather than RGB. See example color_18 on the Color Fill page for example HSV colors. You can use the hsvrgb function to convert from HSV colors to RGB. See example color_13 on the Color Fill page for example showing how to use the hsvrgb function to convert from HSV values to RGB values. Also, the NCL code gsn_color.ncl allows you to change HSV values to generate color maps. hsvrgb, available in version 4.3.2 and later, replaces the obsolete function hsv2rgb.

Create file with triplets

Decide what you are going to call this color table, and create a file called xxxx.rgb, where xxxx is the color table name. At the beginning of this file, add the two lines:

ncolors=n
# r g b
where n is the number of RGB triplets you have in the file.

Immediately following these two lines, put the RGB triplets in this file with one RGB triplet per line. For example:

160 32 240
0   0  180
Here's a sample color table with 8 colors:
ncolors=8
# r   g   b
160 32  240
0   0   180
60  100 230
120 155 242
176 224 230
46  139 87
100 225 0
210 255 47
Once you add the above color table to NCL, it will actually have 10 colors, as the foreground and background colors will be added.

Move file to appropriate directory

To have NCL automatically see this color table like it does the other color tables, move the file xxxx.rgb to the directory $NCARG_ROOT/lib/ncarg/colormaps/.

Otherwise, if you want to keep the file in a personal directory, then you need to set the environment variable NCARG_COLORMAPS to point to the full path of this directory. Note that once you do this, NCL will no longer see the other color tables that reside in the standard location, so you need to add the standard location as well.

Here are some examples of setting your environment variable for csh and bash environments:

csh:

    setenv NCARG_COLORMAPS /home/haley/colormaps:$NCARG_ROOT/lib/ncarg/colormaps
bash:
    export NCARG_COLORMAPS=/home/haley/colormaps:$NCARG_ROOT/lib/ncarg/colormaps

Test your new color table

To test that your new color table has been created correctly and that NCL can see it, run the following script, replacing xxxx with the name you gave the color table (minus the ".rgb" or ".gp" suffix):

load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
    
begin
  wks = gsn_open_wks("x11","test")
  gsn_define_colormap(wks,"xxxx")
  gsn_draw_colormap(wks)
end
The above script should open an X11 window, load your new color table, and draw it. Note that color indices 0 and 1 will represent the background and foreground colors, respectively.