]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssrgbavalue.c
cssvalue: Add _gtk_css_value_transition()
[~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 "gtkstylecontextprivate.h"
23 #include "gtksymboliccolorprivate.h"
24
25 struct _GtkCssValue {
26   GTK_CSS_VALUE_BASE
27   GdkRGBA rgba;
28 };
29
30 static void
31 gtk_css_value_rgba_free (GtkCssValue *value)
32 {
33   g_slice_free (GtkCssValue, value);
34 }
35
36 static gboolean
37 gtk_css_value_rgba_equal (const GtkCssValue *rgba1,
38                           const GtkCssValue *rgba2)
39 {
40   return gdk_rgba_equal (&rgba1->rgba, &rgba2->rgba);
41 }
42
43 static GtkCssValue *
44 gtk_css_value_rgba_transition (GtkCssValue *start,
45                                GtkCssValue *end,
46                                double       progress)
47 {
48   GdkRGBA transition;
49
50   progress = CLAMP (progress, 0, 1);
51   transition.red = start->rgba.red + (end->rgba.red - start->rgba.red) * progress;
52   transition.green = start->rgba.green + (end->rgba.green - start->rgba.green) * progress;
53   transition.blue = start->rgba.blue + (end->rgba.blue - start->rgba.blue) * progress;
54   transition.alpha = start->rgba.alpha + (end->rgba.alpha - start->rgba.alpha) * progress;
55
56   return _gtk_css_rgba_value_new_from_rgba (&transition);
57 }
58
59 static void
60 gtk_css_value_rgba_print (const GtkCssValue *rgba,
61                           GString           *string)
62 {
63   char *s = gdk_rgba_to_string (&rgba->rgba);
64   g_string_append (string, s);
65   g_free (s);
66 }
67
68 static const GtkCssValueClass GTK_CSS_VALUE_RGBA = {
69   gtk_css_value_rgba_free,
70   gtk_css_value_rgba_equal,
71   gtk_css_value_rgba_transition,
72   gtk_css_value_rgba_print
73 };
74
75 GtkCssValue *
76 _gtk_css_rgba_value_new_from_rgba (const GdkRGBA *rgba)
77 {
78   GtkCssValue *value;
79
80   g_return_val_if_fail (rgba != NULL, NULL);
81
82   value = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_RGBA);
83   value->rgba = *rgba;
84
85   return value;
86 }
87
88 const GdkRGBA *
89 _gtk_css_rgba_value_get_rgba (const GtkCssValue *rgba)
90 {
91   g_return_val_if_fail (rgba->class == &GTK_CSS_VALUE_RGBA, NULL);
92
93   return &rgba->rgba;
94 }
95
96 GtkCssValue *
97 _gtk_css_rgba_value_compute_from_symbolic (GtkCssValue     *rgba,
98                                            GtkCssValue     *fallback,
99                                            GtkStyleContext *context,
100                                            gboolean         for_color_property)
101 {
102   GtkSymbolicColor *symbolic;
103   GtkCssValue *resolved;
104
105   g_return_val_if_fail (rgba != NULL, NULL);
106
107   symbolic = _gtk_css_value_get_symbolic_color (rgba);
108
109   if (symbolic == _gtk_symbolic_color_get_current_color ())
110     {
111       /* The computed value of the ‘currentColor’ keyword is the computed
112        * value of the ‘color’ property. If the ‘currentColor’ keyword is
113        * set on the ‘color’ property itself, it is treated as ‘color: inherit’. 
114        */
115       if (for_color_property)
116         {
117           GtkStyleContext *parent = gtk_style_context_get_parent (context);
118
119           if (parent)
120             return _gtk_css_value_ref (_gtk_style_context_peek_property (parent, "color"));
121           else
122             return _gtk_css_rgba_value_compute_from_symbolic (fallback, NULL, context, TRUE);
123         }
124       else
125         {
126           return _gtk_css_value_ref (_gtk_style_context_peek_property (context, "color"));
127         }
128     }
129   
130   resolved = _gtk_style_context_resolve_color_value (context, symbolic);
131
132   if (resolved == NULL)
133     return _gtk_css_rgba_value_compute_from_symbolic (fallback, NULL, context, for_color_property);
134
135   g_assert (resolved->class == &GTK_CSS_VALUE_RGBA);
136   return resolved;
137 }
138