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