[comp.sys.mac.programmer] Help with Palette Manager

rainero@prism.wrc.xerox.com (Emil Rainero) (12/14/90)

I have tried unsuccessfully to use the Palette Manager to dynamically
create a custom palette and have the changes take effect.  Could some
kind soul post a code fragment (C or Pascal) that would create a
256 entry grayscale palette (or 256 random colors) and have the effect
become visible on the display device.

I have tried to use the Palette manager without using CLUT's or a resource
based palette, but have been unsuccessful so far.

Thanks in advance,

Emil Rainero

orpheus@reed.UUCP (Aaron Semplers) (12/14/90)

The major problem I had with using the Palette Manager was my development
system, Think Pascal. Apparently, Think Pascal could not truly represent
unsigned integers. Since the RGBColor structure is based on them, I had to
write a SetRGBColor routine that would take a longint for each of the RGB
values, and subtract 65535 from it if it was over 32767.

	It worked fairly well after that, although there are a few tricks to
it and I have never been able to get the palette to animate. I thought I
should post some of the code I used, because I have probably forgotten
some of the tricks by now. So here it is.

	You know, I was editing the code to post it with this message,
and I became worried. I began to wonder if I would get flamed for it.
I didn't like that feeling very well, so here it is, as is. If there are
any major flaws in it, I would be interested in hearing about them,
but I'm just glad I got it to work.

	orpheus@reed
       "that was the river... this is the sea..."


{There are one or two simple utility routines in here that I thought were}
{too obvious to post, in hindsight. But if you want, I'll send 'em.}

Type
 DisplayKind = (Monochrome, Grayscale, Color);

 DisplayRecord = Record
  Window: CWindowPeek;
  Port: CWindowPtr;
  World: GWorldPtr;
  Center: Point;
  Extents: Rect;

  Kind: DisplayKind;
  Mode: Integer;
  Depth: Integer;

  RGB: RGBColor;
  Gray: Real;

  Colors: CTabHandle;
  Palette: PaletteHandle;
 End;

Procedure PaletteConstruct;
 Var
  i: Longint;
  c: RGBColor;
  Entries: Integer;
Begin

 With theDisplay Do
  If (Kind = Color) Then
   Begin
    Palette := NewPalette(Mode, Nil, pmExplicit + pmTolerant, 0);
    If Granted(Palette) Then
     Begin
      Entries := Mode - 2;

      SetRGBColor(c, $FFFF, $FFFF, $FFFF);
      SetEntryColor(Palette, 0, c);

      Entries := Entries + 1;
      For i := 1 To Entries Do
       Begin
        c := GrayToRGB(Percentage(i, Entries));
        SetEntryColor(Palette, i, c);
       End;

      SetRGBColor(c, $0000, $0000, $0000);
      SetEntryColor(Palette, Entries + 1, c);

      RequestHandle(Colors, SizeOf(CTabHandle));
      Palette2CTab(Palette, Colors);

{Note: I create the palette and color table before the window and}
{offscreen graphics world. I use NSetPalette to attach the palette}
{to the window, and pass the offscreen graphics world the color table.}

     End;
  End;
End;

Procedure PaletteDestruct;
Begin
 With theDisplay Do
  If Kind = Color Then
   Begin
    If Granted(Palette) Then
     DisposePalette(Palette);
    If Granted(Colors) Then
     DisposCTable(Colors);
   End;
 RestoreDeviceClut(GetGDevice);
End;