]> Pileus Git - ~andy/gtk/blob - gdk/gdkcolor.c
Use G_DEFINE_BOXED_TYPE
[~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
34
35 /**
36  * gdk_colormap_get_visual:
37  * @colormap: a #GdkColormap.
38  * 
39  * Returns the visual for which a given colormap was created.
40  * 
41  * Return value: the visual of the colormap.
42  **/
43 GdkVisual *
44 gdk_colormap_get_visual (GdkColormap *colormap)
45 {
46   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), NULL);
47
48   return colormap->visual;
49 }
50
51 /**
52  * gdk_color_copy:
53  * @color: a #GdkColor.
54  * 
55  * Makes a copy of a color structure. The result
56  * must be freed using gdk_color_free().
57  * 
58  * Return value: a copy of @color.
59  **/
60 GdkColor*
61 gdk_color_copy (const GdkColor *color)
62 {
63   GdkColor *new_color;
64   
65   g_return_val_if_fail (color != NULL, NULL);
66
67   new_color = g_slice_new (GdkColor);
68   *new_color = *color;
69   return new_color;
70 }
71
72 /**
73  * gdk_color_free:
74  * @color: a #GdkColor.
75  * 
76  * Frees a color structure created with 
77  * gdk_color_copy().
78  **/
79 void
80 gdk_color_free (GdkColor *color)
81 {
82   g_return_if_fail (color != NULL);
83
84   g_slice_free (GdkColor, color);
85 }
86
87 /********************
88  * Color allocation *
89  ********************/
90
91 /**
92  * gdk_colormap_alloc_color:
93  * @colormap: a #GdkColormap.
94  * @color: the color to allocate. On return the
95  *    <structfield>pixel</structfield> field will be
96  *    filled in if allocation succeeds.
97  * @writeable: this parameter has no effect, and it's here for mere
98  *   compatibility.
99  * @best_match: If %TRUE, GDK will attempt to do matching against
100  *    existing colors if the color cannot be allocated as requested.
101  *
102  * Allocates a single color from a colormap.
103  *
104  * Return value: %TRUE if the allocation succeeded.
105  **/
106 gboolean
107 gdk_colormap_alloc_color (GdkColormap *colormap,
108                           GdkColor    *color,
109                           gboolean     writeable,
110                           gboolean     best_match)
111 {
112   gboolean success;
113
114   gdk_colormap_alloc_colors (colormap, color, 1, writeable, best_match,
115                              &success);
116
117   return success;
118 }
119
120 /**
121  * gdk_color_hash:
122  * @colora: a #GdkColor.
123  * 
124  * A hash function suitable for using for a hash
125  * table that stores #GdkColor's.
126  * 
127  * Return value: The hash function applied to @colora
128  **/
129 guint
130 gdk_color_hash (const GdkColor *colora)
131 {
132   return ((colora->red) +
133           (colora->green << 11) +
134           (colora->blue << 22) +
135           (colora->blue >> 6));
136 }
137
138 /**
139  * gdk_color_equal:
140  * @colora: a #GdkColor.
141  * @colorb: another #GdkColor.
142  * 
143  * Compares two colors. 
144  * 
145  * Return value: %TRUE if the two colors compare equal
146  **/
147 gboolean
148 gdk_color_equal (const GdkColor *colora,
149                  const GdkColor *colorb)
150 {
151   g_return_val_if_fail (colora != NULL, FALSE);
152   g_return_val_if_fail (colorb != NULL, FALSE);
153
154   return ((colora->red == colorb->red) &&
155           (colora->green == colorb->green) &&
156           (colora->blue == colorb->blue));
157 }
158
159 G_DEFINE_BOXED_TYPE (GdkColor, gdk_color,
160                      gdk_color_copy,
161                      gdk_color_free)
162
163 /**
164  * gdk_color_parse:
165  * @spec: the string specifying the color.
166  * @color: (out): the #GdkColor to fill in
167  *
168  * Parses a textual specification of a color and fill in the
169  * <structfield>red</structfield>, <structfield>green</structfield>,
170  * and <structfield>blue</structfield> fields of a #GdkColor
171  * structure. The color is <emphasis>not</emphasis> allocated, you
172  * must call gdk_colormap_alloc_color() yourself. The string can
173  * either one of a large set of standard names. (Taken from the X11
174  * <filename>rgb.txt</filename> file), or it can be a hex value in the
175  * form '&num;rgb' '&num;rrggbb' '&num;rrrgggbbb' or
176  * '&num;rrrrggggbbbb' where 'r', 'g' and 'b' are hex digits of the
177  * red, green, and blue components of the color, respectively. (White
178  * in the four forms is '&num;fff' '&num;ffffff' '&num;fffffffff' and
179  * '&num;ffffffffffff')
180  * 
181  * Return value: %TRUE if the parsing succeeded.
182  **/
183 gboolean
184 gdk_color_parse (const gchar *spec,
185                  GdkColor    *color)
186 {
187   PangoColor pango_color;
188
189   if (pango_color_parse (&pango_color, spec))
190     {
191       color->red = pango_color.red;
192       color->green = pango_color.green;
193       color->blue = pango_color.blue;
194
195       return TRUE;
196     }
197   else
198     return FALSE;
199 }
200
201 /**
202  * gdk_color_to_string:
203  * @color: a #GdkColor
204  *
205  * Returns a textual specification of @color in the hexadecimal form
206  * <literal>&num;rrrrggggbbbb</literal>, where <literal>r</literal>,
207  * <literal>g</literal> and <literal>b</literal> are hex digits
208  * representing the red, green and blue components respectively.
209  *
210  * Return value: a newly-allocated text string
211  *
212  * Since: 2.12
213  **/
214 gchar *
215 gdk_color_to_string (const GdkColor *color)
216 {
217   PangoColor pango_color;
218
219   g_return_val_if_fail (color != NULL, NULL);
220
221   pango_color.red = color->red;
222   pango_color.green = color->green;
223   pango_color.blue = color->blue;
224
225   return pango_color_to_string (&pango_color);
226 }
227
228 /**
229  * gdk_colormap_get_system:
230  * 
231  * Gets the system's default colormap for the default screen. (See
232  * gdk_colormap_get_system_for_screen ())
233  * 
234  * Return value: the default colormap.
235  **/
236 GdkColormap*
237 gdk_colormap_get_system (void)
238 {
239   return gdk_screen_get_system_colormap (gdk_screen_get_default ());
240 }