# include "Ximage.h"
# define NPIXELS 64

static char default_cmap[] = "grayscale";

void MakeColormap (Graphic *graphic, Layout *layout, int argc, char **argv) {

  int i, N, status;
  char *temp_name;

  /* hardwired colors - white, black -- for drawing text and overlay graphics */
  layout[0].white = graphic[0].white;
  layout[0].black = graphic[0].black;
  layout[0].start = 0;
  layout[0].slope = 1;
  temp_name = XGetDefault (graphic[0].display, argv[0], "ROverlay");
  layout[0].overlay[0].color = GetColor (graphic[0].display, (temp_name == (char *) NULL ? "red"    : temp_name), graphic[0].colormap, graphic[0].black);
  temp_name = XGetDefault (graphic[0].display, argv[0], "GOverlay");
  layout[0].overlay[1].color = GetColor (graphic[0].display, (temp_name == (char *) NULL ? "green"  : temp_name), graphic[0].colormap, graphic[0].black);
  temp_name = XGetDefault (graphic[0].display, argv[0], "BOverlay");
  layout[0].overlay[2].color = GetColor (graphic[0].display, (temp_name == (char *) NULL ? "blue"   : temp_name), graphic[0].colormap, graphic[0].black);
  temp_name = XGetDefault (graphic[0].display, argv[0], "YOverlay");
  layout[0].overlay[3].color = GetColor (graphic[0].display, (temp_name == (char *) NULL ? "yellow" : temp_name), graphic[0].colormap, graphic[0].black);

  layout[0].Npixels = graphic[0].Npixels;
  for (i = 0; i < layout[0].Npixels; i++) 
    layout[0].cmap[i].pixel = graphic[0].pixels[i];

  /* decide on a color map */
  temp_name = XGetDefault (graphic[0].display, argv[0], "Colormap");
  if ((N = get_argument (argc, argv, "-cm"))) {
    if (N + 1 < argc) {
      temp_name = argv[N+1];
    } else {
      fprintf (stderr, "error: usage is -cm ColormapName\n");
      exit (1);
    }
  }
  if (temp_name == (char *) NULL) temp_name = default_cmap;

  status = SetColormap (graphic, layout, temp_name);
  if (!status) {
    fprintf (stderr, "invalid colormap, using default %s\n", default_cmap);
    temp_name = default_cmap;
    status = SetColormap (graphic, layout, temp_name);
    if (!status) {
      fprintf (stderr, "problem with default colormap\n");
      exit (1);
    }
  }
}

/* this routine is NOT independent of the number of overlays */

