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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdkscreen.h"
30 #include "gdkinternals.h"
36 * @Short_description: Manipulation of colors
39 * A #GdkColor represents a color.
41 * When working with cairo, it is often more convenient
42 * to use a #GdkRGBA instead.
50 * Makes a copy of a color structure.
52 * The result must be freed using gdk_color_free().
54 * Return value: a copy of @color
57 gdk_color_copy (const GdkColor *color)
61 g_return_val_if_fail (color != NULL, NULL);
63 new_color = g_slice_new (GdkColor);
72 * Frees a color structure created with gdk_color_copy().
75 gdk_color_free (GdkColor *color)
77 g_return_if_fail (color != NULL);
79 g_slice_free (GdkColor, color);
86 * A hash function suitable for using for a hash
87 * table that stores #GdkColors.
89 * Return value: The hash function applied to @color
92 gdk_color_hash (const GdkColor *color)
94 return ((color->red) +
95 (color->green << 11) +
102 * @colora: a #GdkColor
103 * @colorb: another #GdkColor
105 * Compares two colors.
107 * Return value: %TRUE if the two colors compare equal
110 gdk_color_equal (const GdkColor *colora,
111 const GdkColor *colorb)
113 g_return_val_if_fail (colora != NULL, FALSE);
114 g_return_val_if_fail (colorb != NULL, FALSE);
116 return ((colora->red == colorb->red) &&
117 (colora->green == colorb->green) &&
118 (colora->blue == colorb->blue));
121 G_DEFINE_BOXED_TYPE (GdkColor, gdk_color,
127 * @spec: the string specifying the color
128 * @color: (out): the #GdkColor to fill in
130 * Parses a textual specification of a color and fill in the
131 * <structfield>red</structfield>, <structfield>green</structfield>,
132 * and <structfield>blue</structfield> fields of a #GdkColor
135 * The string can either one of a large set of standard names
136 * (taken from the X11 <filename>rgb.txt</filename> file), or
137 * it can be a hex value in the form '#rgb' '#rrggbb'
138 * '#rrrgggbbb' or '#rrrrggggbbbb' where 'r', 'g' and
139 * 'b' are hex digits of the red, green, and blue components
140 * of the color, respectively. (White in the four forms is
141 * '#fff', '#ffffff', '#fffffffff' and
142 * '#ffffffffffff').
144 * Return value: %TRUE if the parsing succeeded
147 gdk_color_parse (const gchar *spec,
150 PangoColor pango_color;
152 if (pango_color_parse (&pango_color, spec))
154 color->red = pango_color.red;
155 color->green = pango_color.green;
156 color->blue = pango_color.blue;
165 * gdk_color_to_string:
166 * @color: a #GdkColor
168 * Returns a textual specification of @color in the hexadecimal form
169 * <literal>#rrrrggggbbbb</literal>, where <literal>r</literal>,
170 * <literal>g</literal> and <literal>b</literal> are hex digits
171 * representing the red, green and blue components respectively.
173 * The returned string can be parsed by gdk_color_parse().
175 * Return value: a newly-allocated text string
180 gdk_color_to_string (const GdkColor *color)
182 PangoColor pango_color;
184 g_return_val_if_fail (color != NULL, NULL);
186 pango_color.red = color->red;
187 pango_color.green = color->green;
188 pango_color.blue = color->blue;
190 return pango_color_to_string (&pango_color);