]> Pileus Git - ~andy/gtk/blob - gdk/gdkcolor.c
Documentation updates.
[~andy/gtk] / gdk / gdkcolor.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include <time.h>
28
29 #include "gdkscreen.h"
30 #include "gdkcolor.h"
31 #include "gdkinternals.h"
32
33 /**
34  * gdk_colormap_ref:
35  * @cmap: a #GdkColormap
36  *
37  * Deprecated function; use g_object_ref() instead.
38  *
39  * Return value: the colormap
40  **/
41 GdkColormap*
42 gdk_colormap_ref (GdkColormap *cmap)
43 {
44   return (GdkColormap *) g_object_ref (cmap);
45 }
46
47 /**
48  * gdk_colormap_unref:
49  * @cmap: a #GdkColormap
50  *
51  * Deprecated function; use g_object_ref() instead.
52  **/
53 void
54 gdk_colormap_unref (GdkColormap *cmap)
55 {
56   g_object_unref (cmap);
57 }
58
59
60 /**
61  * gdk_colormap_get_visual:
62  * @colormap: a #GdkColormap.
63  * 
64  * Returns the visual for which a given colormap was created.
65  * 
66  * Return value: the visual of the colormap.
67  **/
68 GdkVisual *
69 gdk_colormap_get_visual (GdkColormap *colormap)
70 {
71   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), NULL);
72
73   return colormap->visual;
74 }
75
76 /**
77  * gdk_colors_store:
78  * @colormap: a #GdkColormap.
79  * @colors: the new color values.
80  * @ncolors: the number of colors to change.
81  * 
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().
85  * 
86  * Return value: 
87  **/     
88 void
89 gdk_colors_store (GdkColormap   *colormap,
90                   GdkColor      *colors,
91                   gint           ncolors)
92 {
93   gint i;
94
95   for (i = 0; i < ncolors; i++)
96     {
97       colormap->colors[i].pixel = colors[i].pixel;
98       colormap->colors[i].red = colors[i].red;
99       colormap->colors[i].green = colors[i].green;
100       colormap->colors[i].blue = colors[i].blue;
101     }
102
103   gdk_colormap_change (colormap, ncolors);
104 }
105
106 static GMemChunk *color_chunk;
107
108 /**
109  * gdk_color_copy:
110  * @color: a #GdkColor.
111  * 
112  * Makes a copy of a color structure. The result
113  * must be freed using gdk_color_free().
114  * 
115  * Return value: a copy of @color.
116  **/
117 GdkColor*
118 gdk_color_copy (const GdkColor *color)
119 {
120   GdkColor *new_color;
121   
122   g_return_val_if_fail (color != NULL, NULL);
123
124   if (color_chunk == NULL)
125     color_chunk = g_mem_chunk_new ("colors",
126                                    sizeof (GdkColor),
127                                    4096,
128                                    G_ALLOC_AND_FREE);
129
130   new_color = g_chunk_new (GdkColor, color_chunk);
131   *new_color = *color;
132   return new_color;
133 }
134
135 /**
136  * gdk_color_free:
137  * @color: a #GdkColor.
138  * 
139  * Frees a color structure created with 
140  * gdk_color_copy().
141  **/
142 void
143 gdk_color_free (GdkColor *color)
144 {
145   g_assert (color_chunk != NULL);
146   g_return_if_fail (color != NULL);
147
148   g_mem_chunk_free (color_chunk, color);
149 }
150
151 /**
152  * gdk_color_white:
153  * @colormap: a #GdkColormap.
154  * @color: the location to store the color.
155  * 
156  * Returns the white color for a given colormap. The resulting
157  * value has already allocated been allocated. 
158  * 
159  * Return value: %TRUE if the allocation succeeded.
160  **/
161 gboolean
162 gdk_color_white (GdkColormap *colormap,
163                  GdkColor    *color)
164 {
165   gint return_val;
166
167   g_return_val_if_fail (colormap != NULL, FALSE);
168
169   if (color)
170     {
171       color->red = 65535;
172       color->green = 65535;
173       color->blue = 65535;
174
175       return_val = gdk_color_alloc (colormap, color);
176     }
177   else
178     return_val = FALSE;
179
180   return return_val;
181 }
182
183 /**
184  * gdk_color_black:
185  * @colormap: a #GdkColormap.
186  * @color: the location to store the color.
187  * 
188  * Returns the black color for a given colormap. The resulting
189  * value has already benn allocated. 
190  * 
191  * Return value: %TRUE if the allocation succeeded.
192  **/
193 gboolean
194 gdk_color_black (GdkColormap *colormap,
195                  GdkColor    *color)
196 {
197   gint return_val;
198
199   g_return_val_if_fail (colormap != NULL, FALSE);
200
201   if (color)
202     {
203       color->red = 0;
204       color->green = 0;
205       color->blue = 0;
206
207       return_val = gdk_color_alloc (colormap, color);
208     }
209   else
210     return_val = FALSE;
211
212   return return_val;
213 }
214
215 /********************
216  * Color allocation *
217  ********************/
218
219 /**
220  * gdk_colormap_alloc_color:
221  * @colormap: a #GdkColormap.
222  * @color: the color to allocate. On return the
223  *    <structfield>pixel</structfield> field will be
224  *    filled in if allocation succeeds.
225  * @writeable: If %TRUE, the color is allocated writeable
226  *    (their values can later be changed using gdk_color_change()).
227  *    Writeable colors cannot be shared between applications.
228  * @best_match: If %TRUE, GDK will attempt to do matching against
229  *    existing colors if the color cannot be allocated as requested.
230  *
231  * Allocates a single color from a colormap.
232  * 
233  * Return value: %TRUE if the allocation succeeded.
234  **/
235 gboolean
236 gdk_colormap_alloc_color (GdkColormap *colormap,
237                           GdkColor    *color,
238                           gboolean     writeable,
239                           gboolean     best_match)
240 {
241   gboolean success;
242
243   gdk_colormap_alloc_colors (colormap, color, 1, writeable, best_match,
244                              &success);
245
246   return success;
247 }
248
249 /**
250  * gdk_color_alloc:
251  * @colormap: a #GdkColormap.
252  * @color: The color to allocate. On return, the 
253  *    <structfield>pixel</structfield> field will be filled in.
254  * 
255  * Allocates a single color from a colormap.
256  * This function is obsolete. See gdk_colormap_alloc_color().
257  * 
258  * Return value: %TRUE if the allocation succeeded.
259  **/
260 gboolean
261 gdk_color_alloc (GdkColormap *colormap,
262                  GdkColor    *color)
263 {
264   gboolean success;
265
266   gdk_colormap_alloc_colors (colormap, color, 1, FALSE, TRUE, &success);
267
268   return success;
269 }
270
271 /**
272  * gdk_color_hash:
273  * @colora: a #GdkColor.
274  * 
275  * A hash function suitable for using for a hash
276  * table that stores #GdkColor's.
277  * 
278  * Return value: The hash function appled to @colora
279  **/
280 guint
281 gdk_color_hash (const GdkColor *colora)
282 {
283   return ((colora->red) +
284           (colora->green << 11) +
285           (colora->blue << 22) +
286           (colora->blue >> 6));
287 }
288
289 /**
290  * gdk_color_equal:
291  * @colora: a #GdkColor.
292  * @colorb: another #GdkColor.
293  * 
294  * Compares two colors. 
295  * 
296  * Return value: %TRUE if the two colors compare equal
297  **/
298 gboolean
299 gdk_color_equal (const GdkColor *colora,
300                  const GdkColor *colorb)
301 {
302   g_return_val_if_fail (colora != NULL, FALSE);
303   g_return_val_if_fail (colorb != NULL, FALSE);
304
305   return ((colora->red == colorb->red) &&
306           (colora->green == colorb->green) &&
307           (colora->blue == colorb->blue));
308 }
309
310 GType
311 gdk_color_get_type (void)
312 {
313   static GType our_type = 0;
314   
315   if (our_type == 0)
316     our_type = g_boxed_type_register_static ("GdkColor",
317                                              (GBoxedCopyFunc)gdk_color_copy,
318                                              (GBoxedFreeFunc)gdk_color_free);
319   return our_type;
320 }
321
322 /**
323  * gdk_color_parse:
324  * @spec: the string specifying the color.
325  * @color: the #GdkColor to fill in
326  * 
327  * Parses a textual specification of a color and fill in
328  * the <structfield>red</structfield>,
329  * <structfield>green</structfield>, and 
330  * <structfield>blue</structfield> fields of a 
331  * #GdkColor structure. The color is <emphasis>not</emphasis> 
332  * allocated, you must call gdk_colormap_alloc_color() yourself.
333  * The text string can be in any of the forms accepted
334  * by <function>XParseColor</function>; these include
335  * name for a color from <filename>rgb.txt</filename>, such as
336  * <literal>DarkSlateGray</literal>, or a hex specification
337  * such as <literal>305050</literal>.
338  * 
339  * Return value: %TRUE if the parsing succeeded.
340  **/
341 gboolean
342 gdk_color_parse (const gchar *spec,
343                  GdkColor    *color)
344 {
345   PangoColor pango_color;
346
347   if (pango_color_parse (&pango_color, spec))
348     {
349       color->red = pango_color.red;
350       color->green = pango_color.green;
351       color->blue = pango_color.blue;
352
353       return TRUE;
354     }
355   else
356     return FALSE;
357 }
358
359 /**
360  * gdk_colormap_get_system:
361  * 
362  * Gets the system's default colormap for the default screen. (See
363  * gdk_colormap_get_system_for_screen ())
364  * 
365  * Return value: the default colormap.
366  **/
367 GdkColormap*
368 gdk_colormap_get_system (void)
369 {
370   return gdk_screen_get_system_colormap (gdk_screen_get_default ());
371 }
372