Store a value, change it, return to original

edited December 2015 in How To...

Hi there,

I have an array of 16 colours my sketch selects from, in order over time, col[h], When i press a key I want to store the value for h, change the colour to black and then when the key is released I want to return to the original value for h so the colours continue to change in order.

I've managed to switch the colour to black by having the key press select colour 17, black, but when the key is released it relies on the timer stepping to the next colour before it changes and it always goes back to 0.

Is there a way to store the current value for col[h] so that other parts of the program can continue to use it and then return to that value when the key is released?

Cheers Rich

Tagged:

Answers

  • edited December 2015
    // forum.processing.org/two/discussion/13861/
    // store-a-value-change-it-return-to-original
    
    // GoToLoop (2015-Dec-09)
    
    static final color[] PALETTE = {
      #FF0000, #008000, #0000FF, #FFFF00, #00FFFF, #FF00FF
    };
    
    final boolean[] offStates = new boolean[PALETTE.length];
    
    int idx;
    
    void setup() {
      size(600, 400);
      smooth(4);
      frameRate(1);
    }
    
    void draw() {
      offStates[idx] = keyPressed;
      frame.setTitle(idx + "  " + hex(PALETTE[idx], 6) + "  " + offStates[idx]);
      background(offStates[idx]? 0 : PALETTE[idx]);
      idx = (idx + 1) % PALETTE.length;
    }
    
  • _vk_vk
    edited December 2015

    Better if we can see the code to help...

    something like this?

    color toUse;
    
    // do your logic assigning the desired color to `toUse`
    // like toUse = colors[0];
    
    fill(toUse);
    

    when key is pressed

    // suspend your timer?
      toUse = black;
    

    then when key is released

    //  restart timer ?
      toUse = color[0];
    

    perhaps?

Sign In or Register to comment.