# include <kapa_internal.h>

# define N_KAPA_COLORS 23

static char KAPA_COLORS[N_KAPA_COLORS][3][16] = {
{"black",    	 "black",    	 "0.00 0.00 0.00"}, 
{"white",    	 "white",    	 "1.00 1.00 1.00"}, 
{"red",      	 "red",      	 "1.00 0.00 0.00"}, 
{"pink",     	 "pink",     	 "1.00 0.75 0.80"}, 
{"orange",   	 "orange",   	 "1.00 0.65 0.00"}, 
{"yellow",   	 "yellow",   	 "1.00 1.00 0.00"}, 
{"wheat",    	 "wheat",    	 "0.96 0.87 0.70"}, 
{"gold",     	 "gold",     	 "1.00 0.84 0.00"}, 
{"green",    	 "green",    	 "0.00 1.00 0.00"}, 
{"darkgreen",	 "darkgreen",	 "0.00 0.40 0.00"}, 
{"blue",     	 "blue",     	 "0.00 0.00 1.00"}, 
{"skyblue",  	 "skyblue",  	 "0.53 0.81 0.92"}, 
{"indigo",   	 "mediumpurple", "0.57 0.44 0.86"}, 
{"violet",   	 "darkviolet", 	 "0.58 0.00 0.88"},
{"grey10",   	 "grey10",   	 "0.10 0.10 0.10"},
{"grey20",   	 "grey20",   	 "0.20 0.20 0.20"},
{"grey30",   	 "grey30",   	 "0.30 0.30 0.30"},
{"grey40",   	 "grey40",   	 "0.40 0.40 0.40"},
{"grey50",   	 "grey50",   	 "0.50 0.50 0.50"},
{"grey60",   	 "grey60",   	 "0.60 0.60 0.60"},
{"grey70",   	 "grey70",   	 "0.70 0.70 0.70"},
{"grey80",   	 "grey80",   	 "0.80 0.80 0.80"},
{"grey90",   	 "grey90",   	 "0.90 0.90 0.90"}};

int KapaColorByName (char *name) {

  int i;
  
  for (i = 0; i < N_KAPA_COLORS; i++) {
    if (!strcmp (name, KAPA_COLORS[i][0])) {
      return (i);
    }	
  }
  if (!strcasecmp (name, "none")) return (-1);

  fprintf (stderr, "color may be one of:\n");
  for (i = 0; i < N_KAPA_COLORS; i++) {
    fprintf (stderr, "  %s\n", KAPA_COLORS[i][0]);
  }
  return (-1);
}	

int KapaColormapSize () {
  return (N_KAPA_COLORS);
}

char *KapaColorRGBString (int N) {
  return (KAPA_COLORS[N][2]);
}

char *KapaColorName (int N) {
  return (KAPA_COLORS[N][0]);
}

png_color *KapaPNGPalette (int *Npalette) {

  int i;
  float red, green, blue;
  png_color *palette;

  ALLOCATE (palette, png_color, N_KAPA_COLORS);

  /* define the palette */
  for (i = 0; i < N_KAPA_COLORS; i++) {
    sscanf (KAPA_COLORS[i][2], "%f %f %f", &red, &green, &blue);
    palette[i].red = (0xff * red);
    palette[i].green = (0xff * green);
    palette[i].blue = (0xff * blue);
  }
  *Npalette = N_KAPA_COLORS;
  return (palette);
}

unsigned long *KapaX11colors (Display *display, Colormap colormap, unsigned long default_color, int *Ncolors) {

  int i;
  int status;
  unsigned long *colors;
  XColor rgbcolor, hardwarecolor;

  *Ncolors = N_KAPA_COLORS;
  ALLOCATE (colors, unsigned long, N_KAPA_COLORS);

  for (i = 0; i < N_KAPA_COLORS; i++) {
    colors[i] = default_color;
    status = XLookupColor (display, colormap, KAPA_COLORS[i][1], &rgbcolor, &hardwarecolor);
    if (!status) continue;
    status = XAllocColor (display, colormap, &hardwarecolor);
    if (!status) continue;
    colors[i] = hardwarecolor.pixel;
  }
  return (colors);
}

/*

kapa objects, bDraw, and png all use the same pallete sequence, defined by
KAPA_COLORS above.  the color correspond to a color sequence.  

*/
