Lorax
2006-03-04 18:39:32
This is my latest work and I'm quite proud of it, maybe because it took me a hell lot of a time to find out what I needed and then where it was to be found and shiat.. Next ofcourse was the differences from the languages and some dumb mistakes.. Now I'm happy to present ColorConvert procedures by Lorax - hehe..
procedure Color32ToRGB(Color: Integer; var Red, Green, Blue: Integer);
begin
Blue := (Color and $00ff0000) shr 16;
Green := (Color and $0000ff00) shr 8;
Red := (Color and $000000ff);
end;
procedure RGBToColor32(var Color: Integer; Red, Green, Blue: Integer);
begin
Color := Blue shl 16;
Color := Color + Green shl 8;
Color := Color + Red;
end;
function Max3(int1, int2, int3: Extended): Extended;
begin
result:= MaxE(MaxE(int1, int2), MaxE(int2, int3));
end;
function Min3(int1, int2, int3: Extended): Extended;
begin
result:= MinE(MinE(int1, int2), MinE(int2, int3));
end;
procedure RGBToHSL(Red, Green, Blue: Integer; var Hue, Saturation, Luminance: Extended);
var
tRed, tGreen, tBlue, tMax, tMin, Delta: Extended;
begin
tRed:= Red div 255;
tGreen:= Green div 255;
tBlue:= Blue div 255;
tMax:= Max3(tRed, tBlue, tGreen);
tMin:= Min3(tRed, tBlue, tGreen);
Delta:= tMax - tMin;
Luminance:= (tMax + tMin) div 2.0;
if(tMax = tMin)then
begin
Saturation:= 0.0;
Hue:= 0.0;
end else
begin
if Luminance < 0.5 then
Saturation:= Delta div (2 * Luminance)
else
Saturation:= Delta div (2 - tMax - tMin);
if(tMax = tRed)then
Hue:= 0 + (tGreen - tBlue) div Delta;
if(tMax = tGreen)then
Hue:= 2 + (tBlue - tRed) div Delta;
if(tMax = tBlue)then
Hue:= 4 + (tRed - tGreen) div Delta;
Hue:= Hue div 6.0;
end;
Hue:= Hue * 100;
Saturation:= Saturation * 100;
Luminance:= Luminance * 100;
end;
function HueToRGB(temp1, temp2, Hue: Extended): Extended;
begin
if(Hue < 0)then
Hue:= Hue + 1;
if(Hue > 1)then
Hue:= Hue - 1;
if(6 * Hue < 1.0)then
result:= temp1 + (temp2 - temp1) * 6 * Hue
else if(2 * Hue < 1.0)then
result:= temp2
else if(3 * Hue < 2.0)then
result:= temp1 + (temp2 - temp1) * ((2.0 div 3.0) - Hue) * 6
else
result:= temp1;
end;
procedure HSLToRGB(var Red, Green, Blue: Integer; Hue, Saturation, Luminance: Extended);
var
tSaturation, tLuminance, tHue, temp2, temp1: Extended;
begin
tSaturation:= Saturation div 100.0;
tLuminance:= Luminance div 100.0;
tHue:= Hue div 100.0;
if(Saturation = 0)then
begin
Red := Round(tLuminance * 255);
Green := Round(tLuminance * 255);
Blue := Round(tLuminance * 255);
end else
begin
if(tLuminance < 0.5)then
temp2:= tLuminance * (1 + tSaturation)
else
temp2:= (tLuminance + tSaturation) - (tSaturation * tLuminance);
temp1:= 2 * tLuminance - temp2;
Red := Round(255 * HueToRGB(temp1, temp2, tHue + (1.0 div 3.0)));
Green := Round(255 * HueToRGB(temp1, temp2, tHue));
Blue := Round(255 * HueToRGB(temp1, temp2, tHue - (1.0 div 3.0)));
end;
end;