]> git.sur5r.net Git - glabels/blob - glabels2/src/color.c
e8c039dc7078c380ef3ebb62151cdab599fd6e63
[glabels] / glabels2 / src / color.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /*
4  *  (GLABELS) Label and Business Card Creation program for GNOME
5  *
6  *  color.c:  various small utilities for dealing with canvas colors
7  *
8  *  Copyright (C) 2002-2007  Jim Evins <evins@snaught.com>.
9  *
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.
14  *
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.
19  *
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
23  */
24 #include <config.h>
25
26 #include "color.h"
27
28 #include <string.h>
29
30 /*****************************************************************************/
31 /* Apply given opacity to given color.                                       */
32 /*****************************************************************************/
33 guint
34 gl_color_set_opacity (guint   color,
35                       gdouble opacity)
36 {
37         guint new_color;
38
39         new_color = (color & 0xFFFFFF00) | (((guint)(255.0*opacity)) & 0xFF);
40
41         return new_color;
42 }
43
44 /*****************************************************************************/
45 /* Resolve actual shadow color by adjusting opacity.                         */
46 /*****************************************************************************/
47 guint
48 gl_color_shadow (guint   base_color,
49                  gdouble opacity,
50                  guint   object_color)
51 {
52         guint color;
53
54         color = gl_color_set_opacity (base_color,
55                                       opacity * GL_COLOR_F_ALPHA (object_color));
56
57         return color;
58 }
59
60 /*****************************************************************************/
61 /* Convert gLabels color into a GdkColor                                     */
62 /*****************************************************************************/
63 GdkColor *
64 gl_color_to_gdk_color (guint color)
65 {
66         GdkColor *gdk_color;
67
68         gdk_color = g_new0 (GdkColor, 1);
69
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;
73
74         return gdk_color;
75 }
76
77 /*****************************************************************************/
78 /* Convert GdkColor into a gLabels color                                     */
79 /*****************************************************************************/
80 guint
81 gl_color_from_gdk_color (GdkColor *gdk_color)
82 {
83         guint color;
84
85         color = GL_COLOR ((gdk_color->red   >>8),
86                           (gdk_color->green >>8),
87                           (gdk_color->blue  >>8));
88
89         return color;
90 }
91
92 /****************************************************************************/
93 /* Create a single color node with default color.                           */
94 /****************************************************************************/
95 glColorNode *
96 gl_color_node_new_default (void)
97 {
98         glColorNode* color_node;
99         
100         color_node = g_new0(glColorNode,1);
101         
102         color_node->field_flag = FALSE;
103         color_node->color = GL_COLOR_NONE;
104         color_node->key = NULL;
105
106         return color_node;
107 }
108
109 /****************************************************************************/
110 /* Copy a single color node.                                                 */
111 /****************************************************************************/
112 glColorNode *
113 gl_color_node_dup (glColorNode *src)
114 {
115         glColorNode *dst;
116
117         if ( src == NULL ) return NULL;
118
119         dst = g_new0 (glColorNode, 1);
120
121         dst->field_flag = src->field_flag;
122         if (src->key != NULL)
123         {
124                 dst->key = g_strdup (src->key);
125         }
126         else
127         {
128                 dst->key = NULL;
129         }
130         dst->color = src->color;
131
132         return dst;
133 }
134
135 /****************************************************************************/
136 /* Compare 2 color nodes for equality.                                       */
137 /****************************************************************************/
138 gboolean
139 gl_color_node_equal (glColorNode     *color_node1,
140                      glColorNode     *color_node2)
141 {
142         /* First take care of the case of either or both being NULL. */
143         if ( color_node1 == NULL )
144         {
145                 return ( color_node2 == NULL );
146         }
147         else
148         {
149                 if ( color_node2 == NULL )
150                 {
151                         return FALSE;
152                 }
153         }
154
155         /* Bail if field flags differ. */
156         if ( color_node1->field_flag != color_node2->field_flag )
157         {
158                 return FALSE;
159         }
160
161         /* Now take care of the case of either or both color fields being different. */
162         if ( color_node1->color != color_node2->color )
163         {
164                 return FALSE;
165         }
166         
167         /* Then take care of the case of either or both key fields being NULL. */
168         if ( color_node1->key == NULL )
169         {
170                 return ( color_node2->key == NULL );
171         }
172         else
173         {
174                 if ( color_node2->key == NULL )
175                 {
176                         return FALSE;
177                 }
178         }
179
180         /* Field flags are identical, so now compare the keys. */
181         return (strcmp (color_node1->key, color_node2->key) == 0);
182 }
183
184 /****************************************************************************/
185 /* Expand single node into representative color.                            */
186 /****************************************************************************/
187 guint
188 gl_color_node_expand (glColorNode    *color_node,
189                       glMergeRecord  *record)
190 {
191         gchar    *text;
192         GdkColor *gdk_color;
193         guint     color;
194
195         if (color_node->field_flag)
196         {
197                 if (record == NULL)
198                 {
199                         return GL_COLOR_NONE;
200                 }
201                 else
202                 {
203                         text = gl_merge_eval_key (record, color_node->key);
204                         if (text != NULL)
205                         {
206                                 gdk_color = g_new0 (GdkColor, 1);
207                                 if (gdk_color_parse (text, gdk_color))
208                                 {
209                                         color = gl_color_from_gdk_color (gdk_color);
210                                         g_free (gdk_color);
211                                         return color;
212                                 }
213                                 else
214                                 {
215                                         g_free (gdk_color);
216                                         return GL_COLOR_NONE;
217                                 }
218                         }
219                         else
220                         {
221                                 return GL_COLOR_NONE;
222                         }
223                 }
224         }
225         else
226         {
227                 return color_node->color;
228         }
229 }
230
231 /****************************************************************************/
232 /* Free a single color node.                                                */
233 /****************************************************************************/
234 void
235 gl_color_node_free (glColorNode **color_node)
236 {
237         if ( *color_node == NULL ) return;
238
239         g_free ((*color_node)->key);
240         (*color_node)->key = NULL;
241         g_free (*color_node);
242         *color_node = NULL;
243 }