1 /* unit tests for the color parsing routines in gimprgb-parse.c
10 #include <gdk-pixbuf/gdk-pixbuf.h>
12 #include <glib-object.h>
15 #include "gimpcolor.h"
18 #define DBL(c) ((gdouble)(c) / 255.0)
32 static const ColorSample samples
[] =
34 /* sample alpha fail red green blue alpha */
36 { "#000000", FALSE
, FALSE
, 0.0, 0.0, 0.0, 0.0 },
37 { "#FFff00", FALSE
, FALSE
, 1.0, 1.0, 0.0, 0.0 },
38 { "#6495ed", FALSE
, FALSE
, DBL(100), DBL(149), DBL(237), 0.0 },
39 { "#fff", FALSE
, FALSE
, 1.0, 1.0, 1.0, 0.0 },
40 { "#64649595eded", FALSE
, FALSE
, 1.0, 1.0, 0.0, 0.0 },
41 { "rgb(0,0,0)", FALSE
, FALSE
, 0.0, 0.0, 0.0, 0.0 },
42 { "rgb(100,149,237)", FALSE
, FALSE
, DBL(100), DBL(149), DBL(237), 0.0 },
43 { "rgba(100%,0,100%,0.5)", TRUE
, FALSE
, 255.0, 0.0, 255.0, 0.5 },
44 { "rgba(100%,0,100%,0.5)", FALSE
, TRUE
, 255.0, 0.0, 255.0, 0.5 },
45 { "rgb(100%,149,20%)", FALSE
, FALSE
, 1.0, DBL(149), 0.2, 0.0 },
46 { "rgb(100%,149,20%)", TRUE
, TRUE
, 1.0, DBL(149), 0.2, 0.0 },
47 { "rgb(foobar)", FALSE
, TRUE
, 0.0, 0.0, 0.0, 0.0 },
48 { "rgb(100,149,237", FALSE
, TRUE
, 0.0, 0.0, 0.0, 0.0 },
49 { "rED", FALSE
, FALSE
, 1.0, 0.0, 0.0, 0.0 },
50 { "cornflowerblue", FALSE
, FALSE
, DBL(100), DBL(149), DBL(237), 0.0 },
51 { " red", FALSE
, FALSE
, 1.0, 0.0, 0.0, 0.0 },
52 { "red ", FALSE
, FALSE
, 1.0, 0.0, 0.0, 0.0 },
53 { "red", TRUE
, TRUE
, 1.0, 0.0, 0.0, 0.0 },
54 { "red blue", FALSE
, TRUE
, 0.0, 0.0, 0.0, 0.0 },
55 { "transparent", FALSE
, TRUE
, 0.0, 0.0, 0.0, 0.0 },
56 { "transparent", TRUE
, FALSE
, 0.0, 0.0, 0.0, 0.0 },
57 { "23foobar", FALSE
, TRUE
, 0.0, 0.0, 0.0, 0.0 },
58 { "", FALSE
, TRUE
, 0.0, 0.0, 0.0, 0.0 }
63 check_failure (const ColorSample
*sample
,
67 if (success
&& sample
->fail
)
69 g_print ("Parser succeeded for sample \"%s\" but should have failed!\n"
70 " parsed color: (%g, %g, %g, %g)\n",
71 sample
->str
, rgb
->r
, rgb
->g
, rgb
->b
, rgb
->a
);
75 if (!success
&& !sample
->fail
)
77 g_print ("Parser failed for sample \"%s\" but should have succeeded!\n"
78 " parsed color: (%g, %g, %g, %g)\n",
79 sample
->str
, rgb
->r
, rgb
->g
, rgb
->b
, rgb
->a
);
92 g_print ("\nTesting the GIMP color parser ...\n");
94 for (i
= 0; i
< G_N_ELEMENTS (samples
); i
++)
96 GimpRGB rgb
= { 0.0, 0.0, 0.0, 0.0 };
100 success
= gimp_rgba_parse_css (&rgb
, samples
[i
].str
, -1);
102 success
= gimp_rgb_parse_css (&rgb
, samples
[i
].str
, -1);
104 failures
+= check_failure (samples
+ i
, success
, &rgb
);
109 g_print ("%d out of %d samples failed!\n\n",
110 failures
, (int)G_N_ELEMENTS (samples
));
115 g_print ("All %d samples passed.\n\n", (int)G_N_ELEMENTS (samples
));