## RGB to HSL

### RGB to HSL

Hi !!!
Anybody knows the formula that converts the RGB format, in the HSB format
(or HSL); and vice versa.
Thanks

Daniele

### RGB to HSL

Here you go :-)

// ------------------------------------------------------------//                            RGB_to_HSL
// ------------------------------------------------------------

void RGB_to_HSL(double r,double g,double b,double *h,double *s,double *l)
{
double v;
double m;
double vm;
double r2, g2, b2;

v = max(r,g);
v = max(v,b);
m = min(r,g);
m = min(m,b);

if ((*l = (m + v) / 2.0) <= 0.0) return;

if ((*s = vm = v - m) > 0.0) {
*s /= (*l <= 0.5) ? (v + m ) : (2.0 - v - m) ;
}
else
return;

r2 = (v - r) / vm;
g2 = (v - g) / vm;
b2 = (v - b) / vm;

if (r == v)
*h = (g == m ? 5.0 + b2 : 1.0 - g2);
else if (g == v)
*h = (b == m ? 1.0 + r2 : 3.0 - b2);
else
*h = (r == m ? 3.0 + g2 : 5.0 - r2);

*h /= 6;

Quote:}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

void HSL_to_RGB(int Hue,int Saturation,int Lightness,int *Red,int *Green,int *Blue)
{
double dRed, dGreen, dBlue, dHue = Hue, dSaturation = Saturation, dLightness = Lightness;
if (dHue>=240)
dHue=239;

dHue/=240;
dSaturation /= 240;
dLightness /= 240;
HSL_to_RGB( dHue, dSaturation, dLightness, &dRed, &dGreen,&dBlue );
*Red = dRed * 255;
*Green = dGreen * 255;
*Blue = dBlue * 255;

Quote:}

// ------------------------------------------------------------//                            HSL_to_RGB
// ------------------------------------------------------------

void HSL_to_RGB(double h,double sl,double l,double *r,double *g,double *b)
{
double v;

v = ( l <= 0.5 ) ? ( l * ( 1.0 + sl ) ) : ( l + sl - l * sl );

if ( v <= 0 )
*r = *g = *b = 0.0;
else
{
double m;
double sv;
int sextant;
double fract, vsf, mid1, mid2;

m = l + l - v;
sv = (v - m ) / v;
h *= 6.0;
sextant = h;
fract = h - sextant;
vsf = v * sv * fract;
mid1 = m + vsf;
mid2 = v - vsf;
switch (sextant) {
case 0: *r = v; *g = mid1; *b = m; break;
case 1: *r = mid2; *g = v; *b = m; break;
case 2: *r = m; *g = v; *b = mid1; break;
case 3: *r = m; *g = mid2; *b = v; break;
case 4: *r = mid1; *g = m; *b = v; break;
case 5: *r = v; *g = m; *b = mid2; break;
}
}

Quote:}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

>Hi !!!
>Anybody knows the formula that converts the RGB format, in the HSB format
>(or HSL); and vice versa.
>Thanks

>Daniele

### RGB to HSL

Quote:> Hi !!!
> Anybody knows the formula that converts the RGB format, in
the HSB format
> (or HSL); and vice versa.
> Thanks

Maybe the following site could be useful for you:
http://www.efg2.com/Lab/index.html.

Steve.

### RGB to HSL

Quote:> Hi !!!
> Anybody knows the formula that converts the RGB format, in the HSB
> format (or HSL); and vice versa.
> Thanks

I have written a function for CBuilder that does this. Download it (with
description) from
URL:http://www.stud.ntnu.no/~jornki/web/shared_files/hsv_rgb/hsv_rgb.zip

But hurry, I will remove this file in a couple of days.

JT Kinderaas

Hi,

I'm trying to write an algorithm to convert between RGB and HSL.
(Delphi 3)
I use R,G,B values of 0..255
Could anyone help me ?

The conversion beetween R,G,B to H and S is not the problem, but i
don't know how to calculate L.

Thanx

Andreas