unit ColorsForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, MainForm, Math, StrUtils;

type
  TColorsFrm = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    ColorsView: TImage;
    LandWaterRad: TRadioGroup;
    Label1: TLabel;
    IndexEdt: TEdit;
    GroupBox1: TGroupBox;
    Button5: TButton;
    ColorPreview: TPanel;
    ColorSelect: TColorDialog;
    GroupBox2: TGroupBox;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    GradInd1Edt: TEdit;
    GradInd2Edt: TEdit;
    Button6: TButton;
    Button7: TButton;
    GradPrev1Pan: TPanel;
    GradPrev2Pan: TPanel;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    GradColorSel: TColorDialog;
    ColorsOpen: TOpenDialog;
    ColorsSave: TSaveDialog;
    Label7: TLabel;
    GradHtml1Edt: TEdit;
    GradHtml2Edt: TEdit;
    procedure Button5Click(Sender: TObject);
    function LandWater(const v:byte):smallint;
    procedure RefreshBar;
    procedure Button1Click(Sender: TObject);
    procedure IndexEdtKeyPress(Sender: TObject; var Key: Char);
    procedure FormResize(Sender: TObject);
    procedure ColorsViewMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button9Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure LandWaterRadClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure IndexEdtExit(Sender: TObject);
    procedure GradHtml1EdtExit(Sender: TObject);
    procedure GradHtml1EdtKeyPress(Sender: TObject; var Key: Char);
    procedure GradHtml2EdtExit(Sender: TObject);
    procedure GradHtml2EdtKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 


var
  ColorsFrm: TColorsFrm;
  AllColors: TTerrainColors;
  SelectedColor: Smallint;
  ColorScale:single;
  ColorsFileName:string='';

implementation

{$R *.dfm}

procedure TColorsFrm.Button5Click(Sender: TObject);
begin
 if ColorSelect.Execute then begin
  ColorPreview.Color := ColorSelect.Color;
  AllColors[SelectedColor] := ColorSelect.Color;
  RefreshBar;
 end;
end;

function TColorsFrm.LandWater(const v:byte):Smallint;
begin
 if LandWaterRad.ItemIndex=0 then Result := v else Result := -v;
end;

function HTML2Color(const s:string):TColor;
begin
 Result := rgb(StrToInt('$'+MidStr(s,1,2)),StrToInt('$'+MidStr(s,3,2)),
  StrToInt('$'+MidStr(s,5,2)));
end;
function Color2HTML(const c:TColor):string;
begin
 Result := IntToHex(GetRValue(c),2)+IntToHex(GetGValue(c),2)+IntToHex(GetBValue(c),2)
end;

procedure TColorsFrm.RefreshBar;
var i:smallint;
begin
 ColorScale := ColorsView.Width/255;
 ColorsView.Picture.Bitmap.Width := ColorsView.Width;
 ColorsView.Picture.Bitmap.Height := ColorsView.Height;
 with ColorsView.Picture.Bitmap do begin
  if LandWater(1)=1 then for i := 0 to 255 do begin
   Canvas.Brush.Color := AllColors[i];
   Canvas.FillRect(Rect(Floor(i*ColorScale), 0, Ceil((i+1)*ColorScale), Height));
  end else for i := 0 downto -255 do begin
   Canvas.Brush.Color := AllColors[i];
   Canvas.FillRect(Rect(Floor((-i)*ColorScale), 0, Ceil((-i+1)*ColorScale), Height));
  end;
 end;
end;



procedure TColorsFrm.Button1Click(Sender: TObject);
var gc:TColorsArray; i:smallint;
begin
 // Woda
 Gradient(rgb(0,0,255), rgb(20,20,30), 256, gc);
 for i := 0 to 255 do AllColors[-i] := gc[i];
 SetLength(gc, 0);
 // Ląd
 Gradient(rgb(60,30,30), rgb(250,200,50), 256, gc);
 for i := 0 to 255 do AllColors[i] := gc[i];
 RefreshBar;
end;

procedure TColorsFrm.IndexEdtKeyPress(Sender: TObject; var Key: Char);
begin
 if Key=#13 then IndexEdtExit(Sender);
end;

procedure TColorsFrm.FormResize(Sender: TObject);
begin
 RefreshBar;
end;

procedure TColorsFrm.ColorsViewMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
 SelectedColor := LandWater(Round(X/ColorScale));
 IndexEdt.Text := IntToStr(Round(X/ColorScale));
 ColorPreview.Color := AllColors[SelectedColor];
 ColorSelect.Color := AllColors[SelectedColor];
end;

procedure TColorsFrm.Button9Click(Sender: TObject);
begin
 RefreshBar;
end;

procedure TColorsFrm.Button6Click(Sender: TObject);
begin
 if GradColorSel.Execute then begin
  GradPrev1Pan.Color := GradColorSel.Color;
  GradHtml1Edt.Text := Color2HTML(GradColorSel.Color);
 end;
end;

procedure TColorsFrm.Button7Click(Sender: TObject);
begin
 if GradColorSel.Execute then begin
  GradPrev2Pan.Color := GradColorSel.Color;
  GradHtml2Edt.Text := Color2HTML(GradColorSel.Color);
 end;
end;

procedure TColorsFrm.Button8Click(Sender: TObject);
var gc:TColorsArray; i:smallint; indstart, indend, tr:byte;
begin
 indstart := StrToInt(GradInd1Edt.Text);
 indend := StrToInt(GradInd2Edt.Text);
 if indstart>indend then begin
  tr := indstart;
  indstart := indend;
  indend := tr;
 end;
 Gradient(GradPrev1Pan.Color,GradPrev2Pan.Color, (indend-indstart)+1, gc);
 for i := indstart to indend do AllColors[LandWater(i)] := gc[i-indstart];
 RefreshBar;
end;

procedure TColorsFrm.Button2Click(Sender: TObject);
var f:file of TTerrainColors;
begin
 if ColorsOpen.Execute then begin
  ColorsFileName := ColorsOpen.FileName;
  AssignFile(f, ColorsFileName);
  ReSet(f);
  Read(f, AllColors);
  CloseFile(f);
  RefreshBar;
 end;
end;

procedure TColorsFrm.Button3Click(Sender: TObject);
var f:file of TTerrainColors;
begin
 AssignFile(f, ColorsFileName);
 ReWrite(f);
 Write(f, AllColors);
 CloseFile(f);
end;

procedure TColorsFrm.Button4Click(Sender: TObject);
begin
 if ColorsSave.Execute then begin
  ColorsFileName := ColorsSave.FileName;
  Button3Click(Sender); {Save}
 end;

end;

procedure TColorsFrm.Button10Click(Sender: TObject);
begin
 TerrainColors := AllColors;
end;

procedure TColorsFrm.LandWaterRadClick(Sender: TObject);
begin
 RefreshBar;
end;

procedure TColorsFrm.FormCreate(Sender: TObject);
begin
 Button1Click(Sender);
end;

procedure TColorsFrm.IndexEdtExit(Sender: TObject);
begin
 SelectedColor := LandWater(StrToInt(IndexEdt.Text));
 ColorPreview.Color := AllColors[SelectedColor];
 ColorSelect.Color := AllColors[SelectedColor];
end;

procedure TColorsFrm.GradHtml1EdtExit(Sender: TObject);
var c:TColor;
begin
 GradPrev1Pan.Color := HTML2Color(GradHtml1Edt.Text);

end;

procedure TColorsFrm.GradHtml1EdtKeyPress(Sender: TObject; var Key: Char);
begin
 if key=#13 then GradHtml1EdtExit(Sender);
end;

procedure TColorsFrm.GradHtml2EdtExit(Sender: TObject);
begin
 GradPrev2Pan.Color := HTML2Color(GradHtml2Edt.Text);
end;

procedure TColorsFrm.GradHtml2EdtKeyPress(Sender: TObject; var Key: Char);
begin
 if key=#13 then GradHtml2EdtExit(Sender);
end;

end.
