2006-12-03 Dimitris Glezos <dimitris@glezos.com>
[dia.git] / lib / color.c
blob838a219837411011cb97d6c1f78864b433ca6256
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.
19 /** \file color.c Diagram and object tinting */
21 #include <config.h>
23 #include <stdio.h>
24 #include <gtk/gtk.h>
25 #include <gdk/gdk.h>
27 #include "color.h"
29 static GdkColormap *colormap = NULL;
31 #ifndef G_OS_WIN32
32 Color color_black = { 0.0f, 0.0f, 0.0f };
33 Color color_white = { 1.0f, 1.0f, 1.0f };
34 #endif
36 GdkColor color_gdk_black, color_gdk_white;
38 gboolean _color_initialized = FALSE;
40 /** Initialize color access (gdk) and set up default colors.
42 void
43 color_init(void)
45 if (!_color_initialized) {
46 GdkVisual *visual = gtk_widget_get_default_visual();
47 colormap = gdk_colormap_new (visual, FALSE);
49 _color_initialized = TRUE;
51 color_convert(&color_black, &color_gdk_black);
52 color_convert(&color_white, &color_gdk_white);
56 /** Allocate a new color object wtih the given values.
57 * @param r Red component (0 <= r <= 1)
58 * @param g Green component (0 <= g <= 1)
59 * @param b Blue component (0 <= b <= 1)
60 * @returns A newly allocated color object. This should be freed after use.
62 Color *
63 color_new_rgb(float r, float g, float b)
65 Color *col = g_new(Color, 1);
66 col->red = r;
67 col->green = g;
68 col->blue = b;
69 return col;
72 /** Convert a Dia color object to GDK style, including handling allocation.
73 * @param color A color object. This will not be kept by this function.
74 * @param gdkcolor Return value: GDK color object to fill in.
76 void
77 color_convert(Color *color, GdkColor *gdkcolor)
79 gdkcolor->red = (guint16)(color->red*65535);
80 gdkcolor->green = (guint16)(color->green*65535);
81 gdkcolor->blue = (guint16)(color->blue*65535);
83 if (_color_initialized) {
84 if (!gdk_colormap_alloc_color (colormap, gdkcolor, TRUE, TRUE))
85 g_warning ("color_convert failed.");
86 } else {
87 g_warning("Can't color_convert in non-interactive app (w/o color_init())");
91 /** Compare two color objects.
92 * @param color1 One color object
93 * @param color2 Another color object.
94 * @returns TRUE if the color objects are the same color.
96 gboolean
97 color_equals(Color *color1, Color *color2)
99 return (color1->red == color2->red) &&
100 (color1->green == color2->green) &&
101 (color1->blue == color2->blue);