3

I've been trying to create cellular automata, using the Moore neighborhood, in processing and have been pretty successful so far. I've managed to get the basic system working and now I'm looking to play around with it by adding different features. Right now, I check whether the cell is alive or not. If it is, I apply a color using the fill() function and then I might vary the saturation of that color based on how long the cell has been alive for. But, I want to be able to vary the color of the live cells based on their location such as the

Image shown here:

enter image description here

From the looks of it, it seems like an equation was used to achieve this effect although I'm not entirely sure. This has had me stumped for about 2 days now. I don't want the answer as I would like to figure it out on my own. However, if someone could point me in the right direction I would be very grateful!

Right now, I've made each cell an object of the class Cell. In there I store the cell's x,y coordinates, and current living state. It also contains a draw() method:

enter image description here

which applies a different colour to the cell depending on whether it is living or not (the age variable stores the number for how long the cell has been alive for in seconds).

This:

enter image description here

is what the output looks like so far. Like I said before I would like to get it to look like the example image in the first link.

Dwo
  • 79
  • 7
  • Have you tried using circles and then an if loop to color the cells accordingly if in a circle? – Tobias Wilfert Oct 01 '18 at 08:57
  • Circles could work, but wouldn't the transition between colours be too harsh at the circumference of the circle? – Dwo Oct 01 '18 at 10:12
  • @Dwo tricky without more context/code. A few things to watch out for: 1. 255/age does integer division if `age` is `int`: double check you get the expected values 2. you're changing colorMode per cell I assume (maybe `colorMode(RGB)` should be in the `else` block?) : double check something doesn't override your color modes/colours...you can try [pushStyle()](https://processing.org/reference/pushStyle_.html)/[popStyle()](https://processing.org/reference/popStyle_.html) to isolate drawing styles (color modes and fills) per cell. – George Profenza Oct 02 '18 at 14:07
  • @GeorgeProfenza Thanks for the tips! I'll be sure to go through what you suggested when I find the time. – Dwo Oct 03 '18 at 00:44

1 Answers1

3

Use noise(x,y) to calculate a Perlin Noise value for each cell, based upon its coordinates. Map this value to a hue (or saturation, or brightness) when drawing the cells for a global color gradient effect.


Update: Example code to generate noise that maps better to the whole color spectrum (see before vs after).

{
    final float resolution = 0.0175f;
    noiseDetail(20);
    colorMode(HSB, 1, 1, 1);

    float[] hues = new float[width * height];

    loadPixels();

    float hueMax = 0;
    float hueMin = 1;

    for (int x = 0; x < width; x++) { // Create value hue per pixel.
        for (int y = 0; y < height; y++) {
            int i = (y * width) + x;

            hues[i] = noise(x * resolution, y * resolution); 

            if (hues[i] > max) {
                max = s[i];
            } else {
                if (hues[i] < min) {
                    min = hues[i];
                }
            }
        }
    }

    for (int x = 0; x < width; x++) { // Maps hue value to a more complete spectrum; updates PApplet pixels[].
        for (int y = 0; y < height; y++) {
            int i = (y * width) + x;

            float hue = map(hues[i], min/0.4f, max*0.9f, 0, 1); // constants found by experimenting

            pixels[i] = color(hue, 1, 1); // only map hues in this example
        }
    }

    updatePixels();
}
micycle
  • 3,328
  • 14
  • 33
  • Thanks a lot man! I'd never heard of Perlin Noise before so I looked it up and implemented it into my hue and saturation and I got a program that looks very similar to the one I was trying to imitate. Although, the only problem I have is that there is not enough yellow or cyan in the gradient it generates. It's usually reds, purples, blues, and greens. I looked at the color selector and saw that the hue range for yellow and cyan is very narrow compared to other colors. Do you know if there is a way to get around this? – Dwo Oct 03 '18 at 00:42
  • [Here is a screenshot to the output along with the code from the draw() method](https://imgur.com/a/QqVds4V) I've re-run the sketch multiple times and have not been able to spot as much cyan and nearly no yellows. – Dwo Oct 03 '18 at 00:56
  • @Dwo I noticed the same thing too. The Perlin noise values don't seem to be evenly distributed between 0-1 (I'm seeing a lot of cyan (~0.5) and not much red (~0, ~1.0) and as a result, the values don't produce the entire color spectrum. I've updated the answer, introducing a method to get better Perlin noise for the application (gives underrepresented colors at the noise value extremes a better representation by remapping them). – micycle Oct 03 '18 at 01:40
  • yeah so I played around with the noiseDetail() a bit and managed to get some pretty acceptable [results](https://imgur.com/a/e6x0HON). All I did was adjust the 2 parameters inside noiseDetail() and the min and max values for the map() function. I think I'm pretty happy with what came out of this. Thanks you for your help! – Dwo Oct 03 '18 at 13:04
  • @Dwo Looks nice. – micycle Oct 03 '18 at 13:05