]> Pileus Git - ~andy/gtk/blob - gdk/gdkcolor.c
Merge branch 'master' into broadway2
[~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
29 #include "gdkcolor.h"
30
31 #include "gdkscreen.h"
32 #include "gdkinternals.h"
33
34 #include <time.h>
35
36 /**
37  * SECTION:colors
38  * @Short_description: Manipulation of colors
39  * @Title: Colors
40  */
41
42
43 /**
44  * gdk_color_copy:
45  * @color: a #GdkColor.
46  * 
47  * Makes a copy of a color structure. The result
48  * must be freed using gdk_color_free().
49  * 
50  * Return value: a copy of @color.
51  **/
52 GdkColor*
53 gdk_color_copy (const GdkColor *color)
54 {
55   GdkColor *new_color;
56   
57   g_return_val_if_fail (color != NULL, NULL);
58
59   new_color = g_slice_new (GdkColor);
60   *new_color = *color;
61   return new_color;
62 }
63
64 /**
65  * gdk_color_free:
66  * @color: a #GdkColor.
67  * 
68  * Frees a color structure created with 
69  * gdk_color_copy().
70  **/
71 void
72 gdk_color_free (GdkColor *color)
73 {
74   g_return_if_fail (color != NULL);
75
76   g_slice_free (GdkColor, color);
77 }
78
79 /**
80  * gdk_color_hash:
81  * @colora: a #GdkColor.
82  * 
83  * A hash function suitable for using for a hash
84  * table that stores #GdkColor's.
85  * 
86  * Return value: The hash function applied to @colora
87  **/
88 guint
89 gdk_color_hash (const GdkColor *colora)
90 {
91   return ((colora->red) +
92           (colora->green << 11) +
93           (colora->blue << 22) +
94           (colora->blue >> 6));
95 }
96
97 /**
98  * gdk_color_equal:
99  * @colora: a #GdkColor.
100  * @colorb: another #GdkColor.
101  * 
102  * Compares two colors. 
103  * 
104  * Return value: %TRUE if the two colors compare equal
105  **/
106 gboolean
107 gdk_color_equal (const GdkColor *colora,
108                  const GdkColor *colorb)
109 {
110   g_return_val_if_fail (colora != NULL, FALSE);
111   g_return_val_if_fail (colorb != NULL, FALSE);
112
113   return ((colora->red == colorb->red) &&
114           (colora->green == colorb->green) &&
115           (colora->blue == colorb->blue));
116 }
117
118 G_DEFINE_BOXED_TYPE (GdkColor, gdk_color,
119                      gdk_color_copy,
120                      gdk_color_free)
121
122 /**
123  * gdk_color_parse:
124  * @spec: the string specifying the color.
125  * @color: (out): the #GdkColor to fill in
126  *
127  * Parses a textual specification of a color and fill in the
128  * <structfield>red</structfield>, <structfield>green</structfield>,
129  * and <structfield>blue</structfield> fields of a #GdkColor
130  * structure. The string can either one of a large set of standard
131  * names. (Taken from the X11 <filename>rgb.txt</filename> file), or
132  * it can be a hex value in the form '&num;rgb' '&num;rrggbb'
133  * '&num;rrrgggbbb' or '&num;rrrrggggbbbb' where 'r', 'g' and 'b' are
134  * hex digits of the red, green, and blue components of the color,
135  * respectively. (White in the four forms is '&num;fff' '&num;ffffff'
136  * '&num;fffffffff' and '&num;ffffffffffff')
137  * 
138  * Return value: %TRUE if the parsing succeeded.
139  **/
140 gboolean
141 gdk_color_parse (const gchar *spec,
142                  GdkColor    *color)
143 {
144   PangoColor pango_color;
145
146   if (pango_color_parse (&pango_color, spec))
147     {
148       color->red = pango_color.red;
149       color->green = pango_color.green;
150       color->blue = pango_color.blue;
151
152       return TRUE;
153     }
154   else
155     return FALSE;
156 }
157
158 /**
159  * gdk_color_to_string:
160  * @color: a #GdkColor
161  *
162  * Returns a textual specification of @color in the hexadecimal form
163  * <literal>&num;rrrrggggbbbb</literal>, where <literal>r</literal>,
164  * <literal>g</literal> and <literal>b</literal> are hex digits
165  * representing the red, green and blue components respectively.
166  *
167  * Return value: a newly-allocated text string
168  *
169  * Since: 2.12
170  **/
171 gchar *
172 gdk_color_to_string (const GdkColor *color)
173 {
174   PangoColor pango_color;
175
176   g_return_val_if_fail (color != NULL, NULL);
177
178   pango_color.red = color->red;
179   pango_color.green = color->green;
180   pango_color.blue = color->blue;
181
182   return pango_color_to_string (&pango_color);
183 }