2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../../stdafx.h"
24 #include "color_producer.h"
26 #include <core/producer/frame_producer.h>
27 #include <core/frame/frame.h>
28 #include <core/frame/draw_frame.h>
29 #include <core/frame/frame_factory.h>
30 #include <core/frame/pixel_format.h>
31 #include <core/monitor/monitor.h>
33 #include <common/except.h>
34 #include <common/array.h>
36 #include <boost/algorithm/string.hpp>
40 namespace caspar { namespace core {
42 class color_producer : public frame_producer_base
44 monitor::subject monitor_subject_;
46 const std::wstring color_str_;
47 constraints constraints_;
51 color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, uint32_t value)
54 , frame_(create_color_frame(this, frame_factory, value))
56 CASPAR_LOG(info) << print() << L" Initialized";
60 color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& color)
63 , frame_(create_color_frame(this, frame_factory, color))
65 CASPAR_LOG(info) << print() << L" Initialized";
70 draw_frame receive_impl() override
72 monitor_subject_ << monitor::message("color") % color_str_;
77 constraints& pixel_constraints() override
82 std::wstring print() const override
84 return L"color[" + color_str_ + L"]";
87 std::wstring name() const override
92 boost::property_tree::wptree info() const override
94 boost::property_tree::wptree info;
95 info.add(L"type", L"color");
96 info.add(L"color", color_str_);
100 monitor::source& monitor_output() override {return monitor_subject_;}
103 std::wstring get_hex_color(const std::wstring& str)
106 return str.length() == 7 ? L"#FF" + str.substr(1) : str;
108 std::wstring col_str = boost::to_upper_copy(str);
110 if(col_str == L"EMPTY")
113 if(col_str == L"BLACK")
116 if(col_str == L"WHITE")
119 if(col_str == L"RED")
122 if(col_str == L"GREEN")
125 if(col_str == L"BLUE")
128 if(col_str == L"ORANGE")
131 if(col_str == L"YELLOW")
134 if(col_str == L"BROWN")
137 if(col_str == L"GRAY")
140 if(col_str == L"TEAL")
146 bool try_get_color(const std::wstring& str, uint32_t& value)
148 auto color_str = get_hex_color(str);
149 if(color_str.length() != 9 || color_str[0] != '#')
152 std::wstringstream ss(color_str.substr(1));
153 if(!(ss >> std::hex >> value) || !ss.eof())
159 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, uint32_t value)
161 return spl::make_shared<color_producer>(frame_factory, value);
164 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, const std::vector<std::wstring>& params)
166 if(params.size() < 0)
167 return core::frame_producer::empty();
170 if(!try_get_color(params[0], value))
171 return core::frame_producer::empty();
173 return spl::make_shared<color_producer>(frame_factory, params[0]);
176 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, uint32_t value)
178 core::pixel_format_desc desc(pixel_format::bgra);
179 desc.planes.push_back(core::pixel_format_desc::plane(1, 1, 4));
180 auto frame = frame_factory->create_frame(tag, desc);
182 *reinterpret_cast<uint32_t*>(frame.image_data(0).begin()) = value;
184 return core::draw_frame(std::move(frame));
187 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, const std::wstring& str)
190 if(!try_get_color(str, value))
191 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(str) << msg_info("Invalid color."));
193 return create_color_frame(tag, frame_factory, value);