2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../../stdafx.h"
\r
24 #include "color_producer.h"
\r
26 #include "../../monitor/monitor.h"
\r
28 #include "../frame/basic_frame.h"
\r
29 #include "../frame/frame_factory.h"
\r
30 #include "../../mixer/write_frame.h"
\r
32 #include <common/exception/exceptions.h>
\r
34 #include <core/parameters/parameters.h>
\r
36 #include <boost/algorithm/string.hpp>
\r
40 namespace caspar { namespace core {
\r
42 class color_producer : public frame_producer
\r
44 monitor::subject monitor_subject_;
\r
45 safe_ptr<basic_frame> frame_;
\r
46 const std::wstring color_str_;
\r
49 explicit color_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& color)
\r
51 , frame_(create_color_frame(this, frame_factory, color))
\r
57 virtual safe_ptr<basic_frame> receive(int) override
\r
59 monitor_subject_ << monitor::message("/color") % color_str_;
\r
64 virtual safe_ptr<basic_frame> last_frame() const override
\r
69 virtual std::wstring print() const override
\r
71 return L"color[" + color_str_ + L"]";
\r
74 boost::property_tree::wptree info() const override
\r
76 boost::property_tree::wptree info;
\r
77 info.add(L"type", L"color-producer");
\r
78 info.add(L"color", color_str_);
\r
82 monitor::subject& monitor_output()
\r
84 return monitor_subject_;
\r
88 std::wstring get_hex_color(const std::wstring& str)
\r
90 if(str.at(0) == '#')
\r
91 return str.length() == 7 ? L"#FF" + str.substr(1) : str;
\r
93 if(boost::iequals(str, L"EMPTY"))
\r
94 return L"#00000000";
\r
96 if(boost::iequals(str, L"BLACK"))
\r
97 return L"#FF000000";
\r
99 if(boost::iequals(str, L"WHITE"))
\r
100 return L"#FFFFFFFF";
\r
102 if(boost::iequals(str, L"RED"))
\r
103 return L"#FFFF0000";
\r
105 if(boost::iequals(str, L"GREEN"))
\r
106 return L"#FF00FF00";
\r
108 if(boost::iequals(str, L"BLUE"))
\r
109 return L"#FF0000FF";
\r
111 if(boost::iequals(str, L"ORANGE"))
\r
112 return L"#FFFFA500";
\r
114 if(boost::iequals(str, L"YELLOW"))
\r
115 return L"#FFFFFF00";
\r
117 if(boost::iequals(str, L"BROWN"))
\r
118 return L"#FFA52A2A";
\r
120 if(boost::iequals(str, L"GRAY"))
\r
121 return L"#FF808080";
\r
123 if(boost::iequals(str, L"TEAL"))
\r
124 return L"#FF008080";
\r
129 safe_ptr<frame_producer> create_color_producer(
\r
130 const safe_ptr<core::frame_factory>& frame_factory,
\r
131 const core::parameters& params)
\r
134 return core::frame_producer::empty();
\r
136 auto color2 = get_hex_color(params[0]);
\r
137 if(color2.length() != 9 || color2[0] != '#')
\r
138 return core::frame_producer::empty();
\r
140 return create_producer_print_proxy(
\r
141 make_safe<color_producer>(frame_factory, color2));
\r
143 safe_ptr<core::write_frame> create_color_frame(void* tag, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& color)
\r
145 auto color2 = get_hex_color(color);
\r
146 if(color2.length() != 9 || color2[0] != '#')
\r
147 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(narrow(color2)) << msg_info("Invalid color."));
\r
149 core::pixel_format_desc desc;
\r
150 desc.pix_fmt = pixel_format::bgra;
\r
151 desc.planes.push_back(core::pixel_format_desc::plane(1, 1, 4));
\r
152 auto frame = frame_factory->create_frame(tag, desc);
\r
154 // Read color from hex-string and write to frame pixel.
\r
156 auto& value = *reinterpret_cast<uint32_t*>(frame->image_data().begin());
\r
157 std::wstringstream str(color2.substr(1));
\r
158 if(!(str >> std::hex >> value) || !str.eof())
\r
159 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(narrow(color2)) << msg_info("Invalid color."));
\r