Difference between revisions of "Double Gradient/de"

From Lazarus wiki
Jump to navigationJump to search
m (typos)
Line 20: Line 20:
 
     StartColor,StopColor:TColor;
 
     StartColor,StopColor:TColor;
 
     Direction: TGradientDirection;
 
     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
+
     endPercent:single; // This is not the percent of the width, this is the percent of the end of the rect- which means, if this value is 1 - the rect could be from 0.99 to 1 and needs not to be from 0 to 1
 
   end;
 
   end;
  
 
function DoubleGradientFill(ARect: TRect; AStart1,AStop1,AStart2,AStop2: TColor;
 
function DoubleGradientFill(ARect: TRect; AStart1,AStop1,AStart2,AStop2: TColor;
 
   ADirection1,ADirection2,APos: TGradientDirection; AValue: Single): TBitmap;
 
   ADirection1,ADirection2,APos: TGradientDirection; AValue: Single): TBitmap;
function nGradientFill(ARect: TRect;APos: TGradientDirection; AGrandient: array of TnGradientInfo): TBitmap;
+
function nGradientFill(ARect: TRect;APos: TGradientDirection; AGradient: array of TnGradientInfo): TBitmap;
  
 
implementation
 
implementation
  
function nGradientFill(ARect: TRect;APos: TGradientDirection; AGrandient: array of TnGradientInfo): TBitmap;
+
function nGradientFill(ARect: TRect;APos: TGradientDirection; AGradient: array of TnGradientInfo): TBitmap;
 
var
 
var
 
   i:integer;
 
   i:integer;
Line 41: Line 41:
 
     else OldRect.Right := ARect.Left ;  // upside down...  in case of i = 0...
 
     else OldRect.Right := ARect.Left ;  // upside down...  in case of i = 0...
  
   for i := 0 to high(AGrandient) do
+
   for i := 0 to high(AGradient) do
 
       begin
 
       begin
 
       AnRect:=OldRect;
 
       AnRect:=OldRect;
 
       if APos = gdVertical then
 
       if APos = gdVertical then
 
         begin
 
         begin
         AnRect.Bottom:=Round((ARect.Bottom-ARect.Top) * AGrandient[i].endPercent + ARect.Top);
+
         AnRect.Bottom:=Round((ARect.Bottom-ARect.Top) * AGradient[i].endPercent + ARect.Top);
 
         AnRect.Top:=OldRect.Bottom;
 
         AnRect.Top:=OldRect.Bottom;
 
         end
 
         end
 
         else
 
         else
 
         begin
 
         begin
         AnRect.Right:=Round((ARect.Right-ARect.Left) * AGrandient[i].endPercent + ARect.Left);
+
         AnRect.Right:=Round((ARect.Right-ARect.Left) * AGradient[i].endPercent + ARect.Left);
 
         AnRect.Left:=OldRect.Right;
 
         AnRect.Left:=OldRect.Right;
 
         end;
 
         end;
  
       Result.Canvas.GradientFill(AnRect,AGrandient[i].StartColor,AGrandient[i].StopColor,AGrandient[i].Direction);
+
       Result.Canvas.GradientFill(AnRect,AGradient[i].StartColor,AGradient[i].StopColor,AGradient[i].Direction);
 
       OldRect := AnRect;
 
       OldRect := AnRect;
 
       end;
 
       end;
Line 137: Line 137:
 
//Wenn Top und Left 0 waren, gab es keine Probleme ...</delphi>
 
//Wenn Top und Left 0 waren, gab es keine Probleme ...</delphi>
  
<delphi>//Und ich änderte die Zeil
+
<delphi>//Und ich änderte die Zeile
 
GradientPB.Canvas.Draw(0,0,ABitmap);   
 
GradientPB.Canvas.Draw(0,0,ABitmap);   
 
//in
 
//in
Line 164: Line 164:
 
Bearbeiten und verbessern Sie diesen Editor nach Belieben, natürlich kostenlos.
 
Bearbeiten und verbessern Sie diesen Editor nach Belieben, natürlich kostenlos.
  
== External Links ==
+
== Externe Links ==
 
[http://lazarus.freepascal.org/index.php/topic,12145.0.html Forum about this article]
 
[http://lazarus.freepascal.org/index.php/topic,12145.0.html Forum about this article]
  
 
[http://wiki.lazarus.freepascal.org/Gradient_Filler Gradient Filler]
 
[http://wiki.lazarus.freepascal.org/Gradient_Filler Gradient Filler]

Revision as of 00:25, 20 February 2011

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 of the rect- which means, if this value is 1 - the rect could be from 0.99 to 1 and needs not to 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; AGradient: array of TnGradientInfo): TBitmap;

implementation

function nGradientFill(ARect: TRect;APos: TGradientDirection; AGradient: 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(AGradient) do
     begin
     AnRect:=OldRect;
     if APos = gdVertical then
        begin
        AnRect.Bottom:=Round((ARect.Bottom-ARect.Top) * AGradient[i].endPercent + ARect.Top);
        AnRect.Top:=OldRect.Bottom;
        end
       else
        begin
        AnRect.Right:=Round((ARect.Right-ARect.Left) * AGradient[i].endPercent + ARect.Left);
        AnRect.Left:=OldRect.Right;
        end;
     Result.Canvas.GradientFill(AnRect,AGradient[i].StartColor,AGradient[i].StopColor,AGradient[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', einfach weil es nicht mehr doppelt ist...
  • Ich kenn mich nicht nicht so gut mit dem Speicher der Configs aus - bitte korrigiert es. Danke
  • Ich habe die Copy-to-clipboard Sache auskommentiert - zu faul zum Korrigieren...
  • Ich habe die DPI Sache gestoppt, weil der Editor auf meinem PC nicht OK aussieht, entfernt das "//" in Form.Create wenn ihr das verwenden wollt.
  • Ich habe den Editor geändert, ich verwende jetzt eine Stringlist. Doppelklicke auf StartColor/StopColor oder Gradient für eine Combobox / ColorDialog.
  • Ich male auf ein Image.
  • Das Image ist nicht länger in der Mitte - ich hatte einige Probleme, wenn ihr das reparieren könnt : toll.
  • Ihr könnt die Form skalieren.
  • Ich habe versucht, das Rendern und den Rest zu trennen...

Bugfixes: <delphi>(Rect.Bottom) *37 //und ich korrigierte das in (Rect.Bottom - Rect.Top) * 37 + Rect.Top. //Wenn Top und Left 0 waren, gab es keine Probleme ...</delphi>

<delphi>//Und ich änderte die Zeile GradientPB.Canvas.Draw(0,0,ABitmap); //in 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.

Externe Links

Forum about this article

Gradient Filler