1#include <fstream>
2#include <fastuidraw/util/fastuidraw_memory.hpp>
3
4#include "ostream_utility.hpp"
5#include "colorstop_command_line.hpp"
6#include "read_colorstops.hpp"
7
8/////////////////////////////////
9// color_stop_arguments methods
10color_stop_arguments::
11color_stop_arguments(command_line_register &parent):
12 command_line_argument(parent)
13{
14 m_add = produce_formatted_detailed_description("add_stop label place R G B A",
15 "where label is the name of the color stop "
16 "sequence place is \"time\" of color stop, "
17 "R, G, B and A are color value of color stop as"
18 "8-bit integers");
19 m_add2 = produce_formatted_detailed_description("add_stop_file filename",
20 "Creates a color stop from the specified file, "
21 "giving it the label as the file name");
22 m_disc = produce_formatted_detailed_description("discretization label N",
23 "where label is the name of the color stop sequence "
24 "and N is the discretization of the color stop "
25 "(default value is 16)");
26}
27
28
29color_stop_arguments::
30~color_stop_arguments()
31{
32 for(hoard::iterator iter = m_values.begin(), end = m_values.end(); iter != end; ++iter)
33 {
34 FASTUIDRAWdelete(iter->second);
35 }
36}
37
38color_stop_arguments::colorstop_data&
39color_stop_arguments::
40fetch(const std::string &pname)
41{
42 hoard::iterator iter;
43 iter = m_values.find(pname);
44 if (iter != m_values.end())
45 {
46 return *iter->second;
47 }
48
49 colorstop_data *p;
50 p = FASTUIDRAWnew colorstop_data();
51 m_values[pname] = p;
52 return *p;
53}
54
55int
56color_stop_arguments::
57check_arg(const std::vector<std::string> &args, int location)
58{
59 const std::string &str(args[location]);
60 if (static_cast<unsigned int>(location+6) < args.size() && str == "add_stop")
61 {
62 int R, G, B, A;
63 float t;
64 fastuidraw::u8vec4 color;
65 std::string name;
66
67 name = args[location + 1];
68 t = std::atof(args[location + 2].c_str());
69 R = std::atoi(args[location + 3].c_str());
70 G = std::atoi(args[location + 4].c_str());
71 B = std::atoi(args[location + 5].c_str());
72 A = std::atoi(args[location + 6].c_str());
73 color = fastuidraw::u8vec4(R, G, B, A);
74 std::cout << "\n[" << name << "] add color " << std::make_pair(t, fastuidraw::ivec4(color));
75
76 fetch(name).m_stops.add(fastuidraw::ColorStop(color, t));
77 return 7;
78 }
79 else if (static_cast<unsigned int>(location+1) < args.size() && str == "add_stop_file")
80 {
81 std::string name(args[location + 1]);
82 std::ifstream infile(name.c_str());
83 if (infile)
84 {
85 fetch(name).m_stops.clear();
86 read_colorstops(fetch(name).m_stops, infile);
87 std::cout << "\nAdd colorstop from file " << name;
88 }
89 return 2;
90 }
91 else if (static_cast<unsigned int>(location+2) < args.size() && str == "discretization")
92 {
93 std::string name;
94 int N;
95
96 name = args[location + 1];
97 N = std::atoi(args[location + 2].c_str());
98 std::cout << "\n[" << name << " discretization = " << N;
99
100 fetch(name).m_discretization = N;
101 return 3;
102 }
103 return 0;
104}
105
106void
107color_stop_arguments::
108print_command_line_description(std::ostream &ostr) const
109{
110 ostr << "[add_stop label place R G B A] [discretization label N]";
111}
112
113
114void
115color_stop_arguments::
116print_detailed_description(std::ostream &ostr) const
117{
118 ostr << m_add << "\n" << m_add2 << "\n" << m_disc;
119}
120