# Random colors for circle - red/blue/green

**URL:** https://discourse.processing.org/t/random-colors-for-circle-red-blue-green/36442
**Category:** Processing.py
**Tags:** homework
**Created:** [April 19, 2022, 10:09am UTC](https://discourse.processing.org/t/random-colors-for-circle-red-blue-green/36442 "2022-04-19T10:09:48Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![maya](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@maya](https://discourse.processing.org/u/maya)
#### Post date: [April 19, 2022, 10:09am UTC](https://discourse.processing.org/t/random-colors-for-circle-red-blue-green/36442/1 "2022-04-19T10:09:48Z")

</div>

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.

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

```

---

<div class="post-metadata">

### Author: ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)
#### Post date: [April 19, 2022, 10:26am UTC](https://discourse.processing.org/t/random-colors-for-circle-red-blue-green/36442/2 "2022-04-19T10:26:16Z")

</div>

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

- [https://py.processing.org/reference/random.html](https://py.processing.org/reference/random.html)
- [https://py.processing.org/reference/fill.html](https://py.processing.org/reference/fill.html)

---

<div class="post-metadata">

### Author: ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)
#### Post date: [April 19, 2022, 12:41pm UTC](https://discourse.processing.org/t/random-colors-for-circle-red-blue-green/36442/3 "2022-04-19T12:41:03Z")

</div>

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…

```python
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( ... )

```

---

<div class="post-metadata">

### Author: ![maya](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@maya](https://discourse.processing.org/u/maya)
#### Post date: [April 19, 2022, 2:09pm UTC](https://discourse.processing.org/t/random-colors-for-circle-red-blue-green/36442/4 "2022-04-19T14:09:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)
#### Post date: [April 19, 2022, 5:45pm UTC](https://discourse.processing.org/t/random-colors-for-circle-red-blue-green/36442/5 "2022-04-19T17:45:42Z")

</div>

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

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

```
