# Use an array of color \[RESOLVED\]

**URL:** https://discourse.processing.org/t/use-an-array-of-color-resolved/6936
**Category:** Beginners
**Created:** [December 27, 2018, 8:32pm UTC](https://discourse.processing.org/t/use-an-array-of-color-resolved/6936 "2018-12-27T20:32:08Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Pr0tonX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pr0tonx/32/2590_2.png) [@Pr0tonX](https://discourse.processing.org/u/Pr0tonX)
#### Post date: [December 27, 2018, 8:32pm UTC](https://discourse.processing.org/t/use-an-array-of-color-resolved/6936/1 "2018-12-27T20:32:08Z")

</div>

Hi everyone,  
I have trouble with the use of an array of colors : once I setup it, I want to call randomly one of the colors inside.  
For now, I have that :

```auto
var palette = []

function setup(){
 palette[0] = color(154, 202, 62)
    palette[1] = color(151, 71, 140)
    palette[2] = color(212, 42, 41)
    palette[3] = color(252, 217, 76)
    palette[4] = color(74, 184, 219)
    palette[5] = color(255, 140, 231)
    palette[6] = color(193, 115, 15)
}

function draw(){
// I skip details, but I call my function animA when the key "A" is pressed, so a sound is played and my anim is called.
animA()
}

function animA() {
    push()

    soundAFFT.analyze()
    let basses = soundAFFT.getEnergy("bass")
    // console.log(basses)
    translate(width / 2, height / 2)

    let angle = TWO_PI / 80

    for (let j = 0; j < height / 2; j = j + 40) {
        push()
        rotate(random(j))
        noFill()
        strokeWeight(40)
        strokeCap(SQUARE)
        stroke(palette[random(6)])
        console.log(palette[random(6)])

        let direction = int(random(0, 2) < 1) ? 1 : -1

        push()
        if (basses > 225) rotate(map(basses, 0, 255, PI / 8, TWO_PI) * direction)
        beginShape()
        for (let i = 0; i < 72; i++) {
            let x = (cos(angle * i) * ((height / 2) - j))
            let y = (sin(angle * i) * ((height / 2) - j))
            vertex(x, y)
        }
        endShape()
        pop()
    }
    pop()
}

```

And I have this error :

> Uncaught Error: [object Arguments]is not a valid color representation.

wich refers to p5.min.js library, so I don’t know what is really my mistake…  
I saw an example here : [https://forum.processing.org/two/discussion/17621/array-of-colors#Item\_1](https://forum.processing.org/two/discussion/17621/array-of-colors#Item_1)  
wich use a function to fill the array, but I should be ok if I set it up in the setup, right ?

Thanks for your coming answers !

---

<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: [December 27, 2018, 9:09pm UTC](https://discourse.processing.org/t/use-an-array-of-color-resolved/6936/2 "2018-12-27T21:09:42Z")

</div>

**random()** -\> [p5js.org/reference/#/p5/random](http://p5js.org/reference/#/p5/random)

`const randomColor = random(palette);`

---

<div class="post-metadata">

### Author: ![Pr0tonX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pr0tonx/32/2590_2.png) [@Pr0tonX](https://discourse.processing.org/u/Pr0tonX)
#### Post date: [December 27, 2018, 11:11pm UTC](https://discourse.processing.org/t/use-an-array-of-color-resolved/6936/3 "2018-12-27T23:11:35Z")

</div>

Oh, right, thank you.  
What a variable of type **const** can provide in this situation ? A simple **var** isn’t appropriate ?

---

<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: [December 27, 2018, 11:17pm UTC](https://discourse.processing.org/t/use-an-array-of-color-resolved/6936/4 "2018-12-27T23:17:48Z")

</div>

`const` is a more proper way to declare a variable which isn’t supposed to be reassigned later: 🤓

> **[const](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const)**
>
> The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

---

<div class="post-metadata">

### Author: ![Pr0tonX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pr0tonx/32/2590_2.png) [@Pr0tonX](https://discourse.processing.org/u/Pr0tonX)
#### Post date: [December 27, 2018, 11:59pm UTC](https://discourse.processing.org/t/use-an-array-of-color-resolved/6936/5 "2018-12-27T23:59:15Z")

</div>

As I change its value at every iteration, it’s not appropriate so. This is why I didn’t see why it was more proper to use it.  
Thank you !

---

<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: [December 28, 2018, 12:48am UTC](https://discourse.processing.org/t/use-an-array-of-color-resolved/6936/6 "2018-12-28T00:48:50Z")

</div>

> [@Pr0tonX](#):
>
> As I change its value at every iteration, it’s not appropriate so.

We can declare variables w/ `const` even inside a loop too. 😳

---

<div class="post-metadata">

### Author: ![Pr0tonX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/pr0tonx/32/2590_2.png) [@Pr0tonX](https://discourse.processing.org/u/Pr0tonX)
#### Post date: [December 28, 2018, 12:59am UTC](https://discourse.processing.org/t/use-an-array-of-color-resolved/6936/7 "2018-12-28T00:59:32Z")

</div>

Smart…never tried before !
