Colour switching using keys

edited January 2016 in General

I'm trying to swap which array is being used to select the colour of the rectangle drawn.

Was looking for any advice on how to set up the void keyPressed section and how to apply to draw.

Thanks

int R; int G; int B; int C;

color[] Rarray = { color(250, 10, 100), color(225, 100, 100), color(200, 160, 145), color(250, 235, 25), color(225, 25, 195), color(200, 45, 105), color(250, 125, 125), color(225, 190, 75), color(200, 85, 155)};

color[] Garray = { color(100, 250, 10), color(100, 255, 100), color(145, 200, 160), color(25, 250, 165), color(95, 225, 25), color(105, 200, 45), color(125, 250, 125), color(75, 225, 190), color(155, 200, 85)};

color[] Barray = { color(100, 10, 250), color(100, 100, 225), color(145, 160, 200), color(25, 235, 250), color(195, 25, 225), color(105, 45, 200), color(125, 125, 250), color(75, 190, 225), color(155, 85, 200)};

void setup() { size(500,500); strokeWeight(0); background(0); }

void draw() {

R = Rarray[(int)random(0,8)]; G = Garray[(int)random(0,8)]; B = Barray[(int)random(0,8)];

if(mousePressed) { fill(C); drawRect(mouseX, mouseY, 5+(mouseY/8), 5+(mouseX/8)); }

//when mouse released black square formed to erase rectangles else { drawRect(mouseX, mouseY, 15+(mouseY/8), 15+(mouseX/8)); fill(0); } }

void drawRect(int RectX, int RectY, float RectWidth, float RectHeight) { rectMode(CENTER); rect(RectX, RectY, RectWidth, RectHeight); }

void keyPressed() {

if (key == 'r'); { int C = R; }

if (key == 'r'); { int C = G; }

if (key == 'r'); { int C = B; } }

Answers

  • please read the sticky post

    on how to format code

    go back edit your post

    Then

    in keyPressed : you each time ask for r ---- better ask for r then g then b

    moreover, you have ; after the bracket )

    this closes the if clause and stops the part in the brackets { } from working!!!

    now in keyPressed set C = 1

    C=2

    and

    C=3

    in draw()

    use if to evaluate C

    basically if C==1 choose a var from RArray, if C==2 from GArray etc.

  • int R; int G; int B; int C; // fine

    if(mousePressed) { fill(C); // fills with C from top row

    if (key == 'r'); { int C = R; } // makes a new C and doesn't affect the orginial one

    You probably know this and just forgot, but anyways ...
    If you wan't to change the C on the top line in the keyPressed function, then you can't put int infront of it as then you'll create a local int C rather than changing your original global one.
    You even get "The value of the local variable C is not used"-warning
    Local meaning inside of brackets ** {** int C = R; }

    int C = 5;
    void setup(){
      ten();
      println(C); // 5
      twenty();
      println(C); // 20
    }
    void ten(){ int C=10; } // doesn't affect C ontop 
    void twenty(){ C=20; } // changes C ontop  
    

    Also, yes change the 'r' , 'r' , 'r' to 'r' , 'g' , 'b'

  • Please edit your post, select your code and hit ctrl+o to format your code. Make sure there is an empty line above and below your code.

    Kf

Sign In or Register to comment.