# Save a color variable

**URL:** <https://discourse.processing.org/t/save-a-color-variable/3556>\
**Category:** Beginners\
**Created:** [September 15, 2018, 5:32pm UTC](https://discourse.processing.org/t/save-a-color-variable/3556 "2018-09-15T17:32:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@hugo](https://discourse.processing.org/u/hugo)\
**Post date:** [September 15, 2018, 5:32pm UTC](https://discourse.processing.org/t/save-a-color-variable/3556/1 "2018-09-15T17:32:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [September 15, 2018, 5:34pm UTC](https://discourse.processing.org/t/save-a-color-variable/3556/2 "2018-09-15T17:34:39Z")

</div>

When I have a question like this, the first place I look is [the reference](https://processing.org/reference/color_datatype.html).

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

```

---

<div class="post-metadata">

**Author:** ![hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@hugo](https://discourse.processing.org/u/hugo)\
**Post date:** [September 15, 2018, 7:29pm UTC](https://discourse.processing.org/t/save-a-color-variable/3556/3 "2018-09-15T19:29:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 15, 2018, 7:36pm UTC](https://discourse.processing.org/t/save-a-color-variable/3556/4 "2018-09-15T19:36:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [September 15, 2018, 7:59pm UTC](https://discourse.processing.org/t/save-a-color-variable/3556/5 "2018-09-15T19:59:05Z")

</div>

> [@hugo](#):
>
> A = A-1;

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:

```auto
float grayScale = 255;

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

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

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [September 15, 2018, 8:20pm UTC](https://discourse.processing.org/t/save-a-color-variable/3556/6 "2018-09-15T20:20:55Z")

</div>

Greyscale? 😪💤 Get some color in it! 🌈

```auto
float mahue = 500;

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

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

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 15, 2018, 10:12pm UTC](https://discourse.processing.org/t/save-a-color-variable/3556/7 "2018-09-15T22:12:31Z")

</div>

> [@Kevin](#):
>
> Instead, you might consider storing a separate `float` value, like this:

Why `float` when `int` or even the syntax sugar `color` suffice? 😕

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [September 15, 2018, 10:21pm UTC](https://discourse.processing.org/t/save-a-color-variable/3556/8 "2018-09-15T22:21:26Z")

</div>

> [@GoToLoop](#):
>
> Why `float` when `int` or even the syntax sugar `color` suffice? 😕

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.

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [September 15, 2018, 11:07pm UTC](https://discourse.processing.org/t/save-a-color-variable/3556/9 "2018-09-15T23:07:28Z")

</div>

`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…

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 15, 2018, 11:10pm UTC](https://discourse.processing.org/t/save-a-color-variable/3556/10 "2018-09-15T23:10:20Z")

</div>

> [@TfGuy44](#):
>
> 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! 🙄

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [September 16, 2018, 12:40am UTC](https://discourse.processing.org/t/save-a-color-variable/3556/11 "2018-09-16T00:40:52Z")

</div>

> [@GoToLoop](#):
>
> The only re-writing would be switching `int` to `float` .

That’s still re-writing code! 😜

---

<div class="post-metadata">

**Author:** ![hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@hugo](https://discourse.processing.org/u/hugo)\
**Post date:** [September 16, 2018, 8:15am UTC](https://discourse.processing.org/t/save-a-color-variable/3556/12 "2018-09-16T08:15:52Z")

</div>

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 :

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

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [September 19, 2018, 5:26pm UTC](https://discourse.processing.org/t/save-a-color-variable/3556/14 "2018-09-19T17:26:08Z")

</div>

> [@hugo](#):
>
> But it is not more legible to use variable color instead of variable float ?

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.
