Double Gradient/de

From Lazarus wiki
Revision as of 23:45, 19 February 2011 by Billyraybones (talk | contribs) (updated)
Jump to navigationJump to search

Deutsch (de) English (en)

Unit

sampledoublegd.png

Mit dieser Unit 'ngradient' können Sie ganz einfach Farbverläufe (= Gradienten) für Toolbars, Schaltflächen, etc... erzeugen. Speichern Sie den untenstehenden Code in einer Textdatei 'ngradient.pas' und fügen Sie 'ngradient' im Abschnitt 'uses' von Ihrem Projekt hinzu.

<delphi>unit ngradient;

{$mode objfpc}{$H+}

interface

uses

 Classes, Graphics;

type

 TnGradientInfo = record
   StartColor,StopColor:TColor;
   Direction: TGradientDirection;
   endPercent:single; // This is not the percent of the width, this is the percent of the end ob the rect- which means, if this value is 1 - the rect could be from 0.99 to 1 and need not be from 0 to 1
 end;

function DoubleGradientFill(ARect: TRect; AStart1,AStop1,AStart2,AStop2: TColor;

 ADirection1,ADirection2,APos: TGradientDirection; AValue: Single): TBitmap;

function nGradientFill(ARect: TRect;APos: TGradientDirection; AGrandient: array of TnGradientInfo): TBitmap;

implementation

function nGradientFill(ARect: TRect;APos: TGradientDirection; AGrandient: array of TnGradientInfo): TBitmap; var

 i:integer;
 AnRect,OldRect: TRect;

begin

 Result := TBitmap.Create;
 Result.Width:=ARect.Right-ARect.Left;
 Result.Height:=ARect.Bottom-ARect.Top;
 OldRect := ARect;
 if APos = gdVertical then OldRect.Bottom := ARect.Top
    else OldRect.Right := ARect.Left ;   // upside down...  in case of i = 0...
 for i := 0 to high(AGrandient) do
     begin
     AnRect:=OldRect;
     if APos = gdVertical then
        begin
        AnRect.Bottom:=Round((ARect.Bottom-ARect.Top) * AGrandient[i].endPercent + ARect.Top);
        AnRect.Top:=OldRect.Bottom;
        end
       else
        begin
        AnRect.Right:=Round((ARect.Right-ARect.Left) * AGrandient[i].endPercent + ARect.Left);
        AnRect.Left:=OldRect.Right;
        end;
     Result.Canvas.GradientFill(AnRect,AGrandient[i].StartColor,AGrandient[i].StopColor,AGrandient[i].Direction);
     OldRect := AnRect;
     end;

end;


function DoubleGradientFill(ARect: TRect; AStart1,AStop1,AStart2,AStop2: TColor;

 ADirection1,ADirection2,APos: TGradientDirection; AValue: Single): TBitmap;

var

 ABitmap: TBitmap; ARect1,ARect2: TRect;

begin

 ABitmap := TBitmap.Create;
 ABitmap.Width:=ARect.Right;
 ABitmap.Height:=ARect.Bottom;
 if AValue <> 0 then begin
   ARect1:=ARect;
 end;
 if AValue <> 1 then begin
   ARect2:=ARect;
 end;
 if APos = gdVertical then begin
   ARect1.Bottom:=Round(ARect1.Bottom * AValue);
   ARect2.Top:=ARect1.Bottom;
 end
 else if APos = gdHorizontal then begin
   ARect1.Right:=Round(ARect1.Right * AValue);
   ARect2.Left:=ARect1.Right;
 end;
 if AValue <> 0 then begin
   ABitmap.Canvas.GradientFill(ARect1,AStart1,AStop1,ADirection1);
 end;
 if AValue <> 1 then begin
   ABitmap.Canvas.GradientFill(ARect2,AStart2,AStop2,ADirection2);
 end;
 Result:=ABitmap;

end;

end.</delphi>

Gebrauch

Erstens benötigen Sie eine 'TBitmap' um den Gradienten zu speichern, dann können Sie das Image zeichnen, beispielsweise im gesamten sichtbaren Bereich der 'Form1':

defaultdbgd.png

Dies ist das Ergebnis (verwendet DoubleGradientFill).

<delphi>procedure TForm1.FormPaint(Sender: TObject); var

 ABitmap: TBitmap;

begin

 ABitmap:=DoubleGradientFill(Self.ClientRect,clMedGray,clWhite,clSilver,clGray,gdVertical,gdVertical,gdVertical,0.50);
 Self.Canvas.Draw(0,0,ABitmap);
 ABitmap.Free

end;</delphi>

Editor

Der nGradient Editor ist eine aktualisierte Version des Double Gradient Editor mit zahlreichen Änderungen und Verbesserungen, wie der Fähigkeit, Gradienten zu erzeugen mit einer beliebigen Anzahl 'n' von Gradienten.

nGradient Editor

Mit dem nGradient Editor können Sie die Anzahl 'n' der Gradienten festlegen.

ngedit.png

Version 1.1

  • umbenannt in n Gradient Edit, just because it is not any more double...
  • I doesn't realy understand how to save configs - please corret it. Thanks
  • I comment the Copy-to-clipboard thing - to lazy to corret it...
  • i stopped the dpi thing because the editor doesn't look ok on my pc, remove "//" in Form.Create if you want to use it.
  • I changed the editor for arrays, i use a stringlist. Double Click on StartColor /StopColor or Gradient for an Combobox / ColorDialog.
  • I paint on an Image.
  • The Image is not longer in the middle - i Had some problems, if you could fix it : great.
  • You can scale the Form.
  • I tried to seperate render and the rest...

Bugfixes: <delphi>(Rect.Bottom) *37 //and i corrected them to (Rect.Bottom - Rect.Top) * 37 + Rect.Top. //If Top and left was 0 there was no problem ...</delphi>

<delphi>//Also i just changed the line GradientPB.Canvas.Draw(0,0,ABitmap); //to Panel1.Canvas.Draw(0,0,ABitmap);</delphi>

Double Gradient Editor

Sie können aber auch den 'Double Gradient Editor' verwenden. Das ist ein GUI-Programm, das sofort ein sichtbares Ergebnis liefert (nur bei doppelten Gradienten). Speichern Sie den Gradienten in einer Bitmap-Datei (oder als '*.doublegradient' Sitzungsdatei) oder kopieren Sie einfach den Code in die Zwischenablage um ihn in Lazarus zu verwenden.

dbgedit.png

  • Ein Rechtsklick auf die Scrollbox (den Gradientenbereich) zeigt das Kontextmenü an:
    • Save bitmap.. Zeigt einen Dialog zum Speichern als *.bmp Datei.
    • Load gradient.. Zeigt einen Dialog zum Laden einer *.doublegradient Sitzungsdatei.
    • Save gradient.. Zeigt einen Dialog zum Speichern der aktuellen Einstellungen in einer Sitzungsdatei.
    • Copy code to clipboard.. Kopiert einfach die Einstellungen in Textform in die Zwischenablage, die Sie dann im Lazarus-Quelltexteditor einfügen können, um die 'doublegradientfill'-Prozedur aufzurufen.
  • default.doublegradient: Diese Datei enthält die letzten Einstellungen im Editor, wird beim Start geladen und beim Programmende gespeichert.

Downloads

nGradient Editor 1.1 Source Code ngdedit1.1.zip (26.14 KB)

Double Gradient Editor 1.0 Source Code dbgdedit1.0.zip (5.64 KB)

Bearbeiten und verbessern Sie diesen Editor nach Belieben, natürlich kostenlos.

External Links

Forum about this article

Gradient Filler