The Big Commit (tm): Remove Cairo.Color and most of Gdk.Color usage from libprolooks
[libprolooks.git] / demos / HelpersTest.vala
blobb8a4ec6a7133154f0b752e57479b1093b01ca996
1 /*
2 Copyright 2009 by Hans Baier
3 License: LGPLv2+
4 */
6 using Gtk;
7 using Gdk;
9 namespace Prolooks {
10 public static void helpers_unit_tests () {
11 Test.add_func ("/prolooks/helpers/rgba_from_string", () => {
12 RGBA red = rgba_from_string("#ff0000");
13 assert (red.red == 1.0);
14 assert (red.green == 0);
15 assert (red.blue == 0);
19 Test.add_func ("/prolooks/helpers/color_to_string", () => {
20 string original = "rgb(99,111,222)";
21 RGBA color = rgba_from_string(original);
22 string result = color.to_string();
23 stdout.printf(result);
24 assert (result == original);
26 original = "rgb(10,16,5)";
27 color = rgba_from_string(original);
28 result = color.to_string();
29 stdout.printf(result);
30 assert (result == original);
34 Test.add_func ("/prolooks/helpers/rgb2hsv <-> hsv2rgb", () => {
35 RGBA color = rgba_from_string ("rgb(255, 0, 0)");
36 HSV hsv = new HSV ();
37 hsv.from_rgba (color);
38 stdout.printf ("\ncolor %s is in hsv: %s\n", color.to_string(), hsv.to_string());
39 assert (hsv.hue == 0.0);
40 assert (hsv.saturation == 1.0);
41 assert (hsv.value == 1.0);
43 color = hsv.to_rgba ();
44 stdout.printf ("Converting back to RGB yields: %s\n", color.to_string());
45 assert (color.red == 1.0);
46 assert (color.green == 0);
47 assert (color.blue == 0);
49 color = rgba_from_string ("#00ff00");
50 hsv.from_rgba (color);
51 stdout.printf ("\ncolor %s is in hsv: %s\n", color.to_string(), hsv.to_string());
52 assert (hsv.hue == 120.0);
53 assert (hsv.saturation == 1.0);
54 assert (hsv.value == 1.0);
56 color = hsv.to_rgba ();
57 stdout.printf ("Converting back to RGB yields: %s\n", color.to_string());
58 assert (color.red == 0);
59 assert (color.green == 1.0);
60 assert (color.blue == 0);
62 color = rgba_from_string ("#0000ff");
63 hsv.from_rgba (color);
64 stdout.printf ("\ncolor %s is in hsv: %s\n", color.to_string(), hsv.to_string());
65 assert (hsv.hue == 240.0);
66 assert (hsv.saturation == 1.0);
67 assert (hsv.value == 1.0);
69 color = hsv.to_rgba ();
70 stdout.printf ("Converting back to RGB yields: %s\n", color.to_string());
71 assert (color.red == 0);
72 assert (color.green == 0);
73 assert (color.blue == 1.0);
75 color = rgba_from_string ("#ff00ff");
76 hsv.from_rgba (color);
77 stdout.printf ("\ncolor %s is in hsv: %s\n", color.to_string(), hsv.to_string());
78 assert (hsv.hue == 300.0);
79 assert (hsv.saturation == 1.0);
80 assert (hsv.value == 1.0);
82 color = hsv.to_rgba ();
83 stdout.printf ("Converting back to RGB yields: %s\n", color.to_string());
84 assert (color.red == 1.0);
85 assert (color.green == 0);
86 assert (color.blue == 1.0);
88 color = rgba_from_string ("#800080");
89 hsv.from_rgba (color);
90 stdout.printf ("\ncolor %s is in hsv: %s\n", color.to_string(), hsv.to_string());
91 assert (hsv.hue == 300.0);
92 assert (hsv.saturation == 1.0);
93 assert (Math.floor(hsv.value * 10.0) / 10.0 == 0.5);
95 color = hsv.to_rgba ();
96 stdout.printf ("Converting back to RGB yields: %s\n", color.to_string());
97 assert (color.to_string() == "rgb(128,0,128)");
99 color = rgba_from_string ("#ffff00");
100 hsv.from_rgba (color);
101 stdout.printf ("\ncolor %s is in hsv: %s\n", color.to_string(), hsv.to_string());
102 assert (hsv.hue == 60.0);
103 assert (hsv.saturation == 1.0);
104 assert (hsv.value == 1.0);
106 color = hsv.to_rgba ();
107 stdout.printf ("Converting back to RGB yields: %s\n", color.to_string());
108 assert (color.red == 1.0);
109 assert (color.green == 1.0);
110 assert (color.blue == 0);
112 color = rgba_from_string ("#00ffff");
113 hsv.from_rgba (color);
114 stdout.printf ("\ncolor %s is in hsv: %s\n", color.to_string(), hsv.to_string());
115 assert (hsv.hue == 180.0);
116 assert (hsv.saturation == 1.0);
117 assert (hsv.value == 1.0);
119 color = hsv.to_rgba ();
120 stdout.printf ("Converting back to RGB yields: %s\n", color.to_string());
121 assert (color.red == 0);
122 assert (color.green == 1.0);
123 assert (color.blue == 1.0);
125 color = rgba_from_string ("#00ECFF");
126 hsv.from_rgba (color);
127 stdout.printf ("\ncolor %s is in hsv: %s\n", color.to_string(), hsv.to_string());
128 assert (Math.floor(hsv.hue) == 184);
129 assert (hsv.saturation == 1.0);
130 assert (hsv.value == 1.0);
132 /* rounding errors here
133 color = hsv.to_rgba ();
134 stdout.printf ("Converting back to RGB yields: %s\n", color.to_string());
135 assert (color.red == 0);
136 assert (color.green == 61016);
137 assert (color.blue == 1.0);
140 color = rgba_from_string("#ffffff");
141 hsv.from_rgba (color);
142 stdout.printf ("\ncolor %s is in hsv: %s\n", color.to_string(), hsv.to_string());
143 assert (hsv.hue == 0.0);
144 assert (hsv.saturation == 0.0);
145 assert (hsv.value == 1.0);
150 Test.add_func ("/prolooks/helpers/rgb2hsl <-> hsl2rgb", () => {
151 RGBA color = rgba_from_string ("#ff0000");
152 HSL hsl = new HSL ();
153 hsl.from_rgba (color);
154 stdout.printf ("\ncolor %s is in hsl: %s\n", color.to_string(), hsl.to_string());
155 assert (hsl.hue == 0.0);
156 assert (hsl.saturation == 1.0);
157 assert (hsl.lightness == 0.5);
159 color = hsl.to_rgba ();
160 stdout.printf ("Converting back to RGB yields: %s\n", color.to_string());
161 assert (color.red == 1.0);
162 assert (color.green == 0);
163 assert (color.blue == 0);
165 color = rgba_from_string ("#00ff00");
166 hsl.from_rgba (color);
167 stdout.printf ("\ncolor %s is in hsl: %s\n", color.to_string(), hsl.to_string());
168 assert (hsl.hue == 120.0);
169 assert (hsl.saturation == 1.0);
170 assert (hsl.lightness == 0.5);
172 color = hsl.to_rgba ();
173 stdout.printf ("Converting back to RGB yields: %s\n", color.to_string());
174 assert (color.red == 0);
175 assert (color.green == 1.0);
176 assert (color.blue == 0);
178 color = rgba_from_string ("#0000ff");
179 hsl.from_rgba (color);
180 stdout.printf ("\ncolor %s is in hsl: %s\n", color.to_string(), hsl.to_string());
181 assert (hsl.hue == 240.0);
182 assert (hsl.saturation == 1.0);
183 assert (hsl.lightness == 0.5);
185 color = hsl.to_rgba ();
186 stdout.printf ("Converting back to RGB yields: %s\n", color.to_string());
187 assert (color.red == 0);
188 assert (color.green == 0);
189 assert (color.blue == 1.0);
192 color = rgba_from_string("#ffffff");
193 hsl.from_rgba (color);
194 stdout.printf ("\ncolor %s is in hsl: %s\n", color.to_string(), hsl.to_string());
195 assert (hsl.hue == 0.0);
196 assert (hsl.saturation == 0.0);
197 assert (hsl.lightness == 1.0);
202 Test.add_func ("/prolooks/helpers/shade", () => {
203 RGBA gray = rgba_from_string("#0a0a0a");
204 RGBA shaded = rgba_shade (gray, 1.0);
205 stdout.printf ("\noriginal color: %s\n", gray.to_string());
206 stdout.printf ("shaded color: %s\n", shaded.to_string());
207 assert (strcmp(shaded.to_string(), "rgb(10,10,10)") == 0);
209 shaded = rgba_shade (gray, 0.5);
210 stdout.printf ("\noriginal color: %s\n", gray.to_string());
211 stdout.printf ("shaded color: %s\n", shaded.to_string());
212 assert (strcmp(shaded.to_string(), "rgb(5,5,5)") == 0);
217 static int main (string[] args) {
218 Test.init (ref args);
219 helpers_unit_tests ();
220 Test.run ();
221 return 0;
224 } // namespace Prolooks