Color Gradients in Perlin Noise

I'm trying to add color to my Perlin Noise sketch, but having trouble. It's only in gray scale. I know I have to map the colors from 0 to 1, since those are the only values that noise( ) can understand. Here's my code:

float inc = 0.06;
int density = 4;
float znoise = 0.0;

void setup(){
  size(500, 500);
  noStroke();
}

void draw(){
  float xnoise = 0.0;
  float ynoise = 0.0;
  for(int y = 0; y < height; y += density){
    for(int x = 0; x < width; x += density){
      float n = noise(xnoise, ynoise, znoise) * 256;
      fill(n);
      rect(y, x, density, density);
      xnoise += inc;
    }
    xnoise = 0;
    ynoise += inc;
  }
  znoise += inc;
}

Then I found this code in an article that helps you add color gradients to Perlin Noise, but it's in C# language. If anybody can translate it, I'd greatly appreciate it. If not, is there a way you know of that can add color to my Perlin Noise sketch? Thanks for any help.

Color GetColor(Color gradientStart, Color gradientEnd, float t)
{
    float u = 1 - t;

    Color color = Color.FromArgb(
       255,
       (int)(gradientStart.R * u + gradientEnd.R * t),
       (int)(gradientStart.G * u + gradientEnd.G * t),
       (int)(gradientStart.B * u + gradientEnd.B * t));

    return color;
}

Color[][] MapGradient(Color gradientStart, Color gradientEnd, float[][] perlinNoise)
{
   int width = perlinNoise.Length;
   int height = perlinNoise[0].Length;

   Color[][] image = GetEmptyArray(width, height);

   for (int i = 0; i &lt; width; i++)
   {
      for (int j = 0; j &lt; height; j++)
      {
         image[i][j] = GetColor(gradientStart, gradientEnd, perlinNoise[i][j]);
      }
   }

   return image;
}

Answers

Sign In or Register to comment.