We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
116's lerpColor(), HSB? (Read 3239 times)
116's lerpColor(), HSB?
Oct 2nd, 2006, 9:22pm
 
First, thanks for lerpColor in 116, I'm often lerping colors and will be glad to retire my own code for it.

But.. lerping in RGB space frequently produces "mud" if you cross into low saturation territory between the two endpoints (common if hue difference is significant).  Am just wondering if an HSB version, or a version that respected the current colorMode() setting, would be a useful addition?

Something like this perhaps?:
(edited to replace original buggy sample code)

// lerpColorHSB demo

// the original colors from lerpColor() docs
color from = color(204, 102, 0);
color to = color(0, 102, 153);

void setup() {
 size(100,100);
 noLoop();
 stroke(255);
 background(51);
}

void draw() {
 // original lerpColor in RGB space
 // use more steps to illustrate the "mud" near 0.5
 for (int t=0; t<=10; t++) {
   color c = lerpColor(from, to, t/10.0);
   fill(c);
   rect(t*10, 0, 9, 40);
 }
 // lerpColor in HSB space, no "mud"
 for (int t=0; t<=10; t++) {
   color c = lerpColorHSB(from, to, t/10.0);
   fill(c);
   rect(t*10, 50, 9, 40);
 }
}

// linear interpolate two colors in HSB space
color lerpColorHSB(color c1, color c2, float amt) {
 amt = min(max(0.0,amt),1.0);
 float h1 = hue(c1), s1 = saturation(c1), b1 = brightness(c1);
 float h2 = hue(c2), s2 = saturation(c2), b2 = brightness(c2);
 // figure out shortest direction around hue
 float z = g.colorModeZ;
 float dh12 = (h1>=h2) ? h1-h2 : z-h2+h1;
 float dh21 = (h2>=h1) ? h2-h1 : z-h1+h2;
 float h = (dh21 < dh12) ? h1 + dh21 * amt : h1 - dh12 * amt;
 if (h < 0.0) h += z;
 else if (h > z) h -= z;
 colorMode(HSB);
 return color(h, lerp(s1,s2,amt), lerp(b1,b2,amt));
}

Re: 116's lerpColor(), HSB?
Reply #1 - Nov 6th, 2006, 2:25pm
 
yeah, i think its hasty implementation was kinda half-baked. when teaching i'm always encouraging people to use HSB instead, and yet i just did the straight linear lerp for the function.

my thought is actually that it might make more sense to change the function to:

lerpColor(start, stop, amount, MODE);

where MODE could be RGB or HSB, or perhaps an indicator for another algorithm we might develop in the future that does better color interpolation.

my guess is that using the current colorMode() would be annoying because we'd have a lot of people doing this in their code:

colorMode(HSB)
lerpColor(...)
colorMode(RGB)

since you're almost always gonna be in RGB land. the question would be whether lerpColor() without a color mode specified should be allowed, and whether that would default to RGB or HSB or the current color mode.
Re: 116's lerpColor(), HSB?
Reply #2 - Nov 8th, 2006, 9:32pm
 
I'm with you, too many calls to colorMode() otherwise.  I'd vote that the default without a mode specified probably ought to still be RGB, despite any shortcomings, as that's sort of the "native" space.

The mode parameter gets me to wondering if additional colorspace support in general might be beneficial.  Specifically I'm wondering if HSL (HLS) might be a good compliment to HSB (HSV), and might it produce "better" interpolations than HSB?

But I doubt its desirable to clutter the api with all those conversion routines -- how to distinguish the saturation() for HSB versus one for HSL that isn't equivalent?  yikes!  :-D

Anyway I'm not up on the literature, there's probably many fancier ways to interpolate colors - seems like good thesis material.  But just playing around, CIE-L*ab seems to interpolate well.  It's not pretty code, just fwiw as a conversation starter:

www.davebollinger.com/misc/lerpColorTests/
Re: 116's lerpColor(), HSB?
Reply #3 - Nov 8th, 2006, 10:37pm
 
coooool.. thanks for putting that together, that saves me a bunch of time.

CIE-LAB is the one i had in mind when i was talking about other color spaces, though from your example, XYZ looks interesting too.
Page Index Toggle Pages: 1