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 Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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-2000. 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/.
29 #include "gdkscreen.h"
31 #include "gdkinternals.h"
35 * @cmap: a #GdkColormap
37 * Deprecated function; use g_object_ref() instead.
39 * Return value: the colormap
42 gdk_colormap_ref (GdkColormap *cmap)
44 return (GdkColormap *) g_object_ref (cmap);
49 * @cmap: a #GdkColormap
51 * Deprecated function; use g_object_ref() instead.
54 gdk_colormap_unref (GdkColormap *cmap)
56 g_object_unref (cmap);
61 * gdk_colormap_get_visual:
62 * @colormap: a #GdkColormap.
64 * Returns the visual for which a given colormap was created.
66 * Return value: the visual of the colormap.
69 gdk_colormap_get_visual (GdkColormap *colormap)
71 g_return_val_if_fail (GDK_IS_COLORMAP (colormap), NULL);
73 return colormap->visual;
78 * @colormap: a #GdkColormap.
79 * @colors: the new color values.
80 * @ncolors: the number of colors to change.
82 * Changes the value of the first @ncolors colors in
83 * a private colormap. This function is obsolete and
84 * should not be used. See gdk_color_change().
87 gdk_colors_store (GdkColormap *colormap,
93 for (i = 0; i < ncolors; i++)
95 colormap->colors[i].pixel = colors[i].pixel;
96 colormap->colors[i].red = colors[i].red;
97 colormap->colors[i].green = colors[i].green;
98 colormap->colors[i].blue = colors[i].blue;
101 gdk_colormap_change (colormap, ncolors);
104 static GMemChunk *color_chunk;
108 * @color: a #GdkColor.
110 * Makes a copy of a color structure. The result
111 * must be freed using gdk_color_free().
113 * Return value: a copy of @color.
116 gdk_color_copy (const GdkColor *color)
120 g_return_val_if_fail (color != NULL, NULL);
122 if (color_chunk == NULL)
123 color_chunk = g_mem_chunk_new ("colors",
128 new_color = g_chunk_new (GdkColor, color_chunk);
135 * @color: a #GdkColor.
137 * Frees a color structure created with
141 gdk_color_free (GdkColor *color)
143 g_assert (color_chunk != NULL);
144 g_return_if_fail (color != NULL);
146 g_mem_chunk_free (color_chunk, color);
151 * @colormap: a #GdkColormap.
152 * @color: the location to store the color.
154 * Returns the white color for a given colormap. The resulting
155 * value has already allocated been allocated.
157 * Return value: %TRUE if the allocation succeeded.
160 gdk_color_white (GdkColormap *colormap,
165 g_return_val_if_fail (colormap != NULL, FALSE);
170 color->green = 65535;
173 return_val = gdk_colormap_alloc_color (colormap, color, FALSE, TRUE);
183 * @colormap: a #GdkColormap.
184 * @color: the location to store the color.
186 * Returns the black color for a given colormap. The resulting
187 * value has already benn allocated.
189 * Return value: %TRUE if the allocation succeeded.
192 gdk_color_black (GdkColormap *colormap,
197 g_return_val_if_fail (colormap != NULL, FALSE);
205 return_val = gdk_colormap_alloc_color (colormap, color, FALSE, TRUE);
213 /********************
215 ********************/
218 * gdk_colormap_alloc_color:
219 * @colormap: a #GdkColormap.
220 * @color: the color to allocate. On return the
221 * <structfield>pixel</structfield> field will be
222 * filled in if allocation succeeds.
223 * @writeable: If %TRUE, the color is allocated writeable
224 * (their values can later be changed using gdk_color_change()).
225 * Writeable colors cannot be shared between applications.
226 * @best_match: If %TRUE, GDK will attempt to do matching against
227 * existing colors if the color cannot be allocated as requested.
229 * Allocates a single color from a colormap.
231 * Return value: %TRUE if the allocation succeeded.
234 gdk_colormap_alloc_color (GdkColormap *colormap,
241 gdk_colormap_alloc_colors (colormap, color, 1, writeable, best_match,
249 * @colormap: a #GdkColormap.
250 * @color: The color to allocate. On return, the
251 * <structfield>pixel</structfield> field will be filled in.
253 * Allocates a single color from a colormap.
255 * Return value: %TRUE if the allocation succeeded.
257 * Deprecated: Use gdk_colormap_alloc_color() instead.
260 gdk_color_alloc (GdkColormap *colormap,
265 gdk_colormap_alloc_colors (colormap, color, 1, FALSE, TRUE, &success);
272 * @colora: a #GdkColor.
274 * A hash function suitable for using for a hash
275 * table that stores #GdkColor's.
277 * Return value: The hash function appled to @colora
280 gdk_color_hash (const GdkColor *colora)
282 return ((colora->red) +
283 (colora->green << 11) +
284 (colora->blue << 22) +
285 (colora->blue >> 6));
290 * @colora: a #GdkColor.
291 * @colorb: another #GdkColor.
293 * Compares two colors.
295 * Return value: %TRUE if the two colors compare equal
298 gdk_color_equal (const GdkColor *colora,
299 const GdkColor *colorb)
301 g_return_val_if_fail (colora != NULL, FALSE);
302 g_return_val_if_fail (colorb != NULL, FALSE);
304 return ((colora->red == colorb->red) &&
305 (colora->green == colorb->green) &&
306 (colora->blue == colorb->blue));
310 gdk_color_get_type (void)
312 static GType our_type = 0;
315 our_type = g_boxed_type_register_static ("GdkColor",
316 (GBoxedCopyFunc)gdk_color_copy,
317 (GBoxedFreeFunc)gdk_color_free);
323 * @spec: the string specifying the color.
324 * @color: the #GdkColor to fill in
326 * Parses a textual specification of a color and fill in
327 * the <structfield>red</structfield>,
328 * <structfield>green</structfield>, and
329 * <structfield>blue</structfield> fields of a
330 * #GdkColor structure. The color is <emphasis>not</emphasis>
331 * allocated, you must call gdk_colormap_alloc_color() yourself.
332 * The text string can be in any of the forms accepted
333 * by <function>XParseColor</function>; these include
334 * name for a color from <filename>rgb.txt</filename>, such as
335 * <literal>DarkSlateGray</literal>, or a hex specification
336 * such as <literal>305050</literal>.
338 * Return value: %TRUE if the parsing succeeded.
341 gdk_color_parse (const gchar *spec,
344 PangoColor pango_color;
346 if (pango_color_parse (&pango_color, spec))
348 color->red = pango_color.red;
349 color->green = pango_color.green;
350 color->blue = pango_color.blue;
359 * gdk_colormap_get_system:
361 * Gets the system's default colormap for the default screen. (See
362 * gdk_colormap_get_system_for_screen ())
364 * Return value: the default colormap.
367 gdk_colormap_get_system (void)
369 return gdk_screen_get_system_colormap (gdk_screen_get_default ());