SCDzine
Spring 1999, Vol. 20, No. 1
H I N T

From AliceBlue to YellowGreen: Name those colors!

How to use the handy new named colors in NCL . . .

Mary Haley
Mary Haley


Contents

SCDzine

Search

Article index

Subscribe

Contact us

SCD

by Mary Haley

This article is geared to current NCAR Graphics users who are using the NCAR Command Language (NCL) to generate graphics for scientific visualization. The new named colors are included in version 4.1.1 of NCAR Graphics, which is scheduled for release this summer -- but you if you have version 4.1 and would like to use named colors right now, you can do so by getting an updated copy of NCL. Simply send email to ncargfx@ucar.edu to receive the new NCL with this capability.

Blanched Almond. Peach Puff. Lemon Chiffon. Papaya Whip. Mint Cream. The NCAR cafeteria's frozen yogurt flavors of the week? No! They are some of the 650 unique new named colors you can now use in the NCAR Command Language (NCL) to define line colors, font colors, map fill colors, contour level colors, and other color-specific items in your graphical output.

Previously, to set the color of a particular aspect of your plot, you had to use a color index value that represented a color defined in your current color map -- for instance, "1.00,1.00,1.00." Now, with the use of named colors , you have the option of setting colors by name as well -- for instance, "white."

To see a list of the new named colors (from AliceBlue to YellowGreen), click here. To see the actual colors, click here.

This article shows different ways to use named colors, provides several actual NCL code examples, and offers some important notes on usage.


Getting set up

To run the NCL examples in this article, you must either have your own copy of the updated version of NCL (to get a copy, send email to ncargfx@ucar.edu) or run the examples on SCD's Silicon Graphics Origin 2000 (dataproc). For information on how to set up your environment to run NCL on dataproc, see the NCAR Graphics section in the SCD UserDoc "Getting started on dataproc."

Note: The following examples use some special plotting functions that are documented in the "Getting started using NCL (GSUN)" user guide; for more information on GSUN's easy-to-use interface to NCL, see "GSUN: Graphics made easier in NCL," in this issue.


How to set a single-color resource

To use a named color when setting a color resource that previously required a color index value, replace the color index value with a color name enclosed in double quotes. For example, to set the font color of your main title to red, use the following line in your NCL script:

"tiMainFontColor" : "red"
Note: The above directions apply to setting a color resource in an NCL script. If you want to use a color name in a resource file, do not use double quotes around the color name:

*tiMainFontColor : red
Example 1: Producing different-colored text strings
colored text For a complete NCL code example of how to use named colors to set a single color resource, click here and save the file. To run the code, type:
ncl < example1.ncl
This example will draw the graphic pictured at left to an X11 window. (Click on the image to see it enlarged.)


How to set an array color resource

To set a resource that previously required an array of color index values, you can now use a combination of named colors and color index values, with each one enclosed in double quotes. For example, to create a filled contour plot with eight contour levels and the fill colors "Red", "DarkRed", color index 5, "GreenYellow", color index 2, "Aqua", "MediumBlue", and "RoyalBlue", you set the cnFillColors resource as follows in your NCL script:

"cnFillColors" : (/"Red", "DarkRed", "5", "GreenYellow", "2", \
                   "Aqua", "MediumBlue", "RoyalBlue"/)

For a description of the cnFillColors resource, see cnFillColors in the NCAR Graphics website.

Example 2: Producing a contour plot with filled contours
graph

For a complete NCL code example of how to set a color resource that requires an array of colors, click here and save the file.

To run the code, type:

ncl < example2.ncl
This example will draw the graphic pictured at left to an X11 window. (Click on the image to see it enlarged.)


How to define a color map

You can also use named colors to define a color map. Previously, you had to define color maps with RGB triplets. Now you can optionally use a combination of RGB triplets and named colors, with each one enclosed in double quotes. For example, to set the wkColorMap resource with a combination of RGB triplets and named colors , your NCL code might look something like this:

"wkColorMap" : (/"white", "black", \
                 "(/1.00,0.00,0.00/)", "(/0.70,0.10,1.00/)", \
                 "(/0.20,1.00,0.10/)", "(/1.00,1.00,0.20/)", \
                 "(/1.00,0.60,0.00/)", "(/0.40,0.50,0.90/)", \
                 "(/1.00,0.80,0.70/)", "HotPink", "DeepSkyBlue", \
                 "OliveDrab", "Grey56"/)
Using the older method of RGB triplets only, you can specify the above color map with the following code:

"wkColorMap" : (/(/1.00,1.00,1.00/), (/0.00,0.00,0.00/),\
                 (/1.00,0.00,0.00/), (/0.70,0.10,1.00/),\
                 (/0.20,1.00,0.10/), (/1.00,1.00,0.20/),\
                 (/1.00,0.60,0.00/), (/0.40,0.50,0.90/),\
                 (/1.00,0.80,0.70/), (/1.00,0.41,0.71/),\
                 (/0.00,0.75,1.00/), (/0.60,0.80,0.20/),\
                 (/0.56,0.56,0.56/)/)
The RGB values for the named colors referenced above, "HotPink", "DeepSkyBlue", "OliveDrab", and "Grey56", were calculated from the file "rgb.txt" (which resides in the directory $NCARG_ROOT/lib/ncarg/database). This file contains integer RGB values for each named color, so to convert these integer values to float values that range from 0.0 to 1.0, divide each one by 255.

FYI: either one of the above code snippets would produce a color map with the following colors:

Note: For more information on color maps, RGB triplets, and wkColorMap, see their respective entries (color tables, RGB triplets, wkColorMap) in the NCAR Graphics documentation.


Two other examples

Here are two other NCL code examples of producing an XY plot and a map plot, respectively.


Example 3: How to produce an XY plot with multiple colored curves
graph

For a complete NCL code example of how to produce an XY plot with multiple colored curves, click here and save the file.

To run the code, type:

ncl < example3.ncl
This example will draw the graphic pictured at left to an X11 window. (Click on the image to see it enlarged.)


Example 4: How to produce a map plot with filled geographical areas
colored globe For a complete NCL code example of how to produce an XY plot with multiple colored curves, click here and save the file. To run the code, type:
ncl < example4.ncl
This example will draw the graphic pictured at left to an X11 window. (Click on the image to see it enlarged.)


Important usage notes about named colors

  • You can specify named colors with mixed case: "AntiqueWhite," "cornflowerblue," "darkGray," and "LightGoldenrodYellow."

  • When setting color resources in an NCL script using named colors , you must enclose each color in double quotes. If you mix named colors and color index values, you must enclose both in double quotes. When setting color resources in a resource file, don't use double quotes at all!

  • When you access a color by name rather than an integer index value, the current color map is searched for the closest match to that color. This means that if you reference a color name that doesn't have a close match in your color map, you may not get the color you expect. To avoid this problem, define your color map as you normally would, and then add the list of named colors you want to use to the end of it.

  • If you want to retrieve the color index value that is associated with a named color, use the function NhlGetNamedColorIndex. If an exact match is not found, the index value of the closest color match is returned. If a bogus color name is passed to this function, a negative integer will be returned.

    (For more information, see the NhlGetNamedColorIndex entry in the NCAR Graphics documentation.)

  • If you create two workstations with different color maps and set up one or more plots using named colors, when you call the procedure for changing the workstation and redraw the plots, the colors may not come out as you expect the second time. For example, try running the NCL script "colorbad.ncl" as follows:

    ncl < colorbad.ncl
    Notice that in the second X11 window that pops up, the text string is red rather than blue. This is because red is occupying the same location in the second color map that blue occupies in the first color map.

    To avoid this problem, make sure that named colors occupy the same locations in the color maps. Alternatively, you can use setvalues to set the font color to "blue" again. (For a description of Setvalues visualization block, see the Setvalues entry in the NCAR Graphics documentation.) See the NCL script, "colorgood.ncl," for an example how to correct this kind of problem.


NCAR Graphics is a registered trademark of the University Corporation for Atmospheric Research (UCAR). Reference to a company or product name does not imply approval or recommendation of that company or product to the exclusion of others. Copyright: 1987-1999 UCAR. The use of this software is governed by a license agreement.

rule
Contents || SCDzine || Search || Article index || Subscribe || Contact us || SCD