You cannot instruct a slider to draw its own gradient. However, you can draw a gradient on top of a slider.
From How to draw controlP5 controllers under other processing object? - Processing 2.x and 3.x Forum
cp5.setAutoDraw(false) in setup then call cp5.draw() wherever you want.
It’s used in this example file: http://www.sojamo.de/libraries/controlP5/examples/extra/ControlP5withPeasyCam/ControlP5withPeasyCam.pde
setAutoDraw reference:
And, to draw your own linear gradient:
…so…
import controlP5.*;
ControlP5 controlP5;
int from = color(255, 0, 0);
int to = color(0, 120, 200);
void setup() {
size(500, 700);
controlP5 = new ControlP5(this);
//change the original colors
controlP5.setColorForeground(lerpColor(from, to, 0.5));
controlP5.setColorBackground(color(150, 158, 159));
controlP5.setColorActive(lerpColor(from, to, 0.5));
controlP5.addSlider("grad1")
.setRange(0, 30)
.setValue(20)
.setPosition(100, 80)
.setSize(120, 600)
.setColorValue(200)
.setColorLabel(200);
controlP5.addSlider("grad2")
.setRange(0, 30)
.setValue(20)
.setPosition(260, 80)
.setSize(120, 600)
.setColorValue(200)
.setColorLabel(200);
// draw controls manually so that you can draw on top of them
controlP5.setAutoDraw(false);
}
void draw() {
background(0); // background black
// slider 1: color dynamically, render normally with cp5
Controller c = controlP5.getController("grad1");
color g1 = lerpColor(to, from, c.getValue()/(c.getMax()-c.getMin()));
c.setColorActive(g1);
c.setColorForeground(g1);
// slider 2: render default with cp5.draw() then draw gradient on top
controlP5.draw();
Controller c2 = controlP5.getController("grad2");
slideGradient(c2);
}
void slideGradient(Controller c) {
float[] p = c.getPosition();
float amt = c.getValue()/(c.getMax()-c.getMin());
slideGradient(int(p[0]), int(p[1]), c.getWidth(), c.getHeight(), from, to, amt);
}
/**
* Given a geometry, gradient, and value (amt as a % from min-max)
* manually draw a gradient from the bottom of the box to current value,
* using only part of the full gradient range.
* See: https://processing.org/examples/lineargradient.html
*/
void slideGradient(int x, int y, float w, float h, color c1, color c2, float amt) {
int pos = int((y+h)-(h*amt));
for (int i = int(y+h); i >= pos; i--) {
float inter = map(i, y, y+h, 0, 1);
color c = lerpColor(c1, c2, inter);
stroke(c);
line(x, i, x+w, i);
}
}
One changes color, as in @kll 's example, the other reveals a gradient (which is superimposed over the control).