1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gdkprivate.h"
26 static gint gdk_colormap_match_color (GdkColormap *cmap,
28 const gchar *available);
29 static void gdk_colormap_add (GdkColormap *cmap);
30 static void gdk_colormap_remove (GdkColormap *cmap);
31 static guint gdk_colormap_hash (Colormap *cmap);
32 static gint gdk_colormap_cmp (Colormap *a,
34 static void gdk_colormap_real_destroy (GdkColormap *colormap);
36 static GHashTable *colormap_hash = NULL;
40 gdk_colormap_new (GdkVisual *visual,
43 GdkColormap *colormap;
44 GdkColormapPrivate *private;
49 g_return_val_if_fail (visual != NULL, NULL);
51 private = g_new (GdkColormapPrivate, 1);
52 colormap = (GdkColormap*) private;
54 private->xdisplay = gdk_display;
55 private->visual = visual;
56 private->ref_count = 1;
59 private->last_sync_time = 0;
62 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
64 colormap->size = visual->colormap_size;
65 colormap->colors = g_new (GdkColor, colormap->size);
69 case GDK_VISUAL_GRAYSCALE:
70 case GDK_VISUAL_PSEUDO_COLOR:
71 private->info = g_new0 (GdkColorInfo, colormap->size);
72 colormap->colors = g_new (GdkColor, colormap->size);
74 private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
75 (GCompareFunc) gdk_color_equal);
77 private->private_val = private_cmap;
78 private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
79 xvisual, (private_cmap) ? (AllocAll) : (AllocNone));
83 XColor *default_colors;
85 default_colors = g_new (XColor, colormap->size);
87 for (i = 0; i < colormap->size; i++)
88 default_colors[i].pixel = i;
90 XQueryColors (private->xdisplay,
91 DefaultColormap (private->xdisplay, gdk_screen),
92 default_colors, colormap->size);
94 for (i = 0; i < colormap->size; i++)
96 colormap->colors[i].pixel = default_colors[i].pixel;
97 colormap->colors[i].red = default_colors[i].red;
98 colormap->colors[i].green = default_colors[i].green;
99 colormap->colors[i].blue = default_colors[i].blue;
102 gdk_colormap_change (colormap, colormap->size);
104 g_free (default_colors);
108 case GDK_VISUAL_DIRECT_COLOR:
109 private->private_val = TRUE;
110 private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
113 size = 1 << visual->red_prec;
114 for (i = 0; i < size; i++)
115 colormap->colors[i].red = i * 65535 / (size - 1);
117 size = 1 << visual->green_prec;
118 for (i = 0; i < size; i++)
119 colormap->colors[i].green = i * 65535 / (size - 1);
121 size = 1 << visual->blue_prec;
122 for (i = 0; i < size; i++)
123 colormap->colors[i].blue = i * 65535 / (size - 1);
125 gdk_colormap_change (colormap, colormap->size);
128 case GDK_VISUAL_STATIC_GRAY:
129 case GDK_VISUAL_STATIC_COLOR:
130 case GDK_VISUAL_TRUE_COLOR:
131 private->private_val = FALSE;
132 private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
137 gdk_colormap_add (colormap);
143 gdk_colormap_real_destroy (GdkColormap *colormap)
145 GdkColormapPrivate *private = (GdkColormapPrivate*) colormap;
147 g_return_if_fail (colormap != NULL);
148 g_return_if_fail (private->ref_count == 0);
150 gdk_colormap_remove (colormap);
151 XFreeColormap (private->xdisplay, private->xcolormap);
154 g_hash_table_destroy (private->hash);
156 g_free (private->info);
157 g_free (colormap->colors);
162 gdk_colormap_ref (GdkColormap *cmap)
164 GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
165 g_return_val_if_fail (cmap != NULL, NULL);
167 private->ref_count += 1;
172 gdk_colormap_unref (GdkColormap *cmap)
174 GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
175 g_return_if_fail (cmap != NULL);
177 private->ref_count -= 1;
178 if (private->ref_count == 0)
179 gdk_colormap_real_destroy (cmap);
183 gdk_colormap_get_visual (GdkColormap *colormap)
185 GdkColormapPrivate *private;
187 g_return_val_if_fail (colormap != NULL, NULL);
189 private = (GdkColormapPrivate *)colormap;
191 return private->visual;
194 #define MIN_SYNC_TIME 2
197 gdk_colormap_sync (GdkColormap *colormap,
201 GdkColormapPrivate *private = (GdkColormapPrivate *)colormap;
206 g_return_if_fail (colormap != NULL);
208 current_time = time (NULL);
209 if (!force && ((current_time - private->last_sync_time) < MIN_SYNC_TIME))
212 private->last_sync_time = current_time;
215 xpalette = g_new (XColor, colormap->size);
217 for (i = 0; i < colormap->size; i++)
219 if (private->info[i].ref_count == 0)
221 xpalette[nlookup].pixel = i;
222 xpalette[nlookup].red = 0;
223 xpalette[nlookup].green = 0;
224 xpalette[nlookup].blue = 0;
229 XQueryColors (gdk_display, private->xcolormap, xpalette, nlookup);
231 for (i = 0; i < nlookup; i++)
233 gulong pixel = xpalette[i].pixel;
234 colormap->colors[pixel].pixel = pixel;
235 colormap->colors[pixel].red = xpalette[i].red;
236 colormap->colors[pixel].green = xpalette[i].green;
237 colormap->colors[pixel].blue = xpalette[i].blue;
245 gdk_colormap_get_system (void)
247 static GdkColormap *colormap = NULL;
248 GdkColormapPrivate *private;
252 private = g_new (GdkColormapPrivate, 1);
253 colormap = (GdkColormap*) private;
255 private->xdisplay = gdk_display;
256 private->xcolormap = DefaultColormap (gdk_display, gdk_screen);
257 private->visual = gdk_visual_get_system ();
258 private->private_val = FALSE;
259 private->ref_count = 1;
261 private->hash = NULL;
262 private->last_sync_time = 0;
263 private->info = NULL;
265 colormap->colors = NULL;
266 colormap->size = private->visual->colormap_size;
268 if ((private->visual->type == GDK_VISUAL_GRAYSCALE) ||
269 (private->visual->type == GDK_VISUAL_PSEUDO_COLOR))
271 private->info = g_new0 (GdkColorInfo, colormap->size);
272 colormap->colors = g_new (GdkColor, colormap->size);
274 private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
275 (GCompareFunc) gdk_color_equal);
277 gdk_colormap_sync (colormap, TRUE);
280 gdk_colormap_add (colormap);
287 gdk_colormap_get_system_size (void)
289 return DisplayCells (gdk_display, gdk_screen);
293 gdk_colormap_change (GdkColormap *colormap,
296 GdkColormapPrivate *private;
304 g_return_if_fail (colormap != NULL);
306 palette = g_new (XColor, ncolors);
308 private = (GdkColormapPrivate*) colormap;
309 switch (private->visual->type)
311 case GDK_VISUAL_GRAYSCALE:
312 case GDK_VISUAL_PSEUDO_COLOR:
313 for (i = 0; i < ncolors; i++)
315 palette[i].pixel = colormap->colors[i].pixel;
316 palette[i].red = colormap->colors[i].red;
317 palette[i].green = colormap->colors[i].green;
318 palette[i].blue = colormap->colors[i].blue;
319 palette[i].flags = DoRed | DoGreen | DoBlue;
322 XStoreColors (private->xdisplay, private->xcolormap, palette, ncolors);
325 case GDK_VISUAL_DIRECT_COLOR:
326 visual = private->visual;
328 shift = visual->red_shift;
329 max_colors = 1 << visual->red_prec;
330 size = (ncolors < max_colors) ? (ncolors) : (max_colors);
332 for (i = 0; i < size; i++)
334 palette[i].pixel = i << shift;
335 palette[i].red = colormap->colors[i].red;
336 palette[i].flags = DoRed;
339 XStoreColors (private->xdisplay, private->xcolormap, palette, size);
341 shift = visual->green_shift;
342 max_colors = 1 << visual->green_prec;
343 size = (ncolors < max_colors) ? (ncolors) : (max_colors);
345 for (i = 0; i < size; i++)
347 palette[i].pixel = i << shift;
348 palette[i].green = colormap->colors[i].green;
349 palette[i].flags = DoGreen;
352 XStoreColors (private->xdisplay, private->xcolormap, palette, size);
354 shift = visual->blue_shift;
355 max_colors = 1 << visual->blue_prec;
356 size = (ncolors < max_colors) ? (ncolors) : (max_colors);
358 for (i = 0; i < size; i++)
360 palette[i].pixel = i << shift;
361 palette[i].blue = colormap->colors[i].blue;
362 palette[i].flags = DoBlue;
365 XStoreColors (private->xdisplay, private->xcolormap, palette, size);
376 gdk_colors_store (GdkColormap *colormap,
382 for (i = 0; i < ncolors; i++)
384 colormap->colors[i].pixel = colors[i].pixel;
385 colormap->colors[i].red = colors[i].red;
386 colormap->colors[i].green = colors[i].green;
387 colormap->colors[i].blue = colors[i].blue;
390 gdk_colormap_change (colormap, ncolors);
394 gdk_colors_alloc (GdkColormap *colormap,
401 GdkColormapPrivate *private;
405 g_return_val_if_fail (colormap != NULL, 0);
407 private = (GdkColormapPrivate*) colormap;
409 return_val = XAllocColorCells (private->xdisplay, private->xcolormap,
410 contiguous, planes, nplanes, pixels, npixels);
414 for (i=0; i<npixels; i++)
416 private->info[pixels[i]].ref_count++;
417 private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE;
424 /* This is almost identical to gdk_colormap_free_colors.
428 gdk_colors_free (GdkColormap *colormap,
433 GdkColormapPrivate *private;
438 g_return_if_fail (colormap != NULL);
439 g_return_if_fail (in_pixels != NULL);
441 private = (GdkColormapPrivate*) colormap;
443 if ((private->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
444 (private->visual->type != GDK_VISUAL_GRAYSCALE))
447 pixels = g_new (gulong, in_npixels);
449 for (i=0; i<in_npixels; i++)
451 gulong pixel = in_pixels[i];
453 if (private->info[pixel].ref_count)
455 private->info[pixel].ref_count--;
457 if (private->info[pixel].ref_count == 0)
459 pixels[npixels++] = pixel;
460 if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
461 g_hash_table_remove (private->hash, &colormap->colors[in_pixels[i]]);
462 private->info[pixel].flags = 0;
468 XFreeColors (private->xdisplay, private->xcolormap,
469 pixels, npixels, planes);
474 *--------------------------------------------------------------
477 * Copy a color structure into new storage.
480 * "color" is the color struct to copy.
483 * A new color structure. Free it with gdk_color_free.
485 *--------------------------------------------------------------
488 static GMemChunk *color_chunk;
491 gdk_color_copy (GdkColor *color)
495 g_return_val_if_fail (color != NULL, NULL);
497 if (color_chunk == NULL)
498 color_chunk = g_mem_chunk_new ("colors",
503 new_color = g_chunk_new (GdkColor, color_chunk);
509 *--------------------------------------------------------------
512 * Free a color structure obtained from gdk_color_copy. Do not use
513 * with other color structures.
516 * "color" is the color struct to free.
518 *-------------------------------------------------------------- */
521 gdk_color_free (GdkColor *color)
523 g_assert (color_chunk != NULL);
524 g_return_if_fail (color != NULL);
526 g_mem_chunk_free (color_chunk, color);
530 gdk_color_white (GdkColormap *colormap,
535 g_return_val_if_fail (colormap != NULL, FALSE);
539 color->pixel = WhitePixel (gdk_display, gdk_screen);
541 color->green = 65535;
544 return_val = gdk_color_alloc (colormap, color);
553 gdk_color_black (GdkColormap *colormap,
558 g_return_val_if_fail (colormap != NULL, FALSE);
562 color->pixel = BlackPixel (gdk_display, gdk_screen);
567 return_val = gdk_color_alloc (colormap, color);
576 gdk_color_parse (const gchar *spec,
583 g_return_val_if_fail (spec != NULL, FALSE);
584 g_return_val_if_fail (color != NULL, FALSE);
586 xcolormap = DefaultColormap (gdk_display, gdk_screen);
588 if (XParseColor (gdk_display, xcolormap, spec, &xcolor))
591 color->red = xcolor.red;
592 color->green = xcolor.green;
593 color->blue = xcolor.blue;
601 /********************
603 ********************/
605 /* Try to allocate a single color using XAllocColor. If it succeeds,
606 * cache the result in our colormap, and store in ret.
609 gdk_colormap_alloc1 (GdkColormap *colormap,
613 GdkColormapPrivate *private;
616 private = (GdkColormapPrivate*) colormap;
618 xcolor.red = color->red;
619 xcolor.green = color->green;
620 xcolor.blue = color->blue;
621 xcolor.pixel = color->pixel;
622 xcolor.flags = DoRed | DoGreen | DoBlue;
624 if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
626 ret->pixel = xcolor.pixel;
627 ret->red = xcolor.red;
628 ret->green = xcolor.green;
629 ret->blue = xcolor.blue;
631 if (ret->pixel < colormap->size)
633 if (private->info[ret->pixel].ref_count) /* got a duplicate */
635 XFreeColors (private->xdisplay, private->xcolormap,
640 colormap->colors[ret->pixel] = *color;
641 private->info[ret->pixel].ref_count = 1;
643 g_hash_table_insert (private->hash,
644 &colormap->colors[ret->pixel],
645 &colormap->colors[ret->pixel]);
657 gdk_colormap_alloc_colors_writeable (GdkColormap *colormap,
664 GdkColormapPrivate *private;
669 private = (GdkColormapPrivate*) colormap;
671 if (private->private_val)
674 for (i=0; i<ncolors; i++)
676 while ((index < colormap->size) && (private->info[index].ref_count != 0))
679 if (index < colormap->size)
681 colors[i].pixel = index;
683 private->info[index].ref_count++;
684 private->info[i].flags |= GDK_COLOR_WRITEABLE;
693 pixels = g_new (gulong, ncolors);
694 /* Allocation of a writeable color cells */
696 status = XAllocColorCells (private->xdisplay, private->xcolormap,
697 FALSE, NULL, 0, pixels, ncolors);
700 for (i=0; i<ncolors; i++)
702 colors[i].pixel = pixels[i];
703 private->info[pixels[i]].ref_count++;
704 private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE;
710 return status ? ncolors : 0;
715 gdk_colormap_alloc_colors_private (GdkColormap *colormap,
722 GdkColormapPrivate *private;
724 XColor *store = g_new (XColor, ncolors);
728 private = (GdkColormapPrivate*) colormap;
731 /* First, store the colors we have room for */
734 for (i=0; i<ncolors; i++)
738 while ((index < colormap->size) && (private->info[index].ref_count != 0))
741 if (index < colormap->size)
743 store[nstore].red = colors[i].red;
744 store[nstore].blue = colors[i].blue;
745 store[nstore].green = colors[i].green;
746 store[nstore].pixel = index;
751 colors[i].pixel = index;
752 private->info[index].ref_count++;
759 XStoreColors (private->xdisplay, private->xcolormap, store, nstore);
762 if (nremaining > 0 && best_match)
764 /* Get best matches for remaining colors */
766 gchar *available = g_new (gchar, colormap->size);
767 for (i = 0; i < colormap->size; i++)
770 for (i=0; i<ncolors; i++)
774 index = gdk_colormap_match_color (colormap,
779 colors[i] = colormap->colors[index];
780 private->info[index].ref_count++;
790 return (ncolors - nremaining);
794 gdk_colormap_alloc_colors_shared (GdkColormap *colormap,
801 GdkColormapPrivate *private;
806 private = (GdkColormapPrivate*) colormap;
809 for (i=0; i<ncolors; i++)
813 if (gdk_colormap_alloc1 (colormap, &colors[i], &colors[i]))
821 if (nremaining > 0 && best_match)
823 gchar *available = g_new (gchar, colormap->size);
824 for (i = 0; i < colormap->size; i++)
825 available[i] = ((private->info[i].ref_count == 0) ||
826 !(private->info[i].flags && GDK_COLOR_WRITEABLE));
827 gdk_colormap_sync (colormap, FALSE);
829 while (nremaining > 0)
831 for (i=0; i<ncolors; i++)
835 index = gdk_colormap_match_color (colormap, &colors[i], available);
838 if (private->info[index].ref_count)
840 private->info[index].ref_count++;
841 colors[i] = colormap->colors[index];
847 if (gdk_colormap_alloc1 (colormap,
848 &colormap->colors[index],
857 available[index] = FALSE;
865 success[i] = 2; /* flag as permanent failure */
873 /* Change back the values we flagged as permanent failures */
876 for (i=0; i<ncolors; i++)
879 nremaining = nfailed;
882 return (ncolors - nremaining);
886 gdk_colormap_alloc_colors_pseudocolor (GdkColormap *colormap,
893 GdkColormapPrivate *private;
894 GdkColor *lookup_color;
898 private = (GdkColormapPrivate*) colormap;
900 /* Check for an exact match among previously allocated colors */
902 for (i=0; i<ncolors; i++)
906 lookup_color = g_hash_table_lookup (private->hash, &colors[i]);
909 private->info[lookup_color->pixel].ref_count++;
910 colors[i].pixel = lookup_color->pixel;
918 /* If that failed, we try to allocate a new color, or approxmiate
919 * with what we can get if best_match is TRUE.
923 if (private->private_val)
924 return gdk_colormap_alloc_colors_private (colormap, colors, ncolors, writeable, best_match, success);
926 return gdk_colormap_alloc_colors_shared (colormap, colors, ncolors, writeable, best_match, success);
933 gdk_colormap_alloc_colors (GdkColormap *colormap,
940 GdkColormapPrivate *private;
946 g_return_val_if_fail (colormap != NULL, FALSE);
947 g_return_val_if_fail (colors != NULL, FALSE);
949 private = (GdkColormapPrivate*) colormap;
951 for (i=0; i<ncolors; i++)
956 switch (private->visual->type)
958 case GDK_VISUAL_PSEUDO_COLOR:
959 case GDK_VISUAL_GRAYSCALE:
961 return gdk_colormap_alloc_colors_writeable (colormap, colors, ncolors,
962 writeable, best_match, success);
964 return gdk_colormap_alloc_colors_pseudocolor (colormap, colors, ncolors,
965 writeable, best_match, success);
968 case GDK_VISUAL_DIRECT_COLOR:
969 case GDK_VISUAL_TRUE_COLOR:
970 visual = private->visual;
972 for (i=0; i<ncolors; i++)
974 colors[i].pixel = (((colors[i].red >> (16 - visual->red_prec)) << visual->red_shift) +
975 ((colors[i].green >> (16 - visual->green_prec)) << visual->green_shift) +
976 ((colors[i].blue >> (16 - visual->blue_prec)) << visual->blue_shift));
981 case GDK_VISUAL_STATIC_GRAY:
982 case GDK_VISUAL_STATIC_COLOR:
983 for (i=0; i<ncolors; i++)
985 xcolor.red = colors[i].red;
986 xcolor.green = colors[i].green;
987 xcolor.blue = colors[i].blue;
988 xcolor.pixel = colors[i].pixel;
989 xcolor.flags = DoRed | DoGreen | DoBlue;
991 if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
993 colors[i].pixel = xcolor.pixel;
1005 gdk_colormap_alloc_color (GdkColormap *colormap,
1008 gboolean best_match)
1012 gdk_colormap_alloc_colors (colormap, color, 1, writeable, best_match,
1018 /* This is almost identical to gdk_colors_free.
1019 * Keep them in sync!
1022 gdk_colormap_free_colors (GdkColormap *colormap,
1026 GdkColormapPrivate *private;
1031 g_return_if_fail (colormap != NULL);
1032 g_return_if_fail (colors != NULL);
1034 private = (GdkColormapPrivate*) colormap;
1036 if ((private->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
1037 (private->visual->type != GDK_VISUAL_GRAYSCALE))
1040 pixels = g_new (gulong, ncolors);
1042 for (i=0; i<ncolors; i++)
1044 gulong pixel = colors[i].pixel;
1046 if (private->info[pixel].ref_count)
1048 private->info[pixel].ref_count--;
1050 if (private->info[pixel].ref_count == 0)
1052 pixels[npixels++] = pixel;
1053 if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
1054 g_hash_table_remove (private->hash, &colors[i]);
1055 private->info[pixel].flags = 0;
1061 XFreeColors (private->xdisplay, private->xcolormap,
1062 pixels, npixels, 0);
1068 gdk_color_alloc (GdkColormap *colormap,
1073 gdk_colormap_alloc_colors (colormap, color, 1, FALSE, TRUE, &success);
1079 gdk_color_change (GdkColormap *colormap,
1082 GdkColormapPrivate *private;
1085 g_return_val_if_fail (colormap != NULL, FALSE);
1086 g_return_val_if_fail (color != NULL, FALSE);
1088 xcolor.pixel = color->pixel;
1089 xcolor.red = color->red;
1090 xcolor.green = color->green;
1091 xcolor.blue = color->blue;
1092 xcolor.flags = DoRed | DoGreen | DoBlue;
1094 private = (GdkColormapPrivate*) colormap;
1095 XStoreColor (private->xdisplay, private->xcolormap, &xcolor);
1101 gdk_color_hash (const GdkColor *colora,
1102 const GdkColor *colorb)
1104 return ((colora->red) +
1105 (colora->green << 11) +
1106 (colora->blue << 22) +
1107 (colora->blue >> 6));
1111 gdk_color_equal (const GdkColor *colora,
1112 const GdkColor *colorb)
1114 g_return_val_if_fail (colora != NULL, FALSE);
1115 g_return_val_if_fail (colorb != NULL, FALSE);
1117 return ((colora->red == colorb->red) &&
1118 (colora->green == colorb->green) &&
1119 (colora->blue == colorb->blue));
1122 /* XXX: Do not use this function until it is fixed. An X Colormap
1123 * is useless unless we also have the visual.
1126 gdkx_colormap_get (Colormap xcolormap)
1128 GdkColormap *colormap;
1129 GdkColormapPrivate *private;
1131 colormap = gdk_colormap_lookup (xcolormap);
1135 if (xcolormap == DefaultColormap (gdk_display, gdk_screen))
1136 return gdk_colormap_get_system ();
1138 private = g_new (GdkColormapPrivate, 1);
1139 colormap = (GdkColormap*) private;
1141 private->xdisplay = gdk_display;
1142 private->xcolormap = xcolormap;
1143 private->visual = NULL;
1144 private->private_val = TRUE;
1146 /* To do the following safely, we would have to have some way of finding
1147 * out what the size or visual of the given colormap is. It seems
1148 * X doesn't allow this
1152 for (i = 0; i < 256; i++)
1154 xpalette[i].pixel = i;
1155 xpalette[i].red = 0;
1156 xpalette[i].green = 0;
1157 xpalette[i].blue = 0;
1160 XQueryColors (gdk_display, private->xcolormap, xpalette, 256);
1162 for (i = 0; i < 256; i++)
1164 colormap->colors[i].pixel = xpalette[i].pixel;
1165 colormap->colors[i].red = xpalette[i].red;
1166 colormap->colors[i].green = xpalette[i].green;
1167 colormap->colors[i].blue = xpalette[i].blue;
1171 colormap->colors = NULL;
1174 gdk_colormap_add (colormap);
1181 gdk_colormap_match_color (GdkColormap *cmap,
1183 const gchar *available)
1187 gint rdiff, gdiff, bdiff;
1190 g_return_val_if_fail (cmap != NULL, 0);
1191 g_return_val_if_fail (color != NULL, 0);
1193 colors = cmap->colors;
1197 for (i = 0; i < cmap->size; i++)
1199 if ((!available) || (available && available[i]))
1201 rdiff = (color->red - colors[i].red);
1202 gdiff = (color->green - colors[i].green);
1203 bdiff = (color->blue - colors[i].blue);
1205 sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
1220 gdk_colormap_lookup (Colormap xcolormap)
1227 cmap = g_hash_table_lookup (colormap_hash, &xcolormap);
1232 gdk_colormap_add (GdkColormap *cmap)
1234 GdkColormapPrivate *private;
1237 colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
1238 (GCompareFunc) gdk_colormap_cmp);
1240 private = (GdkColormapPrivate*) cmap;
1242 g_hash_table_insert (colormap_hash, &private->xcolormap, cmap);
1246 gdk_colormap_remove (GdkColormap *cmap)
1248 GdkColormapPrivate *private;
1251 colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
1252 (GCompareFunc) gdk_colormap_cmp);
1254 private = (GdkColormapPrivate*) cmap;
1256 g_hash_table_remove (colormap_hash, &private->xcolormap);
1260 gdk_colormap_hash (Colormap *cmap)
1266 gdk_colormap_cmp (Colormap *a,