Found a pair of functions in my old xEdit scripts to convert colors r,g,b <-> h,s,l

I used them to tweak luminance while preserving colors and changing saturation preserving luminace of imagespaces, weather colors, tints, etc in my personal lighting mods, maybe you'll find them useful too.

//============================================================================
procedure rgb2hsl(red, green, blue: Byte; var h, s, l: Double);
var
r, g, b, cmax, cmin, d, dd: double;
begin
cmin := Min(red, Min(green, blue)) / 255;
cmax := Max(red, Max(green, blue)) / 255;
r := red / 255;
g := green / 255;
b := blue / 255;
h := (cmax + cmin) / 2;
s := (cmax + cmin) / 2;
l := (cmax + cmin) / 2;
if cmax = cmin then begin
h := 0;
s := 0;
end
else begin
d := cmax - cmin;
if l > 0.5 then s := d / (2 - cmax - cmin) else s := d / (cmax + cmin);
if g < b then dd := 6 else dd := 0;
if cmax = r then
h := (g - b) / d + dd
else if cmax = g then
h := (b - r) / d + 2
else if cmax = b then
h := (r - g) / d + 4;
h := h / 6;
end;
end;
//============================================================================
function hue2rgb(p, q, t: Double): Double;
begin
if t < 0 then t := t + 1;
if t > 1 then t := t - 1;
if t < 1 / 6 then
Result := p + (q - p) * 6 * t
else if t < 1 / 2 then
Result := q
else if t < 2 / 3 then
Result := p + (q - p) * (2/3 - t) * 6
else
Result := p;
end;
//============================================================================
procedure hsl2rgb(h, s, l: Double; var red, green, blue: Byte);
var
r, g, b, p, q: double;
begin
if s = 0 then begin
r := l;
g := l;
b := l;
end
else begin
if l < 0.5 then q := l * (1 + s) else q := l + s - l * s;
p := 2 * l - q;
r := hue2rgb(p, q, h + 1/3);
g := hue2rgb(p, q, h);
b := hue2rgb(p, q, h - 1/3);
end;
red := Round(r * 255);
green := Round(g * 255);
blue := Round(b * 255);
end;