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 class color_producer : public frame_producer_base
47 monitor::subject monitor_subject_;
49 const std::wstring color_str_;
50 constraints constraints_;
54 color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, uint32_t value)
57 , frame_(create_color_frame(this, frame_factory, value))
59 CASPAR_LOG(info) << print() << L" Initialized";
63 color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& color)
66 , frame_(create_color_frame(this, frame_factory, color))
68 CASPAR_LOG(info) << print() << L" Initialized";
73 draw_frame receive_impl() override
75 monitor_subject_ << monitor::message("color") % color_str_;
80 constraints& pixel_constraints() override
85 std::wstring print() const override
87 return L"color[" + color_str_ + L"]";
90 std::wstring name() const override
95 boost::property_tree::wptree info() const override
97 boost::property_tree::wptree info;
98 info.add(L"type", L"color");
99 info.add(L"color", color_str_);
103 monitor::subject& monitor_output() override {return monitor_subject_;}
106 std::wstring get_hex_color(const std::wstring& str)
109 return str.length() == 7 ? L"#FF" + str.substr(1) : str;
111 std::wstring col_str = boost::to_upper_copy(str);
113 if(col_str == L"EMPTY")
116 if(col_str == L"BLACK")
119 if(col_str == L"WHITE")
122 if(col_str == L"RED")
125 if(col_str == L"GREEN")
128 if(col_str == L"BLUE")
131 if(col_str == L"ORANGE")
134 if(col_str == L"YELLOW")
137 if(col_str == L"BROWN")
140 if(col_str == L"GRAY")
143 if(col_str == L"TEAL")
149 bool try_get_color(const std::wstring& str, uint32_t& value)
151 auto color_str = get_hex_color(str);
152 if(color_str.length() != 9 || color_str[0] != '#')
155 std::wstringstream ss(color_str.substr(1));
156 if(!(ss >> std::hex >> value) || !ss.eof())
162 void describe_color_producer(core::help_sink& sink, const core::help_repository& repo)
164 sink.short_description(L"Fills a layer with a solid color.");
166 L"{#[argb_hex_value:string]}"
167 L",{#[rgb_hex_value:string]}"
168 L",{[named_color:EMPTY,BLACK,WHITE,RED,GREEN,BLUE,ORANGE,YELLOW,BROWN,GRAY,TEAL]}");
169 sink.para()->text(L"A producer that fills a layer with a solid color.");
171 ->text(L"If a ")->code(L"#")
172 ->text(L" sign followed by an 8 character hexadecimal number is given it is interpreted as an 8-bit per channel ARGB color. ")
173 ->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.");
174 sink.para()->text(L"There are also a number of predefined named colors, that are specified without a ")->code(L"#")->text(L" sign:");
176 ->item(L"EMPTY", L"Predefined ARGB value #00000000")
177 ->item(L"BLACK", L"Predefined ARGB value #FF000000")
178 ->item(L"WHITE", L"Predefined ARGB value #FFFFFFFF")
179 ->item(L"RED", L"Predefined ARGB value #FFFF0000")
180 ->item(L"GREEN", L"Predefined ARGB value #FF00FF00")
181 ->item(L"BLUE", L"Predefined ARGB value #FF0000FF")
182 ->item(L"ORANGE", L"Predefined ARGB value #FFFFA500")
183 ->item(L"YELLOW", L"Predefined ARGB value #FFFFFF00")
184 ->item(L"BROWN", L"Predefined ARGB value #FFA52A2A")
185 ->item(L"GRAY", L"Predefined ARGB value #FF808080")
186 ->item(L"TEAL", L"Predefined ARGB value #FF008080");
188 ->strong(L"Note")->text(L" that it is important that all RGB values are multiplied with the alpha ")
189 ->text(L"at all times, otherwise the compositing in the mixer will be incorrect.");
190 sink.para()->text(L"Examples:");
191 sink.example(L"PLAY 1-10 EMPTY", L"For a completely transparent frame.");
192 sink.example(L"PLAY 1-10 #00000000", L"For an explicitly defined completely transparent frame.");
193 sink.example(L"PLAY 1-10 #000000", L"For an explicitly defined completely opaque black frame.");
194 sink.example(L"PLAY 1-10 RED", L"For a completely red frame.");
195 sink.example(L"PLAY 1-10 #7F007F00",
196 L"For a completely green half transparent frame. "
197 L"Since the RGB part has been premultiplied with the A part this is 100% green.");
200 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, uint32_t value)
202 return spl::make_shared<color_producer>(frame_factory, value);
205 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, const std::vector<std::wstring>& params)
207 if(params.size() < 0)
208 return core::frame_producer::empty();
211 if(!try_get_color(params.at(0), value))
212 return core::frame_producer::empty();
214 return spl::make_shared<color_producer>(frame_factory, params.at(0));
217 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, uint32_t value)
219 core::pixel_format_desc desc(pixel_format::bgra);
220 desc.planes.push_back(core::pixel_format_desc::plane(1, 1, 4));
221 auto frame = frame_factory->create_frame(tag, desc, core::audio_channel_layout::invalid());
223 *reinterpret_cast<uint32_t*>(frame.image_data(0).begin()) = value;
225 return core::draw_frame(std::move(frame));
228 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, const std::wstring& str)
231 if(!try_get_color(str, value))
232 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Invalid color: " + str));
234 return create_color_frame(tag, frame_factory, value);