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 */
29 static GdkColormap
*colormap
= NULL
;
32 Color color_black
= { 0.0f
, 0.0f
, 0.0f
};
33 Color color_white
= { 1.0f
, 1.0f
, 1.0f
};
36 GdkColor color_gdk_black
, color_gdk_white
;
38 gboolean _color_initialized
= FALSE
;
40 /** Initialize color access (gdk) and set up default colors.
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.
63 color_new_rgb(float r
, float g
, float b
)
65 Color
*col
= g_new(Color
, 1);
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.
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.");
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.
97 color_equals(Color
*color1
, Color
*color2
)
99 return (color1
->red
== color2
->red
) &&
100 (color1
->green
== color2
->green
) &&
101 (color1
->blue
== color2
->blue
);