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)
66 GdkColor *gdk_color = NULL;
68 if ( GL_COLOR_F_ALPHA (color) )
70 gdk_color = g_new0 (GdkColor, 1);
72 gdk_color->red = GL_COLOR_F_RED (color) * 65535;
73 gdk_color->green = GL_COLOR_F_GREEN (color) * 65535;
74 gdk_color->blue = GL_COLOR_F_BLUE (color) * 65535;
80 /*****************************************************************************/
81 /* Convert GdkColor into a gLabels color */
82 /*****************************************************************************/
84 gl_color_from_gdk_color (GdkColor *gdk_color)
86 guint color = GL_COLOR_NONE;
90 color = GL_COLOR ((gdk_color->red >>8),
91 (gdk_color->green >>8),
92 (gdk_color->blue >>8));
98 /****************************************************************************/
99 /* Create a single color node with default color. */
100 /****************************************************************************/
102 gl_color_node_new_default (void)
104 glColorNode* color_node;
106 color_node = g_new0(glColorNode,1);
108 color_node->field_flag = FALSE;
109 color_node->color = GL_COLOR_NONE;
110 color_node->key = NULL;
115 /****************************************************************************/
116 /* Copy a single color node. */
117 /****************************************************************************/
119 gl_color_node_dup (glColorNode *src)
123 if ( src == NULL ) return NULL;
125 dst = g_new0 (glColorNode, 1);
127 dst->field_flag = src->field_flag;
128 if (src->key != NULL)
130 dst->key = g_strdup (src->key);
136 dst->color = src->color;
141 /****************************************************************************/
142 /* Compare 2 color nodes for equality. */
143 /****************************************************************************/
145 gl_color_node_equal (glColorNode *color_node1,
146 glColorNode *color_node2)
148 /* First take care of the case of either or both being NULL. */
149 if ( color_node1 == NULL )
151 return ( color_node2 == NULL );
155 if ( color_node2 == NULL )
161 /* Bail if field flags differ. */
162 if ( color_node1->field_flag != color_node2->field_flag )
167 /* Now take care of the case of either or both color fields being different. */
168 if ( color_node1->color != color_node2->color )
173 /* Then take care of the case of either or both key fields being NULL. */
174 if ( color_node1->key == NULL )
176 return ( color_node2->key == NULL );
180 if ( color_node2->key == NULL )
186 /* Field flags are identical, so now compare the keys. */
187 return (strcmp (color_node1->key, color_node2->key) == 0);
190 /****************************************************************************/
191 /* Expand single node into representative color. */
192 /****************************************************************************/
194 gl_color_node_expand (glColorNode *color_node,
195 glMergeRecord *record)
201 if (color_node->field_flag)
205 return GL_COLOR_NONE;
209 text = gl_merge_eval_key (record, color_node->key);
212 gdk_color = g_new0 (GdkColor, 1);
213 if (gdk_color_parse (text, gdk_color))
215 color = gl_color_from_gdk_color (gdk_color);
222 return GL_COLOR_NONE;
227 return GL_COLOR_NONE;
233 return color_node->color;
237 /****************************************************************************/
238 /* Free a single color node. */
239 /****************************************************************************/
241 gl_color_node_free (glColorNode **color_node)
243 if ( *color_node == NULL ) return;
245 g_free ((*color_node)->key);
246 (*color_node)->key = NULL;
247 g_free (*color_node);