unit uRadarcol;

interface

uses Classes, SysUtils, GR32, Dialogs, Graphics;

type TRadarcol = class
private
        mulStream : TMemoryStream;
public
        loaded : Boolean;
        constructor Create;
        destructor Free;
        procedure Load( FilePath : String );
        procedure Save( FilePath : String );
        function GetColor( Id : Integer ) : TColor32;
        function GetBlue( Id : Integer ) : Integer;
        function GetGreen( Id : Integer ) : Integer;
        function GetRed( Id : Integer ) : Integer;
        procedure SetColor( Id, Color : Integer );
end;

var     Radarcol : TRadarcol;

implementation

uses uUtilities, StrLib;

constructor TRadarcol.Create;
begin
        mulStream := TMemoryStream.Create;
        loaded := false;
end;

destructor TRadarcol.Free;
begin
        mulStream.Free;
end;

procedure TRadarcol.Load( FilePath : String );
begin
        mulStream.LoadFromFile( FilePath );
        loaded := true;
end;

procedure TRadarcol.Save( FilePath : String );
begin
        mulStream.SaveToFile( FilePath );
end;

function TRadarcol.GetColor( Id : Integer ) : TColor32;
var     Color : Word;
begin
        if not loaded then begin
                Result := 0;
                exit;
        end;
        mulStream.Seek( Id*2, soFromBeginning );
        mulStream.Read( Color, 2 );
        Result := WinColor( Color15to32( Color ) );
end;

function TRadarcol.GetRed( Id : Integer ) : Integer;
begin
        Result := RedComponent( GetColor( Id ) );
end;

function TRadarcol.GetGreen( Id : Integer ) : Integer;
begin
        Result := GreenComponent( GetColor( Id ) );
end;

function TRadarcol.GetBlue( Id : Integer ) : Integer;
begin
        Result := BlueComponent( GetColor( Id ) );
end;

procedure TRadarcol.SetColor( Id, Color : Integer );
var     Color15 : Word;
begin
        Color15 := ((Color and $F80000) shr 19) or ((Color and $F800) shr 6) or ((Color and $F8) shl 7);
        mulStream.Seek( Id*2, soFromBeginning );
        mulStream.Write( Color15, 2 );
end;

end.
