]> git.sesse.net Git - casparcg/blob - core/producer/color/color_producer.cpp
78c89a53c3842579568f9e34c5064b0e401dc4f4
[casparcg] / core / producer / color / color_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "color_producer.h"
25
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>
35
36 #include <common/except.h>
37 #include <common/array.h>
38
39 #include <boost/algorithm/string.hpp>
40
41 #include <sstream>
42
43 namespace caspar { namespace core {
44
45 class color_producer : public frame_producer_base
46 {
47         monitor::subject                monitor_subject_;
48
49         const std::wstring              color_str_;
50         constraints                             constraints_;
51         draw_frame                              frame_;
52
53 public:
54         color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, uint32_t value)
55                 : color_str_(L"")
56                 , constraints_(1, 1)
57                 , frame_(create_color_frame(this, frame_factory, value))
58         {
59                 CASPAR_LOG(info) << print() << L" Initialized";
60         }
61
62
63         color_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& color) 
64                 : color_str_(color)
65                 , constraints_(1, 1)
66                 , frame_(create_color_frame(this, frame_factory, color))
67         {
68                 CASPAR_LOG(info) << print() << L" Initialized";
69         }
70
71         // frame_producer
72                         
73         draw_frame receive_impl() override
74         {
75                 monitor_subject_ << monitor::message("color") % color_str_;
76
77                 return frame_;
78         }
79
80         constraints& pixel_constraints() override
81         {
82                 return constraints_;
83         }
84         
85         std::wstring print() const override
86         {
87                 return L"color[" + color_str_ + L"]";
88         }
89
90         std::wstring name() const override
91         {
92                 return L"color";
93         }
94         
95         boost::property_tree::wptree info() const override
96         {
97                 boost::property_tree::wptree info;
98                 info.add(L"type", L"color");
99                 info.add(L"color", color_str_);
100                 return info;
101         }
102
103         monitor::subject& monitor_output() override {return monitor_subject_;}                                                                          
104 };
105
106 std::wstring get_hex_color(const std::wstring& str)
107 {
108         if(str.at(0) == '#')
109                 return str.length() == 7 ? L"#FF" + str.substr(1) : str;
110         
111         std::wstring col_str = boost::to_upper_copy(str);
112
113         if(col_str == L"EMPTY")
114                 return L"#00000000";
115
116         if(col_str == L"BLACK")
117                 return L"#FF000000";
118         
119         if(col_str == L"WHITE")
120                 return L"#FFFFFFFF";
121         
122         if(col_str == L"RED")
123                 return L"#FFFF0000";
124         
125         if(col_str == L"GREEN")
126                 return L"#FF00FF00";
127         
128         if(col_str == L"BLUE")
129                 return L"#FF0000FF";    
130         
131         if(col_str == L"ORANGE")
132                 return L"#FFFFA500";    
133         
134         if(col_str == L"YELLOW")
135                 return L"#FFFFFF00";
136         
137         if(col_str == L"BROWN")
138                 return L"#FFA52A2A";
139         
140         if(col_str == L"GRAY")
141                 return L"#FF808080";
142         
143         if(col_str == L"TEAL")
144                 return L"#FF008080";
145         
146         return str;
147 }
148
149 bool try_get_color(const std::wstring& str, uint32_t& value)
150 {
151         auto color_str = get_hex_color(str);
152         if(color_str.length() != 9 || color_str[0] != '#')
153                 return false;
154
155         std::wstringstream ss(color_str.substr(1));
156         if(!(ss >> std::hex >> value) || !ss.eof())
157                 return false;
158         
159         return true;
160 }
161
162 void describe_color_producer(core::help_sink& sink, const core::help_repository& repo)
163 {
164         sink.short_description(L"Fills a layer with a solid color.");
165         sink.syntax(
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.");
170         sink.para()
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:");
175         sink.definitions()
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");
187         sink.para()
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.");
198 }
199
200 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, uint32_t value)
201 {
202         return spl::make_shared<color_producer>(frame_factory, value);
203 }
204
205 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, const std::vector<std::wstring>& params)
206 {
207         if(params.size() < 0)
208                 return core::frame_producer::empty();
209
210         uint32_t value = 0;
211         if(!try_get_color(params.at(0), value))
212                 return core::frame_producer::empty();
213
214         return spl::make_shared<color_producer>(frame_factory, params.at(0));
215 }
216
217 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, uint32_t value)
218 {
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());
222         
223         *reinterpret_cast<uint32_t*>(frame.image_data(0).begin()) = value;
224
225         return core::draw_frame(std::move(frame));
226 }
227
228 draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, const std::wstring& str)
229 {
230         uint32_t value = 0;
231         if(!try_get_color(str, value))
232                 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Invalid color: " + str));
233         
234         return create_color_frame(tag, frame_factory, value);
235 }
236
237 }}