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.
21 * Modified by the GTK+ Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #include "gdkprivate.h"
34 static gint gdk_colormap_match_color (GdkColormap *cmap,
36 const gchar *available);
37 static void gdk_colormap_add (GdkColormap *cmap);
38 static void gdk_colormap_remove (GdkColormap *cmap);
39 static guint gdk_colormap_hash (Colormap *cmap);
40 static gint gdk_colormap_cmp (Colormap *a,
42 static void gdk_colormap_real_destroy (GdkColormap *colormap);
44 static GHashTable *colormap_hash = NULL;
48 gdk_colormap_new (GdkVisual *visual,
51 GdkColormap *colormap;
52 GdkColormapPrivate *private;
57 g_return_val_if_fail (visual != NULL, NULL);
59 private = g_new (GdkColormapPrivate, 1);
60 colormap = (GdkColormap*) private;
62 private->xdisplay = gdk_display;
63 private->visual = visual;
64 private->ref_count = 1;
67 private->last_sync_time = 0;
70 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
72 colormap->size = visual->colormap_size;
73 colormap->colors = g_new (GdkColor, colormap->size);
77 case GDK_VISUAL_GRAYSCALE:
78 case GDK_VISUAL_PSEUDO_COLOR:
79 private->info = g_new0 (GdkColorInfo, colormap->size);
81 private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
82 (GCompareFunc) gdk_color_equal);
84 private->private_val = private_cmap;
85 private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
86 xvisual, (private_cmap) ? (AllocAll) : (AllocNone));
90 XColor *default_colors;
92 default_colors = g_new (XColor, colormap->size);
94 for (i = 0; i < colormap->size; i++)
95 default_colors[i].pixel = i;
97 XQueryColors (private->xdisplay,
98 DefaultColormap (private->xdisplay, gdk_screen),
99 default_colors, colormap->size);
101 for (i = 0; i < colormap->size; i++)
103 colormap->colors[i].pixel = default_colors[i].pixel;
104 colormap->colors[i].red = default_colors[i].red;
105 colormap->colors[i].green = default_colors[i].green;
106 colormap->colors[i].blue = default_colors[i].blue;
109 gdk_colormap_change (colormap, colormap->size);
111 g_free (default_colors);
115 case GDK_VISUAL_DIRECT_COLOR:
116 private->private_val = TRUE;
117 private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
120 size = 1 << visual->red_prec;
121 for (i = 0; i < size; i++)
122 colormap->colors[i].red = i * 65535 / (size - 1);
124 size = 1 << visual->green_prec;
125 for (i = 0; i < size; i++)
126 colormap->colors[i].green = i * 65535 / (size - 1);
128 size = 1 << visual->blue_prec;
129 for (i = 0; i < size; i++)
130 colormap->colors[i].blue = i * 65535 / (size - 1);
132 gdk_colormap_change (colormap, colormap->size);
135 case GDK_VISUAL_STATIC_GRAY:
136 case GDK_VISUAL_STATIC_COLOR:
137 case GDK_VISUAL_TRUE_COLOR:
138 private->private_val = FALSE;
139 private->xcolormap = XCreateColormap (private->xdisplay, gdk_root_window,
144 gdk_colormap_add (colormap);
150 gdk_colormap_real_destroy (GdkColormap *colormap)
152 GdkColormapPrivate *private = (GdkColormapPrivate*) colormap;
154 g_return_if_fail (colormap != NULL);
155 g_return_if_fail (private->ref_count == 0);
157 gdk_colormap_remove (colormap);
158 XFreeColormap (private->xdisplay, private->xcolormap);
161 g_hash_table_destroy (private->hash);
163 g_free (private->info);
164 g_free (colormap->colors);
169 gdk_colormap_ref (GdkColormap *cmap)
171 GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
173 g_return_val_if_fail (cmap != NULL, NULL);
175 private->ref_count += 1;
180 gdk_colormap_unref (GdkColormap *cmap)
182 GdkColormapPrivate *private = (GdkColormapPrivate *)cmap;
184 g_return_if_fail (cmap != NULL);
185 g_return_if_fail (private->ref_count > 0);
187 private->ref_count -= 1;
188 if (private->ref_count == 0)
189 gdk_colormap_real_destroy (cmap);
193 gdk_colormap_get_visual (GdkColormap *colormap)
195 GdkColormapPrivate *private;
197 g_return_val_if_fail (colormap != NULL, NULL);
199 private = (GdkColormapPrivate *)colormap;
201 return private->visual;
204 #define MIN_SYNC_TIME 2
207 gdk_colormap_sync (GdkColormap *colormap,
211 GdkColormapPrivate *private = (GdkColormapPrivate *)colormap;
216 g_return_if_fail (colormap != NULL);
218 current_time = time (NULL);
219 if (!force && ((current_time - private->last_sync_time) < MIN_SYNC_TIME))
222 private->last_sync_time = current_time;
225 xpalette = g_new (XColor, colormap->size);
227 for (i = 0; i < colormap->size; i++)
229 if (private->info[i].ref_count == 0)
231 xpalette[nlookup].pixel = i;
232 xpalette[nlookup].red = 0;
233 xpalette[nlookup].green = 0;
234 xpalette[nlookup].blue = 0;
239 XQueryColors (gdk_display, private->xcolormap, xpalette, nlookup);
241 for (i = 0; i < nlookup; i++)
243 gulong pixel = xpalette[i].pixel;
244 colormap->colors[pixel].pixel = pixel;
245 colormap->colors[pixel].red = xpalette[i].red;
246 colormap->colors[pixel].green = xpalette[i].green;
247 colormap->colors[pixel].blue = xpalette[i].blue;
255 gdk_colormap_get_system (void)
257 static GdkColormap *colormap = NULL;
258 GdkColormapPrivate *private;
262 private = g_new (GdkColormapPrivate, 1);
263 colormap = (GdkColormap*) private;
265 private->xdisplay = gdk_display;
266 private->xcolormap = DefaultColormap (gdk_display, gdk_screen);
267 private->visual = gdk_visual_get_system ();
268 private->private_val = FALSE;
269 private->ref_count = 1;
271 private->hash = NULL;
272 private->last_sync_time = 0;
273 private->info = NULL;
275 colormap->colors = NULL;
276 colormap->size = private->visual->colormap_size;
278 if ((private->visual->type == GDK_VISUAL_GRAYSCALE) ||
279 (private->visual->type == GDK_VISUAL_PSEUDO_COLOR))
281 private->info = g_new0 (GdkColorInfo, colormap->size);
282 colormap->colors = g_new (GdkColor, colormap->size);
284 private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
285 (GCompareFunc) gdk_color_equal);
287 gdk_colormap_sync (colormap, TRUE);
290 gdk_colormap_add (colormap);
297 gdk_colormap_get_system_size (void)
299 return DisplayCells (gdk_display, gdk_screen);
303 gdk_colormap_change (GdkColormap *colormap,
306 GdkColormapPrivate *private;
314 g_return_if_fail (colormap != NULL);
316 palette = g_new (XColor, ncolors);
318 private = (GdkColormapPrivate*) colormap;
319 switch (private->visual->type)
321 case GDK_VISUAL_GRAYSCALE:
322 case GDK_VISUAL_PSEUDO_COLOR:
323 for (i = 0; i < ncolors; i++)
325 palette[i].pixel = colormap->colors[i].pixel;
326 palette[i].red = colormap->colors[i].red;
327 palette[i].green = colormap->colors[i].green;
328 palette[i].blue = colormap->colors[i].blue;
329 palette[i].flags = DoRed | DoGreen | DoBlue;
332 XStoreColors (private->xdisplay, private->xcolormap, palette, ncolors);
335 case GDK_VISUAL_DIRECT_COLOR:
336 visual = private->visual;
338 shift = visual->red_shift;
339 max_colors = 1 << visual->red_prec;
340 size = (ncolors < max_colors) ? (ncolors) : (max_colors);
342 for (i = 0; i < size; i++)
344 palette[i].pixel = i << shift;
345 palette[i].red = colormap->colors[i].red;
346 palette[i].flags = DoRed;
349 XStoreColors (private->xdisplay, private->xcolormap, palette, size);
351 shift = visual->green_shift;
352 max_colors = 1 << visual->green_prec;
353 size = (ncolors < max_colors) ? (ncolors) : (max_colors);
355 for (i = 0; i < size; i++)
357 palette[i].pixel = i << shift;
358 palette[i].green = colormap->colors[i].green;
359 palette[i].flags = DoGreen;
362 XStoreColors (private->xdisplay, private->xcolormap, palette, size);
364 shift = visual->blue_shift;
365 max_colors = 1 << visual->blue_prec;
366 size = (ncolors < max_colors) ? (ncolors) : (max_colors);
368 for (i = 0; i < size; i++)
370 palette[i].pixel = i << shift;
371 palette[i].blue = colormap->colors[i].blue;
372 palette[i].flags = DoBlue;
375 XStoreColors (private->xdisplay, private->xcolormap, palette, size);
386 gdk_colors_store (GdkColormap *colormap,
392 for (i = 0; i < ncolors; i++)
394 colormap->colors[i].pixel = colors[i].pixel;
395 colormap->colors[i].red = colors[i].red;
396 colormap->colors[i].green = colors[i].green;
397 colormap->colors[i].blue = colors[i].blue;
400 gdk_colormap_change (colormap, ncolors);
404 gdk_colors_alloc (GdkColormap *colormap,
411 GdkColormapPrivate *private;
415 g_return_val_if_fail (colormap != NULL, 0);
417 private = (GdkColormapPrivate*) colormap;
419 return_val = XAllocColorCells (private->xdisplay, private->xcolormap,
420 contiguous, planes, nplanes, pixels, npixels);
424 for (i=0; i<npixels; i++)
426 private->info[pixels[i]].ref_count++;
427 private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE;
434 /* This is almost identical to gdk_colormap_free_colors.
438 gdk_colors_free (GdkColormap *colormap,
443 GdkColormapPrivate *private;
448 g_return_if_fail (colormap != NULL);
449 g_return_if_fail (in_pixels != NULL);
451 private = (GdkColormapPrivate*) colormap;
453 if ((private->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
454 (private->visual->type != GDK_VISUAL_GRAYSCALE))
457 pixels = g_new (gulong, in_npixels);
459 for (i=0; i<in_npixels; i++)
461 gulong pixel = in_pixels[i];
463 if (private->info[pixel].ref_count)
465 private->info[pixel].ref_count--;
467 if (private->info[pixel].ref_count == 0)
469 pixels[npixels++] = pixel;
470 if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
471 g_hash_table_remove (private->hash, &colormap->colors[in_pixels[i]]);
472 private->info[pixel].flags = 0;
478 XFreeColors (private->xdisplay, private->xcolormap,
479 pixels, npixels, planes);
484 *--------------------------------------------------------------
487 * Copy a color structure into new storage.
490 * "color" is the color struct to copy.
493 * A new color structure. Free it with gdk_color_free.
495 *--------------------------------------------------------------
498 static GMemChunk *color_chunk;
501 gdk_color_copy (GdkColor *color)
505 g_return_val_if_fail (color != NULL, NULL);
507 if (color_chunk == NULL)
508 color_chunk = g_mem_chunk_new ("colors",
513 new_color = g_chunk_new (GdkColor, color_chunk);
519 *--------------------------------------------------------------
522 * Free a color structure obtained from gdk_color_copy. Do not use
523 * with other color structures.
526 * "color" is the color struct to free.
528 *-------------------------------------------------------------- */
531 gdk_color_free (GdkColor *color)
533 g_assert (color_chunk != NULL);
534 g_return_if_fail (color != NULL);
536 g_mem_chunk_free (color_chunk, color);
540 gdk_color_white (GdkColormap *colormap,
545 g_return_val_if_fail (colormap != NULL, FALSE);
549 color->pixel = WhitePixel (gdk_display, gdk_screen);
551 color->green = 65535;
554 return_val = gdk_color_alloc (colormap, color);
563 gdk_color_black (GdkColormap *colormap,
568 g_return_val_if_fail (colormap != NULL, FALSE);
572 color->pixel = BlackPixel (gdk_display, gdk_screen);
577 return_val = gdk_color_alloc (colormap, color);
586 gdk_color_parse (const gchar *spec,
593 g_return_val_if_fail (spec != NULL, FALSE);
594 g_return_val_if_fail (color != NULL, FALSE);
596 xcolormap = DefaultColormap (gdk_display, gdk_screen);
598 if (XParseColor (gdk_display, xcolormap, spec, &xcolor))
601 color->red = xcolor.red;
602 color->green = xcolor.green;
603 color->blue = xcolor.blue;
611 /********************
613 ********************/
615 /* Try to allocate a single color using XAllocColor. If it succeeds,
616 * cache the result in our colormap, and store in ret.
619 gdk_colormap_alloc1 (GdkColormap *colormap,
623 GdkColormapPrivate *private;
626 private = (GdkColormapPrivate*) colormap;
628 xcolor.red = color->red;
629 xcolor.green = color->green;
630 xcolor.blue = color->blue;
631 xcolor.pixel = color->pixel;
632 xcolor.flags = DoRed | DoGreen | DoBlue;
634 if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
636 ret->pixel = xcolor.pixel;
637 ret->red = xcolor.red;
638 ret->green = xcolor.green;
639 ret->blue = xcolor.blue;
641 if (ret->pixel < colormap->size)
643 if (private->info[ret->pixel].ref_count) /* got a duplicate */
645 XFreeColors (private->xdisplay, private->xcolormap,
650 colormap->colors[ret->pixel] = *color;
651 private->info[ret->pixel].ref_count = 1;
653 g_hash_table_insert (private->hash,
654 &colormap->colors[ret->pixel],
655 &colormap->colors[ret->pixel]);
667 gdk_colormap_alloc_colors_writeable (GdkColormap *colormap,
674 GdkColormapPrivate *private;
679 private = (GdkColormapPrivate*) colormap;
681 if (private->private_val)
684 for (i=0; i<ncolors; i++)
686 while ((index < colormap->size) && (private->info[index].ref_count != 0))
689 if (index < colormap->size)
691 colors[i].pixel = index;
693 private->info[index].ref_count++;
694 private->info[i].flags |= GDK_COLOR_WRITEABLE;
703 pixels = g_new (gulong, ncolors);
704 /* Allocation of a writeable color cells */
706 status = XAllocColorCells (private->xdisplay, private->xcolormap,
707 FALSE, NULL, 0, pixels, ncolors);
710 for (i=0; i<ncolors; i++)
712 colors[i].pixel = pixels[i];
713 private->info[pixels[i]].ref_count++;
714 private->info[pixels[i]].flags |= GDK_COLOR_WRITEABLE;
720 return status ? ncolors : 0;
725 gdk_colormap_alloc_colors_private (GdkColormap *colormap,
732 GdkColormapPrivate *private;
734 XColor *store = g_new (XColor, ncolors);
738 private = (GdkColormapPrivate*) colormap;
741 /* First, store the colors we have room for */
744 for (i=0; i<ncolors; i++)
748 while ((index < colormap->size) && (private->info[index].ref_count != 0))
751 if (index < colormap->size)
753 store[nstore].red = colors[i].red;
754 store[nstore].blue = colors[i].blue;
755 store[nstore].green = colors[i].green;
756 store[nstore].pixel = index;
761 colors[i].pixel = index;
762 private->info[index].ref_count++;
769 XStoreColors (private->xdisplay, private->xcolormap, store, nstore);
772 if (nremaining > 0 && best_match)
774 /* Get best matches for remaining colors */
776 gchar *available = g_new (gchar, colormap->size);
777 for (i = 0; i < colormap->size; i++)
780 for (i=0; i<ncolors; i++)
784 index = gdk_colormap_match_color (colormap,
789 colors[i] = colormap->colors[index];
790 private->info[index].ref_count++;
800 return (ncolors - nremaining);
804 gdk_colormap_alloc_colors_shared (GdkColormap *colormap,
811 GdkColormapPrivate *private;
816 private = (GdkColormapPrivate*) colormap;
819 for (i=0; i<ncolors; i++)
823 if (gdk_colormap_alloc1 (colormap, &colors[i], &colors[i]))
831 if (nremaining > 0 && best_match)
833 gchar *available = g_new (gchar, colormap->size);
834 for (i = 0; i < colormap->size; i++)
835 available[i] = ((private->info[i].ref_count == 0) ||
836 !(private->info[i].flags && GDK_COLOR_WRITEABLE));
837 gdk_colormap_sync (colormap, FALSE);
839 while (nremaining > 0)
841 for (i=0; i<ncolors; i++)
845 index = gdk_colormap_match_color (colormap, &colors[i], available);
848 if (private->info[index].ref_count)
850 private->info[index].ref_count++;
851 colors[i] = colormap->colors[index];
857 if (gdk_colormap_alloc1 (colormap,
858 &colormap->colors[index],
867 available[index] = FALSE;
875 success[i] = 2; /* flag as permanent failure */
883 /* Change back the values we flagged as permanent failures */
886 for (i=0; i<ncolors; i++)
889 nremaining = nfailed;
892 return (ncolors - nremaining);
896 gdk_colormap_alloc_colors_pseudocolor (GdkColormap *colormap,
903 GdkColormapPrivate *private;
904 GdkColor *lookup_color;
908 private = (GdkColormapPrivate*) colormap;
910 /* Check for an exact match among previously allocated colors */
912 for (i=0; i<ncolors; i++)
916 lookup_color = g_hash_table_lookup (private->hash, &colors[i]);
919 private->info[lookup_color->pixel].ref_count++;
920 colors[i].pixel = lookup_color->pixel;
928 /* If that failed, we try to allocate a new color, or approxmiate
929 * with what we can get if best_match is TRUE.
933 if (private->private_val)
934 return gdk_colormap_alloc_colors_private (colormap, colors, ncolors, writeable, best_match, success);
936 return gdk_colormap_alloc_colors_shared (colormap, colors, ncolors, writeable, best_match, success);
943 gdk_colormap_alloc_colors (GdkColormap *colormap,
950 GdkColormapPrivate *private;
956 g_return_val_if_fail (colormap != NULL, FALSE);
957 g_return_val_if_fail (colors != NULL, FALSE);
959 private = (GdkColormapPrivate*) colormap;
961 for (i=0; i<ncolors; i++)
966 switch (private->visual->type)
968 case GDK_VISUAL_PSEUDO_COLOR:
969 case GDK_VISUAL_GRAYSCALE:
971 return gdk_colormap_alloc_colors_writeable (colormap, colors, ncolors,
972 writeable, best_match, success);
974 return gdk_colormap_alloc_colors_pseudocolor (colormap, colors, ncolors,
975 writeable, best_match, success);
978 case GDK_VISUAL_DIRECT_COLOR:
979 case GDK_VISUAL_TRUE_COLOR:
980 visual = private->visual;
982 for (i=0; i<ncolors; i++)
984 colors[i].pixel = (((colors[i].red >> (16 - visual->red_prec)) << visual->red_shift) +
985 ((colors[i].green >> (16 - visual->green_prec)) << visual->green_shift) +
986 ((colors[i].blue >> (16 - visual->blue_prec)) << visual->blue_shift));
991 case GDK_VISUAL_STATIC_GRAY:
992 case GDK_VISUAL_STATIC_COLOR:
993 for (i=0; i<ncolors; i++)
995 xcolor.red = colors[i].red;
996 xcolor.green = colors[i].green;
997 xcolor.blue = colors[i].blue;
998 xcolor.pixel = colors[i].pixel;
999 xcolor.flags = DoRed | DoGreen | DoBlue;
1001 if (XAllocColor (private->xdisplay, private->xcolormap, &xcolor))
1003 colors[i].pixel = xcolor.pixel;
1015 gdk_colormap_alloc_color (GdkColormap *colormap,
1018 gboolean best_match)
1022 gdk_colormap_alloc_colors (colormap, color, 1, writeable, best_match,
1028 /* This is almost identical to gdk_colors_free.
1029 * Keep them in sync!
1032 gdk_colormap_free_colors (GdkColormap *colormap,
1036 GdkColormapPrivate *private;
1041 g_return_if_fail (colormap != NULL);
1042 g_return_if_fail (colors != NULL);
1044 private = (GdkColormapPrivate*) colormap;
1046 if ((private->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
1047 (private->visual->type != GDK_VISUAL_GRAYSCALE))
1050 pixels = g_new (gulong, ncolors);
1052 for (i=0; i<ncolors; i++)
1054 gulong pixel = colors[i].pixel;
1056 if (private->info[pixel].ref_count)
1058 private->info[pixel].ref_count--;
1060 if (private->info[pixel].ref_count == 0)
1062 pixels[npixels++] = pixel;
1063 if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
1064 g_hash_table_remove (private->hash, &colors[i]);
1065 private->info[pixel].flags = 0;
1071 XFreeColors (private->xdisplay, private->xcolormap,
1072 pixels, npixels, 0);
1078 gdk_color_alloc (GdkColormap *colormap,
1083 gdk_colormap_alloc_colors (colormap, color, 1, FALSE, TRUE, &success);
1089 gdk_color_change (GdkColormap *colormap,
1092 GdkColormapPrivate *private;
1095 g_return_val_if_fail (colormap != NULL, FALSE);
1096 g_return_val_if_fail (color != NULL, FALSE);
1098 xcolor.pixel = color->pixel;
1099 xcolor.red = color->red;
1100 xcolor.green = color->green;
1101 xcolor.blue = color->blue;
1102 xcolor.flags = DoRed | DoGreen | DoBlue;
1104 private = (GdkColormapPrivate*) colormap;
1105 XStoreColor (private->xdisplay, private->xcolormap, &xcolor);
1111 gdk_color_hash (const GdkColor *colora)
1113 return ((colora->red) +
1114 (colora->green << 11) +
1115 (colora->blue << 22) +
1116 (colora->blue >> 6));
1120 gdk_color_equal (const GdkColor *colora,
1121 const GdkColor *colorb)
1123 g_return_val_if_fail (colora != NULL, FALSE);
1124 g_return_val_if_fail (colorb != NULL, FALSE);
1126 return ((colora->red == colorb->red) &&
1127 (colora->green == colorb->green) &&
1128 (colora->blue == colorb->blue));
1131 /* XXX: Do not use this function until it is fixed. An X Colormap
1132 * is useless unless we also have the visual.
1135 gdkx_colormap_get (Colormap xcolormap)
1137 GdkColormap *colormap;
1138 GdkColormapPrivate *private;
1140 colormap = gdk_colormap_lookup (xcolormap);
1144 if (xcolormap == DefaultColormap (gdk_display, gdk_screen))
1145 return gdk_colormap_get_system ();
1147 private = g_new (GdkColormapPrivate, 1);
1148 colormap = (GdkColormap*) private;
1150 private->xdisplay = gdk_display;
1151 private->xcolormap = xcolormap;
1152 private->visual = NULL;
1153 private->private_val = TRUE;
1155 /* To do the following safely, we would have to have some way of finding
1156 * out what the size or visual of the given colormap is. It seems
1157 * X doesn't allow this
1161 for (i = 0; i < 256; i++)
1163 xpalette[i].pixel = i;
1164 xpalette[i].red = 0;
1165 xpalette[i].green = 0;
1166 xpalette[i].blue = 0;
1169 XQueryColors (gdk_display, private->xcolormap, xpalette, 256);
1171 for (i = 0; i < 256; i++)
1173 colormap->colors[i].pixel = xpalette[i].pixel;
1174 colormap->colors[i].red = xpalette[i].red;
1175 colormap->colors[i].green = xpalette[i].green;
1176 colormap->colors[i].blue = xpalette[i].blue;
1180 colormap->colors = NULL;
1183 gdk_colormap_add (colormap);
1190 gdk_colormap_match_color (GdkColormap *cmap,
1192 const gchar *available)
1196 gint rdiff, gdiff, bdiff;
1199 g_return_val_if_fail (cmap != NULL, 0);
1200 g_return_val_if_fail (color != NULL, 0);
1202 colors = cmap->colors;
1206 for (i = 0; i < cmap->size; i++)
1208 if ((!available) || (available && available[i]))
1210 rdiff = (color->red - colors[i].red);
1211 gdiff = (color->green - colors[i].green);
1212 bdiff = (color->blue - colors[i].blue);
1214 sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
1229 gdk_colormap_lookup (Colormap xcolormap)
1236 cmap = g_hash_table_lookup (colormap_hash, &xcolormap);
1241 gdk_colormap_add (GdkColormap *cmap)
1243 GdkColormapPrivate *private;
1246 colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
1247 (GCompareFunc) gdk_colormap_cmp);
1249 private = (GdkColormapPrivate*) cmap;
1251 g_hash_table_insert (colormap_hash, &private->xcolormap, cmap);
1255 gdk_colormap_remove (GdkColormap *cmap)
1257 GdkColormapPrivate *private;
1260 colormap_hash = g_hash_table_new ((GHashFunc) gdk_colormap_hash,
1261 (GCompareFunc) gdk_colormap_cmp);
1263 private = (GdkColormapPrivate*) cmap;
1265 g_hash_table_remove (colormap_hash, &private->xcolormap);
1269 gdk_colormap_hash (Colormap *cmap)
1275 gdk_colormap_cmp (Colormap *a,