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.
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-color-style.h"
40 #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
,
68 G_DEFINE_TYPE(AdgColorStyle
, adg_color_style
, ADG_TYPE_STYLE
);
72 adg_color_style_class_init(AdgColorStyleClass
*klass
)
74 GObjectClass
*gobject_class
;
75 AdgStyleClass
*style_class
;
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",
90 P_("The red value, where 0 means no red and 1 is full red"),
93 g_object_class_install_property(gobject_class
, PROP_RED
, param
);
95 param
= g_param_spec_double("green",
97 P_("The green value, where 0 means no green and 1 is full green"),
100 g_object_class_install_property(gobject_class
, PROP_GREEN
, param
);
102 param
= g_param_spec_double("blue",
104 P_("The blue value, where 0 means no blue and 1 is full blue"),
107 g_object_class_install_property(gobject_class
, PROP_BLUE
, param
);
109 param
= g_param_spec_double("alpha",
111 P_("The alpha value, where 0 means completely transparent and 1 is fully opaque"),
114 g_object_class_install_property(gobject_class
, PROP_ALPHA
, param
);
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
);
129 color_style
->data
= data
;
133 get_property(GObject
*object
,
134 guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
136 AdgColorStylePrivate
*data
= ((AdgColorStyle
*) object
)->data
;
140 g_value_set_double(value
, data
->red
);
143 g_value_set_double(value
, data
->green
);
146 g_value_set_double(value
, data
->blue
);
149 g_value_set_double(value
, data
->alpha
);
152 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
158 set_property(GObject
*object
,
159 guint prop_id
, const GValue
*value
, GParamSpec
*pspec
)
161 AdgColorStylePrivate
*data
= ((AdgColorStyle
*) object
)->data
;
165 data
->red
= g_value_get_double(value
);
168 data
->green
= g_value_get_double(value
);
171 data
->blue
= g_value_get_double(value
);
174 data
->alpha
= g_value_get_double(value
);
177 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
184 * adg_color_style_new:
186 * Constructs a new color initialized to opaque black.
188 * Returns: a new color style
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.
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
;
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.
236 adg_color_style_set_rgb(AdgColorStyle
*color_style
,
237 gdouble r
, gdouble g
, gdouble b
)
240 AdgColorStylePrivate
*data
;
242 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
244 object
= (GObject
*) color_style
;
245 data
= color_style
->data
;
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
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
;
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.
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
;
297 g_object_notify((GObject
*) color_style
, "alpha");
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
);
309 cairo_set_source_rgba(cr
, data
->red
, data
->green
, data
->blue
,