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