]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssrgbavalue.c
css: Introduce dependencies for value computations
[~andy/gtk] / gtk / gtkcssrgbavalue.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include "gtkcssrgbavalueprivate.h"
21
22 #include "gtkcssstylepropertyprivate.h"
23 #include "gtkstylecontextprivate.h"
24 #include "gtksymboliccolorprivate.h"
25
26 struct _GtkCssValue {
27   GTK_CSS_VALUE_BASE
28   GdkRGBA rgba;
29 };
30
31 static void
32 gtk_css_value_rgba_free (GtkCssValue *value)
33 {
34   g_slice_free (GtkCssValue, value);
35 }
36
37 static GtkCssValue *
38 gtk_css_value_rgba_compute (GtkCssValue        *value,
39                             guint               property_id,
40                             GtkStyleContext    *context,
41                             GtkCssDependencies *dependencies)
42 {
43   return _gtk_css_value_ref (value);
44 }
45
46 static gboolean
47 gtk_css_value_rgba_equal (const GtkCssValue *rgba1,
48                           const GtkCssValue *rgba2)
49 {
50   return gdk_rgba_equal (&rgba1->rgba, &rgba2->rgba);
51 }
52
53 static GtkCssValue *
54 gtk_css_value_rgba_transition (GtkCssValue *start,
55                                GtkCssValue *end,
56                                double       progress)
57 {
58   GdkRGBA transition;
59
60   progress = CLAMP (progress, 0, 1);
61   transition.red = start->rgba.red + (end->rgba.red - start->rgba.red) * progress;
62   transition.green = start->rgba.green + (end->rgba.green - start->rgba.green) * progress;
63   transition.blue = start->rgba.blue + (end->rgba.blue - start->rgba.blue) * progress;
64   transition.alpha = start->rgba.alpha + (end->rgba.alpha - start->rgba.alpha) * progress;
65
66   return _gtk_css_rgba_value_new_from_rgba (&transition);
67 }
68
69 static void
70 gtk_css_value_rgba_print (const GtkCssValue *rgba,
71                           GString           *string)
72 {
73   char *s = gdk_rgba_to_string (&rgba->rgba);
74   g_string_append (string, s);
75   g_free (s);
76 }
77
78 static const GtkCssValueClass GTK_CSS_VALUE_RGBA = {
79   gtk_css_value_rgba_free,
80   gtk_css_value_rgba_compute,
81   gtk_css_value_rgba_equal,
82   gtk_css_value_rgba_transition,
83   gtk_css_value_rgba_print
84 };
85
86 GtkCssValue *
87 _gtk_css_rgba_value_new_from_rgba (const GdkRGBA *rgba)
88 {
89   GtkCssValue *value;
90
91   g_return_val_if_fail (rgba != NULL, NULL);
92
93   value = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_RGBA);
94   value->rgba = *rgba;
95
96   return value;
97 }
98
99 const GdkRGBA *
100 _gtk_css_rgba_value_get_rgba (const GtkCssValue *rgba)
101 {
102   g_return_val_if_fail (rgba->class == &GTK_CSS_VALUE_RGBA, NULL);
103
104   return &rgba->rgba;
105 }