画渐变函数

来源:互联网 发布:淘宝店铺首页怎么全屏 编辑:程序博客网 时间:2024/05/01 08:21

代码

procedure ColorFul(_Sender:TCanvas;_C1,_C2:TColor;_Width,_Height:Integer;_Mode:Integer);
var i:Integer;Rct:TRect;R1,G1,B1,R2,G2,B2,R3,G3,B3:Byte;
begin
  with TCanvas(_Sender) do begin
    R1:=Byte(_C1);
    G1:=Byte(_C1 shr 8);
    B1:=Byte(_C1 shr 16);
    R2:=Byte(_C2);
    G2:=Byte(_C2 shr 8);
    B2:=Byte(_C2 shr 16);
    case _Mode of
    1:begin
      for i:=0 to _Width do begin
        if R1>R2 then R3:=R1-MulDiv(i,R1-R2,_Width) else R3:=R1+MulDiv(i,R2-R1,_Width);
        if G1>G2 then G3:=G1-MulDiv(i,G1-G2,_Width) else G3:=G1+MulDiv(i,G2-G1,_Width);
        if B1>B2 then B3:=B1-MulDiv(i,B1-B2,_Width) else B3:=B1+MulDiv(i,B2-B1,_Width);
        Brush.Color:=RGB(R3,G3,B3);
        Rct:=Rect(i,0,i+1,_Height);
        FillRect(Rct);
      end;
    end;
    2:begin
      for i:=0 to _Height do begin
        if R1>R2 then R3:=R1-MulDiv(i,R1-R2,_Height) else R3:=R1+MulDiv(i,R2-R1,_Height);
        if G1>G2 then G3:=G1-MulDiv(i,G1-G2,_Height) else G3:=G1+MulDiv(i,G2-G1,_Height);
        if B1>B2 then B3:=B1-MulDiv(i,B1-B2,_Height) else B3:=B1+MulDiv(i,B2-B1,_Height);
        Brush.Color:=RGB(R3,G3,B3);
        Rct:=Rect(0,i,_Width,i+1);
        FillRect(Rct);
      end;
    end;
    end;
    SetBkMode(Handle,Windows.TRANSPARENT);
  end;
end;

用法

procedure TForm1.FormPaint(Sender: TObject);
begin
  ColorFul(Self.Canvas,clSkyBlue,clWhite,Self.Width,Self.Height,2);
//1是横着,2是竖着

end;

原创粉丝点击