求教函数RGBTOHSL,HSLTORGB!(100分)

  • 主题发起人 主题发起人 xsxdelphi
  • 开始时间 开始时间
X

xsxdelphi

Unregistered / Unconfirmed
GUEST, unregistred user!
我检索了整个论坛找不到满意的答案,
谁能给满意答复就300分!
谢绝贯水!
 
procedure HSLtoRGB(H,S,L:Integer;var R,G,B:Integer);
var
Sat,Lum : Double;
begin
R := 0;
G := 0;
B := 0;
if (H < 360) and (H >= 0) and (S <= 100) and (S >= 0) and (L <= 100) and (L >= 0) then begin
if H <=60 then begin
R := 255;
G := Round((255/60)*H);
B := 0;
end
else if H <=120 then begin
R := Round(255-(255/60)*(H-60));
G := 255;
B := 0;
end
else if H <=180 then begin
R := 0;
G := 255;
B := Round((255/60)*(H-120));
end
else if H <=240 then begin
R := 0;
G := Round(255-(255/60)*(H-180));
B := 255;
end
else if H <=300 then begin
R := Round((255/60)*(H-240));
G := 0;
B := 255;
end
else if H <360 then begin
R := 255;
G := 0;
B := Round(255-(255/60)*(H-300));
end;

Sat := Abs((S-100)/100);
R := Round(R-((R-128)*Sat));
G := Round(G-((G-128)*Sat));
B := Round(B-((B-128)*Sat));

Lum := (L-50)/50;
if Lum > 0 then begin
R := Round(R+((255-R)*Lum));
G := Round(G+((255-G)*Lum));
B := Round(B+((255-B)*Lum));
end
else if Lum < 0 then begin
R := Round(R+(R*Lum));
G := Round(G+(G*Lum));
B := Round(B+(B*Lum));
end;
end;
end;

procedure RGBtoHSL(R,G,B:Integer;var H,S,L:Integer);
var
Delta : Double;
CMax,CMin : Double;
Red,Green,Blue,Hue,Sat,Lum : Double;
begin
Red := R/255;
Green := G/255;
Blue := B/255;
CMax := Max(Red,Max(Green,Blue));
CMin := Min(Red,Min(Green,Blue));
Lum := (CMax+CMin)/2;
if CMax = CMin then begin
Sat := 0;
Hue := 0;
end
else begin
if Lum < 0.5 then Sat := (CMax-CMin)/(CMax+CMin)
else Sat := (cmax-cmin)/(2-cmax-cmin);
delta := CMax-CMin;
If Red = CMax then Hue := (Green-Blue)/Delta
else if Green = CMax then Hue := 2+(Blue-Red)/Delta
else Hue := 4.0+(Red-Green)/Delta;
Hue := Hue / 6;
If Hue < 0 then Hue := Hue + 1;
end;
H := Round(Hue*360);
S := Round(Sat*100);
L := Round(Lum*100);
end;
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部