////////////////////////////////////////////////////////////////////////////////
// PlugHelper.pas
// -------------------
// This unit contains some functions and classes to help writing plugins.
//
// Last Edit: June 20th 2006
// Certified: NA
//
// This unit is coded in : Delphi 7
//
// Unit is copyright (c) 2006, The CoolMon Project
////////////////////////////////////////////////////////////////////////////////
//
// So what are you seeing ...
//
// TParameterList
// --------------
// This is a class to manage the parameterlist found in very interfaces,
// it will handle a "=|=|=" string and act as a wrapper for it. It works like
// the TStringlist. Create it and add the parameterStr then it can be used as
// a list.
//
// ColorToRGB*
// ----------
// A function that will convert a TColor value to and RGB string value that
// can be used throughout CM2.
//
// RGBToColor*
// ----------
// A function that will convert an RGB string value to a TColor for use when
// painting.
//
// * CM2 Beta an onward needs to work with 4 color value types.
//   (clWhite [Constant, with cl], White [Constant, no cl], $FFFFFF [RGB]
//   and $00FFFFFF [ABGR]
//   This is to ensure backward compatibility. Outlaw Ed will write RGB when
//   using a color dialog.
////////////////////////////////////////////////////////////////////////////////

unit PlugHelper;

interface

Uses
 Graphics,
 Sysutils,
 Filter;

Type
 TParameter = Record
  Name: String;
  Value: String;
 end;
 TParameterList = Class(TObject)
 private
  fParameterStr: String;
  function GETCOUNT: Integer;
  function GETPARAMETER(Index: Integer): TParameter;
 public
  Property ParameterStr: String read fParameterStr write fParameterStr;
  Property Parameter[Index: Integer]: TParameter read GETPARAMETER; default;
  Property Count: Integer read GETCOUNT;
 end;

 Function ColorToRGB(Color: TColor): String;
 Function RGBToColor(RGB: String): TColor;

implementation

Function ColorToRGB(Color: TColor): String;
Var
 s: String;
 RS, GS, BS: String;
begin
 Result := '$FFFFFF';
 S := ColorToString(Color);
 If length(S) = 0 then exit;
 If S[1] = '$' Then begin
  BS := Copy(S, 4, 2);
  GS := Copy(S, 6, 2);
  RS := Copy(S, 8, 2);
  Result := '$' + RS + GS + BS;
 End
  else
 begin
  If uppercase(copy(S, 1, 2)) = 'CL' then begin
   Result := copy(S, 3, length(s));
  end;
 end;
end;

Function RGBToColor(RGB: String): TColor;
Begin
 Result := clWhite;
 If length(RGB) = 0 then exit;
 If RGB[1] = '$' then begin
  If length(RGB) = 7 then begin
   // RGB
   RGB := '$00' + Copy(RGB, 6, 2) + Copy(RGB, 4, 2) + Copy(RGB, 2, 2);
  end;
  // ABGR is not checked because it basically should be converted later
 end
  else
 begin
  // Color name
  If uppercase(copy(rgb, 1, 2)) <> 'CL' then begin
   RGB := 'cl' + RGB;
  end;
 end;
 Try
  Result := StringToColor(RGB);
 except
  on EConvertError do begin
   Result := clWhite;
  end;
 end;
End;

{ TParameterList }

function TParameterList.GETCOUNT: Integer;
begin
 If ParameterStr = '' then begin
  Result := 0;
  exit;
 end;
 Result := CountFilt(ParameterStr, '|');
end;

function TParameterList.GETPARAMETER(Index: Integer): TParameter;
Var
 N, V: String;
begin
 N := GetFilt(ParameterStr, Index, '|');
 V := GetFilt(N, 2, '=');
 N := GetFilt(N, 1, '=');
 Result.Name := N;
 Result.Value := V;
end;

end.
