1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011 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-style.h"
41 #include "adg-color-style.h"
43 #include "adg-color-style.h"
44 #include "adg-color-style-private.h"
47 G_DEFINE_TYPE(AdgColorStyle
, adg_color_style
, ADG_TYPE_STYLE
)
58 static void _adg_get_property (GObject
*object
,
62 static void _adg_set_property (GObject
*object
,
66 static void _adg_apply (AdgStyle
*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
= _adg_get_property
;
84 gobject_class
->set_property
= _adg_set_property
;
86 style_class
->apply
= _adg_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 _adg_get_property(GObject
*object
, guint prop_id
,
134 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 _adg_set_property(GObject
*object
, guint prop_id
,
159 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_set_red:
198 * @color_style: an #AdgColorStyle
199 * @red: the new value
201 * Sets a new value for the red channel, where %0 means no red and
205 adg_color_style_set_red(AdgColorStyle
*color_style
, gdouble red
)
207 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
208 g_object_set(color_style
, "red", red
, NULL
);
212 * adg_color_style_get_red:
213 * @color_style: an #AdgColorStyle
215 * Gets the current value of the red channel, where %0 means no red and
218 * Returns: the requested red value
221 adg_color_style_get_red(AdgColorStyle
*color_style
)
223 AdgColorStylePrivate
*data
;
225 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style
), 0.);
227 data
= color_style
->data
;
233 * adg_color_style_set_green:
234 * @color_style: an #AdgColorStyle
235 * @green: the new value
237 * Sets a new value for the green channel, where %0 means no green and
241 adg_color_style_set_green(AdgColorStyle
*color_style
, gdouble green
)
243 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
244 g_object_set(color_style
, "green", green
, NULL
);
248 * adg_color_style_get_green:
249 * @color_style: an #AdgColorStyle
251 * Gets the current value of the green channel, where %0 means no green and
254 * Returns: the requested green value
257 adg_color_style_get_green(AdgColorStyle
*color_style
)
259 AdgColorStylePrivate
*data
;
261 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style
), 0.);
263 data
= color_style
->data
;
269 * adg_color_style_set_blue:
270 * @color_style: an #AdgColorStyle
271 * @blue: the new value
273 * Sets a new value for the blue channel, where %0 means no blue and
277 adg_color_style_set_blue(AdgColorStyle
*color_style
, gdouble blue
)
279 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
280 g_object_set(color_style
, "blue", blue
, NULL
);
284 * adg_color_style_get_blue:
285 * @color_style: an #AdgColorStyle
287 * Gets the current value of the blue channel, where %0 means no blue and
290 * Returns: the requested blue value
293 adg_color_style_get_blue(AdgColorStyle
*color_style
)
295 AdgColorStylePrivate
*data
;
297 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style
), 0.);
299 data
= color_style
->data
;
305 * adg_color_style_set_rgb:
306 * @color_style: an #AdgColorStyle
307 * @r: the red channel value
308 * @g: the green channel value
309 * @b: the blue channel value
311 * Sets the RGB channels at once.
314 adg_color_style_set_rgb(AdgColorStyle
*color_style
,
315 gdouble red
, gdouble green
, gdouble blue
)
318 AdgColorStylePrivate
*data
;
320 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
322 object
= (GObject
*) color_style
;
323 data
= color_style
->data
;
331 * adg_color_style_put_rgb:
332 * @color_style: an #AdgColorStyle
333 * @r: where to store the red channel value
334 * @g: where to store the green channel value
335 * @b: where to store the blue channel value
337 * Gets the values of the red, green and blue channels of @color_style.
338 * Any of the pointer can be %NULL, in which case the value is not returned.
341 adg_color_style_put_rgb(AdgColorStyle
*color_style
,
342 gdouble
*r
, gdouble
*g
, gdouble
*b
)
344 AdgColorStylePrivate
*data
;
346 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
348 data
= color_style
->data
;
361 * adg_color_style_set_alpha:
362 * @color_style: an #AdgColorStyle
363 * @alpha: the new alpha
365 * Sets a new color alpha value, where %0 means completely transparent
366 * and %1 is fully opaque.
369 adg_color_style_set_alpha(AdgColorStyle
*color_style
, gdouble alpha
)
371 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
372 g_object_set(color_style
, "alpha", alpha
, NULL
);
376 * adg_color_style_get_alpha:
377 * @color_style: an #AdgColorStyle
379 * Gets the alpha channel value, where %0 means completely transparent
380 * and %1 is fully opaque.
382 * Returns: the requested alpha value
385 adg_color_style_get_alpha(AdgColorStyle
*color_style
)
387 AdgColorStylePrivate
*data
;
389 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style
), 0.);
391 data
= color_style
->data
;
398 _adg_apply(AdgStyle
*style
, AdgEntity
*entity
, cairo_t
*cr
)
400 AdgColorStylePrivate
*data
= ((AdgColorStyle
*) style
)->data
;
402 if (data
->alpha
== 1.)
403 cairo_set_source_rgb(cr
, data
->red
, data
->green
, data
->blue
);
405 cairo_set_source_rgba(cr
, data
->red
, data
->green
, data
->blue
,