Save a color variable

hello ,
My question is simple, is it possible to save a color variable ?
thank you.

When I have a question like this, the first place I look is the reference.

color c1 = color(204, 153, 0);
color c2 = #FFCC00;
noStroke();
fill(c1);
rect(0, 0, 25, 100);
fill(c2);
rect(25, 0, 25, 100);
color c3 = get(10, 50);
fill(c3);
rect(50, 0, 50, 100);

Hello,
Thank you for showing me the reference, but I did not understand.
I just made a simple little code that shows my problem :

color A= color(255);

void setup () {
background(0);
size(400,600);
}

void draw () {
fill(A);
rect(100,200,200,200);
if(mousePressed) {
A = A-1;
}
}

How to save the variable A ?
Thank you.

Hi Hugo,

Please format your code.
Hit the pen button on the bottom right of your previous post to edit it, select your code and hit this icon </> in the toolbar of the edit window.
Don’t forget to hit ctrl+t in processing before in order to properly indent your code.

Now back to your code what you are doing is working. If you keep holding down your mouse button you’ll see the rectangle becoming yellow over time then white again and so on.

I assume though that it is not the behavior that you expect right? You expect it to become black over time no?

Although you can treat colors as integer values, it generally doesn’t make sense to add or subtract to them like this.

Instead, you might consider storing a separate float value, like this:

float grayScale = 255;

void setup () {
  background(0);
  size(400,600);
}

void draw () {
  fill(grayScale);
  rect(100,200,200,200);
  if(mousePressed) {
    grayScale = grayScale - 1;
  }
}

Greyscale? :sleepy::zzz: Get some color in it! :rainbow:

float mahue = 500;

void setup () {
  size(400,600);
  colorMode(HSB, 500);
}

void draw () {
  background(mahue, 500, 500);
  if(mousePressed) {
    mahue++; mahue%=500;
  }
}
1 Like

Why float when int or even the syntax sugar color suffice? :confused:

Because float seems to be a reasonable default type to use in Processing. But you’re right that OP should choose whatever type is most appropriate for their context.

float because what if I wanted to change the hue by an amount of 0.1 instead of 1?
If I had picked int for my mahue variable, it would require re-writing code…

The only re-writing would be switching int to float. Nothing else! :roll_eyes:

That’s still re-writing code! :stuck_out_tongue_winking_eye:

Hello,
Thank you all for your responses.
But it is not more legible to use variable color instead of variable float ?
Here is another code :

color A= color(255,255,255);

void setup () {
  background(0);
  size(400,600);
}

void draw () {
  background(255);
  fill(255);
  strokeWeight(10);
  line(width/2,0,width/2,height);
  line(0,height/2,width,height/2);
  fill(A);
  rect(100,200,200,200);
  if(mousePressed && mouseX < 200 && mouseY < 300) {
    A = color(100,0,200);
  }
  if(mousePressed && mouseX > 200 && mouseY < 300) {
    A = color(140,140,140);
  }
  if(mousePressed && mouseX < 200 && mouseY > 300) {
    A = color(150,200,50);
  }
  if(mousePressed && mouseX > 200 && mouseY > 300) {
    A = color(25,100,250);
  }
}

I would like variable A to register so that when I close the program and then open it, the square in the middle has the color that has been given to it before.

Thank you.

That’s really up to you. Which do you think is more eligible?

I would recommend using a better variable name: A doesn’t really tell you what the variable holds or what it’s used for.

As the posts in this thread show, there are many ways to store a color. You can use a float variable (or 3 of them), or you can use a color variable. Which one you choose really depends on you and your preferences.