What is the code to generate a random and automatic color in the background?

Hello, I’m new to processing, and I would like to know if anyone can help me solve a question about how I change my background with random colors. That is, every time I clicked on play and became active, the color would change randomly and automatically.
Thank you

Hi! Welcome to the forum!

Is this a homework assignment? If so please add a tag to the topic. Or are you self learning? Either way, it’s better if you try something, post the code and we give you feedback. Do you have any idea where to begin with?

Yes it is a school exercise. Oh ok sorry, it was the first time I asked a question here. Yes, I already have this, I don’t know if it’s right or not, I’m still very amateur in this:

float r;
float g;
float b;
float a;
float diam;
float x;
float y;

void setup() {
size(2000,2000);
background(0);
smooth();
}

void draw() {
r = random(255);
g = random(255);
b = random(255);
a = random(255);
diam = random(20);
x = random(width);
y = random(height);
}

1 Like

Wow that’s a big screen. I mostly go for 1200, 800

background

use background(r,g,b); in draw()

problem: color changes throughout

you could make a function after the last } :

void mousePressed() {
r = random(255);
g = random(255);
b = random(255);
}
1 Like

Hello,

Please format your code:
https://discourse.processing.org/faq#format-your-code

There are lot of resources (tutorials, references, examples, etc.) here:
processing.org

You want a random number between 0 and 255 ( this is the default colorMode() )

random(256);
will give you a random float between 0 and 256 but not including 256.
You can also convert that to an int.

This is explained in the references.

Some references:

You can slow things down also with frameRate() if you want to get a sense of what your code is doing each cycle of draw().

Suggesting reading:

:)

2 Likes

Okay, thank you very much for your help. I will try!

1 Like

Okay, thank you for your help and for the advices. I will try! :slight_smile:

1 Like

Good afternoon,
Following my initial doubt, I tried to change the code in order to solve my problem, however, I only managed to get the colors to change between black, gray and white and not for more colors. Can someone help me solve the rest of my question? Thank you very much and here’s what I got so far:

float r;
float g;
float b;

void setup() {
 size(1000,1000);
 background(random(255));
 smooth();
 frameRate(1);
}  

void draw() {
  background(random(255)); 
 r = random(RGB, 255);
 g = random(RGB, 255);
 b = random(RGB, 255);
 
 }

There are resources here:
https://processing.org/

There are tutorials, references and examples.

You should be looking at the reference for every method you are using and the related references.

:)

Hello,

Your background is always grey because of this line:

background(random(255));

If you take the reference of the function (that you can find here) you are actually using the following overload: background(gray)

What you do after with your r, g and b variable doesn’t do anything because you are not using them. Try removing those 3 lines and you’ll see your code is working exactly the same way.

It is not because you are using the RGB color mode that setting 3 r, g and b variables are enough to change the color. Actually you can use any name that you want as long as you feed the background() function with the proper value.

Also, you want to check the random() function reference . You are using this overload: random(low, high) where you give low the value RGB and high the value 255

RGB is just an integer that is equal to 1. It is simply used to be easier to read the code when you are changing the color mode because instead of writing colorMode(1) you can write colorMode(RGB) but even they seem different both are equivalent.

All that to say, that what you actually want to do is to create 3 random values and create a color based on them. For example:

void draw() {
  k = random(255);
  l = random(255);
  m = random(255);

  background(k, l, m);
}

As you can see I’m using k, l and m as name of my variables (no need for r, g, b) and I’m using the random(high) overload of the random() function so it will give me a value between 0 included and 255 excluded.

Also you can see I’m using the background(v1, v2, v3) overload to set the color based on my 3 random value.

1 Like

Thank you very much for your help!!! I already got it! :grin: