{ Apophysis AV "Phoenix Edition" Copyright (C) 2021 Alice V. Koryagina }

unit ColorRangeForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Controls, ComCtrls, StdCtrls, ExtCtrls, Vcl.Dialogs,
  Vcl.Forms,  Vcl.Graphics,Vcl.Samples.Spin, Adjust, Translation;

type
  TColorSelection = class(TForm)
    btOK: TButton;
    btCancel: TButton;
    SpinStart: TSpinEdit;
    SpinLast: TSpinEdit;
    StartColor: TShape;
    LastColor: TShape;
    lbStart: TPanel;
    lbLast: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure btOKClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SpinLastChange(Sender: TObject);
    procedure SpinValueKeyPress(Sender: TObject; var Key: Char);
    procedure SpinStartChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  ColorSelection: TColorSelection;

implementation

{$R *.dfm}

procedure TColorSelection.btOKClick(Sender: TObject);
begin
  AdjustForm.selA := SpinStart.Value;
  AdjustForm.selB := SpinLast.Value;
end;

procedure TColorSelection.FormCreate(Sender: TObject);
begin
  self.Caption := TextBykey('selectcolors-title');
  lbStart.Caption := TextBykey('selectcolors-start');
  lbLast.Caption := TextBykey('selectcolors-last');
  btOK.Caption := TextByKey('common-ok');
  btCancel.Caption := TextByKey('common-cancel');
end;

procedure TColorSelection.SpinValueKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = '-' then Key := #0;  // AV: non-negative indices only 
end;

procedure TColorSelection.FormShow(Sender: TObject);
var a, b: byte;
begin
  self.Left := AdjustForm.Left + ((AdjustForm.Width - self.Width) shr 1);
  self.Top := AdjustForm.Top + 55;
  a := AdjustForm.selA;
  b := AdjustForm.selB;
  SpinStart.Value := a;
  SpinLast.Value := b;
  StartColor.Brush.Color := RGB(AdjustForm.palette[a,0], AdjustForm.palette[a,1], AdjustForm.palette[a,2]);
  LastColor.Brush.Color := RGB(AdjustForm.palette[b,0], AdjustForm.palette[b,1], AdjustForm.palette[b,2]);
end;

procedure TColorSelection.SpinStartChange(Sender: TObject);
begin
  if SpinStart.Value > 253 then SpinStart.Value := 253;
  if (SpinLast.Value - SpinStart.Value < 2) then
    SpinStart.Value := SpinLast.Value - 2;
  StartColor.Brush.Color := RGB(AdjustForm.palette[SpinStart.Value,0],
      AdjustForm.palette[SpinStart.Value,1], AdjustForm.palette[SpinStart.Value,2]);
end;

procedure TColorSelection.SpinLastChange(Sender: TObject);
begin
  if SpinLast.Value > 255 then SpinLast.Value := 255;
  if (SpinLast.Value - SpinStart.Value < 2) then
    SpinLast.Value := SpinStart.Value + 2;
  LastColor.Brush.Color := RGB(AdjustForm.palette[SpinLast.Value,0],
      AdjustForm.palette[SpinLast.Value,1], AdjustForm.palette[SpinLast.Value,2]);
end;

end.
