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 <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
32 #include <common/except.h>
\r
34 #include <boost/algorithm/string.hpp>
\r
38 namespace caspar { namespace core {
\r
40 class color_producer : public frame_producer
\r
43 const std::wstring color_str_;
\r
46 explicit color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& color)
\r
48 , frame_(create_color_frame(this, frame_factory, color))
\r
50 CASPAR_LOG(info) << print() << L" Initialized";
\r
55 virtual draw_frame receive(int) override
\r
60 virtual std::wstring print() const override
\r
62 return L"color[" + color_str_ + L"]";
\r
65 virtual std::wstring name() const override
\r
70 virtual draw_frame last_frame() const override
\r
75 boost::property_tree::wptree info() const override
\r
77 boost::property_tree::wptree info;
\r
78 info.add(L"type", L"color");
\r
79 info.add(L"color", color_str_);
\r
84 std::wstring get_hex_color(const std::wstring& str)
\r
86 if(str.at(0) == '#')
\r
87 return str.length() == 7 ? L"#FF" + str.substr(1) : str;
\r
89 if(boost::iequals(str, L"EMPTY"))
\r
90 return L"#00000000";
\r
92 if(boost::iequals(str, L"BLACK"))
\r
93 return L"#FF000000";
\r
95 if(boost::iequals(str, L"WHITE"))
\r
96 return L"#FFFFFFFF";
\r
98 if(boost::iequals(str, L"RED"))
\r
99 return L"#FFFF0000";
\r
101 if(boost::iequals(str, L"GREEN"))
\r
102 return L"#FF00FF00";
\r
104 if(boost::iequals(str, L"BLUE"))
\r
105 return L"#FF0000FF";
\r
107 if(boost::iequals(str, L"ORANGE"))
\r
108 return L"#FFFFA500";
\r
110 if(boost::iequals(str, L"YELLOW"))
\r
111 return L"#FFFFFF00";
\r
113 if(boost::iequals(str, L"BROWN"))
\r
114 return L"#FFA52A2A";
\r
116 if(boost::iequals(str, L"GRAY"))
\r
117 return L"#FF808080";
\r
119 if(boost::iequals(str, L"TEAL"))
\r
120 return L"#FF008080";
\r
125 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, const std::vector<std::wstring>& params)
\r
127 if(params.size() < 0)
\r
128 return core::frame_producer::empty();
\r
130 auto color2 = get_hex_color(params[0]);
\r
131 if(color2.length() != 9 || color2[0] != '#')
\r
132 return core::frame_producer::empty();
\r
134 return spl::make_shared<color_producer>(frame_factory, color2);
\r
137 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, const std::wstring& color)
\r
139 auto color2 = get_hex_color(color);
\r
140 if(color2.length() != 9 || color2[0] != '#')
\r
141 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(color2) << msg_info("Invalid color."));
\r
143 core::pixel_format_desc desc(pixel_format::bgra);
\r
144 desc.planes.push_back(core::pixel_format_desc::plane(1, 1, 4));
\r
145 auto frame = frame_factory->create_frame(tag, desc);
\r
147 // Read color from hex-string and write to frame pixel.
\r
149 auto& value = *reinterpret_cast<uint32_t*>(frame.image_data(0).begin());
\r
150 std::wstringstream str(color2.substr(1));
\r
151 if(!(str >> std::hex >> value) || !str.eof())
\r
152 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(color2) << msg_info("Invalid color."));
\r
154 return core::draw_frame(std::move(frame));
\r