]> Pileus Git - ~andy/gtk/blob - gdk/tests/gdk-color.c
Merge branch 'master' into broadway
[~andy/gtk] / gdk / tests / gdk-color.c
1 #include <locale.h>
2 #include <gdk/gdk.h>
3
4 static void
5 test_color_parse (void)
6 {
7   GdkRGBA color;
8   GdkRGBA expected;
9   gboolean res;
10
11   res = gdk_rgba_parse (&color, "foo");
12   g_assert (!res);
13
14   res = gdk_rgba_parse (&color, "");
15   g_assert (!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 (&color, "rgba(100,90,80,0.1)");
22   g_assert (res);
23   g_assert (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 (&color, "rgba(40%,30%,20%,0.1)");
30   g_assert (res);
31   g_assert (gdk_rgba_equal (&color, &expected));
32
33   res = gdk_rgba_parse (&color, "rgba(  40 % ,  30 %  ,   20 % ,  0.1    )");
34   g_assert (res);
35   g_assert (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 (&color, "red");
42   g_assert (res);
43   g_assert (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 (&color, "#0080ff");
50   g_assert (res);
51   g_assert (gdk_rgba_equal (&color, &expected));
52 }
53
54 static void
55 test_color_to_string (void)
56 {
57   GdkRGBA rgba;
58   GdkRGBA out;
59   gchar *res;
60   gchar *res_de;
61   gchar *res_en;
62   gchar *orig;
63
64   /* Using /255. values for the r, g, b components should
65    * make sure they round-trip exactly without rounding
66    * from the double => integer => double conversions */
67   rgba.red = 1.0;
68   rgba.green = 128/255.;
69   rgba.blue = 64/255.;
70   rgba.alpha = 0.5;
71
72   orig = g_strdup (setlocale (LC_ALL, NULL));
73   res = gdk_rgba_to_string (&rgba);
74   gdk_rgba_parse (&out, res);
75   g_assert (gdk_rgba_equal (&rgba, &out));
76
77   setlocale (LC_ALL, "de_DE.utf-8");
78   res_de = gdk_rgba_to_string (&rgba);
79   g_assert_cmpstr (res, ==, res_de);
80
81   setlocale (LC_ALL, "en_US.utf-8");
82   res_en = gdk_rgba_to_string (&rgba);
83   g_assert_cmpstr (res, ==, res_en);
84
85   g_free (res);
86   g_free (res_de);
87   g_free (res_en);
88
89   setlocale (LC_ALL, orig);
90   g_free (orig);
91 }
92
93 int
94 main (int argc, char *argv[])
95 {
96         g_test_init (&argc, &argv, NULL);
97
98         g_test_add_func ("/color/parse", test_color_parse);
99         g_test_add_func ("/color/to-string", test_color_to_string);
100
101         return g_test_run ();
102 }