A: The key
A gradient shape is a special shape that moves from one end of a form to another while the color slowly changes to the other end of the form, but the graphic has changed to another color.
This bizarre visual effect is quite simple to make, with just a few lines to separate the screen and each line to show a different color. A gradient shape can have many forms, with a variety of display effects, such as a circular gradient, a rectangular gradient, a diagonal gradient, and so on. To display a gradient, use an RGB macro, which changes the color component of the color value slightly each time.
B: Implementation and application
Select Menu File | New application, creating a newer project file. Place a Tcolordialog control on the form Form1, and then switch to Unit1.h, and add the definition of two variables to the private part of TForm1 to set the starting and ending colors, respectively.
Private://User declarations
TColor Startcolor,endcolor;
Then double-click the form, create the OnCreate event handler for the form, and add the following code to let the user select the starting and ending colors.
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
if(ColorDialog1->Execute())
StartColor=ColorDialog1->Color;
if(ColorDialog1->Execute())
EndColor=ColorDialog1->Color;
}
//---------------------------------------------------------------------------
The
Finally creates a form Form1 OnPaint event handler, where you add the following code to draw a series of vertical lines with a slightly changing color value to achieve the gradient effect.
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
float pwidth;
int redstart,greenstart,bluestart,redend,greenend, blueend;
float redinc,greeninc,blueinc;
pwidth=float(Width);
redstart=GetRValue(StartColor);
greenstart=GetGValue(StartColor);
bluestart=GetBValue(StartColor);
redend=GetRValue(EndColor);
greenend=GetGValue(EndColor);
blueend=GetBValue(EndColor);
redinc=(redend-redstart)/pwidth;
greeninc=(greenend-greenstart)/pwidth;
blueinc=(blueend-bluestart)/pwidth;
for(int i=0;i<Width;i++)
{
Canvas->Pen->Color=TColor(RGB(redstart+int(redinc*i),
greenstart+int(greeninc*i),
bluestart+int(blueinc*i)));
Canvas->MoveTo(i,0);
Canvas->LineTo(i,Height);
}
}
//---------------------------------------------------------------------------
C: Expert comments
The color of the beginning of the gradient in the program is StartColor, and the ending color is endcolor. Using Getrvalue, Getgvalue and Getbvalue functions, respectively, the red, green and blue primary color components Redstart Greenstart Bluestart and the end colors of the three primary colors Redend Greenend Blueend. If the width of the rectangle is pwidth pixels, each color component value for each change should be (Redend-redstart)/pwidth, (Greenend-greenstart)/pwidth, (Blueend-bluestart)/ Pwidth, finally write a loop, draw a series of straight lines.
This is just a form of a color gradient, readers can fully refer to its implementation method, change the mathematical operation, such as "gradually deepened from top to bottom", "from left to right gradually deepen" and other color gradient forms, in addition, you can draw a series of color changing circle, make a circular gradient effect. First draw a maximum circle, and then draw the radius of a circle smaller than the previous one pixel circle, and then draw the radius than the previous circle smaller one pixel circle, while the color of the circle also changed.