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