1#include <locale.h>
2#include <gdk/gdk.h>
3
4static void
5test_color_parse (void)
6{
7 GdkRGBA color;
8 GdkRGBA expected;
9 gboolean res;
10
11 res = gdk_rgba_parse (rgba: &color, spec: "foo");
12 g_assert_true (!res);
13
14 res = gdk_rgba_parse (rgba: &color, spec: "");
15 g_assert_true (!res);
16
17 expected.red = 100/255.;
18 expected.green = 90/255.;
19 expected.blue = 80/255.;
20 expected.alpha = 0.1;
21 res = gdk_rgba_parse (rgba: &color, spec: "rgba(100,90,80,0.1)");
22 g_assert_true (res);
23 g_assert_true (gdk_rgba_equal (&color, &expected));
24
25 expected.red = 0.4;
26 expected.green = 0.3;
27 expected.blue = 0.2;
28 expected.alpha = 0.1;
29 res = gdk_rgba_parse (rgba: &color, spec: "rgba(40%,30%,20%,0.1)");
30 g_assert_true (res);
31 g_assert_true (gdk_rgba_equal (&color, &expected));
32
33 res = gdk_rgba_parse (rgba: &color, spec: "rgba( 40 % , 30 % , 20 % , 0.1 )");
34 g_assert_true (res);
35 g_assert_true (gdk_rgba_equal (&color, &expected));
36
37 expected.red = 1.0;
38 expected.green = 0.0;
39 expected.blue = 0.0;
40 expected.alpha = 1.0;
41 res = gdk_rgba_parse (rgba: &color, spec: "red");
42 g_assert_true (res);
43 g_assert_true (gdk_rgba_equal (&color, &expected));
44
45 expected.red = 0.0;
46 expected.green = 0x8080 / 65535.;
47 expected.blue = 1.0;
48 expected.alpha = 1.0;
49 res = gdk_rgba_parse (rgba: &color, spec: "#0080ff");
50 g_assert_true (res);
51 g_assert_true (gdk_rgba_equal (&color, &expected));
52
53 expected.red = 0.0;
54 expected.green = 0.0;
55 expected.blue = 0.0;
56 expected.alpha = 1.0;
57 res = gdk_rgba_parse (rgba: &color, spec: "rgb(0,0,0)");
58 g_assert_true (res);
59 g_assert_true (gdk_rgba_equal (&color, &expected));
60
61 expected.red = 0.0;
62 expected.green = 0x8080 / 65535.;
63 expected.blue = 1.0;
64 expected.alpha = 0x8888 / 65535.;
65 res = gdk_rgba_parse (rgba: &color, spec: "#0080ff88");
66 g_assert_true (res);
67 g_assert_true (gdk_rgba_equal (&color, &expected));
68
69 expected.red = 1.0;
70 expected.green = 0.0;
71 expected.blue = 0.0;
72 expected.alpha = 1.0;
73 res = gdk_rgba_parse (rgba: &color, spec: "hsl (0, 100%, 50%)");
74 g_assert_true (res);
75 g_assert_true (gdk_rgba_equal (&color, &expected));
76
77 expected.red = 0.0;
78 expected.green = 1.0;
79 expected.blue = 0.0;
80 expected.alpha = 0.1;
81 res = gdk_rgba_parse (rgba: &color, spec: "hsla (120, 255, 50%, 0.1)");
82 g_assert_true (res);
83 g_assert_true (gdk_rgba_equal (&color, &expected));
84
85 expected.red = 0.0;
86 expected.green = 0.5;
87 expected.blue = 0.5;
88 expected.alpha = 1.0;
89 res = gdk_rgba_parse (rgba: &color, spec: "hsl(180, 100%, 25%)");
90 g_assert_true (res);
91 g_assert_true (gdk_rgba_equal (&color, &expected));
92}
93
94static void
95test_color_to_string (void)
96{
97 GdkRGBA rgba;
98 GdkRGBA out;
99 char *res;
100 char *res_de;
101 char *res_en;
102 char *orig;
103
104 /* Using /255. values for the r, g, b components should
105 * make sure they round-trip exactly without rounding
106 * from the double => integer => double conversions */
107 rgba.red = 1.0;
108 rgba.green = 128/255.;
109 rgba.blue = 64/255.;
110 rgba.alpha = 0.5;
111
112 orig = g_strdup (str: setlocale (LC_ALL, NULL));
113 res = gdk_rgba_to_string (rgba: &rgba);
114 gdk_rgba_parse (rgba: &out, spec: res);
115 g_assert_true (gdk_rgba_equal (&rgba, &out));
116
117 setlocale (LC_ALL, locale: "de_DE.utf-8");
118 res_de = gdk_rgba_to_string (rgba: &rgba);
119 g_assert_cmpstr (res, ==, res_de);
120
121 setlocale (LC_ALL, locale: "en_US.utf-8");
122 res_en = gdk_rgba_to_string (rgba: &rgba);
123 g_assert_cmpstr (res, ==, res_en);
124
125 g_free (mem: res);
126 g_free (mem: res_de);
127 g_free (mem: res_en);
128
129 setlocale (LC_ALL, locale: orig);
130 g_free (mem: orig);
131}
132
133static void
134test_color_copy (void)
135{
136 GdkRGBA rgba;
137 GdkRGBA *out;
138
139 rgba.red = 0.0;
140 rgba.green = 0.1;
141 rgba.blue = 0.6;
142 rgba.alpha = 0.9;
143
144 out = gdk_rgba_copy (rgba: &rgba);
145 g_assert_true (gdk_rgba_equal (&rgba, out));
146
147 gdk_rgba_free (rgba: out);
148}
149
150static void
151test_color_parse_nonsense (void)
152{
153 GdkRGBA color;
154 gboolean res;
155
156 /*http://bugzilla.gnome.org/show_bug.cgi?id=667485 */
157
158 res = gdk_rgba_parse (rgba: &color, spec: "rgb(,,)");
159 g_assert_false (res);
160
161 res = gdk_rgba_parse (rgba: &color, spec: "rgb(%,%,%)");
162 g_assert_false (res);
163
164 res = gdk_rgba_parse (rgba: &color, spec: "rgb(nan,nan,nan)");
165 g_assert_false (res);
166
167 res = gdk_rgba_parse (rgba: &color, spec: "rgb(inf,inf,inf)");
168 g_assert_false (res);
169
170 res = gdk_rgba_parse (rgba: &color, spec: "rgb(1p12,0,0)");
171 g_assert_false (res);
172
173 res = gdk_rgba_parse (rgba: &color, spec: "rgb(5d1%,1,1)");
174 g_assert_false (res);
175
176 res = gdk_rgba_parse (rgba: &color, spec: "rgb(0,0,0)moo");
177 g_assert_false (res);
178
179 res = gdk_rgba_parse (rgba: &color, spec: "rgb(0,0,0) moo");
180 g_assert_false (res);
181}
182
183int
184main (int argc, char *argv[])
185{
186 (g_test_init) (argc: &argc, argv: &argv, NULL);
187
188 g_test_add_func (testpath: "/rgba/parse", test_func: test_color_parse);
189 g_test_add_func (testpath: "/rgba/parse/nonsense", test_func: test_color_parse_nonsense);
190 g_test_add_func (testpath: "/rgba/to-string", test_func: test_color_to_string);
191 g_test_add_func (testpath: "/rgba/copy", test_func: test_color_copy);
192
193 return g_test_run ();
194}
195

source code of gtk/testsuite/gdk/rgba.c