Color in constructor

Hello there. New to programming here, starting off with processing.

How do i assign color to a constructor?

I tried with a local variable and did not work out well when initializing it in set up with a color code (whether i tried hex or RGB)

constructor in class tab:

class Something{

Something (float tempX, float tempY, color tempC) {
  noStroke();
  fill (tempC);
  x = tempX;
  y = tempY;
  }

}
1 Like

why you not show what/how you try
in a running example?

class Something {
  float posX, posY;
  color colC;
  Something (float posX, float posY, color colC) {
    this.posX = posX;
    this.posY = posY;
    this.colC = colC;
  }

  void draw() {
    noStroke();
    fill (colC);
    circle(posX, posY, 30);
  }
}

Something onething;

void setup() {
  onething = new Something(10, 20, color(200, 200, 0));
}

void draw() {
  background(0, 0, 200);
  translate(width/2, height/2);
  onething.draw();
}

now that is a color made by RGB
color C = color(R,G,B);

https://processing.org/reference/color_.html

1 Like

You made me noticed i had to declare the color variable as global within the class. Solved!, Thanks a lot.

I also noticed i should check out ā€˜thisā€™. Wil it make my life easier not using local variables with different names related to the original global variables?. Here goes the example.
//Class tab

class Walker {

  float x;
  float y;
  color colC;

  Walker (int tempX, int tempY, color colC) {

  fill(colC);
  noStroke();
  x = tempX;
  y = tempY;

  }

void render () {
  
  
  ellipseMode (CENTER);
  ellipse (x,y,25,25);

  }

void step () {

  float chance = random (1);

  if (chance < 0.4){
  x++;
  } else if (chance > 0.4 && chance < 0.6) {
  x--;
  } else if (chance > 0.6 && chance < 0.8) {
  y++; 
  } else if (chance < 0.8 && chance < 0.9) {
  y--;
  }

  constrain (x,0,width-1);
  constrain (y,0,height-1);
  }
}
// Main Tab

Walker [] w = new Walker [3];


void setup () {

  size (1280,600);

  for (int i = 0; i < w.length; i++){

  w [i] = new Walker (width/2,height/2,color (i*50,i*50,i*50,100));  

  }
}

void draw () {
  
  background (0,51,93);
  
  for (int i = 0; i < w.length; i++) {
    
    w[i].render();
    w[i].step();
    
  }

And as we are on it, after you solved my initial issue, i tried to multiply the RGB values by the index, and the three ellipses will run with the same color. Shouldnā€™t it goes like this:

[i] = goes from 0 to 2.

every RGB value in setup is noted as i*50 , shouldn it then give back this:

when [i] = 0  --> i*50 = 0.
when [i] = 1  --> i*50 = 50.
when [i] = 2  --> i*50 = 100.

(i already did the whole processing tutorial by dan in YouTube, now i started in Nature of Code and am trying to add some math logic to basic examples so i can embelish more complex ones i already have made myself, and keep getting stuck with some declarations)

1 Like

pls format your code using

</>

code formatter, you see that above is difficult to read and
shows some things wrong
like ā€œ*ā€ missing ā€¦

Sorry, i noticed other post in the forum look the way you are asking me to do edit this. First time eeeever posting myself a question and donā€™t know how to do that. I might need to earn some ā€œforum posting knowledgeā€ before further posting.

no, just repair above and you see how easy it is.


my version again as array (of class) and RGB color calculated like you do,
works here.

class Something {
  float posX, posY;
  color colC;
  Something (float posX, float posY, color colC) {
    this.posX = posX;
    this.posY = posY;
    this.colC = colC;
  }

  void draw() {
    noStroke();
    fill (colC);
    circle(posX, posY, 30);
  }
}

int many = 3;
Something[] onething = new Something[many];

void setup() {
  for ( int i=0; i<many;i++)   onething[i] = new Something(10*i, 10*i, color(50*i, 50*i, 200-50*i));
}

void draw() {
  background(200, 200, 0);
  translate(width/2, height/2);
  for ( int i=0; i<many;i++)   onething[i].draw();
}

2 Likes

Great, all i had to do is adding the color variable to fill (); in the drawing of the circle. (void draw for you in the class tab).

Thanks, really!!

Iā€™ll try some editing below, ignore.

Blockquote

just trying out the editor, ignore.

fff

fff

ff
fff

```ff

**fff**