2 * Copyright (c) 2011 Sveriges Television AB <info@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 "../frame/basic_frame.h"
\r
27 #include "../frame/frame_factory.h"
\r
28 #include "../../mixer/write_frame.h"
\r
30 #include <common/exception/exceptions.h>
\r
32 #include <boost/algorithm/string.hpp>
\r
36 namespace caspar { namespace core {
\r
38 class color_producer : public frame_producer
\r
40 safe_ptr<basic_frame> frame_;
\r
41 const std::string color_str_;
\r
44 explicit color_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::string& color)
\r
46 , frame_(create_color_frame(this, frame_factory, color))
\r
51 virtual safe_ptr<basic_frame> receive(int) override
\r
56 virtual safe_ptr<basic_frame> last_frame() const override
\r
61 virtual std::string print() const override
\r
63 return "color[" + color_str_ + "]";
\r
66 boost::property_tree::ptree info() const override
\r
68 boost::property_tree::ptree info;
\r
69 info.add("type", "color-producer");
\r
70 info.add("color", color_str_);
\r
75 std::string get_hex_color(const std::string& str)
\r
77 if(str.at(0) == '#')
\r
78 return str.length() == 7 ? "#FF" + str.substr(1) : str;
\r
80 if(iequals(str, "EMPTY"))
\r
83 if(iequals(str, "BLACK"))
\r
86 if(iequals(str, "WHITE"))
\r
89 if(iequals(str, "RED"))
\r
92 if(iequals(str, "GREEN"))
\r
95 if(iequals(str, "BLUE"))
\r
96 return "#FF0000FF";
\r
98 if(iequals(str, "ORANGE"))
\r
99 return "#FFFFA500";
\r
101 if(iequals(str, "YELLOW"))
\r
102 return "#FFFFFF00";
\r
104 if(iequals(str, "BROWN"))
\r
105 return "#FFA52A2A";
\r
107 if(iequals(str, "GRAY"))
\r
108 return "#FF808080";
\r
110 if(iequals(str, "TEA"))
\r
111 return "#FF008080";
\r
116 safe_ptr<frame_producer> create_color_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::string>& params)
\r
118 if(params.size() < 0)
\r
119 return core::frame_producer::empty();
\r
121 auto color2 = get_hex_color(params[0]);
\r
122 if(color2.length() != 9 || color2[0] != '#')
\r
123 return core::frame_producer::empty();
\r
125 return make_safe<color_producer>(frame_factory, color2);
\r
127 safe_ptr<core::write_frame> create_color_frame(void* tag, const safe_ptr<core::frame_factory>& frame_factory, const std::string& color)
\r
129 auto color2 = get_hex_color(color);
\r
130 if(color2.length() != 9 || color2[0] != '#')
\r
131 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info((color2)) << msg_info("Invalid color."));
\r
133 core::pixel_format_desc desc;
\r
134 desc.pix_fmt = pixel_format::bgra;
\r
135 desc.planes.push_back(core::pixel_format_desc::plane(1, 1, 4));
\r
136 auto frame = frame_factory->create_frame(tag, desc);
\r
138 // Read color from hex-string and write to frame pixel.
\r
140 auto& value = *reinterpret_cast<uint32_t*>(frame->image_data().begin());
\r
141 std::stringstream str(color2.substr(1));
\r
142 if(!(str >> std::hex >> value) || !str.eof())
\r
143 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info((color2)) << msg_info("Invalid color."));
\r