Mega Code Archive
EasyGraphicsFunctions
Title: EasyGraphicsFunctions
Question: This article contains the unit EasyGraphicsFunctions. This unit contains standard graphic functions used together with the previous posted components.
NOTE!
You need the other posts to be able to compile this components.
You must disable the Compiled Resources (*.dcr) to compile this components. I'll send the dcr's to the admins and tell them to attach them.
Answer:
Unit EasyGraphicsFunctions;
Interface
Uses Controls, Classes, Windows, Graphics;
Procedure DrawFadedBar(DstCanvas: TCanvas; FadeRect: TRect; DrawVertical: Boolean; StartColor, EndColor: TColor);
Procedure DrawFadedBarEx(DstCanvas: TCanvas; FadeRect: TRect; DrawVertical: Boolean; StartColor, EndColor: TColor);
Procedure MakeDisabledBitmap(var B: TBitmap; TransparentColor: TColor);
Function GetBitmap(ImgList: TImageList; ImageIndex: Integer; Enabled, GotMouse: Boolean) : TBitmap;
Implementation
Procedure DrawFadedBar(DstCanvas: TCanvas; FadeRect: TRect; DrawVertical: Boolean; StartColor, EndColor: TColor);
Var
R, G, B : Real;
Step_R : Real;
Step_G : Real;
Step_B : Real;
X, Y : Integer;
Len : Integer;
I : Integer;
Begin
IF (DrawVertical) Then Len := FadeRect.Right-FadeRect.Left
Else Len := FadeRect.Bottom-FadeRect.Top;
IF (Len
Step_R := (GetRValue(EndColor) - GetRValue(StartColor)) / Len;
Step_G := (GetGValue(EndColor) - GetGValue(StartColor)) / Len;
Step_B := (GetBValue(EndColor) - GetBValue(StartColor)) / Len;
R := GetRValue(StartColor);
G := GetGValue(StartColor);
B := GetBValue(StartColor);
X := FadeRect.Left;
Y := FadeRect.Top;
For I := 0 to Len do
Begin
DstCanvas.Pen.Color := RGB(Round(R), Round(G), Round(B));
IF (DrawVertical) Then
Begin
DstCanvas.MoveTo(X, FadeRect.Top);
DstCanvas.LineTo(X, FadeRect.Bottom);
End
Else
Begin
DstCanvas.MoveTo(FadeRect.Left, Y);
DstCanvas.LineTo(FadeRect.Right, Y);
End;
Inc(X);
Inc(Y);
R := R + Step_R;
G := G + Step_G;
B := B + Step_B;
End;
End;
Procedure DrawFadedBarEx(DstCanvas: TCanvas; FadeRect: TRect; DrawVertical: Boolean; StartColor, EndColor: TColor);
Var
R : TRect;
cR, cG, cB : Integer;
Col1, Col2 : TColor;
Begin
IF (not DrawVertical) Then
Begin
cR := GetRValue(StartColor) - GetRValue(EndColor);
cG := GetGValue(StartColor) - GetGValue(EndColor);
cB := GetBValue(StartColor) - GetBValue(EndColor);
R := FadeRect;
R.Top := R.Bottom-5;
// The bottom color got 6% of the total color change...
Col1 := RGB(
GetRValue(EndColor)+Round(cR*6/100),
GetGValue(EndColor)+Round(cG*6/100),
GetBValue(EndColor)+Round(cB*6/100)
);
Col2 := EndColor;
DrawFadedBar(DstCanvas, R, DrawVertical, Col1, Col2);
Col2 := Col1;
Col1 := RGB(
GetRValue(StartColor)-Round(cR*6/100),
GetGValue(StartColor)-Round(cG*6/100),
GetBValue(StartColor)-Round(cB*6/100)
);
R.Bottom := R.Top-1;
R.Top := 5;
DrawFadedBar(DstCanvas, R, DrawVertical, Col1, Col2);
// The top color got 6% of the total color change...
Col2 := Col1;
Col1 := StartColor;
R.Bottom := R.Top-1;
R.Top := FadeRect.Top;
DrawFadedBar(DstCanvas, R, DrawVertical, Col1, Col2);
End;
End;
Procedure ScrambleBitmap(var BMP: TBitmap; TransparentColor: TColor);
Var
X, Y : Integer;
Color : TColor;
R, G, B : Integer;
Begin
With Bmp.Canvas do
Begin
For Y := 0 to Bmp.Height - 1 do
Begin
For X := 0 to Bmp.Width - 1 do
Begin
Color := Pixels[X, Y];
IF (Color TransparentColor) Then
Begin
R := GetRValue(Color);
G := GetGValue(Color);
B := GetBValue(Color);
Pixels[X, Y] := 0;
IF (R 100) and (G 100) and (B 100) Then Pixels[X, Y] := $FFFFFF;
IF (R-G -16) and (R-G -16) and (R-B Begin
IF (R 80) and (G 80) and (B 80) Then Pixels[X, Y] := $FFFFFF;
End;
IF (Pixels[X, Y] = $FFFFFF) and (R Begin
// Verify that this pixel isn't a boundary that should be displayed afterall...!
IF (R-G -16) and (R-G -16) and (R-B Begin
IF (X Begin
Color := Pixels[X+1, Y];
R := GetRValue(Color);
G := GetGValue(Color);
B := GetBValue(Color);
IF (R 140) and (G 140) and (B 140) Then
Begin
Pixels[X, Y] := 0;
End;
End;
IF (Y Begin
Color := Pixels[X, Y+1];
R := GetRValue(Color);
G := GetGValue(Color);
B := GetBValue(Color);
IF (R 140) and (G 140) and (B 140) Then
Begin
Pixels[X, Y] := 0;
End;
End;
End;
End;
End
Else Pixels[X, Y] := $FFFFFF;
End;
End;
End;
End;
Procedure MakeDisabledBitmap(var B: TBitmap; TransparentColor: TColor);
Var
TmpBmp : TBitmap;
X, Y : Integer;
Begin
IF (not Assigned(B)) Then Exit;
B.PixelFormat := pf24Bit;
TmpBmp := TBitmap.Create;
TmpBmp.PixelFormat := pf24Bit;
TmpBmp.Assign(B);
B.Canvas.Brush.Style := bsSolid;
B.Canvas.Brush.Color := TransparentColor;
B.Canvas.FillRect(Rect(0, 0, B.Width+1, B.Height+1));
ScrambleBitmap(TmpBmp, TransparentColor);
For Y := 0 to TmpBmp.Height-1 do
Begin
For X := 0 to TmpBmp.Width-1 do
Begin
IF (TmpBmp.Canvas.Pixels[X, Y] = 0) Then
Begin
B.Canvas.Pixels[X+1, Y+1] := clBtnHighlight;
B.Canvas.Pixels[X, Y] := clBtnShadow;
End;
// B.Canvas.Pixels[X, Y] := TmpBmp.Canvas.Pixels[X, Y];
// B.Transparent := False;
End;
End;
TmpBmp.Free;
End;
Procedure MakeShadow(var B: TBitmap);
Var
TransparentColor : TColor;
X, Y : Integer;
TmpBmp : TBitmap;
Begin
TransparentColor := B.Canvas.Pixels[B.Width-1, 0];
IF (B.Canvas.Pixels[0, 0] = B.Canvas.Pixels[B.Width-1, B.Height-1]) and
(B.Canvas.Pixels[0, 0] = B.Canvas.Pixels[0, B.Height-1]) Then TransparentColor := B.Canvas.Pixels[0, 0];
TmpBmp := TBitmap.Create;
TmpBmp.Assign(B);
B.Width := B.Width+2;
B.Height := B.Height+2;
B.Canvas.Brush.Color := TransparentColor;
B.Canvas.Brush.Style := bsSolid;
B.Canvas.FillRect(Rect(0,0,B.Width, B.Height));
For Y := 0 to TmpBmp.Height-1 do
Begin
For X := 0 to TmpBmp.Width-1 do
Begin
IF (TmpBmp.Canvas.Pixels[X, Y] TransparentColor) Then B.Canvas.Pixels[X+2,Y+2] := clGray;
End;
End;
B.Canvas.Draw(0, 0, TmpBmp);
TmpBmp.Free;
End;
Function GetBitmap(ImgList: TImageList; ImageIndex: Integer; Enabled, GotMouse: Boolean) : TBitmap;
Begin
Result := TBitmap.Create;
Result.PixelFormat := pf24bit;
Result.Width := ImgList.Width;
Result.Height := ImgList.Height;
Result.Canvas.Brush.Color := RGB(10, 10, 10);
Result.Canvas.FillRect( Rect(0,0,Result.Width,Result.Height) );
Result.Transparent := True;
ImgList.Draw(Result.Canvas, 0, 0, ImageIndex, True);
IF (not Enabled) Then
Begin
MakeDisabledBitmap(Result, Result.Canvas.Brush.Color);
End
Else
IF (GotMouse) Then
Begin
MakeShadow(Result);
End;
End;
End.