]> Pileus Git - ~andy/gtk/blob - gdk/gdkcolor.c
Changes multihead reorganizing code for win32 support, mostly from a patch
[~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 <time.h>
28
29 #include "gdkscreen.h"
30 #include "gdkcolor.h"
31 #include "gdkinternals.h"
32
33 /**
34  * gdk_colormap_ref:
35  * @cmap: a #GdkColormap
36  *
37  * Deprecated function; use g_object_ref() instead.
38  *
39  * Return value: the colormap
40  **/
41 GdkColormap*
42 gdk_colormap_ref (GdkColormap *cmap)
43 {
44   return (GdkColormap *) g_object_ref (G_OBJECT (cmap));
45 }
46
47 /**
48  * gdk_colormap_unref:
49  * @cmap: a #GdkColormap
50  *
51  * Deprecated function; use g_object_ref() instead.
52  **/
53 void
54 gdk_colormap_unref (GdkColormap *cmap)
55 {
56   g_object_unref (G_OBJECT (cmap));
57 }
58
59 GdkVisual *
60 gdk_colormap_get_visual (GdkColormap *colormap)
61 {
62   g_return_val_if_fail (GDK_IS_COLORMAP (colormap), NULL);
63
64   return colormap->visual;
65 }
66      
67 void
68 gdk_colors_store (GdkColormap   *colormap,
69                   GdkColor      *colors,
70                   gint           ncolors)
71 {
72   gint i;
73
74   for (i = 0; i < ncolors; i++)
75     {
76       colormap->colors[i].pixel = colors[i].pixel;
77       colormap->colors[i].red = colors[i].red;
78       colormap->colors[i].green = colors[i].green;
79       colormap->colors[i].blue = colors[i].blue;
80     }
81
82   gdk_colormap_change (colormap, ncolors);
83 }
84
85 /*
86  *--------------------------------------------------------------
87  * gdk_color_copy
88  *
89  *   Copy a color structure into new storage.
90  *
91  * Arguments:
92  *   "color" is the color struct to copy.
93  *
94  * Results:
95  *   A new color structure.  Free it with gdk_color_free.
96  *
97  *--------------------------------------------------------------
98  */
99
100 static GMemChunk *color_chunk;
101
102 GdkColor*
103 gdk_color_copy (const GdkColor *color)
104 {
105   GdkColor *new_color;
106   
107   g_return_val_if_fail (color != NULL, NULL);
108
109   if (color_chunk == NULL)
110     color_chunk = g_mem_chunk_new ("colors",
111                                    sizeof (GdkColor),
112                                    4096,
113                                    G_ALLOC_AND_FREE);
114
115   new_color = g_chunk_new (GdkColor, color_chunk);
116   *new_color = *color;
117   return new_color;
118 }
119
120 /*
121  *--------------------------------------------------------------
122  * gdk_color_free
123  *
124  *   Free a color structure obtained from gdk_color_copy.  Do not use
125  *   with other color structures.
126  *
127  * Arguments:
128  *   "color" is the color struct to free.
129  *
130  *-------------------------------------------------------------- */
131
132 void
133 gdk_color_free (GdkColor *color)
134 {
135   g_assert (color_chunk != NULL);
136   g_return_if_fail (color != NULL);
137
138   g_mem_chunk_free (color_chunk, color);
139 }
140
141 gboolean
142 gdk_color_white (GdkColormap *colormap,
143                  GdkColor    *color)
144 {
145   gint return_val;
146
147   g_return_val_if_fail (colormap != NULL, FALSE);
148
149   if (color)
150     {
151       color->red = 65535;
152       color->green = 65535;
153       color->blue = 65535;
154
155       return_val = gdk_color_alloc (colormap, color);
156     }
157   else
158     return_val = FALSE;
159
160   return return_val;
161 }
162
163 gboolean
164 gdk_color_black (GdkColormap *colormap,
165                  GdkColor    *color)
166 {
167   gint return_val;
168
169   g_return_val_if_fail (colormap != NULL, FALSE);
170
171   if (color)
172     {
173       color->red = 0;
174       color->green = 0;
175       color->blue = 0;
176
177       return_val = gdk_color_alloc (colormap, color);
178     }
179   else
180     return_val = FALSE;
181
182   return return_val;
183 }
184
185 /********************
186  * Color allocation *
187  ********************/
188
189 gboolean
190 gdk_colormap_alloc_color (GdkColormap *colormap,
191                           GdkColor    *color,
192                           gboolean     writeable,
193                           gboolean     best_match)
194 {
195   gboolean success;
196
197   gdk_colormap_alloc_colors (colormap, color, 1, writeable, best_match,
198                              &success);
199
200   return success;
201 }
202
203 gboolean
204 gdk_color_alloc (GdkColormap *colormap,
205                  GdkColor    *color)
206 {
207   gboolean success;
208
209   gdk_colormap_alloc_colors (colormap, color, 1, FALSE, TRUE, &success);
210
211   return success;
212 }
213
214 guint
215 gdk_color_hash (const GdkColor *colora)
216 {
217   return ((colora->red) +
218           (colora->green << 11) +
219           (colora->blue << 22) +
220           (colora->blue >> 6));
221 }
222
223 gboolean
224 gdk_color_equal (const GdkColor *colora,
225                  const GdkColor *colorb)
226 {
227   g_return_val_if_fail (colora != NULL, FALSE);
228   g_return_val_if_fail (colorb != NULL, FALSE);
229
230   return ((colora->red == colorb->red) &&
231           (colora->green == colorb->green) &&
232           (colora->blue == colorb->blue));
233 }
234
235 GType
236 gdk_color_get_type (void)
237 {
238   static GType our_type = 0;
239   
240   if (our_type == 0)
241     our_type = g_boxed_type_register_static ("GdkColor",
242                                              (GBoxedCopyFunc)gdk_color_copy,
243                                              (GBoxedFreeFunc)gdk_color_free);
244   return our_type;
245 }
246
247 gboolean
248 gdk_color_parse (const gchar *spec,
249                  GdkColor    *color)
250 {
251   PangoColor pango_color;
252
253   if (pango_color_parse (&pango_color, spec))
254     {
255       color->red = pango_color.red;
256       color->green = pango_color.green;
257       color->blue = pango_color.blue;
258
259       return TRUE;
260     }
261   else
262     return FALSE;
263 }
264
265 /**
266  * gdk_colormap_get_system:
267  * 
268  * Gets the system's default colormap for the default screen. (See
269  * gdk_colormap_get_system_for_screen ())
270  * 
271  * Return value: the default colormap.
272  **/
273 GdkColormap*
274 gdk_colormap_get_system (void)
275 {
276   return gdk_screen_get_system_colormap (gdk_get_default_screen ());
277 }
278