unit GradientStepsDlg;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;
type
TGradientDialog =
class(TForm)
lblFirstR: TLabel;
lblFirstG: TLabel;
lblFirstB: TLabel;
lblSecondR: TLabel;
lblSecondG: TLabel;
lblSecondB: TLabel;
Bevel: TBevel;
FirstSwatch: TmbColorPreview;
edtRFirst: TBMDSpinEdit;
edtGFirst: TBMDSpinEdit;
edtBFirst: TBMDSpinEdit;
SecondSwatch: TmbColorPreview;
edtRSecond: TBMDSpinEdit;
edtGSecond: TBMDSpinEdit;
edtBSecond: TBMDSpinEdit;
btnOk: TButton;
btnCancel: TButton;
edtSteps: TSpTBXSpinEdit;
pnlGradientMapHolder: TPanel;
imgGradient: TImage32;
cbReverse: TCheckBox;
Gradient: TmbColorPalette;
procedure FirstChange(Sender: TObject);
procedure SecondChange(Sender: TObject);
procedure GradientChange(Sender: TObject);
procedure ColorChange(Sender: TObject);
private
FUpdate: Boolean;
procedure RenderGradient;
public
constructor Create(
const AFirst, ASecond: TColor;
const ASteps: TByteVal);
reintroduce;
end;
var
GradientDialog: TGradientDialog;
implementation
{$R *.dfm}
procedure TGradientDialog.RenderGradient;
begin
imgGradient.Bitmap.SetSizeFrom(imgGradient);
imgGradient.Bitmap.Clear;
TGradient.Linear(imgGradient.Bitmap, Point(0, imgGradient.Bitmap.Height
div 2),
Point(imgGradient.Bitmap.Width, imgGradient.Bitmap.Height
div 2),
ColorArray([FirstSwatch.Color, SecondSwatch.Color]), cbReverse.Checked
);
end;
constructor TGradientDialog.Create(
const AFirst, ASecond: TColor;
const ASteps: TByteVal);
var
R, G, B: Byte;
begin
inherited Create(Application);
FUpdate := True;
try
FirstSwatch.Color := AFirst;
GetRGBValues(AFirst, R, G, B);
edtRFirst.AsInteger := R;
edtGFirst.AsInteger := G;
edtBFirst.AsInteger := B;
SecondSwatch.Color := ASecond;
GetRGBValues(ASecond, R, G, B);
edtRSecond.AsInteger := R;
edtGSecond.AsInteger := G;
edtBSecond.AsInteger := B;
finally
FUpdate := False;
edtSteps.Value := ASteps;
ColorChange(
nil);
end;
end;
procedure TGradientDialog.FirstChange(Sender: TObject);
begin
FirstSwatch.Color := SetRGBValues(edtRFirst.AsInteger, edtGFirst.AsInteger, edtBFirst.AsInteger);
end;
procedure TGradientDialog.ColorChange(Sender: TObject);
begin
if FUpdate
then Exit;
RenderGradient;
GradientChange(
nil);
end;
procedure TGradientDialog.SecondChange(Sender: TObject);
begin
SecondSwatch.Color := SetRGBValues(edtRSecond.AsInteger, edtGSecond.AsInteger, edtBSecond.AsInteger);
end;
procedure TGradientDialog.GradientChange(Sender: TObject);
var
I: Integer;
A: TColorArray;
begin
if FUpdate
then Exit;
Gradient.ClearColors;
A := GetGradientColors(FirstSwatch.Color, SecondSwatch.Color, TByteVal(Round(edtSteps.Value)));
if cbReverse.Checked
then // <-- exceptions here
begin
for I := High(A)
downto 0
do
Gradient.AddColor('
', FormatColor(A[I], cfePascal), False);
;
end
else
begin
for I := 0
to High(A)
do
Gradient.AddColor('
', FormatColor(A[I], cfePascal), False);
;
end;
Gradient.Invalidate;
end;
end.