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/frame/audio_channel_layout.h>
32 #include <core/monitor/monitor.h>
33 #include <core/help/help_repository.h>
34 #include <core/help/help_sink.h>
36 #include <common/except.h>
37 #include <common/array.h>
39 #include <boost/algorithm/string.hpp>
43 namespace caspar { namespace core {
45 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, const std::vector<uint32_t>& values)
47 core::pixel_format_desc desc(pixel_format::bgra);
48 desc.planes.push_back(core::pixel_format_desc::plane(static_cast<int>(values.size()), 1, 4));
49 auto frame = frame_factory->create_frame(tag, desc, core::audio_channel_layout::invalid());
51 for (int i = 0; i < values.size(); ++i)
52 *reinterpret_cast<uint32_t*>(frame.image_data(0).begin() + (i * 4)) = values.at(i);
54 return core::draw_frame(std::move(frame));
57 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, uint32_t value)
59 std::vector<uint32_t> values = { value };
61 return create_color_frame(tag, frame_factory, values);
64 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, const std::vector<std::wstring>& strs)
66 std::vector<uint32_t> values(strs.size());
68 for (int i = 0; i < values.size(); ++i)
70 if (!try_get_color(strs.at(i), values.at(i)))
71 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Invalid color: " + strs.at(i)));
74 return create_color_frame(tag, frame_factory, values);
77 class color_producer : public frame_producer_base
79 monitor::subject monitor_subject_;
81 const std::wstring color_str_;
82 constraints constraints_;
86 color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, uint32_t value)
89 , frame_(create_color_frame(this, frame_factory, value))
91 CASPAR_LOG(info) << print() << L" Initialized";
94 color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& colors)
95 : color_str_(boost::join(colors, L", "))
96 , constraints_(static_cast<int>(colors.size()), 1)
97 , frame_(create_color_frame(this, frame_factory, colors))
99 CASPAR_LOG(info) << print() << L" Initialized";
104 draw_frame receive_impl() override
106 monitor_subject_ << monitor::message("color") % color_str_;
111 constraints& pixel_constraints() override
116 std::wstring print() const override
118 return L"color[" + color_str_ + L"]";
121 std::wstring name() const override
126 boost::property_tree::wptree info() const override
128 boost::property_tree::wptree info;
129 info.add(L"type", L"color");
130 info.add(L"color", color_str_);
134 monitor::subject& monitor_output() override {return monitor_subject_;}
137 std::wstring get_hex_color(const std::wstring& str)
140 return str.length() == 7 ? L"#FF" + str.substr(1) : str;
142 std::wstring col_str = boost::to_upper_copy(str);
144 if(col_str == L"EMPTY")
147 if(col_str == L"BLACK")
150 if(col_str == L"WHITE")
153 if(col_str == L"RED")
156 if(col_str == L"GREEN")
159 if(col_str == L"BLUE")
162 if(col_str == L"ORANGE")
165 if(col_str == L"YELLOW")
168 if(col_str == L"BROWN")
171 if(col_str == L"GRAY")
174 if(col_str == L"TEAL")
180 bool try_get_color(const std::wstring& str, uint32_t& value)
182 auto color_str = get_hex_color(str);
183 if(color_str.length() != 9 || color_str[0] != '#')
186 std::wstringstream ss(color_str.substr(1));
187 if(!(ss >> std::hex >> value) || !ss.eof())
193 void describe_color_producer(core::help_sink& sink, const core::help_repository& repo)
195 sink.short_description(L"Fills a layer with a solid color or a horizontal gradient.");
197 L"{#[argb_hex_value:string]}"
198 L",{#[rgb_hex_value:string]}"
199 L",{[named_color:EMPTY,BLACK,WHITE,RED,GREEN,BLUE,ORANGE,YELLOW,BROWN,GRAY,TEAL]} "
200 L"{...more colors}");
201 sink.para()->text(L"A producer that fills a layer with a solid color. If more than one color is specified it becomes a gradient between those colors.");
203 ->text(L"If a ")->code(L"#")
204 ->text(L" sign followed by an 8 character hexadecimal number is given it is interpreted as an 8-bit per channel ARGB color. ")
205 ->text(L"If it is instead followed by a 6 character hexadecimal number it is interpreted as an 8-bit per channel RGB value with an opaque alpha channel.");
206 sink.para()->text(L"There are also a number of predefined named colors, that are specified without a ")->code(L"#")->text(L" sign:");
208 ->item(L"EMPTY", L"Predefined ARGB value #00000000")
209 ->item(L"BLACK", L"Predefined ARGB value #FF000000")
210 ->item(L"WHITE", L"Predefined ARGB value #FFFFFFFF")
211 ->item(L"RED", L"Predefined ARGB value #FFFF0000")
212 ->item(L"GREEN", L"Predefined ARGB value #FF00FF00")
213 ->item(L"BLUE", L"Predefined ARGB value #FF0000FF")
214 ->item(L"ORANGE", L"Predefined ARGB value #FFFFA500")
215 ->item(L"YELLOW", L"Predefined ARGB value #FFFFFF00")
216 ->item(L"BROWN", L"Predefined ARGB value #FFA52A2A")
217 ->item(L"GRAY", L"Predefined ARGB value #FF808080")
218 ->item(L"TEAL", L"Predefined ARGB value #FF008080");
220 ->strong(L"Note")->text(L" that it is important that all RGB values are multiplied with the alpha ")
221 ->text(L"at all times, otherwise the compositing in the mixer will be incorrect.");
222 sink.para()->text(L"Solid color examples:");
223 sink.example(L">> PLAY 1-10 EMPTY", L"For a completely transparent frame.");
224 sink.example(L">> PLAY 1-10 #00000000", L"For an explicitly defined completely transparent frame.");
225 sink.example(L">> PLAY 1-10 #000000", L"For an explicitly defined completely opaque black frame.");
226 sink.example(L">> PLAY 1-10 RED", L"For a completely red frame.");
227 sink.example(L">> PLAY 1-10 #7F007F00",
228 L"For a completely green half transparent frame. "
229 L"Since the RGB part has been premultiplied with the A part this is 100% green.");
230 sink.para()->text(L"Horizontal gradient examples:");
231 sink.example(L">> PLAY 1-10 WHITE BLACK", L"For a gradient between white and black.");
232 sink.example(L">> PLAY 1-10 WHITE WHITE WHITE BLACK", L"For a gradient between white and black with the white part beings 3 times as long as the black part.");
234 L">> PLAY 1-10 RED EMPTY\n"
235 L">> MIXER 1-10 ANCHOR 0.5 0.5\n"
236 L">> MIXER 1-10 FILL 0.5 0.5 2 2\n"
237 L">> MIXER 1-10 ROTATION 45",
238 L"For a 45 degree gradient covering the screen.");
241 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, uint32_t value)
243 return spl::make_shared<color_producer>(frame_factory, value);
246 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, const std::vector<std::wstring>& params)
248 if(params.size() < 0)
249 return core::frame_producer::empty();
252 if(!try_get_color(params.at(0), value))
253 return core::frame_producer::empty();
255 std::vector<std::wstring> colors;
257 for (auto& param : params)
259 if (try_get_color(param, value))
260 colors.push_back(param);
263 return spl::make_shared<color_producer>(frame_factory, colors);