Controlp5 (cp5): Sync color displayed by colorwheel when variable is changed

I use a colorwheel in my sketch to change the background color. I also have a key set to choose and set random background color from an array. I would like the color wheel to update and display the actual background color when it is changed via the key event. But the colorwheel only displays the last color it was manually set too.

I tried this:
cp5.getController("colorWheel").setValue(currentBackgroundColor) but it yields no result.

I also fiddled with setRGB() instead of setValue(). But to no result. Sliders & Knobs sync seamlessly via setValue() when their variables are changed through keys. I might just miss a tiny detail here but im new to Processing and especially Controlp5. And searching the web did not help much.

Hi @Len,

Welcome to the community!

It helps to troubleshoot your problem if you post your code.
Anyway, please find my attempt to solve your problem in the code below:

import controlP5.*;

ControlP5 cp5;

void setup() {
  size(800, 400);
  cp5 = new ControlP5( this );
  cp5.addColorWheel("c" , 250 , 10 , 200 ).setRGB(color(128,0,255));
  noStroke();
}
  
int c = color(100);

void draw() {  
  background(50);
  fill( c );
  rect(0,240,width,200);
   //println(cp5.get(ColorWheel.class,"c").getRGB()); 
}

void keyPressed(){
 color keyColor = color(0);
 switch(key){
   case 'r':
   keyColor = color(255,0,0);
   break;
   case 'g':
   keyColor = color(0,255,0);
   break;
   case 'b':
   keyColor = color(0,0,255);
   break;
 }
 cp5.get(ColorWheel.class,"c").setRGB(keyColor);
}

Hope it helps!

1 Like

Perfect solution! Thank you so much. :pray: