画图中的颜色是用模型画出来的,你可以搜索一下相关资料 附 RGB<-->HSL算法 function HSLtoRGB (H, S, L: double): TColor; var M1, M2: double; function HueToColourValue (Hue: double) : byte; var V : double; begin if Hue < 0 then Hue := Hue + 1 else if Hue > 1 then Hue := Hue - 1; if 6 * Hue < 1 then V := M1 + (M2 - M1) * Hue * 6 else if 2 * Hue < 1 then V := M2 else if 3 * Hue < 2 then V := M1 + (M2 - M1) * (2/3 - Hue) * 6 else V := M1; Result := round (255 * V) end; var R, G, B: byte; begin if S = 0 then begin R := round (255 * L); G := R; B := R end else begin if L <= 0.5 then M2 := L * (1 + S) else M2 := L + S - L * S; M1 := 2 * L - M2; R := HueToColourValue (H + 1/3); G := HueToColourValue (H); B := HueToColourValue (H - 1/3) end; Result := RGB(R, G, B) end;function HSLRangeToRGB (H, S, L : Integer): TColor; begin Result := HSLToRGB (H / (HSLRange-1), S / HSLRange, L / HSLRange) end;procedure RGBtoHSL (RGB: TColor; var H, S, L : double); function Max (a, b : double): double; begin if a > b then Result := a else Result := b end; function Min (a, b : double): double; begin if a < b then Result := a else Result := b end; var R, G, B, D, Cmax, Cmin: double; begin R := GetRValue (RGB) / 255; G := GetGValue (RGB) / 255; B := GetBValue (RGB) / 255; Cmax := Max (R, Max (G, B)); Cmin := Min (R, Min (G, B)); 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 / (Cmax + Cmin) else S := D / (2 - Cmax - Cmin); if R = Cmax then H := (G - B) / D else if G = Cmax then H := 2 + (B - R) /D else H := 4 + (R - G) / D; H := H / 6; if H < 0 then H := H + 1 end end;
我已经解决了这个问题,我自己写了一个过程,贴出来给大家看看:procedure CavChange(Canvas:TCanvas;StartPt,EndPt:integer;Width:integer;StartCr,EndCr:TColor); var r1,g1,b1,r2,g2,b2:byte; //颜色分量 dh:integer; //开始点和结束点的距离 i:integer; oldcr,cr:TColor; begin dh:=EndPt - StartPt; r1:=GetRValue(StartCr);g1:=GetGValue(StartCr);b1:=GetBValue(StartCr); r2:=GetRValue(EndCr);g2:=GetGValue(EndCr);b2:=GetBValue(EndCr); oldcr:=Canvas.Brush.Color; for i:=0 to dh do begin cr:=RGB(round(r1+i*((r2-r1) / dh)), round(g1+i*((g2-g1) / dh)), round(b1+i*((b2-b1) / dh))); Canvas.Brush.Color:=cr; Canvas.FillRect(Rect(0,StartPt+i-1,Width,StartPt+i+1)); end; Canvas.Brush.Color:=oldcr; end;
我将这个过程重新写了一下,更通用了点: ----------------------------------------- //对画布进行渐变操作的过程,默认为从上到下渐变 procedure CavChange(Canvas:TCanvas; cvRect:TRect; StartCr,EndCr:TColor; VorH:byte=0); var r1,g1,b1,r2,g2,b2:byte; //颜色分量 sPt,ePt:TPoint; //区域的左上角和右下角 dh,dw:integer; //开始点和结束点的高度与宽度 i:integer; oldcr,cr:TColor; begin sPt:=cvRect.TopLeft; ePt:=cvRect.BottomRight; dh:=ePt.Y-sPt.Y; dw:=ePt.X-sPt.X; r1:=GetRValue(StartCr);g1:=GetGValue(StartCr);b1:=GetBValue(StartCr); r2:=GetRValue(EndCr);g2:=GetGValue(EndCr);b2:=GetBValue(EndCr); oldcr:=Canvas.Brush.Color; case VorH of 0:begin for i:=0 to dh do begin cr:=RGB(round(r1+i*((r2-r1) / dh)), round(g1+i*((g2-g1) / dh)), round(b1+i*((b2-b1) / dh))); Canvas.Brush.Color:=cr; Canvas.FillRect(Rect(0,sPt.Y+i-1,dw,sPt.Y+i+1)); end; end; 1:begin for i:=0 to dw do begin cr:=RGB(round(r1+i*((r2-r1) / dw)), round(g1+i*((g2-g1) / dw)), round(b1+i*((b2-b1) / dw))); Canvas.Brush.Color:=cr; Canvas.FillRect(Rect(sPt.X+i-1,0,sPt.X+i+1,dh)); end; end; end; Canvas.Brush.Color:=oldcr; end;
其中,Const为常数,i从0~const循环
附 RGB<-->HSL算法
function HSLtoRGB (H, S, L: double): TColor;
var
M1, M2: double; function HueToColourValue (Hue: double) : byte;
var
V : double;
begin
if Hue < 0 then
Hue := Hue + 1
else if Hue > 1 then
Hue := Hue - 1;
if 6 * Hue < 1 then
V := M1 + (M2 - M1) * Hue * 6
else if 2 * Hue < 1 then
V := M2
else if 3 * Hue < 2 then
V := M1 + (M2 - M1) * (2/3 - Hue) * 6
else
V := M1;
Result := round (255 * V)
end;
var
R, G, B: byte;
begin
if S = 0 then
begin
R := round (255 * L);
G := R;
B := R
end else begin
if L <= 0.5 then
M2 := L * (1 + S)
else
M2 := L + S - L * S;
M1 := 2 * L - M2;
R := HueToColourValue (H + 1/3);
G := HueToColourValue (H);
B := HueToColourValue (H - 1/3)
end;
Result := RGB(R, G, B)
end;function HSLRangeToRGB (H, S, L : Integer): TColor;
begin
Result := HSLToRGB (H / (HSLRange-1), S / HSLRange, L / HSLRange)
end;procedure RGBtoHSL (RGB: TColor; var H, S, L : double);
function Max (a, b : double): double;
begin
if a > b then
Result := a
else
Result := b
end;
function Min (a, b : double): double;
begin
if a < b then
Result := a
else
Result := b
end;
var
R, G, B, D, Cmax, Cmin: double;
begin
R := GetRValue (RGB) / 255;
G := GetGValue (RGB) / 255;
B := GetBValue (RGB) / 255;
Cmax := Max (R, Max (G, B));
Cmin := Min (R, Min (G, B));
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 / (Cmax + Cmin)
else
S := D / (2 - Cmax - Cmin);
if R = Cmax then
H := (G - B) / D
else if G = Cmax then
H := 2 + (B - R) /D
else
H := 4 + (R - G) / D;
H := H / 6;
if H < 0 then
H := H + 1
end
end;
var
r1,g1,b1,r2,g2,b2:byte; //颜色分量
dh:integer; //开始点和结束点的距离
i:integer;
oldcr,cr:TColor;
begin
dh:=EndPt - StartPt;
r1:=GetRValue(StartCr);g1:=GetGValue(StartCr);b1:=GetBValue(StartCr);
r2:=GetRValue(EndCr);g2:=GetGValue(EndCr);b2:=GetBValue(EndCr);
oldcr:=Canvas.Brush.Color;
for i:=0 to dh do
begin
cr:=RGB(round(r1+i*((r2-r1) / dh)),
round(g1+i*((g2-g1) / dh)),
round(b1+i*((b2-b1) / dh)));
Canvas.Brush.Color:=cr;
Canvas.FillRect(Rect(0,StartPt+i-1,Width,StartPt+i+1));
end;
Canvas.Brush.Color:=oldcr;
end;
-----------------------------------------
//对画布进行渐变操作的过程,默认为从上到下渐变
procedure CavChange(Canvas:TCanvas; cvRect:TRect; StartCr,EndCr:TColor; VorH:byte=0);
var
r1,g1,b1,r2,g2,b2:byte; //颜色分量
sPt,ePt:TPoint; //区域的左上角和右下角
dh,dw:integer; //开始点和结束点的高度与宽度
i:integer;
oldcr,cr:TColor;
begin
sPt:=cvRect.TopLeft;
ePt:=cvRect.BottomRight; dh:=ePt.Y-sPt.Y;
dw:=ePt.X-sPt.X;
r1:=GetRValue(StartCr);g1:=GetGValue(StartCr);b1:=GetBValue(StartCr);
r2:=GetRValue(EndCr);g2:=GetGValue(EndCr);b2:=GetBValue(EndCr);
oldcr:=Canvas.Brush.Color; case VorH of
0:begin
for i:=0 to dh do
begin
cr:=RGB(round(r1+i*((r2-r1) / dh)),
round(g1+i*((g2-g1) / dh)),
round(b1+i*((b2-b1) / dh)));
Canvas.Brush.Color:=cr;
Canvas.FillRect(Rect(0,sPt.Y+i-1,dw,sPt.Y+i+1));
end;
end;
1:begin
for i:=0 to dw do
begin
cr:=RGB(round(r1+i*((r2-r1) / dw)),
round(g1+i*((g2-g1) / dw)),
round(b1+i*((b2-b1) / dw)));
Canvas.Brush.Color:=cr;
Canvas.FillRect(Rect(sPt.X+i-1,0,sPt.X+i+1,dh));
end;
end;
end;
Canvas.Brush.Color:=oldcr;
end;