1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2021 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_WITH_PRIVATE(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 gobject_class
->get_property
= _adg_get_property
;
86 gobject_class
->set_property
= _adg_set_property
;
88 style_class
->apply
= _adg_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
= adg_color_style_get_instance_private(color_style
);
130 _adg_get_property(GObject
*object
, guint prop_id
,
131 GValue
*value
, GParamSpec
*pspec
)
133 AdgColorStylePrivate
*data
= adg_color_style_get_instance_private((AdgColorStyle
*) object
);
137 g_value_set_double(value
, data
->red
);
140 g_value_set_double(value
, data
->green
);
143 g_value_set_double(value
, data
->blue
);
146 g_value_set_double(value
, data
->alpha
);
149 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
155 _adg_set_property(GObject
*object
, guint prop_id
,
156 const GValue
*value
, GParamSpec
*pspec
)
158 AdgColorStylePrivate
*data
= adg_color_style_get_instance_private((AdgColorStyle
*) object
);
162 data
->red
= g_value_get_double(value
);
165 data
->green
= g_value_get_double(value
);
168 data
->blue
= g_value_get_double(value
);
171 data
->alpha
= g_value_get_double(value
);
174 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
181 * adg_color_style_new:
183 * Constructs a new color initialized to opaque black.
185 * 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_set_red:
197 * @color_style: an #AdgColorStyle
198 * @red: the new value
200 * Sets a new value for the red channel, where 0 means no red and
206 adg_color_style_set_red(AdgColorStyle
*color_style
, gdouble red
)
208 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
209 g_object_set(color_style
, "red", red
, NULL
);
213 * adg_color_style_get_red:
214 * @color_style: an #AdgColorStyle
216 * Gets the current value of the red channel, where 0 means no red and
219 * Returns: the requested red value
224 adg_color_style_get_red(AdgColorStyle
*color_style
)
226 AdgColorStylePrivate
*data
;
228 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style
), 0.);
230 data
= adg_color_style_get_instance_private(color_style
);
235 * adg_color_style_set_green:
236 * @color_style: an #AdgColorStyle
237 * @green: the new value
239 * Sets a new value for the green channel, where 0 means no green and
245 adg_color_style_set_green(AdgColorStyle
*color_style
, gdouble green
)
247 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
248 g_object_set(color_style
, "green", green
, NULL
);
252 * adg_color_style_get_green:
253 * @color_style: an #AdgColorStyle
255 * Gets the current value of the green channel, where 0 means no green and
258 * Returns: the requested green value
263 adg_color_style_get_green(AdgColorStyle
*color_style
)
265 AdgColorStylePrivate
*data
;
267 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style
), 0.);
269 data
= adg_color_style_get_instance_private(color_style
);
274 * adg_color_style_set_blue:
275 * @color_style: an #AdgColorStyle
276 * @blue: the new value
278 * Sets a new value for the blue channel, where 0 means no blue and
284 adg_color_style_set_blue(AdgColorStyle
*color_style
, gdouble blue
)
286 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
287 g_object_set(color_style
, "blue", blue
, NULL
);
291 * adg_color_style_get_blue:
292 * @color_style: an #AdgColorStyle
294 * Gets the current value of the blue channel, where 0 means no blue and
297 * Returns: the requested blue value
302 adg_color_style_get_blue(AdgColorStyle
*color_style
)
304 AdgColorStylePrivate
*data
;
306 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style
), 0.);
308 data
= adg_color_style_get_instance_private(color_style
);
313 * adg_color_style_set_rgb:
314 * @color_style: an #AdgColorStyle
315 * @red: the red channel value
316 * @green: the green channel value
317 * @blue: the blue channel value
319 * Sets the RGB channels at once.
324 adg_color_style_set_rgb(AdgColorStyle
*color_style
,
325 gdouble red
, gdouble green
, gdouble blue
)
327 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
328 g_object_set(color_style
, "red", red
, "green", green
, "blue", blue
, NULL
);
332 * adg_color_style_put_rgb:
333 * @color_style: an #AdgColorStyle
334 * @red: where to store the red channel value
335 * @green: where to store the green channel value
336 * @blue: where to store the blue channel value
338 * Gets the values of the red, green and blue channels of @color_style.
339 * Any of the pointer can be <constant>NULL</constant>, in which case
340 * the value is not returned.
345 adg_color_style_put_rgb(AdgColorStyle
*color_style
,
346 gdouble
*red
, gdouble
*green
, gdouble
*blue
)
348 AdgColorStylePrivate
*data
;
350 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
352 data
= adg_color_style_get_instance_private(color_style
);
358 *green
= data
->green
;
365 * adg_color_style_set_alpha:
366 * @color_style: an #AdgColorStyle
367 * @alpha: the new alpha
369 * Sets a new color alpha value, where 0 means completely transparent
370 * and 1 is fully opaque.
375 adg_color_style_set_alpha(AdgColorStyle
*color_style
, gdouble alpha
)
377 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style
));
378 g_object_set(color_style
, "alpha", alpha
, NULL
);
382 * adg_color_style_get_alpha:
383 * @color_style: an #AdgColorStyle
385 * Gets the alpha channel value, where 0 means completely transparent
386 * and 1 is fully opaque.
388 * Returns: the requested alpha value
393 adg_color_style_get_alpha(AdgColorStyle
*color_style
)
395 AdgColorStylePrivate
*data
;
397 g_return_val_if_fail(ADG_IS_COLOR_STYLE(color_style
), 0.);
399 data
= adg_color_style_get_instance_private(color_style
);
405 _adg_apply(AdgStyle
*style
, AdgEntity
*entity
, cairo_t
*cr
)
407 AdgColorStylePrivate
*data
= adg_color_style_get_instance_private((AdgColorStyle
*) style
);
409 if (data
->alpha
== 1.)
410 cairo_set_source_rgb(cr
, data
->red
, data
->green
, data
->blue
);
412 cairo_set_source_rgba(cr
, data
->red
, data
->green
, data
->blue
,