1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * color.c: various small utilities for dealing with canvas colors
8 * Copyright (C) 2002-2007 Jim Evins <evins@snaught.com>.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 /*****************************************************************************/
31 /* Apply given opacity to given color. */
32 /*****************************************************************************/
34 gl_color_set_opacity (guint color,
39 new_color = (color & 0xFFFFFF00) | (((guint)(255.0*opacity)) & 0xFF);
44 /*****************************************************************************/
45 /* Resolve actual shadow color by adjusting opacity. */
46 /*****************************************************************************/
48 gl_color_shadow (guint base_color,
54 color = gl_color_set_opacity (base_color,
55 opacity * GL_COLOR_F_ALPHA (object_color));
60 /*****************************************************************************/
61 /* Convert gLabels color into a GdkColor */
62 /*****************************************************************************/
64 gl_color_to_gdk_color (guint color)
68 gdk_color = g_new0 (GdkColor, 1);
70 gdk_color->red = GL_COLOR_F_RED (color) * 65535;
71 gdk_color->green = GL_COLOR_F_GREEN (color) * 65535;
72 gdk_color->blue = GL_COLOR_F_BLUE (color) * 65535;
77 /*****************************************************************************/
78 /* Convert GdkColor into a gLabels color */
79 /*****************************************************************************/
81 gl_color_from_gdk_color (GdkColor *gdk_color)
85 color = GL_COLOR ((gdk_color->red >>8),
86 (gdk_color->green >>8),
87 (gdk_color->blue >>8));
92 /****************************************************************************/
93 /* Create a single color node with default color. */
94 /****************************************************************************/
96 gl_color_node_new_default (void)
98 glColorNode* color_node;
100 color_node = g_new0(glColorNode,1);
102 color_node->field_flag = FALSE;
103 color_node->color = GL_COLOR_NONE;
104 color_node->key = NULL;
109 /****************************************************************************/
110 /* Copy a single color node. */
111 /****************************************************************************/
113 gl_color_node_dup (glColorNode *src)
117 if ( src == NULL ) return NULL;
119 dst = g_new0 (glColorNode, 1);
121 dst->field_flag = src->field_flag;
122 if (src->key != NULL)
124 dst->key = g_strdup (src->key);
130 dst->color = src->color;
135 /****************************************************************************/
136 /* Compare 2 color nodes for equality. */
137 /****************************************************************************/
139 gl_color_node_equal (glColorNode *color_node1,
140 glColorNode *color_node2)
142 /* First take care of the case of either or both being NULL. */
143 if ( color_node1 == NULL )
145 return ( color_node2 == NULL );
149 if ( color_node2 == NULL )
155 /* Bail if field flags differ. */
156 if ( color_node1->field_flag != color_node2->field_flag )
161 /* Now take care of the case of either or both color fields being different. */
162 if ( color_node1->color != color_node2->color )
167 /* Then take care of the case of either or both key fields being NULL. */
168 if ( color_node1->key == NULL )
170 return ( color_node2->key == NULL );
174 if ( color_node2->key == NULL )
180 /* Field flags are identical, so now compare the keys. */
181 return (strcmp (color_node1->key, color_node2->key) == 0);
184 /****************************************************************************/
185 /* Expand single node into representative color. */
186 /****************************************************************************/
188 gl_color_node_expand (glColorNode *color_node,
189 glMergeRecord *record)
195 if (color_node->field_flag)
199 return GL_COLOR_NONE;
203 text = gl_merge_eval_key (record, color_node->key);
206 gdk_color = g_new0 (GdkColor, 1);
207 if (gdk_color_parse (text, gdk_color))
209 color = gl_color_from_gdk_color (gdk_color);
216 return GL_COLOR_NONE;
221 return GL_COLOR_NONE;
227 return color_node->color;
231 /****************************************************************************/
232 /* Free a single color node. */
233 /****************************************************************************/
235 gl_color_node_free (glColorNode **color_node)
237 if ( *color_node == NULL ) return;
239 g_free ((*color_node)->key);
240 (*color_node)->key = NULL;
241 g_free (*color_node);