Random colors for circle - red/blue/green

Hello all, I am starting a processing project for school and I want the circle to randomly change color (either red, blue or green) everytime it is mousepressed.

def setup():
    size(500, 500)
    global x, y, speed, acceleration, bgImage
    x, y = -50, -50
    speed = 2
    acceleration = 0.4
    bgImage = loadImage("backgroundimage.jpeg")
    
def mouseReleased():
    global x, y, speed
    x = mouseX
    y = mouseY
    speed = 1
    
def draw():
    noStroke()
    global x, y, speed, bgImage
    image(bgImage, 0, 0, width, height) 
    fill(random?) # here 
    ellipse(x, y, 40, 40)
    y = y + speed
    speed = speed + acceleration
    fill(0, 255, 0)
    rect(400,390,20,100)
    fill(255, 0, 0)
    rect(350,350,20,100)
    fill(0, 0, 255)
    rect(200,330,20,100)
1 Like

You’ll want to combine the random() and fill() functions –

2 Likes

Also, if you want the color to be fixed if you have not pressed the mouse, you may want to have a global variable, something like fill_color, to keep the color, and the color() function will build a color for you to store that you can change when you press the mouse…

fill_color = color(100, 100, 200) # blueish start

def draw():
    noStroke()
    global x, y, speed, bgImage
    image(bgImage, 0, 0, width, height) 
    fill(fill_color)
    ...

def mousePressed():
    global fill_color
    fill_color = color(    ...   )

3 Likes

Thank you! I put this into my code using fill_color = color(255, 0, 0), but say I don’t want it just to be red, I want it to be green sometimes and blue sometimes (randomly), how do I do that? I tried using the random() function but the color changes too quickly

1 Like

I didn’t want to spoil your research, but see if this makes sense:

def mousePressed():   # this will run once in the event of the mouse being pressed
    global fill_color
    fill_color = color(random(256), random(256), random(256))