1 #include <simgear_config.h>
2 #include <simgear/compiler.h>
3 
4 #include "parse_color.hxx"
5 #include "ColorInterpolator.hxx"
6 #include <simgear/props/props.hxx>
7 #include <simgear/misc/test_macros.hxx>
8 
9 #include <iostream>
10 #include <memory>
11 
12 #define VERIFY_COLOR(str, r, g, b, a) \
13   SG_VERIFY(simgear::parseColor(str, color)) \
14   SG_CHECK_EQUAL_NOSTREAM(color, osg::Vec4(r, g, b, a))
15 
16 #define VERIFY_NODE_STR(node, str) \
17   SG_CHECK_EQUAL(node.getStringValue(), std::string(str))
18 
main(int ac,char ** av)19 int main (int ac, char ** av)
20 {
21   osg::Vec4 color;
22   VERIFY_COLOR("#ff0000", 1,0,0,1);
23   VERIFY_COLOR("#00ff00", 0,1,0,1);
24   VERIFY_COLOR("#0000ff", 0,0,1,1);
25   VERIFY_COLOR("rgb( 255,\t127.5,0)", 1, 0.5, 0, 1);
26   VERIFY_COLOR("rgba(255,  127.5,0, 0.5)", 1, 0.5, 0, 0.5);
27 
28   SGPropertyNode color_node, color_arg;
29   color_arg.setStringValue("#000000");
30 
31   auto interp = std::unique_ptr<simgear::ColorInterpolator>(new simgear::ColorInterpolator);
32   interp->reset(color_arg);
33 
34   interp->update(color_node, 0.5); // with no color it should immediately set to the target
35   VERIFY_NODE_STR(color_node, "rgb(0,0,0)");
36 
37   color_arg.setStringValue("rgba(255,0,0,0.5)");
38   interp->reset(color_arg);
39 
40   interp->update(color_node, 0.5);
41   VERIFY_NODE_STR(color_node, "rgba(127,0,0,0.75)");
42 
43   interp->update(color_node, 0.5);
44   VERIFY_NODE_STR(color_node, "rgba(255,0,0,0.5)");
45 
46   // Animation has already completed and therefore should be reset and start a
47   // new animation starting with the current value of the animation. As this
48   // is already the same as the target value, nothing should change.
49   interp->update(color_node, 0.5);
50   VERIFY_NODE_STR(color_node, "rgba(255,0,0,0.5)");
51 
52   color_arg.setStringValue("#00ff00");
53   interp->reset(color_arg);
54   interp->update(color_node, 1.0);
55   VERIFY_NODE_STR(color_node, "rgb(0,255,0)");
56 
57   std::cout << "all tests passed successfully!" << std::endl;
58   return 0;
59 }
60