]> git.mxchange.org Git - simgear.git/blob - simgear/scene/util/parse_color_test.cxx
Work around apparent OSG 3.2.0 normal binding bug.
[simgear.git] / simgear / scene / util / parse_color_test.cxx
1 #include <simgear/compiler.h>
2
3 #include "parse_color.hxx"
4 #include "ColorInterpolator.hxx"
5 #include <simgear/props/props.hxx>
6
7 #include <iostream>
8
9 #define COMPARE(a, b) \
10   if( (a) != (b) ) \
11   { \
12     std::cerr << "line " << __LINE__ << ": failed: "\
13               << #a << " != " << #b << std::endl; \
14     return 1; \
15   }
16
17 #define VERIFY(a) \
18   if( !(a) ) \
19   { \
20     std::cerr << "line " << __LINE__ << ": failed: "\
21               << #a << std::endl; \
22     return 1; \
23   }
24
25 #define VERIFY_COLOR(str, r, g, b, a) \
26   VERIFY(simgear::parseColor(str, color)) \
27   COMPARE(color, osg::Vec4(r, g, b, a))
28
29 #define VERIFY_NODE_STR(node, str) \
30   COMPARE(node.getStringValue(), std::string(str))
31     
32 int main (int ac, char ** av)
33 {
34   osg::Vec4 color;
35   VERIFY_COLOR("#ff0000", 1,0,0,1);
36   VERIFY_COLOR("#00ff00", 0,1,0,1);
37   VERIFY_COLOR("#0000ff", 0,0,1,1);
38   VERIFY_COLOR("rgb( 255,\t127.5,0)", 1, 0.5, 0, 1);
39   VERIFY_COLOR("rgba(255,  127.5,0, 0.5)", 1, 0.5, 0, 0.5);
40
41   SGPropertyNode color_node, color_arg;
42   color_arg.setStringValue("#000000");
43
44   simgear::PropertyInterpolator* interp = new simgear::ColorInterpolator;
45   interp->reset(color_arg);
46
47   interp->update(color_node, 0.5); // with no color it should immediately set to the target
48   VERIFY_NODE_STR(color_node, "rgb(0,0,0)");
49
50   color_arg.setStringValue("rgba(255,0,0,0.5)");
51   interp->reset(color_arg);
52
53   interp->update(color_node, 0.5);
54   VERIFY_NODE_STR(color_node, "rgba(127,0,0,0.75)");
55
56   interp->update(color_node, 0.5);
57   VERIFY_NODE_STR(color_node, "rgba(255,0,0,0.5)");
58
59   // Animation has already completed and therefore should be reset and start a
60   // new animation starting with the current value of the animation. As this
61   // is already the same as the target value, nothing should change.
62   interp->update(color_node, 0.5);
63   VERIFY_NODE_STR(color_node, "rgba(255,0,0,0.5)");
64
65   color_arg.setStringValue("#00ff00");
66   interp->reset(color_arg);
67   interp->update(color_node, 1.0);
68   VERIFY_NODE_STR(color_node, "rgb(0,255,0)");
69
70   std::cout << "all tests passed successfully!" << std::endl;
71   return 0;
72 }