[AdgColorStyle] Code cleanup and corrected rgb facility
[adg.git] / src / adg / adg-color-style.c
blob45bf60f251e71783fcc87fa9cd46aa72a8baae88
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:adg-color-style
23 * @short_description: RGBA color information
25 * A style containing a single color expressed in RGB format.
26 * #AdgColorStyle also supports the alpha compositing that should be
27 * expressed with a double value between %0 and %1, where %0 is the
28 * "completely transparent" value while %1 is "fully opaque".
30 * Since: 1.0
31 **/
33 /**
34 * AdgColorStyle:
36 * All fields are private and should not be used directly.
37 * Use its public methods instead.
39 * Since: 1.0
40 **/
43 #include "adg-internal.h"
44 #include "adg-style.h"
45 #include "adg-color-style.h"
47 #include "adg-color-style.h"
48 #include "adg-color-style-private.h"
51 G_DEFINE_TYPE(AdgColorStyle, adg_color_style, ADG_TYPE_STYLE)
53 enum {
54 PROP_0,
55 PROP_RED,
56 PROP_GREEN,
57 PROP_BLUE,
58 PROP_ALPHA
62 static void _adg_get_property (GObject *object,
63 guint prop_id,
64 GValue *value,
65 GParamSpec *pspec);
66 static void _adg_set_property (GObject *object,
67 guint prop_id,
68 const GValue *value,
69 GParamSpec *pspec);
70 static void _adg_apply (AdgStyle *style,
71 AdgEntity *entity,
72 cairo_t *cr);
75 static void
76 adg_color_style_class_init(AdgColorStyleClass *klass)
78 GObjectClass *gobject_class;
79 AdgStyleClass *style_class;
80 GParamSpec *param;
82 gobject_class = (GObjectClass *) klass;
83 style_class = (AdgStyleClass *) klass;
85 g_type_class_add_private(klass, sizeof(AdgColorStylePrivate));
87 gobject_class->get_property = _adg_get_property;
88 gobject_class->set_property = _adg_set_property;
90 style_class->apply = _adg_apply;
92 param = g_param_spec_double("red",
93 P_("Red Channel"),
94 P_("The red value, where 0 means no red and 1 is full red"),
95 0, 1, 0,
96 G_PARAM_READWRITE);
97 g_object_class_install_property(gobject_class, PROP_RED, param);
99 param = g_param_spec_double("green",
100 P_("Green Channel"),
101 P_("The green value, where 0 means no green and 1 is full green"),
102 0, 1, 0,
103 G_PARAM_READWRITE);
104 g_object_class_install_property(gobject_class, PROP_GREEN, param);
106 param = g_param_spec_double("blue",
107 P_("Blue Channel"),
108 P_("The blue value, where 0 means no blue and 1 is full blue"),
109 0, 1, 0,
110 G_PARAM_READWRITE);
111 g_object_class_install_property(gobject_class, PROP_BLUE, param);
113 param = g_param_spec_double("alpha",
114 P_("Alpha Channel"),
115 P_("The alpha value, where 0 means completely transparent and 1 is fully opaque"),
116 0, 1, 1,
117 G_PARAM_READWRITE);
118 g_object_class_install_property(gobject_class, PROP_ALPHA, param);
121 static void
122 adg_color_style_init(AdgColorStyle *color_style)
124 AdgColorStylePrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(color_style,
125 ADG_TYPE_COLOR_STYLE,
126 AdgColorStylePrivate);
128 data->red = 0;
129 data->green = 0;
130 data->blue = 0;
131 data->alpha = 1;
133 color_style->data = data;
136 static void
137 _adg_get_property(GObject *object, guint prop_id,
138 GValue *value, GParamSpec *pspec)
140 AdgColorStylePrivate *data = ((AdgColorStyle *) object)->data;
142 switch (prop_id) {
143 case PROP_RED:
144 g_value_set_double(value, data->red);
145 break;
146 case PROP_GREEN:
147 g_value_set_double(value, data->green);
148 break;
149 case PROP_BLUE:
150 g_value_set_double(value, data->blue);
151 break;
152 case PROP_ALPHA:
153 g_value_set_double(value, data->alpha);
154 break;
155 default:
156 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
157 break;
161 static void
162 _adg_set_property(GObject *object, guint prop_id,
163 const GValue *value, GParamSpec *pspec)
165 AdgColorStylePrivate *data = ((AdgColorStyle *) object)->data;
167 switch (prop_id) {
168 case PROP_RED:
169 data->red = g_value_get_double(value);
170 break;
171 case PROP_GREEN:
172 data->green = g_value_get_double(value);
173 break;
174 case PROP_BLUE:
175 data->blue = g_value_get_double(value);
176 break;
177 case PROP_ALPHA:
178 data->alpha = g_value_get_double(value);
179 break;
180 default:
181 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
182 break;
188 * adg_color_style_new:
190 * Constructs a new color initialized to opaque black.
192 * Returns: a new color style
194 * Since: 1.0
196 AdgColorStyle *
197 adg_color_style_new(void)
199 return g_object_new(ADG_TYPE_COLOR_STYLE, NULL);
203 * adg_color_style_set_red:
204 * @color_style: an #AdgColorStyle
205 * @red: the new value
207 * Sets a new value for the red channel, where %0 means no red and
208 * %1 is full red.
210 * Since: 1.0
212 void
213 adg_color_style_set_red(AdgColorStyle *color_style, gdouble red)
215 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
216 g_object_set(color_style, "red", red, NULL);
220 * adg_color_style_get_red:
221 * @color_style: an #AdgColorStyle
223 * Gets the current value of the red channel, where %0 means no red and
224 * %1 is full red.
226 * Returns: the requested red value
228 * Since: 1.0
230 gdouble
231 adg_color_style_get_red(AdgColorStyle *color_style)
233 AdgColorStylePrivate *data;
235 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style), 0.);
237 data = color_style->data;
239 return data->red;
243 * adg_color_style_set_green:
244 * @color_style: an #AdgColorStyle
245 * @green: the new value
247 * Sets a new value for the green channel, where %0 means no green and
248 * %1 is full green.
250 * Since: 1.0
252 void
253 adg_color_style_set_green(AdgColorStyle *color_style, gdouble green)
255 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
256 g_object_set(color_style, "green", green, NULL);
260 * adg_color_style_get_green:
261 * @color_style: an #AdgColorStyle
263 * Gets the current value of the green channel, where %0 means no green and
264 * %1 is full green.
266 * Returns: the requested green value
268 * Since: 1.0
270 gdouble
271 adg_color_style_get_green(AdgColorStyle *color_style)
273 AdgColorStylePrivate *data;
275 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style), 0.);
277 data = color_style->data;
279 return data->green;
283 * adg_color_style_set_blue:
284 * @color_style: an #AdgColorStyle
285 * @blue: the new value
287 * Sets a new value for the blue channel, where %0 means no blue and
288 * %1 is full blue.
290 * Since: 1.0
292 void
293 adg_color_style_set_blue(AdgColorStyle *color_style, gdouble blue)
295 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
296 g_object_set(color_style, "blue", blue, NULL);
300 * adg_color_style_get_blue:
301 * @color_style: an #AdgColorStyle
303 * Gets the current value of the blue channel, where %0 means no blue and
304 * %1 is full blue.
306 * Returns: the requested blue value
308 * Since: 1.0
310 gdouble
311 adg_color_style_get_blue(AdgColorStyle *color_style)
313 AdgColorStylePrivate *data;
315 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style), 0.);
317 data = color_style->data;
319 return data->blue;
323 * adg_color_style_set_rgb:
324 * @color_style: an #AdgColorStyle
325 * @red: the red channel value
326 * @green: the green channel value
327 * @blue: the blue channel value
329 * Sets the RGB channels at once.
331 * Since: 1.0
333 void
334 adg_color_style_set_rgb(AdgColorStyle *color_style,
335 gdouble red, gdouble green, gdouble blue)
337 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
338 g_object_set(color_style, "red", red, "green", green, "blue", blue, NULL);
342 * adg_color_style_put_rgb:
343 * @color_style: an #AdgColorStyle
344 * @red: where to store the red channel value
345 * @green: where to store the green channel value
346 * @blue: where to store the blue channel value
348 * Gets the values of the red, green and blue channels of @color_style.
349 * Any of the pointer can be %NULL, in which case the value is not returned.
351 * Since: 1.0
353 void
354 adg_color_style_put_rgb(AdgColorStyle *color_style,
355 gdouble *red, gdouble *green, gdouble *blue)
357 AdgColorStylePrivate *data;
359 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
361 data = color_style->data;
363 if (red != NULL)
364 *red = data->red;
366 if (green != NULL)
367 *green = data->green;
369 if (blue != NULL)
370 *blue = data->blue;
374 * adg_color_style_set_alpha:
375 * @color_style: an #AdgColorStyle
376 * @alpha: the new alpha
378 * Sets a new color alpha value, where %0 means completely transparent
379 * and %1 is fully opaque.
381 * Since: 1.0
383 void
384 adg_color_style_set_alpha(AdgColorStyle *color_style, gdouble alpha)
386 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
387 g_object_set(color_style, "alpha", alpha, NULL);
391 * adg_color_style_get_alpha:
392 * @color_style: an #AdgColorStyle
394 * Gets the alpha channel value, where %0 means completely transparent
395 * and %1 is fully opaque.
397 * Returns: the requested alpha value
399 * Since: 1.0
401 gdouble
402 adg_color_style_get_alpha(AdgColorStyle *color_style)
404 AdgColorStylePrivate *data;
406 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style), 0.);
408 data = color_style->data;
410 return data->alpha;
414 static void
415 _adg_apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
417 AdgColorStylePrivate *data = ((AdgColorStyle *) style)->data;
419 if (data->alpha == 1.)
420 cairo_set_source_rgb(cr, data->red, data->green, data->blue);
421 else
422 cairo_set_source_rgba(cr, data->red, data->green, data->blue,
423 data->alpha);