[demo] Updated adg-demo to use named pairs where possible
[adg.git] / adg / adg-color-style.c
blob87cba7401d7c5f70aeac64f88cebc70922d73575
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 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-color-style.h"
40 #include "adg-color-style-private.h"
41 #include "adg-intl.h"
43 #define PARENT_STYLE_CLASS ((AdgStyleClass *) adg_color_style_parent_class)
46 enum {
47 PROP_0,
48 PROP_RED,
49 PROP_GREEN,
50 PROP_BLUE,
51 PROP_ALPHA
55 static void get_property (GObject *object,
56 guint prop_id,
57 GValue *value,
58 GParamSpec *pspec);
59 static void set_property (GObject *object,
60 guint prop_id,
61 const GValue *value,
62 GParamSpec *pspec);
63 static void apply (AdgStyle *style,
64 AdgEntity *entity,
65 cairo_t *cr);
68 G_DEFINE_TYPE(AdgColorStyle, adg_color_style, ADG_TYPE_STYLE);
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 = get_property;
84 gobject_class->set_property = set_property;
86 style_class->apply = 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 get_property(GObject *object,
134 guint prop_id, 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 set_property(GObject *object,
159 guint prop_id, 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_get_rgb:
198 * @color_style: an #AdgColorStyle
199 * @r: where to store the red channel value
200 * @g: where to store the green channel value
201 * @b: where to store the blue channel value
203 * Gets the values of the red, green and blue channels of @color_style.
204 * Any of the pointer can be %NULL, in which case the value is not returned.
206 void
207 adg_color_style_get_rgb(AdgColorStyle *color_style,
208 gdouble *r, gdouble *g, gdouble *b)
210 AdgColorStylePrivate *data;
212 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
214 data = color_style->data;
216 if (r != NULL)
217 *r = data->red;
219 if (g != NULL)
220 *g = data->green;
222 if (b != NULL)
223 *b = data->blue;
227 * adg_color_style_set_rgb:
228 * @color_style: an #AdgColorStyle
229 * @r: the red channel value
230 * @g: the green channel value
231 * @b: the blue channel value
233 * Sets the RGB channels at once.
235 void
236 adg_color_style_set_rgb(AdgColorStyle *color_style,
237 gdouble r, gdouble g, gdouble b)
239 GObject *object;
240 AdgColorStylePrivate *data;
242 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
244 object = (GObject *) color_style;
245 data = color_style->data;
247 data->red = r;
248 data->green = g;
249 data->blue = b;
251 g_object_freeze_notify(object);
252 g_object_notify(object, "red");
253 g_object_notify(object, "green");
254 g_object_notify(object, "blue");
255 g_object_thaw_notify(object);
259 * adg_color_style_get_alpha:
260 * @color_style: an #AdgColorStyle
262 * Gets the alpha channel value, where %0 means completely transparent
263 * and %1 is fully opaque.
265 * Returns: the requested alpha value
267 gdouble
268 adg_color_style_get_alpha(AdgColorStyle *color_style)
270 AdgColorStylePrivate *data;
272 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style), 0.);
274 data = color_style->data;
276 return data->alpha;
280 * adg_color_style_set_alpha:
281 * @color_style: an #AdgColorStyle
282 * @alpha: the new alpha
284 * Sets a new color alpha value, where %0 means completely transparent
285 * and %1 is fully opaque.
287 void
288 adg_color_style_set_alpha(AdgColorStyle *color_style, gdouble alpha)
290 AdgColorStylePrivate *data;
292 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
294 data = color_style->data;
295 data->alpha = alpha;
297 g_object_notify((GObject *) color_style, "alpha");
301 static void
302 apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
304 AdgColorStylePrivate *data = ((AdgColorStyle *) style)->data;
306 if (data->alpha == 1.)
307 cairo_set_source_rgb(cr, data->red, data->green, data->blue);
308 else
309 cairo_set_source_rgba(cr, data->red, data->green, data->blue,
310 data->alpha);