]> Pileus Git - ~andy/gtk/blob - gdk/gdkcolor.c
Merge branch 'master' into toolpalette
[~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  * Deprecated: 2.0: Use g_object_ref() instead.
44  **/
45 GdkColormap*
46 gdk_colormap_ref (GdkColormap *cmap)
47 {
48   return (GdkColormap *) g_object_ref (cmap);
49 }
50
51 /**
52  * gdk_colormap_unref:
53  * @cmap: a #GdkColormap
54  *
55  * Deprecated function; use g_object_unref() instead.
56  *
57  * Deprecated: 2.0: Use g_object_unref() instead.
58  **/
59 void
60 gdk_colormap_unref (GdkColormap *cmap)
61 {
62   g_object_unref (cmap);
63 }
64
65
66 /**
67  * gdk_colormap_get_visual:
68  * @colormap: a #GdkColormap.
69  * 
70  * Returns the visual for which a given colormap was created.
71  * 
72  * Return value: the visual of the colormap.
73  **/
74 GdkVisual *
75 gdk_colormap_get_visual (GdkColormap *colormap)
76 {
77   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), NULL);
78
79   return colormap->visual;
80 }
81
82 /**
83  * gdk_colors_store:
84  * @colormap: a #GdkColormap.
85  * @colors: the new color values.
86  * @ncolors: the number of colors to change.
87  * 
88  * Changes the value of the first @ncolors colors in
89  * a private colormap. This function is obsolete and
90  * should not be used. See gdk_color_change().
91  **/     
92 void
93 gdk_colors_store (GdkColormap   *colormap,
94                   GdkColor      *colors,
95                   gint           ncolors)
96 {
97   gint i;
98
99   for (i = 0; i < ncolors; i++)
100     {
101       colormap->colors[i].pixel = colors[i].pixel;
102       colormap->colors[i].red = colors[i].red;
103       colormap->colors[i].green = colors[i].green;
104       colormap->colors[i].blue = colors[i].blue;
105     }
106
107   gdk_colormap_change (colormap, ncolors);
108 }
109
110 /**
111  * gdk_color_copy:
112  * @color: a #GdkColor.
113  * 
114  * Makes a copy of a color structure. The result
115  * must be freed using gdk_color_free().
116  * 
117  * Return value: a copy of @color.
118  **/
119 GdkColor*
120 gdk_color_copy (const GdkColor *color)
121 {
122   GdkColor *new_color;
123   
124   g_return_val_if_fail (color != NULL, NULL);
125
126   new_color = g_slice_new (GdkColor);
127   *new_color = *color;
128   return new_color;
129 }
130
131 /**
132  * gdk_color_free:
133  * @color: a #GdkColor.
134  * 
135  * Frees a color structure created with 
136  * gdk_color_copy().
137  **/
138 void
139 gdk_color_free (GdkColor *color)
140 {
141   g_return_if_fail (color != NULL);
142
143   g_slice_free (GdkColor, color);
144 }
145
146 /**
147  * gdk_color_white:
148  * @colormap: a #GdkColormap.
149  * @color: the location to store the color.
150  * 
151  * Returns the white color for a given colormap. The resulting
152  * value has already allocated been allocated. 
153  * 
154  * Return value: %TRUE if the allocation succeeded.
155  **/
156 gboolean
157 gdk_color_white (GdkColormap *colormap,
158                  GdkColor    *color)
159 {
160   gint return_val;
161
162   g_return_val_if_fail (colormap != NULL, FALSE);
163
164   if (color)
165     {
166       color->red = 65535;
167       color->green = 65535;
168       color->blue = 65535;
169
170       return_val = gdk_colormap_alloc_color (colormap, color, FALSE, TRUE);
171     }
172   else
173     return_val = FALSE;
174
175   return return_val;
176 }
177
178 /**
179  * gdk_color_black:
180  * @colormap: a #GdkColormap.
181  * @color: the location to store the color.
182  * 
183  * Returns the black color for a given colormap. The resulting
184  * value has already been allocated. 
185  * 
186  * Return value: %TRUE if the allocation succeeded.
187  **/
188 gboolean
189 gdk_color_black (GdkColormap *colormap,
190                  GdkColor    *color)
191 {
192   gint return_val;
193
194   g_return_val_if_fail (colormap != NULL, FALSE);
195
196   if (color)
197     {
198       color->red = 0;
199       color->green = 0;
200       color->blue = 0;
201
202       return_val = gdk_colormap_alloc_color (colormap, color, FALSE, TRUE);
203     }
204   else
205     return_val = FALSE;
206
207   return return_val;
208 }
209
210 /********************
211  * Color allocation *
212  ********************/
213
214 /**
215  * gdk_colormap_alloc_color:
216  * @colormap: a #GdkColormap.
217  * @color: the color to allocate. On return the
218  *    <structfield>pixel</structfield> field will be
219  *    filled in if allocation succeeds.
220  * @writeable: If %TRUE, the color is allocated writeable
221  *    (their values can later be changed using gdk_color_change()).
222  *    Writeable colors cannot be shared between applications.
223  * @best_match: If %TRUE, GDK will attempt to do matching against
224  *    existing colors if the color cannot be allocated as requested.
225  *
226  * Allocates a single color from a colormap.
227  * 
228  * Return value: %TRUE if the allocation succeeded.
229  **/
230 gboolean
231 gdk_colormap_alloc_color (GdkColormap *colormap,
232                           GdkColor    *color,
233                           gboolean     writeable,
234                           gboolean     best_match)
235 {
236   gboolean success;
237
238   gdk_colormap_alloc_colors (colormap, color, 1, writeable, best_match,
239                              &success);
240
241   return success;
242 }
243
244 /**
245  * gdk_color_alloc:
246  * @colormap: a #GdkColormap.
247  * @color: The color to allocate. On return, the 
248  *    <structfield>pixel</structfield> field will be filled in.
249  * 
250  * Allocates a single color from a colormap.
251  * 
252  * Return value: %TRUE if the allocation succeeded.
253  *
254  * Deprecated: 2.2: Use gdk_colormap_alloc_color() instead.
255  **/
256 gboolean
257 gdk_color_alloc (GdkColormap *colormap,
258                  GdkColor    *color)
259 {
260   gboolean success;
261
262   gdk_colormap_alloc_colors (colormap, color, 1, FALSE, TRUE, &success);
263
264   return success;
265 }
266
267 /**
268  * gdk_color_hash:
269  * @colora: a #GdkColor.
270  * 
271  * A hash function suitable for using for a hash
272  * table that stores #GdkColor's.
273  * 
274  * Return value: The hash function applied to @colora
275  **/
276 guint
277 gdk_color_hash (const GdkColor *colora)
278 {
279   return ((colora->red) +
280           (colora->green << 11) +
281           (colora->blue << 22) +
282           (colora->blue >> 6));
283 }
284
285 /**
286  * gdk_color_equal:
287  * @colora: a #GdkColor.
288  * @colorb: another #GdkColor.
289  * 
290  * Compares two colors. 
291  * 
292  * Return value: %TRUE if the two colors compare equal
293  **/
294 gboolean
295 gdk_color_equal (const GdkColor *colora,
296                  const GdkColor *colorb)
297 {
298   g_return_val_if_fail (colora != NULL, FALSE);
299   g_return_val_if_fail (colorb != NULL, FALSE);
300
301   return ((colora->red == colorb->red) &&
302           (colora->green == colorb->green) &&
303           (colora->blue == colorb->blue));
304 }
305
306 GType
307 gdk_color_get_type (void)
308 {
309   static GType our_type = 0;
310   
311   if (our_type == 0)
312     our_type = g_boxed_type_register_static (g_intern_static_string ("GdkColor"),
313                                              (GBoxedCopyFunc)gdk_color_copy,
314                                              (GBoxedFreeFunc)gdk_color_free);
315   return our_type;
316 }
317
318 /**
319  * gdk_color_parse:
320  * @spec: the string specifying the color.
321  * @color: the #GdkColor to fill in
322  * 
323  * Parses a textual specification of a color and fill in the
324  * <structfield>red</structfield>, <structfield>green</structfield>,
325  * and <structfield>blue</structfield> fields of a #GdkColor
326  * structure. The color is <emphasis>not</emphasis> allocated, you
327  * must call gdk_colormap_alloc_color() yourself. The string can
328  * either one of a large set of standard names. (Taken from the X11
329  * <filename>rgb.txt</filename> file), or it can be a hex value in the
330  * form '&num;rgb' '&num;rrggbb' '&num;rrrgggbbb' or
331  * '&num;rrrrggggbbbb' where 'r', 'g' and 'b' are hex digits of the
332  * red, green, and blue components of the color, respectively. (White
333  * in the four forms is '&num;fff' '&num;ffffff' '&num;fffffffff' and
334  * '&num;ffffffffffff')
335  * 
336  * Return value: %TRUE if the parsing succeeded.
337  **/
338 gboolean
339 gdk_color_parse (const gchar *spec,
340                  GdkColor    *color)
341 {
342   PangoColor pango_color;
343
344   if (pango_color_parse (&pango_color, spec))
345     {
346       color->red = pango_color.red;
347       color->green = pango_color.green;
348       color->blue = pango_color.blue;
349
350       return TRUE;
351     }
352   else
353     return FALSE;
354 }
355
356 /**
357  * gdk_color_to_string:
358  * @color: a #GdkColor
359  *
360  * Returns a textual specification of @color in the hexadecimal form
361  * <literal>&num;rrrrggggbbbb</literal>, where <literal>r</literal>,
362  * <literal>g</literal> and <literal>b</literal> are hex digits
363  * representing the red, green and blue components respectively.
364  *
365  * Return value: a newly-allocated text string
366  *
367  * Since: 2.12
368  **/
369 gchar *
370 gdk_color_to_string (const GdkColor *color)
371 {
372   PangoColor pango_color;
373
374   g_return_val_if_fail (color != NULL, NULL);
375
376   pango_color.red = color->red;
377   pango_color.green = color->green;
378   pango_color.blue = color->blue;
379
380   return pango_color_to_string (&pango_color);
381 }
382
383 /**
384  * gdk_colormap_get_system:
385  * 
386  * Gets the system's default colormap for the default screen. (See
387  * gdk_colormap_get_system_for_screen ())
388  * 
389  * Return value: the default colormap.
390  **/
391 GdkColormap*
392 gdk_colormap_get_system (void)
393 {
394   return gdk_screen_get_system_colormap (gdk_screen_get_default ());
395 }
396
397 #define __GDK_COLOR_C__
398 #include "gdkaliasdef.c"