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