]> Pileus Git - ~andy/gtk/blob - gdk/gdkcolor.c
Small documentation additions.
[~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 <config.h>
28 #include <time.h>
29
30 #include "gdkscreen.h"
31 #include "gdkcolor.h"
32 #include "gdkinternals.h"
33 #include "gdkalias.h"
34
35 /**
36  * gdk_colormap_ref:
37  * @cmap: a #GdkColormap
38  *
39  * Deprecated function; use g_object_ref() instead.
40  *
41  * Return value: the colormap
42  **/
43 GdkColormap*
44 gdk_colormap_ref (GdkColormap *cmap)
45 {
46   return (GdkColormap *) g_object_ref (cmap);
47 }
48
49 /**
50  * gdk_colormap_unref:
51  * @cmap: a #GdkColormap
52  *
53  * Deprecated function; use g_object_ref() instead.
54  **/
55 void
56 gdk_colormap_unref (GdkColormap *cmap)
57 {
58   g_object_unref (cmap);
59 }
60
61
62 /**
63  * gdk_colormap_get_visual:
64  * @colormap: a #GdkColormap.
65  * 
66  * Returns the visual for which a given colormap was created.
67  * 
68  * Return value: the visual of the colormap.
69  **/
70 GdkVisual *
71 gdk_colormap_get_visual (GdkColormap *colormap)
72 {
73   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), NULL);
74
75   return colormap->visual;
76 }
77
78 /**
79  * gdk_colors_store:
80  * @colormap: a #GdkColormap.
81  * @colors: the new color values.
82  * @ncolors: the number of colors to change.
83  * 
84  * Changes the value of the first @ncolors colors in
85  * a private colormap. This function is obsolete and
86  * should not be used. See gdk_color_change().
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_colormap_alloc_color (colormap, color, FALSE, TRUE);
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_colormap_alloc_color (colormap, color, FALSE, TRUE);
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  * 
257  * Return value: %TRUE if the allocation succeeded.
258  *
259  * Deprecated: Use gdk_colormap_alloc_color() instead.
260  **/
261 gboolean
262 gdk_color_alloc (GdkColormap *colormap,
263                  GdkColor    *color)
264 {
265   gboolean success;
266
267   gdk_colormap_alloc_colors (colormap, color, 1, FALSE, TRUE, &success);
268
269   return success;
270 }
271
272 /**
273  * gdk_color_hash:
274  * @colora: a #GdkColor.
275  * 
276  * A hash function suitable for using for a hash
277  * table that stores #GdkColor's.
278  * 
279  * Return value: The hash function appled to @colora
280  **/
281 guint
282 gdk_color_hash (const GdkColor *colora)
283 {
284   return ((colora->red) +
285           (colora->green << 11) +
286           (colora->blue << 22) +
287           (colora->blue >> 6));
288 }
289
290 /**
291  * gdk_color_equal:
292  * @colora: a #GdkColor.
293  * @colorb: another #GdkColor.
294  * 
295  * Compares two colors. 
296  * 
297  * Return value: %TRUE if the two colors compare equal
298  **/
299 gboolean
300 gdk_color_equal (const GdkColor *colora,
301                  const GdkColor *colorb)
302 {
303   g_return_val_if_fail (colora != NULL, FALSE);
304   g_return_val_if_fail (colorb != NULL, FALSE);
305
306   return ((colora->red == colorb->red) &&
307           (colora->green == colorb->green) &&
308           (colora->blue == colorb->blue));
309 }
310
311 GType
312 gdk_color_get_type (void)
313 {
314   static GType our_type = 0;
315   
316   if (our_type == 0)
317     our_type = g_boxed_type_register_static ("GdkColor",
318                                              (GBoxedCopyFunc)gdk_color_copy,
319                                              (GBoxedFreeFunc)gdk_color_free);
320   return our_type;
321 }
322
323 /**
324  * gdk_color_parse:
325  * @spec: the string specifying the color.
326  * @color: the #GdkColor to fill in
327  * 
328  * Parses a textual specification of a color and fill in
329  * the <structfield>red</structfield>,
330  * <structfield>green</structfield>, and 
331  * <structfield>blue</structfield> fields of a 
332  * #GdkColor structure. The color is <emphasis>not</emphasis> 
333  * allocated, you must call gdk_colormap_alloc_color() yourself.
334  * The text string can be in any of the forms accepted
335  * by <function>XParseColor</function>; these include
336  * name for a color from <filename>rgb.txt</filename>, such as
337  * <literal>DarkSlateGray</literal>, or a hex specification
338  * such as <literal>305050</literal>.
339  * 
340  * Return value: %TRUE if the parsing succeeded.
341  **/
342 gboolean
343 gdk_color_parse (const gchar *spec,
344                  GdkColor    *color)
345 {
346   PangoColor pango_color;
347
348   if (pango_color_parse (&pango_color, spec))
349     {
350       color->red = pango_color.red;
351       color->green = pango_color.green;
352       color->blue = pango_color.blue;
353
354       return TRUE;
355     }
356   else
357     return FALSE;
358 }
359
360 /**
361  * gdk_colormap_get_system:
362  * 
363  * Gets the system's default colormap for the default screen. (See
364  * gdk_colormap_get_system_for_screen ())
365  * 
366  * Return value: the default colormap.
367  **/
368 GdkColormap*
369 gdk_colormap_get_system (void)
370 {
371   return gdk_screen_get_system_colormap (gdk_screen_get_default ());
372 }
373
374 /**
375  * gdk_cairo_set_source_color:
376  * @cr: a #cairo_t
377  * @color: a #GdkColor
378  * 
379  * Convenience function to set the specified GdkColor as the
380  * source color of the given Cairo context.
381  *
382  * Since: 2.8
383  **/
384 void
385 gdk_cairo_set_source_color (cairo_t  *cr,
386                             GdkColor *color)
387 {
388   cairo_set_source_rgb (cr,
389                         color->red / 65535.,
390                         color->green / 65535.,
391                         color->blue / 65535.);
392 }
393
394 #define __GDK_COLOR_C__
395 #include "gdkaliasdef.c"