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
,
67 G_DEFINE_TYPE(AdgColorStyle
, adg_color_style
, ADG_TYPE_STYLE
);
71 adg_color_style_class_init(AdgColorStyleClass
*klass
)
73 GObjectClass
*gobject_class
;
74 AdgStyleClass
*style_class
;
77 gobject_class
= (GObjectClass
*) klass
;
78 style_class
= (AdgStyleClass
*) klass
;
80 g_type_class_add_private(klass
, sizeof(AdgColorStylePrivate
));
82 gobject_class
->get_property
= get_property
;
83 gobject_class
->set_property
= set_property
;
85 style_class
->apply
= apply
;
87 param
= g_param_spec_double("red",
89 P_("The red value, where 0 means no red and 1 is full red"),
92 g_object_class_install_property(gobject_class
, PROP_RED
, param
);
94 param
= g_param_spec_double("green",
96 P_("The green value, where 0 means no green and 1 is full green"),
99 g_object_class_install_property(gobject_class
, PROP_GREEN
, param
);
101 param
= g_param_spec_double("blue",
103 P_("The blue value, where 0 means no blue and 1 is full blue"),
106 g_object_class_install_property(gobject_class
, PROP_BLUE
, param
);
108 param
= g_param_spec_double("alpha",
110 P_("The alpha value, where 0 means completely transparent and 1 is fully opaque"),
113 g_object_class_install_property(gobject_class
, PROP_ALPHA
, param
);
117 adg_color_style_init(AdgColorStyle
*color_style
)
119 AdgColorStylePrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(color_style
,
120 ADG_TYPE_COLOR_STYLE
,
121 AdgColorStylePrivate
);
128 color_style
->data
= data
;
132 get_property(GObject
*object
,
133 guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
135 AdgColorStylePrivate
*data
= ((AdgColorStyle
*) object
)->data
;
139 g_value_set_double(value
, data
->red
);
142 g_value_set_double(value
, data
->green
);
145 g_value_set_double(value
, data
->blue
);
148 g_value_set_double(value
, data
->alpha
);
151 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
157 set_property(GObject
*object
,
158 guint prop_id
, const GValue
*value
, GParamSpec
*pspec
)
160 AdgColorStylePrivate
*data
= ((AdgColorStyle
*) object
)->data
;
164 data
->red
= g_value_get_double(value
);
167 data
->green
= g_value_get_double(value
);
170 data
->blue
= g_value_get_double(value
);
173 data
->alpha
= g_value_get_double(value
);
176 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
183 * adg_color_style_new:
185 * Constructs a new color initialized to opaque black.
187 * Returns: a new color style
190 adg_color_style_new(void)
192 return g_object_new(ADG_TYPE_COLOR_STYLE
, NULL
);
196 * adg_color_style_get_rgb:
197 * @color_style: an #AdgColorStyle
198 * @r: where to store the red channel value
199 * @g: where to store the green channel value
200 * @b: where to store the blue channel value
202 * Gets the values of the red, green and blue channels of @color_style.
203 * Any of the pointer can be %NULL, in which case the value is not returned.
206 adg_color_style_get_rgb(AdgColorStyle
*color_style
,
207 gdouble
*r
, gdouble
*g
, gdouble
*b
)
209 AdgColorStylePrivate
*data
;
211 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
213 data
= color_style
->data
;
226 * adg_color_style_set_rgb:
227 * @color_style: an #AdgColorStyle
228 * @r: the red channel value
229 * @g: the green channel value
230 * @b: the blue channel value
232 * Sets the RGB channels at once.
235 adg_color_style_set_rgb(AdgColorStyle
*color_style
,
236 gdouble r
, gdouble g
, gdouble b
)
239 AdgColorStylePrivate
*data
;
241 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
243 object
= (GObject
*) color_style
;
244 data
= color_style
->data
;
250 g_object_freeze_notify(object
);
251 g_object_notify(object
, "red");
252 g_object_notify(object
, "green");
253 g_object_notify(object
, "blue");
254 g_object_thaw_notify(object
);
258 * adg_color_style_get_alpha:
259 * @color_style: an #AdgColorStyle
261 * Gets the alpha channel value, where %0 means completely transparent
262 * and %1 is fully opaque.
264 * Returns: the requested alpha value
267 adg_color_style_get_alpha(AdgColorStyle
*color_style
)
269 AdgColorStylePrivate
*data
;
271 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style
), 0.);
273 data
= color_style
->data
;
279 * adg_color_style_set_alpha:
280 * @color_style: an #AdgColorStyle
281 * @alpha: the new alpha
283 * Sets a new color alpha value, where %0 means completely transparent
284 * and %1 is fully opaque.
287 adg_color_style_set_alpha(AdgColorStyle
*color_style
, gdouble alpha
)
289 AdgColorStylePrivate
*data
;
291 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
293 data
= color_style
->data
;
296 g_object_notify((GObject
*) color_style
, "alpha");
301 apply(AdgStyle
*style
, cairo_t
*cr
)
303 AdgColorStylePrivate
*data
= ((AdgColorStyle
*) style
)->data
;
305 if (data
->alpha
== 1.)
306 cairo_set_source_rgb(cr
, data
->red
, data
->green
, data
->blue
);
308 cairo_set_source_rgba(cr
, data
->red
, data
->green
, data
->blue
,