Advanced colorramps in NetLogo

Welcome

Welcome everybody to my fresh webpage. Every now and then I’ll post short little code exercises that I think might be helpful for people out there. Most of this will be related to NetLogo and R and the combination of both tools.

In this first post, I will demonstrate how you can use R colorramps, such as the wonderful viridis colorramp, directly in your NetLogo models.

Colorramps

The R package Viridis provides color palettes that are optimized to represent data. The colorramps are easier to read by those with colorblindness and can be easily distinguished when printed in grey scale.

I use viridis colorramps for many data visualization purposes every day. However, I also work quite a lot with NetLogo models and was always missing the possibilty to use nice color palettes for coloring the cells or turtles according to certain properties of these agents.

The palette extension, which comes as a bundled extension with NetLogo actually provides primitives for scaling RGB color lists to agent variables. There is also a function to access the RColorBrewer palettes. However, the extension does not support custom palettes directly. Thus, I created a short code snippet in R that allows you to export any color palette. You can then use these color palettes directly in NetLogo.

Extract color values from the viridis palettes

In order to implement the viridis colorramp into NetLogo, we first need to extract the RGB color values from the viridis palette. In the following code snippet, we use the colormap package to create a palette with RGB values. Afterwards, we write the palette values to a *.txt file in a “NetLogo-friendly” format.

The colormap package supports many different colorramps, including additional viridis palettes, such as the magma or plasma palette. A complete list can be found here. In order to create output files for these colorramps, you only need to adjust the colormaps$viridis parameter within the colormap function call. For example, colormap(colormap = colormaps$magma, format = 'rgb', nshades = n) will report n values of the magma palette.

How to use the palettes

Of course, we need a function that scales our color palette to the variable values of agents. We can use the color scaling function from the palette extension to achieve this. The palette:scale-gradient function works similar to the scale-color primitive of NetLogo. It takes 4 arguments: rgb-color-list number range1 range2. The first argument is a list of RGB color values, such as the list we created with our palette reporter. The function will further take the variable value of the current agent (number). The remaining two values (range1, range2) define the variable range. The function will then scale the variable range onto the range of the selected color palette and selects the item that corresponds to the variable value of the actual agent. Similar to the scale-color primitive, you can revert the direction of the colorramp by switching the range1 and range2 values.

Using the palette:scale-gradient functions works very similar to the scale-color primitive. First you need to load the palette extension in your model (extensions[palette]). Assuming our turtles have a turtles-own variable t_x, we can use this variable to color the turtles according to these values. We first store the extremes of our variable distribution as local variables. We then ask all turtles to set their color, using the palette:scale-gradient function. As function arguments we provide the name of the reporter for the palette of our choice, the name of the turtle variable and the corresponding extreme values.

;; Store extremevalues:
let xmin min [t_x] of turtles
let xmax max [t_x] of turtles

;; Set colors
ask turtles [set color palette:scale-gradient pal.viridis t_x xmin xmax]

Of course, we can do the same for patches:

;; Store extremevalues:
let xmin min [p_x] of patches
let xmax max [p_x] of patches

;; Set pcolors
ask patches [set pcolor palette:scale-gradient pal.viridis p_x xmin xmax]

As explained above, if you want to revert the colorramp, you can just switch the xmin and xmax values:

ask patches [set pcolor palette:scale-gradient pal.viridis p_x xmax xmin]

Of course, we can also use one specific color from a palette. For example, if we want to color one random patch with the 42nd color of the viridis palette:

ask one-of patches [set pcolor item 42 pal.viridis]

Complete NetLogo example including four palettes

Download a NetLogo example model here: colormap.nlogo; colormap.nls

The colormap.nls file contains reporters of four color palettes (viridis, magma, plasma, inferno) and can be included to any NetLogo model. The colormap.nlogo model shows some examples of these color reporters in action. Please download both files, put them in the same folder and open the colormap.nlogo file to run the examples (NetLogo 6.1.0, but should work with older versions as well). Of course you can edit the colormap.nls file and add more palette reporters of any palettes you wish to use.

Have fun coloring your agents, prettier than ever before and stay tuned for more NetLogo and R related content ;)