unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, ExtCtrls,
  Buttons, StdCtrls, Math, Types;

type

  { TForm1 }

  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    LabeledEdit1: TLabeledEdit;
    LabeledEdit2: TLabeledEdit;
    LabeledEdit3: TLabeledEdit;
    LabeledEdit4: TLabeledEdit;
    LabeledEdit5: TLabeledEdit;
    LabeledEdit6: TLabeledEdit;
    PageControl1: TPageControl;
    ML: TTabSheet;
    TabSheet1: TTabSheet;
    procedure BitBtn1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure LabeledEdit1Change(Sender: TObject);
    procedure LabeledEdit2Change(Sender: TObject);
    procedure LabeledEdit3Change(Sender: TObject);
    procedure LabeledEdit4Change(Sender: TObject);
    procedure LabeledEdit4EditingDone(Sender: TObject);
    procedure LabeledEdit5Change(Sender: TObject);
    procedure LabeledEdit5EditingDone(Sender: TObject);
    procedure LabeledEdit6Change(Sender: TObject);
    procedure MLContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
    procedure PageControl1Change(Sender: TObject);
  private
    function CalcAbsMFromL(L: Double): Double;
    function CalcLstarFromAbsM(M: Double): Double;
    function CalcLFromAbsM(M: Double): Double;
    function CalcLFromLstar(Lstar: Double): Double;
    function CalcLstarFromL(L: Double): Double;
  public

  end;
const
  c = 299792458;
  Msun = 4.84;
  Lsun = 3.828e26;
var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.PageControl1Change(Sender: TObject);
begin

end;

procedure TForm1.LabeledEdit2Change(Sender: TObject);
begin

end;

procedure TForm1.LabeledEdit1Change(Sender: TObject);
begin

end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var M, Lstar, L: Double;
begin
  try
    M := StrToFloat(LabeledEdit1.Text);
  except
    ShowMessage('keine Fließkommazahl');
    M := 0;
  end;
  Lstar := CalcLstarFromAbsM(M);//Power(10, (M-Msun)/(-2.5));
  L := CalcLFromLstar(Lstar);

  Lstar := RoundTo(Lstar, -4);
  L := RoundTo(L,23);
  LabeledEdit2.Text := FloatToStr(Lstar);
  LabeledEdit3.Text := FloatToStr(L);
end;

procedure TForm1.Button1Click(Sender: TObject);
var L, M: Double;
begin
  try
    if LabeledEdit5.Text = '' then L := Lsun
    else
      L := StrToFloat(LabeledEdit5.Text);
  except
    ShowMessage('keine Fließkommazahl');
    L := Lsun;
  end;
  M := CalcAbsMFromL(L);
  LabeledEdit6.Text := FormatFloat('#.##',M);
end;

procedure TForm1.LabeledEdit3Change(Sender: TObject);
begin

end;

procedure TForm1.LabeledEdit4Change(Sender: TObject);
begin

end;

procedure TForm1.LabeledEdit4EditingDone(Sender: TObject);
var L, Lstar: Double;
begin
  try
    if LabeledEdit4.Text = '' then Lstar := 1
    else
      Lstar := StrToFloat(LabeledEdit4.Text);
  except
    ShowMessage('keine Fließkommazahl');
    Lstar := 1;
  end;
  L := CalcLFromLstar(Lstar);
  LabeledEdit5.Text := FormatFloat('0.000E+00',L);
end;

procedure TForm1.LabeledEdit5Change(Sender: TObject);
begin

end;

procedure TForm1.LabeledEdit5EditingDone(Sender: TObject);
var L, Lstar: Double;
begin
  try
    if LabeledEdit5.Text = '' then L := Lsun
    else
      L := StrToFloat(LabeledEdit5.Text);
  except
    ShowMessage('keine Fließkommazahl');
    L := Lsun;
  end;
  Lstar := CalcLStarFromL(L);
  LabeledEdit4.Text := FormatFloat('#.##',Lstar);
end;

procedure TForm1.LabeledEdit6Change(Sender: TObject);
begin

end;

procedure TForm1.MLContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin

end;

function TForm1.CalcAbsMFromL(L: Double): Double;
begin
  CalcAbsMFromL := -2.5 * Log10( L / Lsun )+ Msun;
end;

function TForm1.CalcLstarFromAbsM(M: Double): Double;
begin
  CalcLstarFromAbsM :=  Power(10, (M-Msun)/(-2.5));
end;

function TForm1.CalcLFromAbsM(M: Double): Double;
begin
  CalcLFromAbsM :=  Lsun * Power(10, (M-Msun)/(-2.5));
end;

function TForm1.CalcLFromLstar(Lstar: Double): Double;
begin
  CalcLFromLstar := Lstar * Lsun;
end;

function TForm1.CalcLstarFromL(L: Double): Double;
begin
  CalcLstarFromL := L / Lsun;
end;

end.

