lerpColor and float[] values do not seem to change

Hi everyone :blush:

I’m currently new to coding and am taking a class for it at a university.

I have a code running which is currently reading the data from the NYC Tree Census and loading svg’s of the leaf shape for the most frequent tree species as they appear in the CSV file. The size of the leaf is determined by how many of each tree species appears in the spreadsheet.

While this is working fine, I am trying to run code which also causes each leaf to change color according to health status of the tree. I have been attempting to use the lerpColor function and ascribing a value for each status as a float between 0 and 1. However, while the float array I have created is functioning, the values do not apply to the lerpColor function as I would have hoped. I have tried numerous methods and have spent hours reading through forums but I can’t seem to find anything that helps.

All of this is happening in a Leaf class which I then call on in my main sketch. This is the specific code related to my issue.

class Leaf {

  color worst, best;
  float[] lerpPosition;
  color finalColor;

Leaf(String filename, String searchterm ) {

    worst = color(255, 0, 0);
    best = color(0, 255, 0); 

    myLeafShape = loadShape(filename);

    lerpPosition = leafColor95().array();

}

 public FloatList leafColor95() {

    FloatList l = new FloatList();

    for (int i = 0; i < ninefive.getRowCount(); i += 1) {

      String species = ninefive.getString(i, "spc_common");
      String health = ninefive.getString(i, "health");

      if (species.indexOf(searchterm) != -1 && health.equals("Dead")) {
        l.append(0.1);
      } else if (species.indexOf(searchterm) != -1 && health.equals("Critical")) {
        l.append(0.2);
      } else if (species.indexOf(searchterm) != -1 && health.equals("Poor")) {
        l.append(0.3);
      } else if (species.indexOf(searchterm) != -1 && health.equals("Fair")) {
        l.append(0.5);
      } else if (species.indexOf(searchterm) != -1 && health.equals("Good")) {
        l.append(0.7);
      } else if (species.indexOf(searchterm) != -1 && health.equals("Excellent")) {
        l.append(0.9);
      } else {
        println("NA");
      }
    }

    l.sort();
    return l;
  }

void changeColor() {

    for (float i : lerpPosition) {
      finalColor = lerpColor(worst, best, i);
    }
  }

  void update() {

    if (year == 1) {
      if (leafSize < numOfNinetyFive) {
        leafSize++;
      }
    }
  }


  void display() {

    da.shapeMode(CENTER); 
    changeColor();
    myLeafShape.setFill(color(finalColor)); 
    da.shape(myLeafShape, xPos, yPos, leafSize, leafSize);
  }
}

What I need to happen is for the color to update alongside leafSize, while right now the entire array loads correctly but the values do not seem to change accordingly.
Thank you for your help! Sorry if this isn’t formatted correctly this is my first post but I’ll try my best to fix it accordingly.

You have a class called Leaf but the actual class defines an array that seems to store all the leaves. Therefore, your class could be renamed so to show this. I would call it instead: class Leaves.

A bit of a challenge as the code you provided is just partial and one cannot run it to see the issue. Notice you are calling sort() inside leafColor95() and that might not work as intended. I might be wrong but I just wanted to call this to your attention.

There is also something that you need to revise. Consider the following code:

void changeColor() {

  for (float i : lerpPosition) { 
        finalColor = lerpColor(worst, best, i); 
   } 
}

Do you see that you iterate through all the array and you seems to store all the values using the same variable, finalColor? I do not think this is what you intend to do. Either you need to draw each leaf individually or you need to define the health color based on an operation on the health status of each leaf. This is not clear when read your post:

While this is working fine, I am trying to run code which also causes each leaf to change color according to health status of the tree

You mean the health status of each leaf? What is the health status of the tree? Can you explain this better?

Kf

Sorry I wasn’t clear, thank you for your reply.
This is the entire code of my Leaf class at the moment.
The health status needs to change incrementally according to each relevant entry in my CSV file.

Here is a sample of my data, and so for e.g., if you take “MAPLE” as an entry, then for each MAPLE entry in the data, according to it’s health status the leaves should be changing color as they grow. I was thinking that the lerpColor values could be replaced with floats from a float array that was linked to this if/else statement block.

Here is the code with the entire class and above I also have an image of what an earlier iteration of my code looked like, however, the colors did not change as I desired for that one as well.

class Leaf {

  float size = 0;
  PShape myLeafShape;
  String searchterm;
  float xPos;
  float yPos;
  float leafSize = 0;
  Float numOfNinetyFive;
  
  color worst, best;
  float[] lerpPosition;
  color finalColor;

  Leaf(String filename, String _searchterm, float _xPos, float _yPos) {

    
    searchterm = _searchterm;
    yPos = _yPos;
    xPos = _xPos;

    worst = color(255, 0, 0);
    best = color(0, 255, 0); 

    myLeafShape = loadShape(filename);

    lerpPosition = leafColor95().array();

    //mapped values, a little misleading
    numOfNinetyFive = map(ninetyfive(), 0, 173937, 150, 1000);



    //purely divided values
    //numOfNinetyFive = ninetyfive()/200;


    println(searchterm + " " + year + ": " + numOfNinetyFive);
  }



  public Float ninetyfive() {

    Float c = 0.0;


    for (int i = 0; i < ninefive.getRowCount(); i += 1) {

      String species = ninefive.getString(i, "spc_common");

      if (species.indexOf(searchterm) != -1) {
        c +=1;
      }
    }
    return c;
  }


  public FloatList leafColor95() {

    FloatList l = new FloatList();

    for (int i = 0; i < ninefive.getRowCount(); i += 1) {

      String species = ninefive.getString(i, "spc_common");
      String health = ninefive.getString(i, "health");

      if (species.indexOf(searchterm) != -1 && health.equals("Dead")) {

        l.append(0.1);
      } else if (species.indexOf(searchterm) != -1 && health.equals("Critical")) {

        l.append(0.2);
      } else if (species.indexOf(searchterm) != -1 && health.equals("Poor")) {

        l.append(0.3);
      } else if (species.indexOf(searchterm) != -1 && health.equals("Fair")) {

        l.append(0.5);
      } else if (species.indexOf(searchterm) != -1 && health.equals("Good")) {

        l.append(0.7);
      } else if (species.indexOf(searchterm) != -1 && health.equals("Excellent")) {

        l.append(0.9);
      } else {
        println("NA");
      }
    }

    return l;
  }


  void changeColor() {

    for (float i : lerpPosition) {
      finalColor = lerpColor(worst, best, i);
    }
  }

  void update() {

    if (year == 1) {
      if (leafSize < numOfNinetyFive) {
        leafSize++;
      }
    }
  }


  void display() {

    da.shapeMode(CENTER); 
    changeColor();
    myLeafShape.setFill(color(finalColor)); 
    da.shape(myLeafShape, xPos, yPos, leafSize, leafSize);
  }
}

Can you please provide a file that we can download for the leaves list, the images if there are some, and the full code with your setup() and draw() method?

We still can’t run it and as @kfrajer said, it is hard to figure out what is going on just by looking at the code.

Thank you @ kfrajer and @ jb4x for your help! I was able to figure out the issue with the help of my tutor. The if statement block is fine, however, it needs to be called in the main sketch instead and the value needs to be updated alongside the leafSize.

1 Like