[AdgRuledFill] Improved ruled creation algorithm
[adg.git] / adg / adg-color-style.c
blobf0d8df42840223ac0bfa943b128d0d38a1773063
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.
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".
29 **/
31 /**
32 * AdgColorStyle:
34 * All fields are private and should not be used directly.
35 * Use its public methods instead.
36 **/
39 #include "adg-color-style.h"
40 #include "adg-color-style-private.h"
41 #include "adg-intl.h"
43 #define PARENT_STYLE_CLASS ((AdgStyleClass *) adg_color_style_parent_class)
46 enum {
47 PROP_0,
48 PROP_RED,
49 PROP_GREEN,
50 PROP_BLUE,
51 PROP_ALPHA
55 static void get_property (GObject *object,
56 guint prop_id,
57 GValue *value,
58 GParamSpec *pspec);
59 static void set_property (GObject *object,
60 guint prop_id,
61 const GValue *value,
62 GParamSpec *pspec);
63 static void apply (AdgStyle *style,
64 cairo_t *cr);
67 G_DEFINE_TYPE(AdgColorStyle, adg_color_style, ADG_TYPE_STYLE);
70 static void
71 adg_color_style_class_init(AdgColorStyleClass *klass)
73 GObjectClass *gobject_class;
74 AdgStyleClass *style_class;
75 GParamSpec *param;
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",
88 P_("Red Channel"),
89 P_("The red value, where 0 means no red and 1 is full red"),
90 0, G_MAXDOUBLE, 0,
91 G_PARAM_READWRITE);
92 g_object_class_install_property(gobject_class, PROP_RED, param);
94 param = g_param_spec_double("green",
95 P_("Green Channel"),
96 P_("The green value, where 0 means no green and 1 is full green"),
97 0, G_MAXDOUBLE, 0,
98 G_PARAM_READWRITE);
99 g_object_class_install_property(gobject_class, PROP_GREEN, param);
101 param = g_param_spec_double("blue",
102 P_("Blue Channel"),
103 P_("The blue value, where 0 means no blue and 1 is full blue"),
104 0, G_MAXDOUBLE, 0,
105 G_PARAM_READWRITE);
106 g_object_class_install_property(gobject_class, PROP_BLUE, param);
108 param = g_param_spec_double("alpha",
109 P_("Alpha Channel"),
110 P_("The alpha value, where 0 means completely transparent and 1 is fully opaque"),
111 0, G_MAXDOUBLE, 1,
112 G_PARAM_READWRITE);
113 g_object_class_install_property(gobject_class, PROP_ALPHA, param);
116 static void
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);
123 data->red = 0;
124 data->green = 0;
125 data->blue = 0;
126 data->alpha = 1;
128 color_style->data = data;
131 static void
132 get_property(GObject *object,
133 guint prop_id, GValue *value, GParamSpec *pspec)
135 AdgColorStylePrivate *data = ((AdgColorStyle *) object)->data;
137 switch (prop_id) {
138 case PROP_RED:
139 g_value_set_double(value, data->red);
140 break;
141 case PROP_GREEN:
142 g_value_set_double(value, data->green);
143 break;
144 case PROP_BLUE:
145 g_value_set_double(value, data->blue);
146 break;
147 case PROP_ALPHA:
148 g_value_set_double(value, data->alpha);
149 break;
150 default:
151 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
152 break;
156 static void
157 set_property(GObject *object,
158 guint prop_id, const GValue *value, GParamSpec *pspec)
160 AdgColorStylePrivate *data = ((AdgColorStyle *) object)->data;
162 switch (prop_id) {
163 case PROP_RED:
164 data->red = g_value_get_double(value);
165 break;
166 case PROP_GREEN:
167 data->green = g_value_get_double(value);
168 break;
169 case PROP_BLUE:
170 data->blue = g_value_get_double(value);
171 break;
172 case PROP_ALPHA:
173 data->alpha = g_value_get_double(value);
174 break;
175 default:
176 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
177 break;
183 * adg_color_style_new:
185 * Constructs a new color initialized to opaque black.
187 * Returns: a new color style
189 AdgColorStyle *
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.
205 void
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;
215 if (r != NULL)
216 *r = data->red;
218 if (g != NULL)
219 *g = data->green;
221 if (b != NULL)
222 *b = data->blue;
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.
234 void
235 adg_color_style_set_rgb(AdgColorStyle *color_style,
236 gdouble r, gdouble g, gdouble b)
238 GObject *object;
239 AdgColorStylePrivate *data;
241 g_return_if_fail(ADG_IS_COLOR_STYLE(color_style));
243 object = (GObject *) color_style;
244 data = color_style->data;
246 data->red = r;
247 data->green = g;
248 data->blue = b;
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
266 gdouble
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;
275 return data->alpha;
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.
286 void
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;
294 data->alpha = alpha;
296 g_object_notify((GObject *) color_style, "alpha");
300 static void
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);
307 else
308 cairo_set_source_rgba(cr, data->red, data->green, data->blue,
309 data->alpha);