📄 psgradient.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include "PSGradient.h"
//---------------------------------------------------------------------------
__fastcall TPSGradient::TPSGradient(void)
{
FColorsUsed = 5;
FColor_1 = clRed;
FColor_2 = clYellow;
FColor_3 = clLime;
FColor_4 = clBlue;
FColor_5 = clPurple;
FDirection = pgdHorizontal;
FShape = pgsLine;
}
//---------------------------------------------------------------------------
void __fastcall TPSGradient::SetColor_1 (TColor val)
{
FColor_1 = val;
Change ();
}
//---------------------------------------------------------------------------
void __fastcall TPSGradient::SetColor_2 (TColor val)
{
FColor_2 = val;
Change ();
}
//---------------------------------------------------------------------------
void __fastcall TPSGradient::SetColor_3 (TColor val)
{
FColor_3 = val;
Change ();
}
//---------------------------------------------------------------------------
void __fastcall TPSGradient::SetColor_4 (TColor val)
{
FColor_4 = val;
Change ();
}
//---------------------------------------------------------------------------
void __fastcall TPSGradient::SetColor_5 (TColor val)
{
FColor_5 = val;
Change ();
}
//---------------------------------------------------------------------------
void __fastcall TPSGradient::SetColorsUsed (int val)
{
if (val < 0 || val > 5) return;
FColorsUsed = val;
Change ();
}
//---------------------------------------------------------------------------
void __fastcall TPSGradient::SetDirection (TPSGradientDirection val)
{
FDirection = val;
Change ();
}
//---------------------------------------------------------------------------
void __fastcall TPSGradient::SetShape (TPSGradientShape val)
{
FShape = val;
Change ();
}
//---------------------------------------------------------------------------
void __fastcall TPSGradient::Change (void)
{
if (OnChange) OnChange (this);
}
//---------------------------------------------------------------------------
void __fastcall TPSGradient::DrawGradient (Graphics::TCanvas *can, TRect rect)
{
union {TColor col; BYTE dat[4];} From, To, Temp;
TColor TCol[5];
int i, d, part, start = 0, x, y;
float r, g, b, rtemp, gtemp, btemp, hAdd, vAdd;
if (FColorsUsed == 1)
{
can->Brush->Color = FColor_1;
can->FillRect (Rect(rect.Left, rect.Top, rect.Right, rect.Bottom));
return;
}
TCol[0] = FColor_1;
TCol[1] = FColor_2;
TCol[2] = FColor_3;
TCol[3] = FColor_4;
TCol[4] = FColor_5;
if (FShape == pgsLine)
{
switch (FDirection)
{
case pgdHorizontal: part = (rect.Bottom - rect.Top + 1) / (FColorsUsed - 1);
break;
case pgdVertical: part = (rect.Right - rect.Left + 1) / (FColorsUsed - 1);
break;
case pgdDiagonal1:
case pgdDiagonal2: part = ((rect.Right - rect.Left) +
(rect.Bottom - rect.Top) + 2) / (FColorsUsed - 1);
break;
}
}
else
{
if (rect.Right - rect.Left > rect.Bottom - rect.Top)
{
part = (rect.Right - rect.Left) / ((FColorsUsed - 1) * 2) + 1;
hAdd = 1;
vAdd = (float)(rect.Bottom - rect.Top) / (rect.Right - rect.Left);
}
else
{
part = (rect.Bottom - rect.Top) / ((FColorsUsed - 1) * 2) + 1;
vAdd = 1;
hAdd = (float)(rect.Right - rect.Left) / (rect.Bottom - rect.Top);
}
}
Temp.dat[3] = 0;
for (i = 0;i < FColorsUsed - 1;i++)
{
From.col = TCol[i];
To.col = TCol[i + 1];
rtemp = (float)(From.dat[2] - To.dat[2]) / part;
gtemp = (float)(From.dat[1] - To.dat[1]) / part;
btemp = (float)(From.dat[0] - To.dat[0]) / part;
r = From.dat[2];
g = From.dat[1];
b = From.dat[0];
for (d = start;d < start + part;d++)
{
Temp.dat[2] = r;
Temp.dat[1] = g;
Temp.dat[0] = b;
can->Pen->Color = Temp.col;
if (FShape == pgsLine)
{
switch (FDirection)
{
case pgdHorizontal: can->MoveTo (rect.Left, rect.Top + d);
can->LineTo (rect.Right + 1, rect.Top + d);
break;
case pgdVertical: can->MoveTo (rect.Left + d, rect.Top);
can->LineTo (rect.Left + d, rect.Bottom + 1);
break;
case pgdDiagonal1:
case pgdDiagonal2: y = rect.Top;
if (d > (rect.Right - rect.Left))
{
x = rect.Right;
y = d - (rect.Right - rect.Left) + rect.Top;
}
else x = d + rect.Left;
if (FDirection == pgdDiagonal1) can->MoveTo (x, y);
else can->MoveTo (rect.Right + rect.Left - x , y);
x = rect.Left;
if (d > (rect.Bottom - rect.Top))
{
y = rect.Bottom;
x = d - (rect.Bottom - rect.Top) + rect.Left;
}
else y = d + rect.Top;
if (FDirection == pgdDiagonal1) can->LineTo (x - 1, y + 1);
else can->LineTo (rect.Right + rect.Left - x + 1, y + 1);
break;
}
}
else if (FShape == pgsRectangular)
{
can->Brush->Color = Temp.col;
can->Rectangle (rect.Left + (d * hAdd), rect.Top + (d * vAdd),
rect.Right - (d * hAdd), rect.Bottom - (d * vAdd));
}
else
{
can->Brush->Color = Temp.col;
can->Ellipse (rect.Left + (d * hAdd), rect.Top + (d * vAdd),
rect.Right - (d * hAdd), rect.Bottom - (d * vAdd));
}
r -= rtemp;
g -= gtemp;
b -= btemp;
}
start += part;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -