program RGB;
{----------------------------------------------------------\
| This program illustrates how to use the SetRGBPalette    |
| command from the Turbo Pascal 5.0 Graph unit to create   |
| 16 shades of gray on a VGA graphics card.                |
|                                                          |
| Author     : John Sieraski (Borland technical support)   |
| Last update: 12/21/88                                    |
| Ware-ness  : Released to the public domain by the author |
\----------------------------------------------------------}
uses
  Crt, Graph;

function Int2Str(L : LongInt) : string;
{ Converts an integer to a string for use with OutText, OutTextXY }
var
  S : string;
begin
  Str(L, S);
  Int2Str := S;
end; { Int2Str }

procedure Wait;
{ Wait for key, then flush the buffer }
var
  Ch : char;
begin
  Ch := ReadKey;
  while KeyPressed do
    Ch := ReadKey;
end; { Wait }

var
  Driver, Mode, ErrorCode, BarHeight, Y : integer;
  PalIndex : byte;

begin
  Driver := VGA;                    { Initialize VGA 640x480 graphics mode }
  Mode := VGAHi;
  InitGraph(Driver, Mode, '');      { Assumes EGAVGA.BGI in default dir }
  ErrorCode := GraphResult;
  if ErrorCode <> grOK then
  begin
    Writeln('Error: ', GraphErrorMsg(ErrorCode));
    Wait; Halt;
  end;

  for PalIndex := 0 to 15 do        { Set raw colors in palette so that }
    SetPalette(PalIndex, PalIndex); { they're in sequence from 0..15    }


  for PalIndex := 0 to 15 do        { Create gray scale in DACs 0..15 }
    SetRGBPalette(PalIndex, PalIndex*4, PalIndex*4, PalIndex*4);

  BarHeight := GetMaxY div 16;      { Display the gray scale }
  Y := 0;
  SetColor(GetMaxColor);            { Color for 1st 8 OutText calls in loop }
  for PalIndex := 0 to 15 do
  begin
    SetFillStyle(SolidFill, PalIndex);
    Bar(0, Y, GetMaxX, Y+BarHeight);
    if PalIndex = 8 then
      SetColor(0);                  { Color for last 8 OutText calls }
    OutTextXY(10,  Y+(BarHeight div 2), Int2Str(PalIndex));
    Inc(Y, BarHeight);
  end;

  Wait; CloseGraph;
end.
