Skip to content

Instantly share code, notes, and snippets.

@nataliefreed
Created September 28, 2016 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nataliefreed/2df85829c59d9a118eaa130ba4f0b52f to your computer and use it in GitHub Desktop.
Save nataliefreed/2df85829c59d9a118eaa130ba4f0b52f to your computer and use it in GitHub Desktop.
Array example in P5, make an array of colors and draw a rainbow. Includes how to append and splice in elements
var rainbow; //global variable
function setup() {
createCanvas(600, 600);
//make the list (array) of rainbow colors
rainbow = [color('#c0392b'), color('#e67e22'), color('#f1c40f'), color('#2ecc71'), color('#3498db'), color('#8e44ad')];
//shift to the center and down a bit
translate(width / 2, height / 3);
//draw the rainbow!
drawTheRainbow();
//woops we forgot to include indigo! Let's splice it in
//let's retrieve red
var blue = rainbow[4];
//and violet
var violet = rainbow[5];
//halfway between blue and violet
var indigo = lerpColor(blue, violet, 0.5);
//splice arguments are where to splice, how many to delete, item to splice in
rainbow.splice(5, 0, indigo);
//draw the rainbow again!
translate(0, height / 3);
drawTheRainbow();
}
function drawTheRainbow() {
strokeWeight(10);
for (var i = 0; i < rainbow.length; i++) {
stroke(rainbow[i]);
arc(0, 0, 300 - i * 30, 250 - i * 30, PI, TWO_PI);
}
}
function draw() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment