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_color_alloc (colormap, color);
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_color_alloc (colormap, color);
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.
254 * This function is obsolete. See gdk_colormap_alloc_color().
256 * Return value: %TRUE if the allocation succeeded.
259 gdk_color_alloc (GdkColormap *colormap,
264 gdk_colormap_alloc_colors (colormap, color, 1, FALSE, TRUE, &success);
271 * @colora: a #GdkColor.
273 * A hash function suitable for using for a hash
274 * table that stores #GdkColor's.
276 * Return value: The hash function appled to @colora
279 gdk_color_hash (const GdkColor *colora)
281 return ((colora->red) +
282 (colora->green << 11) +
283 (colora->blue << 22) +
284 (colora->blue >> 6));
289 * @colora: a #GdkColor.
290 * @colorb: another #GdkColor.
292 * Compares two colors.
294 * Return value: %TRUE if the two colors compare equal
297 gdk_color_equal (const GdkColor *colora,
298 const GdkColor *colorb)
300 g_return_val_if_fail (colora != NULL, FALSE);
301 g_return_val_if_fail (colorb != NULL, FALSE);
303 return ((colora->red == colorb->red) &&
304 (colora->green == colorb->green) &&
305 (colora->blue == colorb->blue));
309 gdk_color_get_type (void)
311 static GType our_type = 0;
314 our_type = g_boxed_type_register_static ("GdkColor",
315 (GBoxedCopyFunc)gdk_color_copy,
316 (GBoxedFreeFunc)gdk_color_free);
322 * @spec: the string specifying the color.
323 * @color: the #GdkColor to fill in
325 * Parses a textual specification of a color and fill in
326 * the <structfield>red</structfield>,
327 * <structfield>green</structfield>, and
328 * <structfield>blue</structfield> fields of a
329 * #GdkColor structure. The color is <emphasis>not</emphasis>
330 * allocated, you must call gdk_colormap_alloc_color() yourself.
331 * The text string can be in any of the forms accepted
332 * by <function>XParseColor</function>; these include
333 * name for a color from <filename>rgb.txt</filename>, such as
334 * <literal>DarkSlateGray</literal>, or a hex specification
335 * such as <literal>305050</literal>.
337 * Return value: %TRUE if the parsing succeeded.
340 gdk_color_parse (const gchar *spec,
343 PangoColor pango_color;
345 if (pango_color_parse (&pango_color, spec))
347 color->red = pango_color.red;
348 color->green = pango_color.green;
349 color->blue = pango_color.blue;
358 * gdk_colormap_get_system:
360 * Gets the system's default colormap for the default screen. (See
361 * gdk_colormap_get_system_for_screen ())
363 * Return value: the default colormap.
366 gdk_colormap_get_system (void)
368 return gdk_screen_get_system_colormap (gdk_screen_get_default ());