1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012 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".
36 * All fields are private and should not be used directly.
37 * Use its public methods instead.
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
)
62 static void _adg_get_property (GObject
*object
,
66 static void _adg_set_property (GObject
*object
,
70 static void _adg_apply (AdgStyle
*style
,
76 adg_color_style_class_init(AdgColorStyleClass
*klass
)
78 GObjectClass
*gobject_class
;
79 AdgStyleClass
*style_class
;
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",
94 P_("The red value, where 0 means no red and 1 is full red"),
97 g_object_class_install_property(gobject_class
, PROP_RED
, param
);
99 param
= g_param_spec_double("green",
101 P_("The green value, where 0 means no green and 1 is full green"),
104 g_object_class_install_property(gobject_class
, PROP_GREEN
, param
);
106 param
= g_param_spec_double("blue",
108 P_("The blue value, where 0 means no blue and 1 is full blue"),
111 g_object_class_install_property(gobject_class
, PROP_BLUE
, param
);
113 param
= g_param_spec_double("alpha",
115 P_("The alpha value, where 0 means completely transparent and 1 is fully opaque"),
118 g_object_class_install_property(gobject_class
, PROP_ALPHA
, param
);
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
);
133 color_style
->data
= data
;
137 _adg_get_property(GObject
*object
, guint prop_id
,
138 GValue
*value
, GParamSpec
*pspec
)
140 AdgColorStylePrivate
*data
= ((AdgColorStyle
*) object
)->data
;
144 g_value_set_double(value
, data
->red
);
147 g_value_set_double(value
, data
->green
);
150 g_value_set_double(value
, data
->blue
);
153 g_value_set_double(value
, data
->alpha
);
156 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
162 _adg_set_property(GObject
*object
, guint prop_id
,
163 const GValue
*value
, GParamSpec
*pspec
)
165 AdgColorStylePrivate
*data
= ((AdgColorStyle
*) object
)->data
;
169 data
->red
= g_value_get_double(value
);
172 data
->green
= g_value_get_double(value
);
175 data
->blue
= g_value_get_double(value
);
178 data
->alpha
= g_value_get_double(value
);
181 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
188 * adg_color_style_new:
190 * Constructs a new color initialized to opaque black.
192 * Returns: a new color style
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
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
226 * Returns: the requested red value
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
;
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
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
266 * 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
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
306 * Returns: the requested blue value
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
;
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.
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.
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
;
367 *green
= data
->green
;
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.
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
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
;
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
);
422 cairo_set_source_rgba(cr
, data
->red
, data
->green
, data
->blue
,