Signal patch
[dia.git] / lib / color.c
blob54ede96d1595dc638b86159b52b27468930f8904
1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #include <config.h>
20 #include <stdio.h>
21 #include <gtk/gtk.h>
22 #include <gdk/gdk.h>
24 #include "color.h"
26 static GdkColormap *colormap = NULL;
28 #ifndef G_OS_WIN32
29 Color color_black = { 0.0f, 0.0f, 0.0f };
30 Color color_white = { 1.0f, 1.0f, 1.0f };
31 #endif
33 GdkColor color_gdk_black, color_gdk_white;
35 gboolean _color_initialized = FALSE;
37 /** Initialize color access (gdk) and set up default colors.
39 void
40 color_init(void)
42 if (!_color_initialized) {
43 GdkVisual *visual = gtk_widget_get_default_visual();
44 colormap = gdk_colormap_new (visual, FALSE);
46 _color_initialized = TRUE;
48 color_convert(&color_black, &color_gdk_black);
49 color_convert(&color_white, &color_gdk_white);
53 /** Allocate a new color object wtih the given values.
54 * @param r Red component (0 <= r <= 1)
55 * @param g Green component (0 <= g <= 1)
56 * @param b Blue component (0 <= b <= 1)
57 * @returns A newly allocated color object. This should be freed after use.
59 Color *
60 color_new_rgb(float r, float g, float b)
62 Color *col = g_new(Color, 1);
63 col->red = r;
64 col->green = g;
65 col->blue = b;
66 return col;
69 /** Convert a Dia color object to GDK style, including handling allocation.
70 * @param color A color object. This will not be kept by this function.
71 * @param gdkcolor Return value: GDK color object to fill in.
73 void
74 color_convert(Color *color, GdkColor *gdkcolor)
76 gdkcolor->red = color->red*65535;
77 gdkcolor->green = color->green*65535;
78 gdkcolor->blue = color->blue*65535;
80 if (_color_initialized) {
81 if (!gdk_colormap_alloc_color (colormap, gdkcolor, TRUE, TRUE))
82 g_warning ("color_convert failed.");
83 } else {
84 g_warning("Can't color_convert in non-interactive app (w/o color_init())");
88 /** Compare two color objects.
89 * @param color1 One color object
90 * @param color2 Another color object.
91 * @returns TRUE if the color objects are the same color.
93 gboolean
94 color_equals(Color *color1, Color *color2)
96 return (color1->red == color2->red) &&
97 (color1->green == color2->green) &&
98 (color1->blue == color2->blue);