doc: update copyright line for 2021
[adg.git] / src / adg / adg-color-style.c
blob5afdbc35c4256cac153df96cebc508b8b31d7e4f
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.
21 /**
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".
30 * Since: 1.0
31 **/
33 /**
34 * AdgColorStyle:
36 * All fields are private and should not be used directly.
37 * Use its public methods instead.
39 * Since: 1.0
40 **/
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)
53 enum {
54 PROP_0,
55 PROP_RED,
56 PROP_GREEN,
57 PROP_BLUE,
58 PROP_ALPHA
62 static void _adg_get_property (GObject *object,
63 guint prop_id,
64 GValue *value,
65 GParamSpec *pspec);
66 static void _adg_set_property (GObject *object,
67 guint prop_id,
68 const GValue *value,
69 GParamSpec *pspec);
70 static void _adg_apply (AdgStyle *style,
71 AdgEntity *entity,
72 cairo_t *cr);
75 static void
76 adg_color_style_class_init(AdgColorStyleClass *klass)
78 GObjectClass *gobject_class;
79 AdgStyleClass *style_class;
80 GParamSpec *param;
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",
91 P_("Red Channel"),
92 P_("The red value, where 0 means no red and 1 is full red"),
93 0, 1, 0,
94 G_PARAM_READWRITE);
95 g_object_class_install_property(gobject_class, PROP_RED, param);
97 param = g_param_spec_double("green",
98 P_("Green Channel"),
99 P_("The green value, where 0 means no green and 1 is full green"),
100 0, 1, 0,
101 G_PARAM_READWRITE);
102 g_object_class_install_property(gobject_class, PROP_GREEN, param);
104 param = g_param_spec_double("blue",
105 P_("Blue Channel"),
106 P_("The blue value, where 0 means no blue and 1 is full blue"),
107 0, 1, 0,
108 G_PARAM_READWRITE);
109 g_object_class_install_property(gobject_class, PROP_BLUE, param);
111 param = g_param_spec_double("alpha",
112 P_("Alpha Channel"),
113 P_("The alpha value, where 0 means completely transparent and 1 is fully opaque"),
114 0, 1, 1,
115 G_PARAM_READWRITE);
116 g_object_class_install_property(gobject_class, PROP_ALPHA, param);
119 static void
120 adg_color_style_init(AdgColorStyle *color_style)
122 AdgColorStylePrivate *data = adg_color_style_get_instance_private(color_style);
123 data->red = 0;
124 data->green = 0;
125 data->blue = 0;
126 data->alpha = 1;
129 static void
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);
135 switch (prop_id) {
136 case PROP_RED:
137 g_value_set_double(value, data->red);
138 break;
139 case PROP_GREEN:
140 g_value_set_double(value, data->green);
141 break;
142 case PROP_BLUE:
143 g_value_set_double(value, data->blue);
144 break;
145 case PROP_ALPHA:
146 g_value_set_double(value, data->alpha);
147 break;
148 default:
149 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
150 break;
154 static void
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);
160 switch (prop_id) {
161 case PROP_RED:
162 data->red = g_value_get_double(value);
163 break;
164 case PROP_GREEN:
165 data->green = g_value_get_double(value);
166 break;
167 case PROP_BLUE:
168 data->blue = g_value_get_double(value);
169 break;
170 case PROP_ALPHA:
171 data->alpha = g_value_get_double(value);
172 break;
173 default:
174 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
175 break;
181 * adg_color_style_new:
183 * Constructs a new color initialized to opaque black.
185 * Returns: a new color style
187 * Since: 1.0
189 AdgColorStyle *
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
201 * 1 is full red.
203 * Since: 1.0
205 void
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
217 * 1 is full red.
219 * Returns: the requested red value
221 * Since: 1.0
223 gdouble
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);
231 return data->red;
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
240 * 1 is full green.
242 * Since: 1.0
244 void
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
256 * 1 is full green.
258 * Returns: the requested green value
260 * Since: 1.0
262 gdouble
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);
270 return data->green;
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
279 * 1 is full blue.
281 * Since: 1.0
283 void
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
295 * 1 is full blue.
297 * Returns: the requested blue value
299 * Since: 1.0
301 gdouble
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);
309 return data->blue;
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.
321 * Since: 1.0
323 void
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.
342 * Since: 1.0
344 void
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);
354 if (red != NULL)
355 *red = data->red;
357 if (green != NULL)
358 *green = data->green;
360 if (blue != NULL)
361 *blue = data->blue;
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.
372 * Since: 1.0
374 void
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
390 * Since: 1.0
392 gdouble
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);
400 return data->alpha;
404 static void
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);
411 else
412 cairo_set_source_rgba(cr, data->red, data->green, data->blue,
413 data->alpha);