Monday, February 12, 2018

DELPHI - How to make "stack" for colors

Returned colors are repeated still in same order.
{ ***************************************************************************

  TColorStack - stack for colors.

  ************************************************************************** }

  TColorStack = class
  private
    pList : TList;

    iCurrent : integer;
  public
    constructor Create;
    destructor Destroy; override;

    function GetColor : TColor;
    procedure Reset;
  end;

constructor TColorStack.Create;
begin
  inherited Create;

  pList := TList.Create;

  { -- add some colors to list }

  Reset;
end;

destructor TColorStack.Destroy;
begin
  pList.Free;

  inherited destroy;
end;

{ ---------------------------------------------------------------------------
  Get next color from list.
  -------------------------------------------------------------------------- }
function TColorStack.GetColor : TColor;
begin
  result := TColor( pList[ iCurrent ] );

  inc( iCurrent );
  if iCurrent > pList.Count - 1 then iCurrent := 0;
end;

{ ---------------------------------------------------------------------------
  Reset to initial state.
  -------------------------------------------------------------------------- }
procedure TColorStack.Reset;
begin
  pList.Clear;

  pList.Add( Pointer( TColor( rgb( 99, 167, 54 ) ) ) );
  pList.Add( Pointer( TColor( rgb( 245, 71, 106 ) ) ) );
  pList.Add( Pointer( TColor( rgb( 91, 115, 196 ) ) ) );
  pList.Add( Pointer( TColor( rgb( 249, 251, 155 ) ) ) );
  pList.Add( Pointer( TColor( rgb( 148, 148, 148 ) ) ) );
  pList.Add( Pointer( TColor( rgb( 207, 118, 189 ) ) ) );
  pList.Add( Pointer( TColor( rgb( 170, 231, 152 ) ) ) );
  pList.Add( Pointer( TColor( rgb( 126, 221, 226 ) ) ) );
  pList.Add( Pointer( TColor( rgb( 181, 176, 130 ) ) ) );
  pList.Add( Pointer( TColor( rgb( 244, 202, 53 ) ) ) );
  pList.Add( Pointer( TColor( rgb( 209, 209, 209 ) ) ) );
  pList.Add( Pointer( TColor( rgb( 74, 61, 245 ) ) ) );

  iCurrent := 0;
end;

No comments:

Post a Comment