Question

I want to set the color of the turtles based on a variable that I can change in the interface. I am thinking of using a slider which will have values from 1-5, and that value will dictate how many colors will be used to color the turtles.

For example, if the value is two, then the turtles will be colored with two colors. I would appreciate it if I can control what colors are there to be chosen, but random colors will do fine as well.

I used to have a static, two-color setup:

set color one-of [ red blue green brown orange ]

And it was simple as that. But with the dynamic setup, I did it this way. Is there a more efficient way of doing this?

 if groups = 1 [ set color red ]
    if groups = 2 [ set color one-of [ red blue ] ]
    if groups = 3 [ set color one-of [ red blue green ] ]
    if groups = 4 [ set color one-of [ red blue green orange ] ]
    if groups = 5 [ set color one-of [ red blue green orange brown ] ]
Was it helpful?

Solution

You can use sublist to extract to number of colors that you want from your list of desired colors:

let colors sublist [ red blue green orange brown ] 0 groups
ask turtles [ set color one-of colors ]

If you didn't want to always get the same colors in the same order, you could use n-of and base-colors:

let colors n-of groups base-colors
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top