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.
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".
34 * All fields are private and should not be used directly.
35 * Use its public methods instead.
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)
55 static void get_property (GObject
*object
,
59 static void set_property (GObject
*object
,
63 static void apply (AdgStyle
*style
,
66 static gboolean
set_channel (gdouble
*channel
,
70 G_DEFINE_TYPE(AdgColorStyle
, adg_color_style
, ADG_TYPE_STYLE
);
74 adg_color_style_class_init(AdgColorStyleClass
*klass
)
76 GObjectClass
*gobject_class
;
77 AdgStyleClass
*style_class
;
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",
92 P_("The red value, where 0 means no red and 1 is full red"),
95 g_object_class_install_property(gobject_class
, PROP_RED
, param
);
97 param
= g_param_spec_double("green",
99 P_("The green value, where 0 means no green and 1 is full green"),
102 g_object_class_install_property(gobject_class
, PROP_GREEN
, param
);
104 param
= g_param_spec_double("blue",
106 P_("The blue value, where 0 means no blue and 1 is full blue"),
109 g_object_class_install_property(gobject_class
, PROP_BLUE
, param
);
111 param
= g_param_spec_double("alpha",
113 P_("The alpha value, where 0 means completely transparent and 1 is fully opaque"),
116 g_object_class_install_property(gobject_class
, PROP_ALPHA
, param
);
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
);
131 color_style
->data
= data
;
135 get_property(GObject
*object
,
136 guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
138 AdgColorStylePrivate
*data
= ((AdgColorStyle
*) object
)->data
;
142 g_value_set_double(value
, data
->red
);
145 g_value_set_double(value
, data
->green
);
148 g_value_set_double(value
, data
->blue
);
151 g_value_set_double(value
, data
->alpha
);
154 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
160 set_property(GObject
*object
,
161 guint prop_id
, const GValue
*value
, GParamSpec
*pspec
)
163 AdgColorStylePrivate
*data
= ((AdgColorStyle
*) object
)->data
;
167 set_channel(&data
->red
, g_value_get_double(value
));
170 set_channel(&data
->green
, g_value_get_double(value
));
173 set_channel(&data
->blue
, g_value_get_double(value
));
176 set_channel(&data
->alpha
, g_value_get_double(value
));
179 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
186 * adg_color_style_new:
188 * Constructs a new color initialized to opaque black.
190 * Returns: a new color style
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
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
226 * Returns: the requested red value
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
;
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
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
268 * Returns: the requested green value
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
;
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
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
310 * Returns: the requested blue value
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
;
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.
334 adg_color_style_set_rgb(AdgColorStyle
*color_style
,
335 gdouble red
, gdouble green
, gdouble blue
)
338 AdgColorStylePrivate
*data
;
340 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
342 object
= (GObject
*) color_style
;
343 data
= color_style
->data
;
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.
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
;
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.
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
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
;
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
);
431 cairo_set_source_rgba(cr
, data
->red
, data
->green
, data
->blue
,
436 set_channel(gdouble
*channel
, gdouble value
)
438 g_return_val_if_fail(value
>= 0 && value
<=1, FALSE
);
440 if (*channel
== value
)