2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
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
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
20 #include "../../stdafx.h"
\r
22 #include "color_producer.h"
\r
24 #include "../frame/basic_frame.h"
\r
25 #include "../frame/frame_factory.h"
\r
26 #include "../../mixer/write_frame.h"
\r
28 #include <common/exception/exceptions.h>
\r
30 #include <boost/algorithm/string.hpp>
\r
34 namespace caspar { namespace core {
\r
36 class color_producer : public frame_producer
\r
38 safe_ptr<basic_frame> frame_;
\r
39 const std::wstring color_str_;
\r
42 explicit color_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& color)
\r
44 , frame_(create_color_frame(this, frame_factory, color))
\r
49 virtual safe_ptr<basic_frame> receive(int) { return frame_; }
\r
50 virtual safe_ptr<basic_frame> last_frame() const { return frame_; }
\r
51 virtual std::wstring print() const { return L"color[" + color_str_ + L"]"; }
\r
54 std::wstring get_hex_color(const std::wstring& str)
\r
56 if(str.at(0) == '#')
\r
57 return str.length() == 7 ? L"#FF" + str.substr(1) : str;
\r
59 if(boost::iequals(str, L"BLACK"))
\r
60 return L"#000000FF";
\r
62 if(boost::iequals(str, L"WHITE"))
\r
63 return L"#FFFFFFFF";
\r
65 if(boost::iequals(str, L"RED"))
\r
66 return L"#FFFF0000";
\r
68 if(boost::iequals(str, L"GREEN"))
\r
69 return L"#FF00FF00";
\r
71 if(boost::iequals(str, L"BLUE"))
\r
72 return L"#FF0000FF";
\r
74 if(boost::iequals(str, L"ORANGE"))
\r
75 return L"#FFFFA500";
\r
77 if(boost::iequals(str, L"YELLOW"))
\r
78 return L"#FFFFFF00";
\r
80 if(boost::iequals(str, L"BROWN"))
\r
81 return L"#FFA52A2A";
\r
83 if(boost::iequals(str, L"GRAY"))
\r
84 return L"#FF808080";
\r
86 if(boost::iequals(str, L"TEAL"))
\r
87 return L"#FF008080";
\r
92 safe_ptr<frame_producer> create_color_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)
\r
94 if(params.size() < 0)
\r
95 return core::frame_producer::empty();
\r
97 auto color2 = get_hex_color(params[0]);
\r
98 if(color2.length() != 9 || color2[0] != '#')
\r
99 return core::frame_producer::empty();
\r
101 return make_safe<color_producer>(frame_factory, color2);
\r
103 safe_ptr<core::write_frame> create_color_frame(void* tag, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& color)
\r
105 auto color2 = get_hex_color(color);
\r
106 if(color2.length() != 9 || color2[0] != '#')
\r
107 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(narrow(color2)) << msg_info("Invalid color."));
\r
109 auto frame = frame_factory->create_frame(tag, 1, 1, pixel_format::bgra);
\r
111 // Read color from hex-string and write to frame pixel.
\r
113 auto& value = *reinterpret_cast<uint32_t*>(frame->image_data().begin());
\r
114 std::wstringstream str(color2.substr(1));
\r
115 if(!(str >> std::hex >> value) || !str.eof())
\r
116 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(narrow(color2)) << msg_info("Invalid color."));
\r