random color from array
in
Programming Questions
•
6 months ago
Hi all,
I'm very new to Processing (still) but wrote a program were you can type a sentence and every word of that sentence will appear at a random spot. It works fine, but now I want to add one more thing: random text color.
I want every word to be red, yellow or blue on a random basis.
I know how to make the words really random colors by putting "random(255), random(255), random(255)" as the fill or something like that, but I want the color of the words to be randomly selected from an array.
I already created some sort of array and it picks a random color every time I click play, but then all the words appear in the same color. I want every word to be another color, so every time I hit spacebar there should be selected one, but I don't know how to fix that.
I hope I explained it clear enough. Any help is appreciated.
Here's the code I have so far:
- import processing.pdf.*;
- boolean saveOneFrame = false;
- String tekst = "";
- PGraphics textImage;
- float x, y;
- PImage b;
- color cr=color(255,0,0); //defines RED
- color cg=color(0,255,0); //defines GREEN
- color cb=color(0,0,255); //defines BLUE
- color cy=color(255,255,0); //defines YELLOW
- color[] colors = { //random selects one of above colors
- cr,cg,cb,cy
- };
- color c1=(colors[int(random(0,4))]); //assigns a random color from above to c1-4
- void setup() {
- size(1280, 800);
- x = random(340, 880);
- y = random(10, 100);
- PFont font = createFont("moosextrabold", 18);
- textFont(font);
- textImage = createGraphics(width, height, JAVA2D);
- textImage.beginDraw();
- textImage.textFont(font);
- textImage.endDraw();
- b = loadImage("achtergrondzwart.jpg");
- }
- void draw() {
- if (saveOneFrame == true) {
- beginRecord(PDF, "klas-####.pdf");
- }
- background(0);
- noSmooth();
- image(textImage, 0, 0);
- fill(c1);
- text(tekst, x, y);
- text("/schrap/", 20, 30);
- text("typ een zin -", 20, 70);
- text("en sla 'm op ↵", 20, 95);
- if (saveOneFrame == true) {
- endRecord();
- setup();
- saveOneFrame = false;
- }
- stroke(70);
- line(330, 0, 330, height);
- stroke(70);
- line(930, 0, 930, height);
- }
- void keyReleased() {
- switch(key) {
- case BACKSPACE:
- tekst = tekst.substring(0, max(0, tekst.length()-1));
- break;
- case DELETE:
- tekst = "";
- break;
- case TAB:
- tekst += " ";
- break;
- case ENTER:
- textImage.beginDraw();
- textImage.fill(c1);
- tekst = "";
- saveFrame("wintertuin-####.tif");
- setup();
- // saveOneFrame = true;
- case ' ':
- textImage.beginDraw();
- textImage.fill(c1);
- textImage.text(tekst, x, y);
- textImage.endDraw();
- tekst = "";
- x = random(340, 880);
- y = y + random(1, 170);
- break;
- default:
- tekst += key;
- }
- }
Thanks in advance,
roos
1