 # Use an array of color [RESOLVED]

#1

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 :

``````var palette = []

function setup(){
palette = color(154, 202, 62)
palette = color(151, 71, 140)
palette = color(212, 42, 41)
palette = color(252, 217, 76)
palette = color(74, 184, 219)
palette = color(255, 140, 231)
palette = 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
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 !

1 Like
#2

random() -> p5js.org/reference/#/p5/random

`const randomColor = random(palette);`

1 Like
#3

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

#4

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

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 !

#6

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

Smart…never tried before !