]> git.sur5r.net Git - glabels/blob - src/color.c
Imported Upstream version 2.2.8
[glabels] / 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 = NULL;
67
68         if ( GL_COLOR_F_ALPHA (color) )
69         {
70                 gdk_color = g_new0 (GdkColor, 1);
71
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;
75         }
76
77         return gdk_color;
78 }
79
80 /*****************************************************************************/
81 /* Convert GdkColor into a gLabels color                                     */
82 /*****************************************************************************/
83 guint
84 gl_color_from_gdk_color (GdkColor *gdk_color)
85 {
86         guint color = GL_COLOR_NONE;
87
88         if ( gdk_color )
89         {
90                 color = GL_COLOR ((gdk_color->red   >>8),
91                                   (gdk_color->green >>8),
92                                   (gdk_color->blue  >>8));
93         }
94
95         return color;
96 }
97
98 /****************************************************************************/
99 /* Create a single color node with default color.                           */
100 /****************************************************************************/
101 glColorNode *
102 gl_color_node_new_default (void)
103 {
104         glColorNode* color_node;
105         
106         color_node = g_new0(glColorNode,1);
107         
108         color_node->field_flag = FALSE;
109         color_node->color = GL_COLOR_NONE;
110         color_node->key = NULL;
111
112         return color_node;
113 }
114
115 /****************************************************************************/
116 /* Copy a single color node.                                                 */
117 /****************************************************************************/
118 glColorNode *
119 gl_color_node_dup (glColorNode *src)
120 {
121         glColorNode *dst;
122
123         if ( src == NULL ) return NULL;
124
125         dst = g_new0 (glColorNode, 1);
126
127         dst->field_flag = src->field_flag;
128         if (src->key != NULL)
129         {
130                 dst->key = g_strdup (src->key);
131         }
132         else
133         {
134                 dst->key = NULL;
135         }
136         dst->color = src->color;
137
138         return dst;
139 }
140
141 /****************************************************************************/
142 /* Compare 2 color nodes for equality.                                       */
143 /****************************************************************************/
144 gboolean
145 gl_color_node_equal (glColorNode     *color_node1,
146                      glColorNode     *color_node2)
147 {
148         /* First take care of the case of either or both being NULL. */
149         if ( color_node1 == NULL )
150         {
151                 return ( color_node2 == NULL );
152         }
153         else
154         {
155                 if ( color_node2 == NULL )
156                 {
157                         return FALSE;
158                 }
159         }
160
161         /* Bail if field flags differ. */
162         if ( color_node1->field_flag != color_node2->field_flag )
163         {
164                 return FALSE;
165         }
166
167         /* Now take care of the case of either or both color fields being different. */
168         if ( color_node1->color != color_node2->color )
169         {
170                 return FALSE;
171         }
172         
173         /* Then take care of the case of either or both key fields being NULL. */
174         if ( color_node1->key == NULL )
175         {
176                 return ( color_node2->key == NULL );
177         }
178         else
179         {
180                 if ( color_node2->key == NULL )
181                 {
182                         return FALSE;
183                 }
184         }
185
186         /* Field flags are identical, so now compare the keys. */
187         return (strcmp (color_node1->key, color_node2->key) == 0);
188 }
189
190 /****************************************************************************/
191 /* Expand single node into representative color.                            */
192 /****************************************************************************/
193 guint
194 gl_color_node_expand (glColorNode    *color_node,
195                       glMergeRecord  *record)
196 {
197         gchar    *text;
198         GdkColor *gdk_color;
199         guint     color;
200
201         if (color_node->field_flag)
202         {
203                 if (record == NULL)
204                 {
205                         return GL_COLOR_NONE;
206                 }
207                 else
208                 {
209                         text = gl_merge_eval_key (record, color_node->key);
210                         if (text != NULL)
211                         {
212                                 gdk_color = g_new0 (GdkColor, 1);
213                                 if (gdk_color_parse (text, gdk_color))
214                                 {
215                                         color = gl_color_from_gdk_color (gdk_color);
216                                         g_free (gdk_color);
217                                         return color;
218                                 }
219                                 else
220                                 {
221                                         g_free (gdk_color);
222                                         return GL_COLOR_NONE;
223                                 }
224                         }
225                         else
226                         {
227                                 return GL_COLOR_NONE;
228                         }
229                 }
230         }
231         else
232         {
233                 return color_node->color;
234         }
235 }
236
237 /****************************************************************************/
238 /* Free a single color node.                                                */
239 /****************************************************************************/
240 void
241 gl_color_node_free (glColorNode **color_node)
242 {
243         if ( *color_node == NULL ) return;
244
245         g_free ((*color_node)->key);
246         (*color_node)->key = NULL;
247         g_free (*color_node);
248         *color_node = NULL;
249 }