unit PluginTools;

interface

uses
  Windows, SysUtils, Classes, Graphics, tlHelp32,
  Filter;

const
  ColorsEx: array[0..120] of TIdentMapEntry = (
    (Value: $FFF8F0; Name: 'clAliceBlue'),
    (Value: $D7EBFA; Name: 'clAntiqueWhite'),
    (Value: $D4FF7F; Name: 'clAquamarine'),
    (Value: $FFFFF0; Name: 'clAzure'),
    (Value: $DCF5F5; Name: 'clBeige'),
    (Value: $C4E4FF; Name: 'clBisque'),
    (Value: $CDEBFF; Name: 'clBlanchedAlmond'),
    (Value: $E22B8A; Name: 'clBlueViolet'),
    (Value: $2A2AA5; Name: 'clBrown'),
    (Value: $87B8DE; Name: 'clBurlyWood'),
    (Value: $A09E5F; Name: 'clCadetBlue'),
    (Value: $00FF7F; Name: 'clChartreuse'),
    (Value: $1E69D2; Name: 'clChocolate'),
    (Value: $507FFF; Name: 'clCoral'),
    (Value: $ED9564; Name: 'clCornflowerBlue'),
    (Value: $DCF8FF; Name: 'clCornsilk'),
    (Value: $DC143C; Name: 'clCrimson'),
    (Value: $FFFF00; Name: 'clCyan'),
    (Value: $8B0000; Name: 'clDarkBlue'),
    (Value: $8B8B00; Name: 'clDarkCyan'),
    (Value: $0B86B8; Name: 'clDarkGoldenrod'),
    (Value: $A9A9A9; Name: 'clDarkGray'),
    (Value: $006400; Name: 'clDarkGreen'),
    (Value: $6BB7BD; Name: 'clDarkKhaki'),
    (Value: $8B008B; Name: 'clDarkMagenta'),
    (Value: $2F6B55; Name: 'clDarkOliveGreen'),
    (Value: $008CFF; Name: 'clDarkOrange'),
    (Value: $CC3299; Name: 'clDarkOrchid'),
    (Value: $00008B; Name: 'clDarkRed'),
    (Value: $7A96E9; Name: 'clDarkSalmon'),
    (Value: $8BBC8F; Name: 'clDarkSeaGreen'),
    (Value: $8B3D48; Name: 'clDarkSlateBlue'),
    (Value: $4F4F2F; Name: 'clDarkSlateGray'),
    (Value: $D1CE00; Name: 'clDarkTurquoise'),
    (Value: $D30094; Name: 'clDarkViolet'),
    (Value: $9314FF; Name: 'clDeepPink'),
    (Value: $FFBF00; Name: 'clDeepSkyBlue'),
    (Value: $696969; Name: 'clDimGray'),
    (Value: $FF901E; Name: 'clDodgerBlue'),
    (Value: $2222B2; Name: 'clFirebrick'),
    (Value: $228B22; Name: 'clForestGreen'),
    (Value: $DCDCDC; Name: 'clGainsboro'),
    (Value: $FFF8F8; Name: 'clGhostWhite'),
    (Value: $00D7FF; Name: 'clGold'),
    (Value: $20A5DA; Name: 'clGoldenrod'),
    (Value: $2FFFAD; Name: 'clGreenYellow'),
    (Value: $F0FFF0; Name: 'clHoneydew'),
    (Value: $B469FF; Name: 'clHotPink'),
    (Value: $5C5CCD; Name: 'clIndianRed'),
    (Value: $82004B; Name: 'clIndigo'),
    (Value: $F0FFFF; Name: 'clIvory'),
    (Value: $8CE6F0; Name: 'clKhaki'),
    (Value: $FAE6E6; Name: 'clLavender'),
    (Value: $F5F0FF; Name: 'clLavenderBlush'),
    (Value: $00FC7C; Name: 'clLawnGreen'),
    (Value: $CDFAFF; Name: 'clLemonChiffon'),
    (Value: $E6D8AD; Name: 'clLightBlue'),
    (Value: $8080F0; Name: 'clLightCoral'),
    (Value: $FFFFE0; Name: 'clLightCyan'),
    (Value: $D2FAFA; Name: 'clLightGoldenrodYellow'),
    (Value: $90EE90; Name: 'clLightGreen'),
    (Value: $C1B6FF; Name: 'clLightPink'),
    (Value: $7AA0FF; Name: 'clLightSalmon'),
    (Value: $AAB220; Name: 'clLightSeaGreen'),
    (Value: $FACE87; Name: 'clLightSkyBlue'),
    (Value: $998877; Name: 'clLightSlateGray'),
    (Value: $DEC4B0; Name: 'clLightSteelBlue'),
    (Value: $E0FFFF; Name: 'clLightYellow'),
    (Value: $32CD32; Name: 'clLimeGreen'),
    (Value: $E6F0FA; Name: 'clLinen'),
    (Value: $FF00FF; Name: 'clMagenta'),
    (Value: $AACD66; Name: 'clMediumAquamarine'),
    (Value: $CD0000; Name: 'clMediumBlue'),
    (Value: $D355BA; Name: 'clMediumOrchid'),
    (Value: $DB7093; Name: 'clMediumPurple'),
    (Value: $71B33C; Name: 'clMediumSeaGreen'),
    (Value: $EE687B; Name: 'clMediumSlateBlue'),
    (Value: $9AFA00; Name: 'clMediumSpringGreen'),
    (Value: $CCD148; Name: 'clMediumTurquoise'),
    (Value: $8515C7; Name: 'clMediumVioletRed'),
    (Value: $701919; Name: 'clMidnightBlue'),
    (Value: $E1E4FF; Name: 'clMistyRose'),
    (Value: $B5E4FF; Name: 'clMoccasin'),
    (Value: $ADDEFF; Name: 'clNavajoWhite'),
    (Value: $E6F5FD; Name: 'clOldLace'),
    (Value: $238E6B; Name: 'clOliveDrab'),
    (Value: $00A5FF; Name: 'clOrange'),
    (Value: $0045FF; Name: 'clOrangeRed'),
    (Value: $D670DA; Name: 'clOrchid'),
    (Value: $AAE8EE; Name: 'clPaleGoldenrod'),
    (Value: $98FB98; Name: 'clPaleGreen'),
    (Value: $EEEEAF; Name: 'clPaleTurquoise'),
    (Value: $9370DB; Name: 'clPaleVioletRed'),
    (Value: $D5EFFF; Name: 'clPapayaWhip'),
    (Value: $B9DAFF; Name: 'clPeachPuff'),
    (Value: $3F85CD; Name: 'clPeru'),
    (Value: $CBC0FF; Name: 'clPink'),
    (Value: $DDA0DD; Name: 'clPlum'),
    (Value: $E6E0B0; Name: 'clPowderBlue'),
    (Value: $8F8FBC; Name: 'clRosyBrown'),
    (Value: $E16941; Name: 'clRoyalBlue'),
    (Value: $13458B; Name: 'clSaddleBrown'),
    (Value: $7280FA; Name: 'clSalmon'),
    (Value: $60A4F4; Name: 'clSandyBrown'),
    (Value: $578B2E; Name: 'clSeaGreen'),
    (Value: $EEF5FF; Name: 'clSeaShell'),
    (Value: $2D52A0; Name: 'clSienna'),
    (Value: $EBCE87; Name: 'clSkyBlue2'),
    (Value: $CD5A6A; Name: 'clSlateBlue'),
    (Value: $908070; Name: 'clSlateGray'),
    (Value: $FAFAFF; Name: 'clSnow'),
    (Value: $7FFF00; Name: 'clSpringGreen'),
    (Value: $B48246; Name: 'clSteelBlue'),
    (Value: $8CB4D2; Name: 'clTan'),
    (Value: $D8BFD8; Name: 'clThistle'),
    (Value: $4763FF; Name: 'clTomato'),
    (Value: $D0E040; Name: 'clTurquoise'),
    (Value: $EE82EE; Name: 'clViolet'),
    (Value: $B3DEF5; Name: 'clWheat'),
    (Value: $F5F5F5; Name: 'clWhiteSmoke'),
    (Value: $32CD9A; Name: 'clYellowGreen')    
);

function ByteToHex(aByte: Byte): shortstring;
function GetGradientColorize(colors: array of TColor; mult: double): TColor;
function HexToByte(aHex: string): byte;
procedure IntToBinary(aInt: integer; var BinArray: array of boolean);
function IntToRoman(aInt: LongInt): string;
function IsProcessRunning(aProcessName: string): boolean;
function RemoveSpaces(aStr: string): string;
procedure SetFont(fontStr: string; aFont: TFont);
function Split(aStr: string; aDelim: char; const aStrList: TStrings): boolean;
function StringIsColor(aStr: string; out ResColor: TColor): boolean;
function StringToCase(aStr: string; aStrArray: array of string; WildCards: boolean): integer;
function StringToTColor(aStr: string): TColor;

implementation

function ByteToHex(aByte: Byte): shortstring;
const
  Digits: array[0..15] of char = '0123456789ABCDEF';
begin
  Result := Digits[aByte shr 4] + Digits[aByte and $0F];
end;

function GetGradientColorize(colors: array of TColor; mult: double): TColor;
type
  RGBArray = array[0..2] of byte;
var
  x, y: integer;
  a: RGBArray;
  b: array of RGBArray;
begin
  if High(Colors) = 1 then
    begin
      SetLength(b, 2);
      for x := 0 to 1 do
        begin
          Colors[x] := ColorToRGB(Colors[x]);
          b[x][0] := GetRValue(Colors[x]);
          b[x][1] := GetGValue(Colors[x]);
          b[x][2] := GetBValue(Colors[x]);
        end;
      for y := 0 to 2 do a[y] := Trunc(b[0][y] + ((b[1][y] - b[0][y]) * mult));
      Result := RGB(a[0], a[1], a[2]);
      b := nil;
    end
  else Result := -1;
end;

function HexToByte(aHex: string): byte;
begin
  Result := StrToInt(aHex);
end;

function IntToRoman(aInt: LongInt): string;
const
  Arabics: array[1..13] of integer = (1,4,5,9,10,40,50,90,100,400,500,900,1000);
  Romans: array[1..13] of string = ('I','IV','V','IX','X','XL','L','XC','C','CD','D','CM','M');
var
  i: integer;
begin
  for i := 13 downto 1 do
  while aInt >= Arabics[i] do
    begin
      aInt := aInt - Arabics[i];
      Result := Result + Romans[i];
    end;
end;

function IsProcessRunning(aProcessName: string): boolean;
var
  ssHandle: HWND;
  processInfo: PROCESSENTRY32;
begin
  result := false;
  ssHandle := CreateToolHelp32SnapShot(2, 0);
  if ssHandle <> INVALID_HANDLE_VALUE then
    begin
      processInfo.dwSize := sizeof(PROCESSENTRY32);
      if process32First(ssHandle, processInfo) then
        repeat
          if processInfo.szExeFile = aProcessName then
            begin
              result := true;
              break;
            end;
        until not process32Next(ssHandle, processInfo);
    end;
  try CloseHandle(ssHandle); except end;
end;

//used in BinaryClock plugin
//can be expanded to handle larger numbers
procedure IntToBinary(aInt: integer; var BinArray: array of boolean);
var
  i: integer;
begin
  for i := 0 to 3 do
    begin
      BinArray[i] := aInt mod 2 = 1;
      aInt := aInt div 2;
    end;
end;

function RemoveSpaces(aStr: string): string;
var
  i: integer;
  s: string;
begin
  Result := '';
  s := '';
  for i := 1 to Length(aStr) do if aStr[i] <> ' ' then s := s + aStr[i];
  Result := s;
end;

procedure SetFont(fontStr: string; aFont: TFont);
var
  fStyle: string;
begin
  if CountFilt(fontStr, ',') = 3 then
    begin
      try aFont.Name := GetFilt(fontStr, 1, ','); except aFont.Name := 'Arial'; end;
      try aFont.Size := StrToInt(GetFilt(fontStr, 2, ',')); except aFont.Size := 8; end;
      fStyle := GetFilt(fontStr, 3, ',');
      if Length(fStyle) <> 4 then aFont.Style := []
      else
        begin
          aFont.Style := [];
          if fStyle[1] = '1' then aFont.Style := [fsBold];
          if fStyle[2] = '1' then aFont.Style := aFont.Style + [fsItalic];
          if fStyle[3] = '1' then aFont.Style := aFont.Style + [fsUnderline];
          if fStyle[4] = '1' then aFont.Style := aFont.Style + [fsStrikeOut];
        end;
    end;
end;

function Split(aStr: string; aDelim: char; const aStrList: TStrings): boolean;
begin
  try
    Assert(Assigned(aStrList));
  except
    Result := false;
    Exit;
  end;
  Result := true;
  aStrList.Clear;
  aStrList.Delimiter := aDelim;
  aStrList.DelimitedText := aStr;
end;

function StringIsColor(aStr: string; out ResColor: TColor): boolean;
begin
  Result := true;
  try
    ResColor := StringToColor(aStr);
  except
    Result := false;
  end;
end;

function StringToCase(aStr: string; aStrArray: array of string; WildCards: boolean): integer;
var
  i, wPos: integer;
  sBuff, slBuff: string;
begin
  result := -1;
  for i := 0 to length(aStrArray) - 1 do
    begin
      sBuff := aStr;
      slBuff := Trim(aStrArray[i]);
      if WildCards then
        begin
          wPos := Pos('*', aStrArray[i]);
          if wPos > 0 then
            begin
              sBuff := Copy(sBuff, 1, wPos-1);
              slBuff := Copy(slBuff, 1, wPos-1);
            end;
        end;
      if CompareText(sBuff, slBuff) = 0 then
        begin
          result := i;
          break;
        end;
    end;
end;

function StringToTColor(aStr: string): TColor;
var
  r, g, b: string;
  i: integer;
begin
  Result := clWhite;
  if aStr[1] = '$' then
    case Length(aStr) of
      7: begin
           r := Copy(aStr, 2, 2);
           g := Copy(aStr, 4, 2);
           b := Copy(aStr, 6, 2);
           aStr := '$' + b + g + r;
           try Result := StringToColor(aStr); except end;
         end;
      9: try Result := StringToColor(aStr); except end;
    end
  else
    try
      if Uppercase(Copy(aStr, 1, 2)) <> 'CL' then aStr := 'cl' + aStr;
      if not IdentToColor(aStr, i) then IdentToInt(aStr, i, ColorsEx);
      Result := TColor(i);
    except
    end;
end;

end.
