[build] Moved project dirs under src/
[adg.git] / src / adg / adg-color-style.c
bloba1d905a939d45dfb33a75c4ecb2f5a55039832ce
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010 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-color-style.h"
41 #include "adg-color-style-private.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);
66 static gboolean set_channel (gdouble *channel,
67 gdouble value);
70 G_DEFINE_TYPE(AdgColorStyle, adg_color_style, ADG_TYPE_STYLE);
73 static void
74 adg_color_style_class_init(AdgColorStyleClass *klass)
76 GObjectClass *gobject_class;
77 AdgStyleClass *style_class;
78 GParamSpec *param;
80 gobject_class = (GObjectClass *) klass;
81 style_class = (AdgStyleClass *) klass;
83 g_type_class_add_private(klass, sizeof(AdgColorStylePrivate));
85 gobject_class->get_property = get_property;
86 gobject_class->set_property = set_property;
88 style_class->apply = apply;
90 param = g_param_spec_double("red",
91 P_("Red Channel"),
92 P_("The red value, where 0 means no red and 1 is full red"),
93 0, 1, 0,
94 G_PARAM_READWRITE);
95 g_object_class_install_property(gobject_class, PROP_RED, param);
97 param = g_param_spec_double("green",
98 P_("Green Channel"),
99 P_("The green value, where 0 means no green and 1 is full green"),
100 0, 1, 0,
101 G_PARAM_READWRITE);
102 g_object_class_install_property(gobject_class, PROP_GREEN, param);
104 param = g_param_spec_double("blue",
105 P_("Blue Channel"),
106 P_("The blue value, where 0 means no blue and 1 is full blue"),
107 0, 1, 0,
108 G_PARAM_READWRITE);
109 g_object_class_install_property(gobject_class, PROP_BLUE, param);
111 param = g_param_spec_double("alpha",
112 P_("Alpha Channel"),
113 P_("The alpha value, where 0 means completely transparent and 1 is fully opaque"),
114 0, 1, 1,
115 G_PARAM_READWRITE);
116 g_object_class_install_property(gobject_class, PROP_ALPHA, param);
119 static void
120 adg_color_style_init(AdgColorStyle *color_style)
122 AdgColorStylePrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(color_style,
123 ADG_TYPE_COLOR_STYLE,
124 AdgColorStylePrivate);
126 data->red = 0;
127 data->green = 0;
128 data->blue = 0;
129 data->alpha = 1;
131 color_style->data = data;
134 static void
135 get_property(GObject *object,
136 guint prop_id, GValue *value, GParamSpec *pspec)
138 AdgColorStylePrivate *data = ((AdgColorStyle *) object)->data;
140 switch (prop_id) {
141 case PROP_RED:
142 g_value_set_double(value, data->red);
143 break;
144 case PROP_GREEN:
145 g_value_set_double(value, data->green);
146 break;
147 case PROP_BLUE:
148 g_value_set_double(value, data->blue);
149 break;
150 case PROP_ALPHA:
151 g_value_set_double(value, data->alpha);
152 break;
153 default:
154 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
155 break;
159 static void
160 set_property(GObject *object,
161 guint prop_id, const GValue *value, GParamSpec *pspec)
163 AdgColorStylePrivate *data = ((AdgColorStyle *) object)->data;
165 switch (prop_id) {
166 case PROP_RED:
167 set_channel(&data->red, g_value_get_double(value));
168 break;
169 case PROP_GREEN:
170 set_channel(&data->green, g_value_get_double(value));
171 break;
172 case PROP_BLUE:
173 set_channel(&data->blue, g_value_get_double(value));
174 break;
175 case PROP_ALPHA:
176 set_channel(&data->alpha, g_value_get_double(value));
177 break;
178 default:
179 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
180 break;
186 * adg_color_style_new:
188 * Constructs a new color initialized to opaque black.
190 * Returns: a new color style
192 AdgColorStyle *
193 adg_color_style_new(void)
195 return g_object_new(ADG_TYPE_COLOR_STYLE, NULL);
199 * adg_color_style_set_red:
200 * @color_style: an #AdgColorStyle
201 * @red: the new value
203 * Sets a new value for the red channel, where %0 means no red and
204 * %1 is full red.
206 void
207 adg_color_style_set_red(AdgColorStyle *color_style, gdouble red)
209 AdgColorStylePrivate *data;
211 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
213 data = color_style->data;
215 if (set_channel(&data->red, red))
216 g_object_notify((GObject *) color_style, "red");
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 gdouble
229 adg_color_style_get_red(AdgColorStyle *color_style)
231 AdgColorStylePrivate *data;
233 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style), 0.);
235 data = color_style->data;
237 return data->red;
241 * adg_color_style_set_green:
242 * @color_style: an #AdgColorStyle
243 * @green: the new value
245 * Sets a new value for the green channel, where %0 means no green and
246 * %1 is full green.
248 void
249 adg_color_style_set_green(AdgColorStyle *color_style, gdouble green)
251 AdgColorStylePrivate *data;
253 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
255 data = color_style->data;
257 if (set_channel(&data->green, green))
258 g_object_notify((GObject *) color_style, "green");
262 * adg_color_style_get_green:
263 * @color_style: an #AdgColorStyle
265 * Gets the current value of the green channel, where %0 means no green and
266 * %1 is full green.
268 * Returns: the requested green value
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 void
291 adg_color_style_set_blue(AdgColorStyle *color_style, gdouble blue)
293 AdgColorStylePrivate *data;
295 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
297 data = color_style->data;
299 if (set_channel(&data->blue, blue))
300 g_object_notify((GObject *) color_style, "blue");
304 * adg_color_style_get_blue:
305 * @color_style: an #AdgColorStyle
307 * Gets the current value of the blue channel, where %0 means no blue and
308 * %1 is full blue.
310 * Returns: the requested blue value
312 gdouble
313 adg_color_style_get_blue(AdgColorStyle *color_style)
315 AdgColorStylePrivate *data;
317 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style), 0.);
319 data = color_style->data;
321 return data->blue;
325 * adg_color_style_set_rgb:
326 * @color_style: an #AdgColorStyle
327 * @r: the red channel value
328 * @g: the green channel value
329 * @b: the blue channel value
331 * Sets the RGB channels at once.
333 void
334 adg_color_style_set_rgb(AdgColorStyle *color_style,
335 gdouble red, gdouble green, gdouble blue)
337 GObject *object;
338 AdgColorStylePrivate *data;
340 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
342 object = (GObject *) color_style;
343 data = color_style->data;
345 data->red = red;
346 data->green = green;
347 data->blue = blue;
351 * adg_color_style_put_rgb:
352 * @color_style: an #AdgColorStyle
353 * @r: where to store the red channel value
354 * @g: where to store the green channel value
355 * @b: where to store the blue channel value
357 * Gets the values of the red, green and blue channels of @color_style.
358 * Any of the pointer can be %NULL, in which case the value is not returned.
360 void
361 adg_color_style_put_rgb(AdgColorStyle *color_style,
362 gdouble *r, gdouble *g, gdouble *b)
364 AdgColorStylePrivate *data;
366 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
368 data = color_style->data;
370 if (r != NULL)
371 *r = data->red;
373 if (g != NULL)
374 *g = data->green;
376 if (b != NULL)
377 *b = data->blue;
381 * adg_color_style_set_alpha:
382 * @color_style: an #AdgColorStyle
383 * @alpha: the new alpha
385 * Sets a new color alpha value, where %0 means completely transparent
386 * and %1 is fully opaque.
388 void
389 adg_color_style_set_alpha(AdgColorStyle *color_style, gdouble alpha)
391 AdgColorStylePrivate *data;
393 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
395 data = color_style->data;
397 if (set_channel(&data->alpha, alpha))
398 g_object_notify((GObject *) color_style, "alpha");
402 * adg_color_style_get_alpha:
403 * @color_style: an #AdgColorStyle
405 * Gets the alpha channel value, where %0 means completely transparent
406 * and %1 is fully opaque.
408 * Returns: the requested alpha value
410 gdouble
411 adg_color_style_get_alpha(AdgColorStyle *color_style)
413 AdgColorStylePrivate *data;
415 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style), 0.);
417 data = color_style->data;
419 return data->alpha;
423 static void
424 apply(AdgStyle *style, AdgEntity *entity, cairo_t *cr)
426 AdgColorStylePrivate *data = ((AdgColorStyle *) style)->data;
428 if (data->alpha == 1.)
429 cairo_set_source_rgb(cr, data->red, data->green, data->blue);
430 else
431 cairo_set_source_rgba(cr, data->red, data->green, data->blue,
432 data->alpha);
435 static gboolean
436 set_channel(gdouble *channel, gdouble value)
438 g_return_val_if_fail(value >= 0 && value <=1, FALSE);
440 if (*channel == value)
441 return FALSE;
443 *channel = value;
444 return TRUE;