Mega Code Archive

 
Categories / Delphi / Examples
 

Gradientfill

Richard E: *************************************************************************************** my code to do a gradient fill in a given rectangle (from StartColour to EndColor)... procedure TDrawObj.GradientFillRectangle(thisCanvas: TCanvas; fRect: TRect; StartColor, EndColor: TColor); var y: Integer; GradientDistance: Integer; tempIntOne: Integer; tempIntTwo: Integer; tempColor: TColor; NewRed, NewGreen, NewBlue: Byte; StartRed, StartGreen, StartBlue: Byte; EndRed, EndGreen, EndBlue: Byte; RedIncrement, GreenIncrement, BlueIncrement: Byte; IncRed, IncGreen, IncBlue: Boolean; begin IncRed := True; IncGreen := True; IncBlue := True; //calculate the total gradient distance (height) GradientDistance := Round(fRect.Bottom - fRect.Top); //find the initial red, green, and blue values StartRed := GetRValue(StartColor); StartGreen := GetGValue(StartColor); StartBlue := GetBValue(StartColor); //find the final red, green, and blue values EndRed := GetRValue(EndColor); EndGreen := GetGValue(EndColor); EndBlue := GetBValue(EndColor); //find the proper increment (for one loop iteration, below) //for each of the three red, green and blue values... tempIntOne := (EndRed - StartRed); tempIntOne := abs(tempIntOne); tempIntTwo := Round(tempIntOne / GradientDistance); RedIncrement := byte(tempIntTwo); tempIntOne := (EndGreen - StartGreen); tempIntOne := abs(tempIntOne); tempIntTwo := Round(tempIntOne / GradientDistance); GreenIncrement := byte(tempIntTwo); tempIntOne := (EndBlue - StartBlue); tempIntOne := abs(tempIntOne); tempIntTwo := Round(tempIntOne / GradientDistance); //iterate through GradientDistance BlueIncrement := byte(tempIntTwo); //drawing a line on each iteration NewRed := StartRed; NewGreen := StartGreen; NewBlue := StartBlue; for y := 0 to GradientDistance do begin //set new colour according to changed //red, green, and blue values... tempColor := RGB(NewRed, NewGreen, NewBlue); //set the new pen color thisCanvas.Pen.Color := tempColor; //and finally draw a line in the new color thisCanvas.MoveTo(fRect.Left, (fRect.Top + y)); thisCanvas.LineTo(fRect.Right, (fRect.Top + y)); //increment the red, green and blue values... //BUT take into account the fact that if we //go over 255, values become meaningless -this //we do by decrementing instead of incrementing //(so then we need to check for going below 0 //as well)... if ((IncRed = True) and ((NewRed + RedIncrement) > 255)) then IncRed := False; if ((IncRed = False) and ((NewRed - RedIncrement) < 0)) then IncRed := True; if ((IncGreen = True) and ((NewGreen + GreenIncrement) > 255)) then IncGreen := False; if ((IncGreen = False) and ((NewGreen - GreenIncrement) < 0)) then IncGreen := True; if ((IncBlue = True) and ((NewBlue + BlueIncrement) > 255)) then IncBlue := False; if ((IncBlue = False) and ((NewBlue - BlueIncrement) < 0)) then IncBlue := True; if (IncRed = True) then NewRed := (NewRed + RedIncrement) else NewRed := (NewRed - RedIncrement); if (IncGreen = True) then NewGreen := (NewGreen + GreenIncrement) else NewGreen := (NewGreen - GreenIncrement); if (IncBlue = True) then NewBlue := (NewBlue + BlueIncrement) else NewBlue := (NewBlue - BlueIncrement); end; end; ******************************************************************************************* my code to do a gradient fill in a given rectangle (from BLACK to EndColor)... procedure TDrawObj.GradientFillRectangle(thisCanvas: TCanvas; fRect: TRect; EndColor: TColor); var y: Integer; GradientDistance: Integer; GradientWidth: Integer; tmpColor: TColor; NewRed, NewGreen, NewBlue: Byte; EndRed, EndGreen, EndBlue: Byte; begin //initialize tmpcolor tmpColor := EndColor; //set our gradient distance GradientDistance := Round(fRect.Bottom - fRect.Top); //set our gradient width GradientWidth := Round(fRect.Right - fRect.Left); //get initial red, green, and blue EndRed := GetRValue(EndColor); EndBlue := GetBValue(EndColor); EndGreen := GetGValue(EndColor); //go from start to finish of our gradient for y := 0 to GradientDistance do begin //offset our red, green, and blue according //to the distance into our gradient against //the total distance to go NewRed := (y * EndRed) div GradientDistance; NewBlue := (y * EndBlue) div GradientDistance; NewGreen := (y * EndGreen) div GradientDistance; //set our new color according to our changed //red, green, and blue values tmpColor := RGB(NewRed, NewGreen, NewBlue); //set the new pen color thisCanvas.Pen.Color := tmpColor; //draw our line according to the new color thisCanvas.MoveTo(fRect.Left, (fRect.Top + y)); thisCanvas.LineTo(fRect.Right, (fRect.Top + y)); end; end;