banner-nll

 Colors

NetLogo represents colors as numbers in the range 0 to 140, with the exception of 140 itself. Below is a chart showing the range of colors you can use in NetLogo.

The chart shows that:

  • Some of the colors have names. (You can use these names in your code.)
  • Every named color except black and white has a number ending in 5.
  • On either side of each named color are darker and lighter shades of the color.
  • 0, 10, 20, and so on are all black. 9.999, 19.999, 29.999 and so on are all white.
Code Example: The color chart was made in NetLogo with the Color Chart Example model.

Note: If you use a number outside the 0 to 140 range, NetLogo will repeatedly add or subtract 140 from the number until it is in the 0 to 140 range. For example, 25 is orange, so 165, 305, 445, and so on are orange too, and so are -115, -255, -395, etc. This calculation is done automatically whenever you set the turtle variablecolor or the patch variable pcolor. Should you need to perform this calculation in some other context, use the wrap-color primitive.

If you want a color that's not on the chart, more can be found between the integers. For example, 26.5 is a shade of orange halfway between 26 and 27. This doesn't mean you can make any color in NetLogo; the NetLogo color space is only a subset of all possible colors. A fixed set of discrete hues is available, and you can either decrease the brightness (darken) or decrease the saturation (lighten) of those hues to get your desired color, but you may not decrease both the brightness and saturation.

There are a few primitives that are helpful for working with color shades. The scale-color primitive is useful for converting numeric data into colors. And shade-of? will tell you if two colors are "shades" of the same basic hue. For example,shade-of? orange 27 is true, because 27 is a lighter shade of orange.

Code Example: Scale-color Example shows you how to use the scale-color reporter.

ask turtles [ set color scale-color red age 0 50 ]
;; colors each turtle a shade of red proportional
;; to its value for the age variable

For many models, the NetLogo color system is a convenient way of expressing colors. But sometimes you'd like to be able to specify colors the conventional way, by specifying HSB (hue/saturation/brightness) or RGB (red/green/blue) values. The hsb and rgb primitives let you do this. extract-hsb and extract-hsb let you convert colors in the other direction.

Since the NetLogo color space doesn't include all hues,hsb and rgb can't always give you the exact color you ask for, but they try to come as close as possible.

Code Example: You can use the HSB and RGB Example model to experiment with the HSB and RGB color systems.

With Support from:

The NetLogo Learning Lab is part of modelingcomplexity.org, the home of the Mesa State College Center for Agent-Based Modeling.

This website is copyright by Mesa State College, 2004. All rights are reserved.

Some materials are adapted from the NetLogo User manual, and are copyright Wilensky, U. (1999). NetLogo.  Center for Connected Learning and Computer-Based Modeling. Northwestern University, Evanston, IL.