# Interpolating between two HSL Values

## Recommended Posts

I am attempting to interpolate between two different HSL value, but I can't seem to figure out why this does not work:

```double lerp(double a, double b, double t){
return a * (1 - t) + b * t ;
}

ColorBgra out_hsl(double hue_a,double hue_b,double sat_a,double sat_b,double intensity_a,double intensity_b,double pos){
//new_pos is always in range [0,1).
double new_pos = pos - Math.Floor(pos);
double H = lerp(hue_a,hue_b,new_pos);
double S = lerp(sat_a,sat_b,new_pos);
double L = lerp(intensity_a,intensity_b,new_pos);
int section = (int)(H/60);

double C=(1 - Math.Abs(2*L-1))*S;
double X=C*(1-Math.Abs((double)(section%2-1)));
double m = L - C/2;
double red,green,blue;
switch(section){
case 0:
red = C; green = X; blue = 0;
break;
case 1:
red = X; green = C; blue = 0;
break;
case 2:
red = 0; green = C; blue = X;
break;
case 3:
red = 0; green = X; blue = C;
break;
case 4:
red = X; green = 0; blue = C;
break;
default:
red = C; green = 0; blue = X;
break;
}
red=(red+m)*255;
green=(green+m)*255;
blue=(blue+m)*255;
return ColorBgra.FromBgraClamped((int)(blue),(int)(green),(int)(red),255);
}```

I have used this source : https://www.rapidtables.com/convert/color/hsl-to-rgb.html

It should work because the formula used here are the same.

Edited by Reptillian

G'MIC Filter Developer

##### Share on other sites

What do you mean by "does not work" ... what outcome are you getting, and what were you expecting

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

##### Share on other sites

Well, I already solved it.  I solved it by isolating the HSL2RGB function into a test code, and figured what's wrong. For anyone stumbling upon this problem:

```ColorBgra hsl2rgb(double H,double S,double L){
int iH = (int)(H)/60;
double C = (1-Math.Abs(2*L-1))*S;
double X = C * (1 - Math.Abs((H/60)%2-1));
double m = L - C/2;
double tr,tg,tb;
switch(iH){
case 0:
tr=C;
tg=X;
tb=0;
break;
case 1:
tr=X;
tg=C;
tb=0;
break;
case 2:
tr=0;
tg=C;
tb=X;
break;
case 3:
tr=0;
tg=X;
tb=C;
break;
case 4:
tr=X;
tg=0;
tb=C;
break;
default:
tr=C;
tg=0;
tb=X;
break;
}
tr+=m;
tg+=m;
tb+=m;
tr*=255;
tg*=255;
tb*=255;
tr=Math.Round(tr);
tg=Math.Round(tg);
tb=Math.Round(tb);
return ColorBgra.FromBgraClamped((int)(tb),(int)(tg),(int)(tr),255);
}```

Edited by Reptillian

G'MIC Filter Developer

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×