# Color in constructor

**URL:** <https://discourse.processing.org/t/color-in-constructor/8254>\
**Category:** Beginners\
**Created:** [February 9, 2019, 5:52pm UTC](https://discourse.processing.org/t/color-in-constructor/8254 "2019-02-09T17:52:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZAF](https://avatars.discourse-cdn.com/v4/letter/z/c57346/32.png) [@ZAF](https://discourse.processing.org/u/ZAF)\
**Post date:** [February 9, 2019, 5:52pm UTC](https://discourse.processing.org/t/color-in-constructor/8254/1 "2019-02-09T17:52:06Z")

</div>

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:

```auto
class Something{

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

}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 9, 2019, 6:33pm UTC](https://discourse.processing.org/t/color-in-constructor/8254/2 "2019-02-09T18:33:04Z")

</div>

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

```auto
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](https://processing.org/reference/color_.html)

---

<div class="post-metadata">

**Author:** ![ZAF](https://avatars.discourse-cdn.com/v4/letter/z/c57346/32.png) [@ZAF](https://discourse.processing.org/u/ZAF)\
**Post date:** [February 9, 2019, 7:27pm UTC](https://discourse.processing.org/t/color-in-constructor/8254/3 "2019-02-09T19:27:11Z")

</div>

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

```auto
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);
  }
}

```

```auto
// 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:

```auto
[i] = goes from 0 to 2.

```

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

```auto
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)

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 9, 2019, 7:45pm UTC](https://discourse.processing.org/t/color-in-constructor/8254/4 "2019-02-09T19:45:38Z")

</div>

pls format your code using

```auto
</>

```

code formatter, you see that above is difficult to read and  
shows some things wrong  
like “\*” missing …

---

<div class="post-metadata">

**Author:** ![ZAF](https://avatars.discourse-cdn.com/v4/letter/z/c57346/32.png) [@ZAF](https://discourse.processing.org/u/ZAF)\
**Post date:** [February 9, 2019, 7:57pm UTC](https://discourse.processing.org/t/color-in-constructor/8254/5 "2019-02-09T19:57:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 9, 2019, 8:03pm UTC](https://discourse.processing.org/t/color-in-constructor/8254/6 "2019-02-09T20:03:26Z")

</div>

> [@ZAF](#):
>
> 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.

```auto
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();
}

```

 ![2019-02-10_02-59-49_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/d9ae04f3139e9326077c3149263c0116ea23ad7c.png)

---

<div class="post-metadata">

**Author:** ![ZAF](https://avatars.discourse-cdn.com/v4/letter/z/c57346/32.png) [@ZAF](https://discourse.processing.org/u/ZAF)\
**Post date:** [February 9, 2019, 8:39pm UTC](https://discourse.processing.org/t/color-in-constructor/8254/7 "2019-02-09T20:39:30Z")

</div>

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_

```auto
ff

```

```auto
fff

```

````ff

```ff

**fff**
````
