private int allocateRGB(int rgb) {
      if (rgb >= -16 && rgb < 0)
         return rgb + 16;
      if (rgb < 0 || rgb > 0xFFFFFF)
         return -1;
      for (int a = 16; a<n_colour; a++) 
         if (colour[a] == rgb)
            return a;
      if (n_colour < colour.length) {
         int ii = n_colour;
         colour[n_colour++] = rgb;
         putSetColour(ii, rgb);
         return ii;
      }
      return -1;
   }