]> git.sesse.net Git - casparcg/blob - core/producer/color/color_producer.cpp
7e7fe3a3cf13c8ca25f3c944c04ac8d6f9826a6e
[casparcg] / core / producer / color / color_producer.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../../stdafx.h"\r
23 \r
24 #include "color_producer.h"\r
25 \r
26 #include <core/producer/frame_producer.h>\r
27 #include <core/frame/frame.h>\r
28 #include <core/frame/draw_frame.h>\r
29 #include <core/frame/frame_factory.h>\r
30 #include <core/frame/pixel_format.h>\r
31 #include <core/monitor/monitor.h>\r
32 \r
33 #include <common/except.h>\r
34 #include <common/array.h>\r
35 \r
36 #include <boost/algorithm/string.hpp>\r
37 \r
38 #include <sstream>\r
39 \r
40 namespace caspar { namespace core {\r
41         \r
42 class color_producer : public frame_producer_base\r
43 {\r
44         monitor::basic_subject  event_subject_;\r
45 \r
46         draw_frame                              frame_;\r
47         const std::wstring              color_str_;\r
48 \r
49 public:\r
50         explicit color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& color) \r
51                 : color_str_(color)\r
52                 , frame_(create_color_frame(this, frame_factory, color))\r
53         {\r
54                 CASPAR_LOG(info) << print() << L" Initialized";\r
55         }\r
56 \r
57         // frame_producer\r
58                         \r
59         draw_frame receive_impl() override\r
60         {\r
61                 event_subject_ << monitor::event("color") % color_str_;\r
62 \r
63                 return frame_;\r
64         }       \r
65         \r
66         std::wstring print() const override\r
67         {\r
68                 return L"color[" + color_str_ + L"]";\r
69         }\r
70 \r
71         std::wstring name() const override\r
72         {\r
73                 return L"color";\r
74         }\r
75         \r
76         boost::property_tree::wptree info() const override\r
77         {\r
78                 boost::property_tree::wptree info;\r
79                 info.add(L"type", L"color");\r
80                 info.add(L"color", color_str_);\r
81                 return info;\r
82         }\r
83 \r
84         void subscribe(const monitor::observable::observer_ptr& o) override                                                                                                                     \r
85         {\r
86                 return event_subject_.subscribe(o);\r
87         }\r
88 \r
89         void unsubscribe(const monitor::observable::observer_ptr& o) override           \r
90         {\r
91                 return event_subject_.unsubscribe(o);\r
92         }\r
93 };\r
94 \r
95 std::wstring get_hex_color(const std::wstring& str)\r
96 {\r
97         if(str.at(0) == '#')\r
98                 return str.length() == 7 ? L"#FF" + str.substr(1) : str;\r
99         \r
100         if(boost::iequals(str, L"EMPTY"))\r
101                 return L"#00000000";\r
102 \r
103         if(boost::iequals(str, L"BLACK"))\r
104                 return L"#FF000000";\r
105         \r
106         if(boost::iequals(str, L"WHITE"))\r
107                 return L"#FFFFFFFF";\r
108         \r
109         if(boost::iequals(str, L"RED"))\r
110                 return L"#FFFF0000";\r
111         \r
112         if(boost::iequals(str, L"GREEN"))\r
113                 return L"#FF00FF00";\r
114         \r
115         if(boost::iequals(str, L"BLUE"))\r
116                 return L"#FF0000FF";    \r
117         \r
118         if(boost::iequals(str, L"ORANGE"))\r
119                 return L"#FFFFA500";    \r
120         \r
121         if(boost::iequals(str, L"YELLOW"))\r
122                 return L"#FFFFFF00";\r
123         \r
124         if(boost::iequals(str, L"BROWN"))\r
125                 return L"#FFA52A2A";\r
126         \r
127         if(boost::iequals(str, L"GRAY"))\r
128                 return L"#FF808080";\r
129         \r
130         if(boost::iequals(str, L"TEAL"))\r
131                 return L"#FF008080";\r
132         \r
133         return str;\r
134 }\r
135 \r
136 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
137 {\r
138         if(params.size() < 0)\r
139                 return core::frame_producer::empty();\r
140 \r
141         auto color2 = get_hex_color(params[0]);\r
142         if(color2.length() != 9 || color2[0] != '#')\r
143                 return core::frame_producer::empty();\r
144 \r
145         return spl::make_shared<color_producer>(frame_factory, color2);\r
146 }\r
147 \r
148 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, const std::wstring& color)\r
149 {\r
150         auto color2 = get_hex_color(color);\r
151         if(color2.length() != 9 || color2[0] != '#')\r
152                 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(color2) << msg_info("Invalid color."));\r
153         \r
154         core::pixel_format_desc desc(pixel_format::bgra);\r
155         desc.planes.push_back(core::pixel_format_desc::plane(1, 1, 4));\r
156         auto frame = frame_factory->create_frame(tag, desc);\r
157                 \r
158         // Read color from hex-string and write to frame pixel.\r
159 \r
160         auto& value = *reinterpret_cast<uint32_t*>(frame.image_data(0).begin());\r
161         std::wstringstream str(color2.substr(1));\r
162         if(!(str >> std::hex >> value) || !str.eof())\r
163                 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(color2) << msg_info("Invalid color."));\r
164                         \r
165         return core::draw_frame(std::move(frame));\r
166 }\r
167 \r
168 }}