1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
31 #include "gdkscreen.h"
32 #include "gdkinternals.h"
38 * @Short_description: Manipulation of colors
41 * A #GdkColor represents a color.
43 * When working with cairo, it is often more convenient
44 * to use a #GdkRGBA instead.
52 * Makes a copy of a color structure.
54 * The result must be freed using gdk_color_free().
56 * Return value: a copy of @color
59 gdk_color_copy (const GdkColor *color)
63 g_return_val_if_fail (color != NULL, NULL);
65 new_color = g_slice_new (GdkColor);
74 * Frees a color structure created with gdk_color_copy().
77 gdk_color_free (GdkColor *color)
79 g_return_if_fail (color != NULL);
81 g_slice_free (GdkColor, color);
88 * A hash function suitable for using for a hash
89 * table that stores #GdkColors.
91 * Return value: The hash function applied to @color
94 gdk_color_hash (const GdkColor *color)
96 return ((color->red) +
97 (color->green << 11) +
104 * @colora: a #GdkColor
105 * @colorb: another #GdkColor
107 * Compares two colors.
109 * Return value: %TRUE if the two colors compare equal
112 gdk_color_equal (const GdkColor *colora,
113 const GdkColor *colorb)
115 g_return_val_if_fail (colora != NULL, FALSE);
116 g_return_val_if_fail (colorb != NULL, FALSE);
118 return ((colora->red == colorb->red) &&
119 (colora->green == colorb->green) &&
120 (colora->blue == colorb->blue));
123 G_DEFINE_BOXED_TYPE (GdkColor, gdk_color,
129 * @spec: the string specifying the color
130 * @color: (out): the #GdkColor to fill in
132 * Parses a textual specification of a color and fill in the
133 * <structfield>red</structfield>, <structfield>green</structfield>,
134 * and <structfield>blue</structfield> fields of a #GdkColor
137 * The string can either one of a large set of standard names
138 * (taken from the X11 <filename>rgb.txt</filename> file), or
139 * it can be a hex value in the form '#rgb' '#rrggbb'
140 * '#rrrgggbbb' or '#rrrrggggbbbb' where 'r', 'g' and
141 * 'b' are hex digits of the red, green, and blue components
142 * of the color, respectively. (White in the four forms is
143 * '#fff', '#ffffff', '#fffffffff' and
144 * '#ffffffffffff').
146 * Return value: %TRUE if the parsing succeeded
149 gdk_color_parse (const gchar *spec,
152 PangoColor pango_color;
154 if (pango_color_parse (&pango_color, spec))
156 color->red = pango_color.red;
157 color->green = pango_color.green;
158 color->blue = pango_color.blue;
167 * gdk_color_to_string:
168 * @color: a #GdkColor
170 * Returns a textual specification of @color in the hexadecimal form
171 * <literal>#rrrrggggbbbb</literal>, where <literal>r</literal>,
172 * <literal>g</literal> and <literal>b</literal> are hex digits
173 * representing the red, green and blue components respectively.
175 * The returned string can be parsed by gdk_color_parse().
177 * Return value: a newly-allocated text string
182 gdk_color_to_string (const GdkColor *color)
184 PangoColor pango_color;
186 g_return_val_if_fail (color != NULL, NULL);
188 pango_color.red = color->red;
189 pango_color.green = color->green;
190 pango_color.blue = color->blue;
192 return pango_color_to_string (&pango_color);