]> git.sesse.net Git - casparcg/commitdiff
* exposed function bool try_get_color(const std::wstring& str, uint32_t& value) from...
authorniklaspandersson <niklas.p.andersson@svt.se>
Thu, 8 Aug 2013 07:22:39 +0000 (09:22 +0200)
committerniklaspandersson <niklas.p.andersson@svt.se>
Thu, 8 Aug 2013 07:22:39 +0000 (09:22 +0200)
core/producer/color/color_producer.cpp
core/producer/color/color_producer.h

index a63cc8af102330cd282d85733a06de9c53407c15..91d91afeead349b331425e0f4af752c80e53d18d 100644 (file)
@@ -104,71 +104,81 @@ std::wstring get_hex_color(const std::wstring& str)
        if(str.at(0) == '#')
                return str.length() == 7 ? L"#FF" + str.substr(1) : str;
        
-       if(boost::iequals(str, L"EMPTY"))
+       std::wstring col_str = boost::to_upper_copy(str);
+
+       if(col_str == L"EMPTY")
                return L"#00000000";
 
-       if(boost::iequals(str, L"BLACK"))
+       if(col_str == L"BLACK")
                return L"#FF000000";
        
-       if(boost::iequals(str, L"WHITE"))
+       if(col_str == L"WHITE")
                return L"#FFFFFFFF";
        
-       if(boost::iequals(str, L"RED"))
+       if(col_str == L"RED")
                return L"#FFFF0000";
        
-       if(boost::iequals(str, L"GREEN"))
+       if(col_str == L"GREEN")
                return L"#FF00FF00";
        
-       if(boost::iequals(str, L"BLUE"))
+       if(col_str == L"BLUE")
                return L"#FF0000FF";    
        
-       if(boost::iequals(str, L"ORANGE"))
+       if(col_str == L"ORANGE")
                return L"#FFFFA500";    
        
-       if(boost::iequals(str, L"YELLOW"))
+       if(col_str == L"YELLOW")
                return L"#FFFFFF00";
        
-       if(boost::iequals(str, L"BROWN"))
+       if(col_str == L"BROWN")
                return L"#FFA52A2A";
        
-       if(boost::iequals(str, L"GRAY"))
+       if(col_str == L"GRAY")
                return L"#FF808080";
        
-       if(boost::iequals(str, L"TEAL"))
+       if(col_str == L"TEAL")
                return L"#FF008080";
        
        return str;
 }
 
+bool try_get_color(const std::wstring& str, uint32_t& value)
+{
+       auto color_str = get_hex_color(str);
+       if(color_str.length() != 9 || color_str[0] != '#')
+               return false;
+
+       std::wstringstream ss(color_str.substr(1));
+       if(!(ss >> std::hex >> value) || !ss.eof())
+               return false;
+       
+       return true;
+}
+
 spl::shared_ptr<frame_producer> create_color_producer(const spl::shared_ptr<frame_factory>& frame_factory, const std::vector<std::wstring>& params)
 {
        if(params.size() < 0)
                return core::frame_producer::empty();
 
-       auto color2 = get_hex_color(params[0]);
-       if(color2.length() != 9 || color2[0] != '#')
+       uint32_t value = 0;
+       if(!try_get_color(params[0], value))
                return core::frame_producer::empty();
 
-       return spl::make_shared<color_producer>(frame_factory, color2);
+       return spl::make_shared<color_producer>(frame_factory, params[0]);
 }
 
-draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, const std::wstring& color)
+draw_frame create_color_frame(void* tag, const spl::shared_ptr<frame_factory>& frame_factory, const std::wstring& str)
 {
-       auto color2 = get_hex_color(color);
-       if(color2.length() != 9 || color2[0] != '#')
-               CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(color2) << msg_info("Invalid color."));
-       
        core::pixel_format_desc desc(pixel_format::bgra);
        desc.planes.push_back(core::pixel_format_desc::plane(1, 1, 4));
        auto frame = frame_factory->create_frame(tag, desc);
-               
-       // Read color from hex-string and write to frame pixel.
+       
+       uint32_t value = 0;
+       if(!try_get_color(str, value))
+               BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(str) << msg_info("Invalid color."));
+
+       *reinterpret_cast<uint32_t*>(frame.image_data(0).begin()) = value;
 
-       auto& value = *reinterpret_cast<uint32_t*>(frame.image_data(0).begin());
-       std::wstringstream str(color2.substr(1));
-       if(!(str >> std::hex >> value) || !str.eof())
-               CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("color") << arg_value_info(color2) << msg_info("Invalid color."));
-                       
        return core::draw_frame(std::move(frame));
 }
 
index 153745da71559166d5e72b65ad2d002ad913ed5f..86950e9ad0d3d82e41344ea4054d93974c259361 100644 (file)
@@ -27,7 +27,9 @@
 #include <vector>
 
 namespace caspar { namespace core {
-       
+
+bool try_get_color(const std::wstring& str, uint32_t& value);
+
 spl::shared_ptr<class frame_producer> create_color_producer(const spl::shared_ptr<class frame_factory>& frame_factory, const std::vector<std::wstring>& params);
 class draw_frame create_color_frame(void* tag, const spl::shared_ptr<class frame_factory>& frame_factory, const std::wstring& color);