]> Pileus Git - ~andy/gtk/blobdiff - gdk/gdkcolor.c
[ Merges from gtk-1-2 ]
[~andy/gtk] / gdk / gdkcolor.c
index a83a7400e6e6717827e2579d3ad709bf1a72f4ce..2baa200ee03cd54e74fa9b9e24255ecff37e3cbb 100644 (file)
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
+
+/*
+ * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
+ * file for a list of people on the GTK+ Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
+ */
+
+#include <time.h>
 #include <X11/Xlib.h>
 #include "gdk.h"
 #include "gdkprivate.h"
+#include "gdkx.h"
 
 
 static gint  gdk_colormap_match_color (GdkColormap *cmap,
@@ -51,17 +61,27 @@ gdk_colormap_new (GdkVisual *visual,
 
   private->xdisplay = gdk_display;
   private->visual = visual;
-  private->next_color = 0;
   private->ref_count = 1;
+
+  private->hash = NULL;
+  private->last_sync_time = 0;
+  private->info = NULL;
+  
   xvisual = ((GdkVisualPrivate*) visual)->xvisual;
 
   colormap->size = visual->colormap_size;
-  colormap->colors = g_new (GdkColor, colormap->size);
+  colormap->colors = NULL;
 
   switch (visual->type)
     {
     case GDK_VISUAL_GRAYSCALE:
     case GDK_VISUAL_PSEUDO_COLOR:
+      private->info = g_new0 (GdkColorInfo, colormap->size);
+      colormap->colors = g_new (GdkColor, colormap->size);
+      
+      private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
+                                       (GCompareFunc) gdk_color_equal);
+      
       private->private_val = private_cmap;
       private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
                                            xvisual, (private_cmap) ? (AllocAll) : (AllocNone));
@@ -97,6 +117,7 @@ gdk_colormap_new (GdkVisual *visual,
       private->private_val = TRUE;
       private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
                                            xvisual, AllocAll);
+      colormap->colors = g_new (GdkColor, colormap->size);
 
       size = 1 << visual->red_prec;
       for (i = 0; i < size; i++)
@@ -133,12 +154,15 @@ gdk_colormap_real_destroy (GdkColormap *colormap)
   GdkColormapPrivate *private = (GdkColormapPrivate*) colormap;
 
   g_return_if_fail (colormap != NULL);
-
-  if (private->ref_count > 0)
-    return;
+  g_return_if_fail (private->ref_count == 0);
 
   gdk_colormap_remove (colormap);
   XFreeColormap (private->xdisplay, private->xcolormap);
+
+  if (private->hash)
+    g_hash_table_destroy (private->hash);
+  
+  g_free (private->info);
   g_free (colormap->colors);
   g_free (colormap);
 }
@@ -147,6 +171,7 @@ GdkColormap*
 gdk_colormap_ref (GdkColormap *cmap)
 {
   GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
+
   g_return_val_if_fail (cmap != NULL, NULL);
 
   private->ref_count += 1;
@@ -157,20 +182,82 @@ void
 gdk_colormap_unref (GdkColormap *cmap)
 {
   GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
+
   g_return_if_fail (cmap != NULL);
+  g_return_if_fail (private->ref_count > 0);
 
   private->ref_count -= 1;
   if (private->ref_count == 0)
     gdk_colormap_real_destroy (cmap);
 }
 
+GdkVisual *
+gdk_colormap_get_visual (GdkColormap *colormap)
+{
+  GdkColormapPrivate *private;
+
+  g_return_val_if_fail (colormap != NULL, NULL);
+  
+  private = (GdkColormapPrivate *)colormap;
+
+  return private->visual;
+}
+     
+#define MIN_SYNC_TIME 2
+
+void
+gdk_colormap_sync (GdkColormap *colormap,
+                  gboolean     force)
+{
+  time_t current_time;
+  GdkColormapPrivate *private = (GdkColormapPrivate *)colormap;
+  XColor *xpalette;
+  gint nlookup;
+  gint i;
+  
+  g_return_if_fail (colormap != NULL);
+
+  current_time = time (NULL);
+  if (!force && ((current_time - private->last_sync_time) < MIN_SYNC_TIME))
+    return;
+
+  private->last_sync_time = current_time;
+
+  nlookup = 0;
+  xpalette = g_new (XColor, colormap->size);
+  
+  for (i = 0; i < colormap->size; i++)
+    {
+      if (private->info[i].ref_count == 0)
+       {
+         xpalette[nlookup].pixel = i;
+         xpalette[nlookup].red = 0;
+         xpalette[nlookup].green = 0;
+         xpalette[nlookup].blue = 0;
+         nlookup++;
+       }
+    }
+  
+  XQueryColors (gdk_display, private->xcolormap, xpalette, nlookup);
+  
+  for (i = 0; i < nlookup; i++)
+    {
+      gulong pixel = xpalette[i].pixel;
+      colormap->colors[pixel].pixel = pixel;
+      colormap->colors[pixel].red = xpalette[i].red;
+      colormap->colors[pixel].green = xpalette[i].green;
+      colormap->colors[pixel].blue = xpalette[i].blue;
+    }
+  
+  g_free (xpalette);
+}
+                  
+
 GdkColormap*
 gdk_colormap_get_system (void)
 {
   static GdkColormap *colormap = NULL;
   GdkColormapPrivate *private;
-  XColor *xpalette;
-  gint i;
 
   if (!colormap)
     {
@@ -181,37 +268,25 @@ gdk_colormap_get_system (void)
       private->xcolormap = DefaultColormap (gdk_display, gdk_screen);
       private->visual = gdk_visual_get_system ();
       private->private_val = FALSE;
-      private->next_color = 0;
       private->ref_count = 1;
 
+      private->hash = NULL;
+      private->last_sync_time = 0;
+      private->info = NULL;
+
+      colormap->colors = NULL;
       colormap->size = private->visual->colormap_size;
-      colormap->colors = g_new (GdkColor, colormap->size);
 
       if ((private->visual->type == GDK_VISUAL_GRAYSCALE) ||
          (private->visual->type == GDK_VISUAL_PSEUDO_COLOR))
        {
-         xpalette = g_new (XColor, colormap->size);
-         
-         for (i = 0; i < colormap->size; i++)
-           {
-             xpalette[i].pixel = i;
-             xpalette[i].red = 0;
-             xpalette[i].green = 0;
-             xpalette[i].blue = 0;
-           }
+         private->info = g_new0 (GdkColorInfo, colormap->size);
+         colormap->colors = g_new (GdkColor, colormap->size);
          
-         XQueryColors (gdk_display, private->xcolormap, xpalette, 
-                       colormap->size);
-         
-         for (i = 0; i < colormap->size; i++)
-           {
-             colormap->colors[i].pixel = xpalette[i].pixel;
-             colormap->colors[i].red = xpalette[i].red;
-             colormap->colors[i].green = xpalette[i].green;
-             colormap->colors[i].blue = xpalette[i].blue;
-           }
+         private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
+                                           (GCompareFunc) gdk_color_equal);
 
-         g_free (xpalette);
+         gdk_colormap_sync (colormap, TRUE);
        }
 
       gdk_colormap_add (colormap);
@@ -257,7 +332,6 @@ gdk_colormap_change (GdkColormap *colormap,
        }
 
       XStoreColors (private->xdisplay, private->xcolormap, palette, ncolors);
-      private->next_color = MAX (private->next_color, ncolors);
       break;
 
     case GDK_VISUAL_DIRECT_COLOR:
@@ -338,6 +412,7 @@ gdk_colors_alloc (GdkColormap   *colormap,
 {
   GdkColormapPrivate *private;
   gint return_val;
+  gint i;
 
   g_return_val_if_fail (colormap != NULL, 0);
 
@@ -346,23 +421,16 @@ gdk_colors_alloc (GdkColormap   *colormap,
   return_val = XAllocColorCells (private->xdisplay, private->xcolormap,
                                 contiguous, planes, nplanes, pixels, npixels);
 
-  return return_val;
-}
-
-void
-gdk_colors_free (GdkColormap *colormap,
-                gulong      *pixels,
-                gint         npixels,
-                gulong       planes)
-{
-  GdkColormapPrivate *private;
-
-  g_return_if_fail (colormap != NULL);
-
-  private = (GdkColormapPrivate*) colormap;
+  if (return_val)
+    {
+      for (i=0; i<npixels; i++)
+       {
+         private->info[pixels[i]].ref_count++;
+         private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE;
+       }
+    }
 
-  XFreeColors (private->xdisplay, private->xcolormap,
-              pixels, npixels, planes);
+  return return_val != 0;
 }
 
 /*
@@ -421,7 +489,7 @@ gdk_color_free (GdkColor *color)
   g_mem_chunk_free (color_chunk, color);
 }
 
-gint
+gboolean
 gdk_color_white (GdkColormap *colormap,
                 GdkColor    *color)
 {
@@ -444,7 +512,7 @@ gdk_color_white (GdkColormap *colormap,
   return return_val;
 }
 
-gint
+gboolean
 gdk_color_black (GdkColormap *colormap,
                 GdkColor    *color)
 {
@@ -493,19 +561,120 @@ gdk_color_parse (const gchar *spec,
   return return_val;
 }
 
-gboolean
-gdk_color_alloc (GdkColormap *colormap,
-                GdkColor    *color)
+/* This is almost identical to gdk_colormap_free_colors.
+ * Keep them in sync!
+ */
+void
+gdk_colors_free (GdkColormap *colormap,
+                gulong      *in_pixels,
+                gint         in_npixels,
+                gulong       planes)
+{
+  GdkColormapPrivate *private;
+  gulong *pixels;
+  gint npixels = 0;
+  gint i;
+
+  g_return_if_fail (colormap != NULL);
+  g_return_if_fail (in_pixels != NULL);
+
+  private = (GdkColormapPrivate*) colormap;
+
+  if ((private->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
+      (private->visual->type != GDK_VISUAL_GRAYSCALE))
+    return;
+  
+  pixels = g_new (gulong, in_npixels);
+
+  for (i=0; i<in_npixels; i++)
+    {
+      gulong pixel = in_pixels[i];
+      
+      if (private->info[pixel].ref_count)
+       {
+         private->info[pixel].ref_count--;
+
+         if (private->info[pixel].ref_count == 0)
+           {
+             pixels[npixels++] = pixel;
+             if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
+               g_hash_table_remove (private->hash, &colormap->colors[pixel]);
+             private->info[pixel].flags = 0;
+           }
+       }
+    }
+
+  if (npixels)
+    XFreeColors (private->xdisplay, private->xcolormap,
+                pixels, npixels, planes);
+  g_free (pixels);
+}
+
+/* This is almost identical to gdk_colors_free.
+ * Keep them in sync!
+ */
+void
+gdk_colormap_free_colors (GdkColormap *colormap,
+                         GdkColor    *colors,
+                         gint         ncolors)
+{
+  GdkColormapPrivate *private;
+  gulong *pixels;
+  gint npixels = 0;
+  gint i;
+
+  g_return_if_fail (colormap != NULL);
+  g_return_if_fail (colors != NULL);
+
+  private = (GdkColormapPrivate*) colormap;
+
+  if ((private->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
+      (private->visual->type != GDK_VISUAL_GRAYSCALE))
+    return;
+
+  pixels = g_new (gulong, ncolors);
+
+  for (i=0; i<ncolors; i++)
+    {
+      gulong pixel = colors[i].pixel;
+      
+      if (private->info[pixel].ref_count)
+       {
+         private->info[pixel].ref_count--;
+
+         if (private->info[pixel].ref_count == 0)
+           {
+             pixels[npixels++] = pixel;
+             if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
+               g_hash_table_remove (private->hash, &colormap->colors[pixel]);
+             private->info[pixel].flags = 0;
+           }
+       }
+    }
+
+  if (npixels)
+    XFreeColors (private->xdisplay, private->xcolormap,
+                pixels, npixels, 0);
+
+  g_free (pixels);
+}
+
+/********************
+ * Color allocation *
+ ********************/
+
+/* Try to allocate a single color using XAllocColor. If it succeeds,
+ * cache the result in our colormap, and store in ret.
+ */
+static gboolean 
+gdk_colormap_alloc1 (GdkColormap *colormap,
+                    GdkColor    *color,
+                    GdkColor    *ret)
 {
   GdkColormapPrivate *private;
-  GdkVisual *visual;
   XColor xcolor;
-  gchar *available = NULL;
-  gboolean return_val;
-  gint i, index;
 
-  g_return_val_if_fail (colormap != NULL, FALSE);
-  g_return_val_if_fail (color != NULL, FALSE);
+  private = (GdkColormapPrivate*) colormap;
 
   xcolor.red = color->red;
   xcolor.green = color->green;
@@ -513,113 +682,410 @@ gdk_color_alloc (GdkColormap *colormap,
   xcolor.pixel = color->pixel;
   xcolor.flags = DoRed | DoGreen | DoBlue;
 
-  return_val = FALSE;
+  if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
+    {
+      ret->pixel = xcolor.pixel;
+      ret->red = xcolor.red;
+      ret->green = xcolor.green;
+      ret->blue = xcolor.blue;
+      
+      if (ret->pixel < colormap->size)
+       {
+         if (private->info[ret->pixel].ref_count) /* got a duplicate */
+           {
+             XFreeColors (private->xdisplay, private->xcolormap,
+                          &ret->pixel, 1, 0);
+           }
+         else
+           {
+             colormap->colors[ret->pixel] = *color;
+             colormap->colors[ret->pixel].pixel = ret->pixel;
+             private->info[ret->pixel].ref_count = 1;
+
+             g_hash_table_insert (private->hash,
+                                  &colormap->colors[ret->pixel],
+                                  &colormap->colors[ret->pixel]);
+           }
+       }
+      return TRUE;
+    }
+  else
+    {
+      return FALSE;
+    }
+}
+
+static gint
+gdk_colormap_alloc_colors_writeable (GdkColormap *colormap,
+                                    GdkColor    *colors,
+                                    gint         ncolors,
+                                    gboolean     writeable,
+                                    gboolean     best_match,
+                                    gboolean    *success)
+{
+  GdkColormapPrivate *private;
+  gulong *pixels;
+  Status status;
+  gint i, index;
+
   private = (GdkColormapPrivate*) colormap;
 
-  switch (private->visual->type)
+  if (private->private_val)
     {
-    case GDK_VISUAL_GRAYSCALE:
-    case GDK_VISUAL_PSEUDO_COLOR:
-      if (private->private_val)
+      index = 0;
+      for (i=0; i<ncolors; i++)
        {
-         if (private->next_color >= colormap->size)
+         while ((index < colormap->size) && (private->info[index].ref_count != 0))
+           index++;
+         
+         if (index < colormap->size)
            {
-             available = g_new (gchar, colormap->size);
-             for (i = 0; i < colormap->size; i++)
-               available[i] = TRUE;
-
-             index = gdk_colormap_match_color (colormap, color, available);
-             if (index != -1)
-               {
-                 available[index] = FALSE;
-                 *color = colormap->colors[index];
-                 return_val = TRUE;
-               }
-             else
-               {
-                 return_val = FALSE;
-               }
+             colors[i].pixel = index;
+             success[i] = TRUE;
+             private->info[index].ref_count++;
+             private->info[i].flags |= GDK_COLOR_WRITEABLE;
            }
          else
+           break;
+       }
+      return i;
+    }
+  else
+    {
+      pixels = g_new (gulong, ncolors);
+      /* Allocation of a writeable color cells */
+      
+      status =  XAllocColorCells (private->xdisplay, private->xcolormap,
+                                 FALSE, NULL, 0, pixels, ncolors);
+      if (status)
+       {
+         for (i=0; i<ncolors; i++)
            {
-             xcolor.pixel = colormap->size - 1 -private->next_color;
-             color->pixel = xcolor.pixel;
-             private->next_color += 1;
+             colors[i].pixel = pixels[i];
+             private->info[pixels[i]].ref_count++;
+             private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE;
+           }
+       }
+      
+      g_free (pixels);
 
-             XStoreColor (private->xdisplay, private->xcolormap, &xcolor);
-             return_val = TRUE;
+      return status ? ncolors : 0; 
+    }
+}
+
+static gint
+gdk_colormap_alloc_colors_private (GdkColormap *colormap,
+                                  GdkColor    *colors,
+                                  gint         ncolors,
+                                  gboolean     writeable,
+                                  gboolean     best_match,
+                                  gboolean    *success)
+{
+  GdkColormapPrivate *private;
+  gint i, index;
+  XColor *store = g_new (XColor, ncolors);
+  gint nstore = 0;
+  gint nremaining = 0;
+  
+  private = (GdkColormapPrivate*) colormap;
+  index = -1;
+
+  /* First, store the colors we have room for */
+
+  index = 0;
+  for (i=0; i<ncolors; i++)
+    {
+      if (!success[i])
+       {
+         while ((index < colormap->size) && (private->info[index].ref_count != 0))
+           index++;
+
+         if (index < colormap->size)
+           {
+             store[nstore].red = colors[i].red;
+             store[nstore].blue = colors[i].blue;
+             store[nstore].green = colors[i].green;
+             store[nstore].pixel = index;
+             nstore++;
+
+             success[i] = TRUE;
+
+             colors[i].pixel = index;
+             private->info[index].ref_count++;
            }
+         else
+           nremaining++;
        }
-      else
+    }
+  
+  XStoreColors (private->xdisplay, private->xcolormap, store, nstore);
+  g_free (store);
+
+  if (nremaining > 0 && best_match)
+    {
+      /* Get best matches for remaining colors */
+
+      gchar *available = g_new (gchar, colormap->size);
+      for (i = 0; i < colormap->size; i++)
+       available[i] = TRUE;
+
+      for (i=0; i<ncolors; i++)
        {
-         while (1)
+         if (!success[i])
            {
-             if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
+             index = gdk_colormap_match_color (colormap, 
+                                               &colors[i], 
+                                               available);
+             if (index != -1)
                {
-                 color->pixel = xcolor.pixel;
-                 color->red = xcolor.red;
-                 color->green = xcolor.green;
-                 color->blue = xcolor.blue;
-
-                 if (color->pixel < colormap->size)
-                   colormap->colors[color->pixel] = *color;
+                 colors[i] = colormap->colors[index];
+                 private->info[index].ref_count++;
 
-                 return_val = TRUE;
-                 break;
+                 success[i] = TRUE;
+                 nremaining--;
                }
-             else
-               {
-                 if (available == NULL)
-                   {
-                     available = g_new (gchar, colormap->size);
-                     for (i = 0; i < colormap->size; i++)
-                       available[i] = TRUE;
-                   }
+           }
+       }
+      g_free (available);
+    }
+
+  return (ncolors - nremaining);
+}
 
-                 index = gdk_colormap_match_color (colormap, color, available);
+static gint
+gdk_colormap_alloc_colors_shared (GdkColormap *colormap,
+                                 GdkColor    *colors,
+                                 gint         ncolors,
+                                 gboolean     writeable,
+                                 gboolean     best_match,
+                                 gboolean    *success)
+{
+  GdkColormapPrivate *private;
+  gint i, index;
+  gint nremaining = 0;
+  gint nfailed = 0;
+
+  private = (GdkColormapPrivate*) colormap;
+  index = -1;
+
+  for (i=0; i<ncolors; i++)
+    {
+      if (!success[i])
+       {
+         if (gdk_colormap_alloc1 (colormap, &colors[i], &colors[i]))
+           success[i] = TRUE;
+         else
+           nremaining++;
+       }
+    }
+
+
+  if (nremaining > 0 && best_match)
+    {
+      gchar *available = g_new (gchar, colormap->size);
+      for (i = 0; i < colormap->size; i++)
+       available[i] = ((private->info[i].ref_count == 0) ||
+                       !(private->info[i].flags && GDK_COLOR_WRITEABLE));
+      gdk_colormap_sync (colormap, FALSE);
+      
+      while (nremaining > 0)
+       {
+         for (i=0; i<ncolors; i++)
+           {
+             if (!success[i])
+               {
+                 index = gdk_colormap_match_color (colormap, &colors[i], available);
                  if (index != -1)
                    {
-                     available[index] = FALSE;
-                     xcolor.red = colormap->colors[index].red;
-                     xcolor.green = colormap->colors[index].green;
-                     xcolor.blue = colormap->colors[index].blue;
+                     if (private->info[index].ref_count)
+                       {
+                         private->info[index].ref_count++;
+                         colors[i] = colormap->colors[index];
+                         success[i] = TRUE;
+                         nremaining--;
+                       }
+                     else
+                       {
+                         if (gdk_colormap_alloc1 (colormap, 
+                                                  &colormap->colors[index],
+                                                  &colors[i]))
+                           {
+                             success[i] = TRUE;
+                             nremaining--;
+                             break;
+                           }
+                         else
+                           {
+                             available[index] = FALSE;
+                           }
+                       }
                    }
                  else
                    {
-                     return_val = FALSE;
-                     break;
+                     nfailed++;
+                     nremaining--;
+                     success[i] = 2; /* flag as permanent failure */
                    }
                }
            }
        }
+      g_free (available);
+    }
+
+  /* Change back the values we flagged as permanent failures */
+  if (nfailed > 0)
+    {
+      for (i=0; i<ncolors; i++)
+       if (success[i] == 2)
+         success[i] = FALSE;
+      nremaining = nfailed;
+    }
+  
+  return (ncolors - nremaining);
+}
+
+static gint
+gdk_colormap_alloc_colors_pseudocolor (GdkColormap *colormap,
+                                      GdkColor    *colors,
+                                      gint         ncolors,
+                                      gboolean     writeable,
+                                      gboolean     best_match,
+                                      gboolean    *success)
+{
+  GdkColormapPrivate *private;
+  GdkColor *lookup_color;
+  gint i;
+  gint nremaining = 0;
+
+  private = (GdkColormapPrivate*) colormap;
+
+  /* Check for an exact match among previously allocated colors */
+
+  for (i=0; i<ncolors; i++)
+    {
+      if (!success[i])
+       {
+         lookup_color = g_hash_table_lookup (private->hash, &colors[i]);
+         if (lookup_color)
+           {
+             private->info[lookup_color->pixel].ref_count++;
+             colors[i].pixel = lookup_color->pixel;
+             success[i] = TRUE;
+           }
+         else
+           nremaining++;
+       }
+    }
+
+  /* If that failed, we try to allocate a new color, or approxmiate
+   * with what we can get if best_match is TRUE.
+   */
+  if (nremaining > 0)
+    {
+      if (private->private_val)
+       return gdk_colormap_alloc_colors_private (colormap, colors, ncolors, writeable, best_match, success);
+      else
+       return gdk_colormap_alloc_colors_shared (colormap, colors, ncolors, writeable, best_match, success);
+    }
+  else
+    return 0;
+}
+
+gint
+gdk_colormap_alloc_colors (GdkColormap *colormap,
+                          GdkColor    *colors,
+                          gint         ncolors,
+                          gboolean     writeable,
+                          gboolean     best_match,
+                          gboolean    *success)
+{
+  GdkColormapPrivate *private;
+  GdkVisual *visual;
+  gint i;
+  gint nremaining = 0;
+  XColor xcolor;
+
+  g_return_val_if_fail (colormap != NULL, FALSE);
+  g_return_val_if_fail (colors != NULL, FALSE);
+
+  private = (GdkColormapPrivate*) colormap;
+
+  for (i=0; i<ncolors; i++)
+    {
+      success[i] = FALSE;
+    }
+
+  switch (private->visual->type)
+    {
+    case GDK_VISUAL_PSEUDO_COLOR:
+    case GDK_VISUAL_GRAYSCALE:
+      if (writeable)
+       return gdk_colormap_alloc_colors_writeable (colormap, colors, ncolors,
+                                                   writeable, best_match, success);
+      else
+       return gdk_colormap_alloc_colors_pseudocolor (colormap, colors, ncolors,
+                                                   writeable, best_match, success);
       break;
 
     case GDK_VISUAL_DIRECT_COLOR:
+    case GDK_VISUAL_TRUE_COLOR:
       visual = private->visual;
-      xcolor.pixel = (((xcolor.red >> (16 - visual->red_prec)) << visual->red_shift) +
-                     ((xcolor.green >> (16 - visual->green_prec)) << visual->green_shift) +
-                     ((xcolor.blue >> (16 - visual->blue_prec)) << visual->blue_shift));
-      color->pixel = xcolor.pixel;
-      return_val = TRUE;
+
+      for (i=0; i<ncolors; i++)
+       {
+         colors[i].pixel = (((colors[i].red >> (16 - visual->red_prec)) << visual->red_shift) +
+                            ((colors[i].green >> (16 - visual->green_prec)) << visual->green_shift) +
+                            ((colors[i].blue >> (16 - visual->blue_prec)) << visual->blue_shift));
+         success[i] = TRUE;
+       }
       break;
 
     case GDK_VISUAL_STATIC_GRAY:
     case GDK_VISUAL_STATIC_COLOR:
-    case GDK_VISUAL_TRUE_COLOR:
-      if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
+      for (i=0; i<ncolors; i++)
        {
-         color->pixel = xcolor.pixel;
-         return_val = TRUE;
+         xcolor.red = colors[i].red;
+         xcolor.green = colors[i].green;
+         xcolor.blue = colors[i].blue;
+         xcolor.pixel = colors[i].pixel;
+         xcolor.flags = DoRed | DoGreen | DoBlue;
+
+         if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
+           {
+             colors[i].pixel = xcolor.pixel;
+             success[i] = TRUE;
+           }
+         else
+           nremaining++;
        }
-      else
-       return_val = FALSE;
       break;
     }
+  return nremaining;
+}
 
-  if (available)
-    g_free (available);
-  
-  return return_val;
+gboolean
+gdk_colormap_alloc_color (GdkColormap *colormap,
+                         GdkColor    *color,
+                         gboolean     writeable,
+                         gboolean     best_match)
+{
+  gboolean success;
+
+  gdk_colormap_alloc_colors (colormap, color, 1, writeable, best_match,
+                            &success);
+
+  return success;
+}
+
+gboolean
+gdk_color_alloc (GdkColormap *colormap,
+                GdkColor    *color)
+{
+  gboolean success;
+
+  gdk_colormap_alloc_colors (colormap, color, 1, FALSE, TRUE, &success);
+
+  return success;
 }
 
 gboolean
@@ -644,9 +1110,18 @@ gdk_color_change (GdkColormap *colormap,
   return TRUE;
 }
 
+guint
+gdk_color_hash (const GdkColor *colora)
+{
+  return ((colora->red) +
+         (colora->green << 11) +
+         (colora->blue << 22) +
+         (colora->blue >> 6));
+}
+
 gint
-gdk_color_equal (GdkColor *colora,
-                GdkColor *colorb)
+gdk_color_equal (const GdkColor *colora,
+                const GdkColor *colorb)
 {
   g_return_val_if_fail (colora != NULL, FALSE);
   g_return_val_if_fail (colorb != NULL, FALSE);
@@ -656,6 +1131,9 @@ gdk_color_equal (GdkColor *colora,
          (colora->blue == colorb->blue));
 }
 
+/* XXX: Do not use this function until it is fixed. An X Colormap
+ *      is useless unless we also have the visual.
+ */
 GdkColormap*
 gdkx_colormap_get (Colormap xcolormap)
 {
@@ -676,7 +1154,6 @@ gdkx_colormap_get (Colormap xcolormap)
   private->xcolormap = xcolormap;
   private->visual = NULL;
   private->private_val = TRUE;
-  private->next_color = 0;
 
   /* To do the following safely, we would have to have some way of finding
    * out what the size or visual of the given colormap is. It seems