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