We are about to switch to a new forum software. Until then we have removed the registration on this forum.
``I should work from rgb to hsb , but for some reason the sliders are freezing I got the color right at least ( more ore less ) Slider RSlider ; Slider GSlider ; Slider BSlider ; Slider HSlider ; Slider SSlider ; Slider VSlider ; float rs = 0, gs=0, bs=0, hs=0, ss=0, vs=0; color filler = color(rs, gs, bs); color filler2= color (hs, ss, vs);
void setup()
{
size(1200, 500);
RSlider = new Slider (150, 350, "R");
GSlider = new Slider (150, 400, "G");
BSlider = new Slider (150, 450, "B");
HSlider = new Slider (450, 350, "H");
SSlider = new Slider (450, 400, "S");
VSlider = new Slider (450, 450, "V");
RSlider.lock();
GSlider.lock();
BSlider.lock();
HSlider.lock();
SSlider.lock();
VSlider.lock();
}
void draw()
{
background (255);
colorMode(RGB, 255);
fill(0, 0, 0);
rect(0, 300, width, height-300);
RSlider.display();
GSlider.display();
BSlider.display();
HSlider.display();
SSlider.display();
VSlider.display();
if (mouseX>0&&mouseX<=150+180) {
float rs = map(RSlider.x, RSlider.rctx, 180, 0, 255);
float bs = map(BSlider.x, BSlider.rctx, 180, 0, 255);
float gs = map(GSlider.x, GSlider.rctx, 180, 0, 255);
fill(rs, gs, bs);
rect(0, 0, width, height-200);
}
if (mouseX>330&&mouseX<width) {
colorMode(HSB, 255);
HSlider.x=(int)hue(filler);
SSlider.x=(int)saturation(filler);
VSlider.x=(int)brightness(filler);
float hs = map(HSlider.x, HSlider.rctx, 180, 0, 255);
float ss = map(SSlider.x, SSlider.rctx, 180, 0, 255);
float vs = map(VSlider.x, VSlider.rctx, 180, 0, 255);
fill(hs, ss, vs);
rect(0, 0, width, height-200);
}
}
void mousePressed()
{
RSlider.unlock();
GSlider.unlock();
BSlider.unlock();
HSlider.unlock();
SSlider.unlock();
VSlider.unlock();
}
void mouseDragged()
{
RSlider.drag();
GSlider.drag();
BSlider.drag();
HSlider.drag();
SSlider.drag();
VSlider.drag();
}
void mouseReleased()
{
RSlider.lock();
GSlider.lock();
BSlider.lock();
HSlider.lock();
SSlider.lock();
VSlider.lock();
}
class Slider
{
// Slider mySlider ; // ?????????????????????????????????????
PFont f;
float x, y,
w=180, h = 10,
rctx;
String text;
boolean locked ;
Slider (int x, int y, String text ) {
rctx=x;
this.x = x ;
this.y = y ;
locked = true ;
this.text=text;
f = createFont(text, 20, true);
textFont(f);
}
void lock ()
{
locked = true ;
}
void unlock ()
{
if (mouseX>=x-(w/2) &&
mouseX<= x+(w/2) &&
mouseY>= (y)-(h/2) &&
mouseY<= (y)+(h/2))
locked = false;
}
void drag()
{
if (!locked)
{
x=mouseX;
}
}
void display()
{
x=constrain (x, rctx, rctx+w);
fill(200, 200, 200);
rect(rctx, y, 180, 10);
ellipse(x, y+5, 20, 20);
text(text, rctx-40, y+12 );
}//method
//
}// class
Answers
this
won't work.
this:
why do you have a textFont H or R ??
in 32-34 and 42 - 45 you used float so those were local vars forgetting immediately there values. But you want the vars from before setup in fact.
Oh yeah , forgot to mention when rgb sliders change hsb sliders should change accordingly and viceversa . This is why I initialized all those values .
I see a corrected a bunch of other things
you should try this now yourself:
in the mean time this is what I did btw . Nothing is changing though why ? Slider RSlider ; Slider GSlider ; Slider BSlider ; Slider HSlider ; Slider SSlider ; Slider VSlider ; float rs, gs, bs, hs, ss, vs; color filler = color(rs, gs, bs); color filler2= color (hs, ss, vs);
as I said better work with my version, yours has too many errors
a few errors I already told you and you ignored it. Bad.
another error that is corrected only in my code (which you ignored as well):
This
is inside the ifs. That's why it doesn't have effect.
Solution: see my code.
this is a mess:
here some corrections:
I rewrote pretty much all of your code now.
What bothers me is that we still check on which slider we work from draw (see above
if (mouseX>=SSlider.x-22....
) and check it inside the class. It would be much better to have it only inside the class and each slider has a unique ID to give back to draw and draw decides which color to change.One thing
One thing I changed throughout: your x in the class was ambiguous. I think it had sometimes an absolute value (where the ellipse is measured from the window border on the left side) and sometimes a relative value (measured from the left side of the current slider). I made it so, that always the last way is used (measured from the left side of the current slider). This means x is for every slider between 0 and w (not matter where the slider is).
Where the slider is is stored in rctX.
But this change (that x is only between 0 and w) brought a lot of changes throughout your code. E.g. I invented a new function in your class:
Also constrain is now:
Also in the constructor we set rctX from the parameter x but not this.x, the x in the class (since this is initially 0).
we draw the ellipse on the slider as
Best, Chrisir ;-)
I wrote:
I worked on that and we only check it inside the class now.
The new variable
lastSliderName
tells us which slider was moved last and whether we copy from rgb to hsb or vice versa.Also, we don't care anymore if slider HSlider or SSlider or VSlider was changed for the lines of type
hs = map(HSlider.x, 0, HSlider.w, 0, 255);
; we just always read all three sliders whenlastSliderName
tells us, whether rgb or hsb slider group has been changed.