Heat Map Color Resolution

The code below is my answer to getting "finer" color resolution on the heat Map, ie., adding more colors to choose from.

Is there a more elegant and/or more efficient solution in Processing to do this? Hues, saturation ??

Thanks,

Ken

void applyColor() {  // Generate the heat map

  color c1 = color(0, 0, 255);      // Blue color
  color c2 = color(0, 255, 255);    // color Cyan
  color c3 = color(0, 255, 0);      // Green color
  color c4 = color(255, 255, 0);    // Yellow color
  color c5 = color(240, 150, 5);   // Orange color
  color c6 = color(255, 0, 0);      // Red color

  loadPixels();
  int p = 0;
  color c;
  float fraction;

  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      float value = interp_array[i][j];

      if (value>= 25 && value<26) {
        fraction = value;
        c = lerpColor(c1, c2, fraction);
      } else if (value>=26 && value<27) {
        fraction = value;
        c = lerpColor(c2, c3, fraction);
      } else if (value>=27 && value<28) {
        fraction = value;
        c = lerpColor(c3, c4, fraction);
      } else if (value>=28 && value<29) {
        fraction = value;
        c = lerpColor(c4, c5, fraction);
      } else if (value>=29 && value<30) {
        fraction = value;
        c = lerpColor(c5, c6, fraction);
      } else
        c = c6;
      pixels[p++] = c;
    }
  }
  updatePixels();

Answers

  • Use hsb colours and map() the temp to the hue value.

  • Thanks for your reply and advice. I have tried a few examples but don't understand how these functions go together and also how they fit with the program. Do I eliminate the lerpcolor function? If you could provide me some direction on this it would be greatly appreciated. Thanks, Ken

  • Answer ✓

    The sketch below produces random heat maps like this one. The code should be self explanatory but you might want to investigate the colorMode() method.

    map

    float[][] interp_array;
    
    void setup() {
      size(400, 400);
      interp_array = new float[width][height];
      makeArray();
      applyColor();
    }
    
    // Fill array with Perlin noise (smooth random) values
    void makeArray() {
      for (int r = 0; r < height; r++) {
        for (int c = 0; c < width; c++) {
          // Range is 24.8 - 30.8
          interp_array[c][r] = 24.8 + 6.0 * noise(r * 0.02, c * 0.02);
        }
      }
    }
    
    void applyColor() {  // Generate the heat map
      pushStyle(); // Save current drawing style
      // Set drawing mode to HSB instead of RGB
      colorMode(HSB, 1, 1, 1);
      loadPixels();
      int p = 0;
      for (int r = 0; r < height; r++) {
        for (int c = 0; c < width; c++) {
          // Get the heat map value 
          float value = interp_array[c][r];
          // Constrain value to acceptable range.
          value = constrain(value, 25, 30);
          // Map the value to the hue
          // 0.2 blue
          // 1.0 red
          value = map(value, 25, 30, 0.2, 1.0);
          pixels[p++] = color(value, 0.9, 1);
        }
      }
      updatePixels();
      popStyle(); // Restore original drawing style
    }
    
  • Thanks, I will dig into it and see if I can understand well enough to make it work . . . . I may be back though. :)>-

  • Nice! Works GREAT! . . . . Thanks again Mr. Quark.

  • edited June 2017

    Hey I know this is 2017 but I was wondering if someone could help me out with a related problem. I'm trying to make a real time heat map using data from an arduino using the serial library. I'm sensing temperature from 16 locations on a 4x4 grid and need to extrapolate the measurements between each point. Do you know how i can adapt this to suit my purposes?

  • What resolution are you mapping your 4x4 onto -- 256x256?

    Are your measurements at the outside edges of the grid (eg a tic-tac-toe board of 4x4 points) or in the center of a set of checkerboard squares?

    Can you use linear interpellation, or are you trying to model something about the real properties of temperature?

  • https://en.wikipedia.org/wiki/Diamond-square_algorithm ?

    that uses 4 seed points (and random points) to generate a terrain. i guess the random bits might make this unsuitable (because it's different every time) but you could use noise or seed the random numbers to make it stable.

Sign In or Register to comment.