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