]> Pileus Git - ~andy/gtk/blob - gdk/gdkcolor.c
doc comment fixes.
[~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 void
87 gdk_colors_store (GdkColormap   *colormap,
88                   GdkColor      *colors,
89                   gint           ncolors)
90 {
91   gint i;
92
93   for (i = 0; i < ncolors; i++)
94     {
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;
99     }
100
101   gdk_colormap_change (colormap, ncolors);
102 }
103
104 static GMemChunk *color_chunk;
105
106 /**
107  * gdk_color_copy:
108  * @color: a #GdkColor.
109  * 
110  * Makes a copy of a color structure. The result
111  * must be freed using gdk_color_free().
112  * 
113  * Return value: a copy of @color.
114  **/
115 GdkColor*
116 gdk_color_copy (const GdkColor *color)
117 {
118   GdkColor *new_color;
119   
120   g_return_val_if_fail (color != NULL, NULL);
121
122   if (color_chunk == NULL)
123     color_chunk = g_mem_chunk_new ("colors",
124                                    sizeof (GdkColor),
125                                    4096,
126                                    G_ALLOC_AND_FREE);
127
128   new_color = g_chunk_new (GdkColor, color_chunk);
129   *new_color = *color;
130   return new_color;
131 }
132
133 /**
134  * gdk_color_free:
135  * @color: a #GdkColor.
136  * 
137  * Frees a color structure created with 
138  * gdk_color_copy().
139  **/
140 void
141 gdk_color_free (GdkColor *color)
142 {
143   g_assert (color_chunk != NULL);
144   g_return_if_fail (color != NULL);
145
146   g_mem_chunk_free (color_chunk, color);
147 }
148
149 /**
150  * gdk_color_white:
151  * @colormap: a #GdkColormap.
152  * @color: the location to store the color.
153  * 
154  * Returns the white color for a given colormap. The resulting
155  * value has already allocated been allocated. 
156  * 
157  * Return value: %TRUE if the allocation succeeded.
158  **/
159 gboolean
160 gdk_color_white (GdkColormap *colormap,
161                  GdkColor    *color)
162 {
163   gint return_val;
164
165   g_return_val_if_fail (colormap != NULL, FALSE);
166
167   if (color)
168     {
169       color->red = 65535;
170       color->green = 65535;
171       color->blue = 65535;
172
173       return_val = gdk_color_alloc (colormap, color);
174     }
175   else
176     return_val = FALSE;
177
178   return return_val;
179 }
180
181 /**
182  * gdk_color_black:
183  * @colormap: a #GdkColormap.
184  * @color: the location to store the color.
185  * 
186  * Returns the black color for a given colormap. The resulting
187  * value has already benn allocated. 
188  * 
189  * Return value: %TRUE if the allocation succeeded.
190  **/
191 gboolean
192 gdk_color_black (GdkColormap *colormap,
193                  GdkColor    *color)
194 {
195   gint return_val;
196
197   g_return_val_if_fail (colormap != NULL, FALSE);
198
199   if (color)
200     {
201       color->red = 0;
202       color->green = 0;
203       color->blue = 0;
204
205       return_val = gdk_color_alloc (colormap, color);
206     }
207   else
208     return_val = FALSE;
209
210   return return_val;
211 }
212
213 /********************
214  * Color allocation *
215  ********************/
216
217 /**
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.
228  *
229  * Allocates a single color from a colormap.
230  * 
231  * Return value: %TRUE if the allocation succeeded.
232  **/
233 gboolean
234 gdk_colormap_alloc_color (GdkColormap *colormap,
235                           GdkColor    *color,
236                           gboolean     writeable,
237                           gboolean     best_match)
238 {
239   gboolean success;
240
241   gdk_colormap_alloc_colors (colormap, color, 1, writeable, best_match,
242                              &success);
243
244   return success;
245 }
246
247 /**
248  * gdk_color_alloc:
249  * @colormap: a #GdkColormap.
250  * @color: The color to allocate. On return, the 
251  *    <structfield>pixel</structfield> field will be filled in.
252  * 
253  * Allocates a single color from a colormap.
254  * This function is obsolete. See gdk_colormap_alloc_color().
255  * 
256  * Return value: %TRUE if the allocation succeeded.
257  **/
258 gboolean
259 gdk_color_alloc (GdkColormap *colormap,
260                  GdkColor    *color)
261 {
262   gboolean success;
263
264   gdk_colormap_alloc_colors (colormap, color, 1, FALSE, TRUE, &success);
265
266   return success;
267 }
268
269 /**
270  * gdk_color_hash:
271  * @colora: a #GdkColor.
272  * 
273  * A hash function suitable for using for a hash
274  * table that stores #GdkColor's.
275  * 
276  * Return value: The hash function appled to @colora
277  **/
278 guint
279 gdk_color_hash (const GdkColor *colora)
280 {
281   return ((colora->red) +
282           (colora->green << 11) +
283           (colora->blue << 22) +
284           (colora->blue >> 6));
285 }
286
287 /**
288  * gdk_color_equal:
289  * @colora: a #GdkColor.
290  * @colorb: another #GdkColor.
291  * 
292  * Compares two colors. 
293  * 
294  * Return value: %TRUE if the two colors compare equal
295  **/
296 gboolean
297 gdk_color_equal (const GdkColor *colora,
298                  const GdkColor *colorb)
299 {
300   g_return_val_if_fail (colora != NULL, FALSE);
301   g_return_val_if_fail (colorb != NULL, FALSE);
302
303   return ((colora->red == colorb->red) &&
304           (colora->green == colorb->green) &&
305           (colora->blue == colorb->blue));
306 }
307
308 GType
309 gdk_color_get_type (void)
310 {
311   static GType our_type = 0;
312   
313   if (our_type == 0)
314     our_type = g_boxed_type_register_static ("GdkColor",
315                                              (GBoxedCopyFunc)gdk_color_copy,
316                                              (GBoxedFreeFunc)gdk_color_free);
317   return our_type;
318 }
319
320 /**
321  * gdk_color_parse:
322  * @spec: the string specifying the color.
323  * @color: the #GdkColor to fill in
324  * 
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>.
336  * 
337  * Return value: %TRUE if the parsing succeeded.
338  **/
339 gboolean
340 gdk_color_parse (const gchar *spec,
341                  GdkColor    *color)
342 {
343   PangoColor pango_color;
344
345   if (pango_color_parse (&pango_color, spec))
346     {
347       color->red = pango_color.red;
348       color->green = pango_color.green;
349       color->blue = pango_color.blue;
350
351       return TRUE;
352     }
353   else
354     return FALSE;
355 }
356
357 /**
358  * gdk_colormap_get_system:
359  * 
360  * Gets the system's default colormap for the default screen. (See
361  * gdk_colormap_get_system_for_screen ())
362  * 
363  * Return value: the default colormap.
364  **/
365 GdkColormap*
366 gdk_colormap_get_system (void)
367 {
368   return gdk_screen_get_system_colormap (gdk_screen_get_default ());
369 }
370