Tested build process under ss-dev of OpenIndiana
[adg.git] / src / adg / adg-color-style.c
blob44b4296858c75232fbb781101af885e3274a36b4
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".
29 **/
31 /**
32 * AdgColorStyle:
34 * All fields are private and should not be used directly.
35 * Use its public methods instead.
36 **/
39 #include "adg-internal.h"
40 #include "adg-style.h"
41 #include "adg-color-style.h"
43 #include "adg-color-style.h"
44 #include "adg-color-style-private.h"
47 G_DEFINE_TYPE(AdgColorStyle, adg_color_style, ADG_TYPE_STYLE)
49 enum {
50 PROP_0,
51 PROP_RED,
52 PROP_GREEN,
53 PROP_BLUE,
54 PROP_ALPHA
58 static void _adg_get_property (GObject *object,
59 guint prop_id,
60 GValue *value,
61 GParamSpec *pspec);
62 static void _adg_set_property (GObject *object,
63 guint prop_id,
64 const GValue *value,
65 GParamSpec *pspec);
66 static void _adg_apply (AdgStyle *style,
67 AdgEntity *entity,
68 cairo_t *cr);
71 static void
72 adg_color_style_class_init(AdgColorStyleClass *klass)
74 GObjectClass *gobject_class;
75 AdgStyleClass *style_class;
76 GParamSpec *param;
78 gobject_class = (GObjectClass *) klass;
79 style_class = (AdgStyleClass *) klass;
81 g_type_class_add_private(klass, sizeof(AdgColorStylePrivate));
83 gobject_class->get_property = _adg_get_property;
84 gobject_class->set_property = _adg_set_property;
86 style_class->apply = _adg_apply;
88 param = g_param_spec_double("red",
89 P_("Red Channel"),
90 P_("The red value, where 0 means no red and 1 is full red"),
91 0, 1, 0,
92 G_PARAM_READWRITE);
93 g_object_class_install_property(gobject_class, PROP_RED, param);
95 param = g_param_spec_double("green",
96 P_("Green Channel"),
97 P_("The green value, where 0 means no green and 1 is full green"),
98 0, 1, 0,
99 G_PARAM_READWRITE);
100 g_object_class_install_property(gobject_class, PROP_GREEN, param);
102 param = g_param_spec_double("blue",
103 P_("Blue Channel"),
104 P_("The blue value, where 0 means no blue and 1 is full blue"),
105 0, 1, 0,
106 G_PARAM_READWRITE);
107 g_object_class_install_property(gobject_class, PROP_BLUE, param);
109 param = g_param_spec_double("alpha",
110 P_("Alpha Channel"),
111 P_("The alpha value, where 0 means completely transparent and 1 is fully opaque"),
112 0, 1, 1,
113 G_PARAM_READWRITE);
114 g_object_class_install_property(gobject_class, PROP_ALPHA, param);
117 static void
118 adg_color_style_init(AdgColorStyle *color_style)
120 AdgColorStylePrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(color_style,
121 ADG_TYPE_COLOR_STYLE,
122 AdgColorStylePrivate);
124 data->red = 0;
125 data->green = 0;
126 data->blue = 0;
127 data->alpha = 1;
129 color_style->data = data;
132 static void
133 _adg_get_property(GObject *object, guint prop_id,
134 GValue *value, GParamSpec *pspec)
136 AdgColorStylePrivate *data = ((AdgColorStyle *) object)->data;
138 switch (prop_id) {
139 case PROP_RED:
140 g_value_set_double(value, data->red);
141 break;
142 case PROP_GREEN:
143 g_value_set_double(value, data->green);
144 break;
145 case PROP_BLUE:
146 g_value_set_double(value, data->blue);
147 break;
148 case PROP_ALPHA:
149 g_value_set_double(value, data->alpha);
150 break;
151 default:
152 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
153 break;
157 static void
158 _adg_set_property(GObject *object, guint prop_id,
159 const GValue *value, GParamSpec *pspec)
161 AdgColorStylePrivate *data = ((AdgColorStyle *) object)->data;
163 switch (prop_id) {
164 case PROP_RED:
165 data->red = g_value_get_double(value);
166 break;
167 case PROP_GREEN:
168 data->green = g_value_get_double(value);
169 break;
170 case PROP_BLUE:
171 data->blue = g_value_get_double(value);
172 break;
173 case PROP_ALPHA:
174 data->alpha = g_value_get_double(value);
175 break;
176 default:
177 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
178 break;
184 * adg_color_style_new:
186 * Constructs a new color initialized to opaque black.
188 * Returns: a new color style
190 AdgColorStyle *
191 adg_color_style_new(void)
193 return g_object_new(ADG_TYPE_COLOR_STYLE, NULL);
197 * adg_color_style_set_red:
198 * @color_style: an #AdgColorStyle
199 * @red: the new value
201 * Sets a new value for the red channel, where %0 means no red and
202 * %1 is full red.
204 void
205 adg_color_style_set_red(AdgColorStyle *color_style, gdouble red)
207 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
208 g_object_set(color_style, "red", red, NULL);
212 * adg_color_style_get_red:
213 * @color_style: an #AdgColorStyle
215 * Gets the current value of the red channel, where %0 means no red and
216 * %1 is full red.
218 * Returns: the requested red value
220 gdouble
221 adg_color_style_get_red(AdgColorStyle *color_style)
223 AdgColorStylePrivate *data;
225 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style), 0.);
227 data = color_style->data;
229 return data->red;
233 * adg_color_style_set_green:
234 * @color_style: an #AdgColorStyle
235 * @green: the new value
237 * Sets a new value for the green channel, where %0 means no green and
238 * %1 is full green.
240 void
241 adg_color_style_set_green(AdgColorStyle *color_style, gdouble green)
243 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
244 g_object_set(color_style, "green", green, NULL);
248 * adg_color_style_get_green:
249 * @color_style: an #AdgColorStyle
251 * Gets the current value of the green channel, where %0 means no green and
252 * %1 is full green.
254 * Returns: the requested green value
256 gdouble
257 adg_color_style_get_green(AdgColorStyle *color_style)
259 AdgColorStylePrivate *data;
261 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style), 0.);
263 data = color_style->data;
265 return data->green;
269 * adg_color_style_set_blue:
270 * @color_style: an #AdgColorStyle
271 * @blue: the new value
273 * Sets a new value for the blue channel, where %0 means no blue and
274 * %1 is full blue.
276 void
277 adg_color_style_set_blue(AdgColorStyle *color_style, gdouble blue)
279 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
280 g_object_set(color_style, "blue", blue, NULL);
284 * adg_color_style_get_blue:
285 * @color_style: an #AdgColorStyle
287 * Gets the current value of the blue channel, where %0 means no blue and
288 * %1 is full blue.
290 * Returns: the requested blue value
292 gdouble
293 adg_color_style_get_blue(AdgColorStyle *color_style)
295 AdgColorStylePrivate *data;
297 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style), 0.);
299 data = color_style->data;
301 return data->blue;
305 * adg_color_style_set_rgb:
306 * @color_style: an #AdgColorStyle
307 * @r: the red channel value
308 * @g: the green channel value
309 * @b: the blue channel value
311 * Sets the RGB channels at once.
313 void
314 adg_color_style_set_rgb(AdgColorStyle *color_style,
315 gdouble red, gdouble green, gdouble blue)
317 GObject *object;
318 AdgColorStylePrivate *data;
320 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
322 object = (GObject *) color_style;
323 data = color_style->data;
325 data->red = red;
326 data->green = green;
327 data->blue = blue;
331 * adg_color_style_put_rgb:
332 * @color_style: an #AdgColorStyle
333 * @r: where to store the red channel value
334 * @g: where to store the green channel value
335 * @b: where to store the blue channel value
337 * Gets the values of the red, green and blue channels of @color_style.
338 * Any of the pointer can be %NULL, in which case the value is not returned.
340 void
341 adg_color_style_put_rgb(AdgColorStyle *color_style,
342 gdouble *r, gdouble *g, gdouble *b)
344 AdgColorStylePrivate *data;
346 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
348 data = color_style->data;
350 if (r != NULL)
351 *r = data->red;
353 if (g != NULL)
354 *g = data->green;
356 if (b != NULL)
357 *b = data->blue;
361 * adg_color_style_set_alpha:
362 * @color_style: an #AdgColorStyle
363 * @alpha: the new alpha
365 * Sets a new color alpha value, where %0 means completely transparent
366 * and %1 is fully opaque.
368 void
369 adg_color_style_set_alpha(AdgColorStyle *color_style, gdouble alpha)
371 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
372 g_object_set(color_style, "alpha", alpha, NULL);
376 * adg_color_style_get_alpha:
377 * @color_style: an #AdgColorStyle
379 * Gets the alpha channel value, where %0 means completely transparent
380 * and %1 is fully opaque.
382 * Returns: the requested alpha value
384 gdouble
385 adg_color_style_get_alpha(AdgColorStyle *color_style)
387 AdgColorStylePrivate *data;
389 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style), 0.);
391 data = color_style->data;
393 return data->alpha;
397 static void
398 _adg_apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
400 AdgColorStylePrivate *data = ((AdgColorStyle *) style)->data;
402 if (data->alpha == 1.)
403 cairo_set_source_rgb(cr, data->red, data->green, data->blue);
404 else
405 cairo_set_source_rgba(cr, data->red, data->green, data->blue,
406 data->alpha);