]> git.sur5r.net Git - glabels/blob - src/color.c
Imported Upstream version 3.0.0
[glabels] / src / color.c
1 /*
2  *  color.c
3  *  Copyright (C) 2002-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "color.h"
24
25 #include <string.h>
26
27
28 /*****************************************************************************/
29 /* Apply given opacity to given color.                                       */
30 /*****************************************************************************/
31 guint
32 gl_color_set_opacity (guint   color,
33                       gdouble opacity)
34 {
35         guint new_color;
36
37         new_color = (color & 0xFFFFFF00) | (((guint)(255.0*opacity)) & 0xFF);
38
39         return new_color;
40 }
41
42
43 /*****************************************************************************/
44 /* Resolve actual shadow color by adjusting opacity.                         */
45 /*****************************************************************************/
46 guint
47 gl_color_shadow (guint   base_color,
48                  gdouble opacity,
49                  guint   object_color)
50 {
51         guint color;
52
53         color = gl_color_set_opacity (base_color,
54                                       opacity * GL_COLOR_F_ALPHA (object_color));
55
56         return color;
57 }
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 /*****************************************************************************/
79 /* Convert GdkColor into a gLabels color                                     */
80 /*****************************************************************************/
81 guint
82 gl_color_from_gdk_color (GdkColor *gdk_color)
83 {
84         guint color;
85
86         color = GL_COLOR ((gdk_color->red   >>8),
87                           (gdk_color->green >>8),
88                           (gdk_color->blue  >>8));
89
90         return color;
91 }
92
93
94 /****************************************************************************/
95 /* Create a single color node with default color.                           */
96 /****************************************************************************/
97 glColorNode *
98 gl_color_node_new_default (void)
99 {
100         glColorNode* color_node;
101         
102         color_node = g_new0(glColorNode,1);
103         
104         color_node->field_flag = FALSE;
105         color_node->color = GL_COLOR_NONE;
106         color_node->key = NULL;
107
108         return color_node;
109 }
110
111
112 /****************************************************************************/
113 /* Copy a single color node.                                                 */
114 /****************************************************************************/
115 glColorNode *
116 gl_color_node_dup (glColorNode *src)
117 {
118         glColorNode *dst;
119
120         if ( src == NULL ) return NULL;
121
122         dst = g_new0 (glColorNode, 1);
123
124         dst->field_flag = src->field_flag;
125         if (src->key != NULL)
126         {
127                 dst->key = g_strdup (src->key);
128         }
129         else
130         {
131                 dst->key = NULL;
132         }
133         dst->color = src->color;
134
135         return dst;
136 }
137
138
139 /****************************************************************************/
140 /* Compare 2 color nodes for equality.                                       */
141 /****************************************************************************/
142 gboolean
143 gl_color_node_equal (glColorNode     *color_node1,
144                      glColorNode     *color_node2)
145 {
146         /* First take care of the case of either or both being NULL. */
147         if ( color_node1 == NULL )
148         {
149                 return ( color_node2 == NULL );
150         }
151         else
152         {
153                 if ( color_node2 == NULL )
154                 {
155                         return FALSE;
156                 }
157         }
158
159         /* Bail if field flags differ. */
160         if ( color_node1->field_flag != color_node2->field_flag )
161         {
162                 return FALSE;
163         }
164
165         /* Now take care of the case of either or both color fields being different. */
166         if ( color_node1->color != color_node2->color )
167         {
168                 return FALSE;
169         }
170         
171         /* Then take care of the case of either or both key fields being NULL. */
172         if ( color_node1->key == NULL )
173         {
174                 return ( color_node2->key == NULL );
175         }
176         else
177         {
178                 if ( color_node2->key == NULL )
179                 {
180                         return FALSE;
181                 }
182         }
183
184         /* Field flags are identical, so now compare the keys. */
185         return (strcmp (color_node1->key, color_node2->key) == 0);
186 }
187
188
189 /****************************************************************************/
190 /* Expand single node into representative color.                            */
191 /****************************************************************************/
192 guint
193 gl_color_node_expand (glColorNode    *color_node,
194                       glMergeRecord  *record)
195 {
196         gchar    *text;
197         GdkColor *gdk_color;
198         guint     color;
199
200         if (color_node->field_flag)
201         {
202                 if (record == NULL)
203                 {
204                         return GL_COLOR_NONE;
205                 }
206                 else
207                 {
208                         text = gl_merge_eval_key (record, color_node->key);
209                         if (text != NULL)
210                         {
211                                 gdk_color = g_new0 (GdkColor, 1);
212                                 if (gdk_color_parse (text, gdk_color))
213                                 {
214                                         color = gl_color_from_gdk_color (gdk_color);
215                                         g_free (gdk_color);
216                                         return color;
217                                 }
218                                 else
219                                 {
220                                         g_free (gdk_color);
221                                         return GL_COLOR_NONE;
222                                 }
223                         }
224                         else
225                         {
226                                 return GL_COLOR_NONE;
227                         }
228                 }
229         }
230         else
231         {
232                 return color_node->color;
233         }
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 }
250
251
252
253 /*
254  * Local Variables:       -- emacs
255  * mode: C                -- emacs
256  * c-basic-offset: 8      -- emacs
257  * tab-width: 8           -- emacs
258  * indent-tabs-mode: nil  -- emacs
259  * End:                   -- emacs
260  */