3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "parse_color.hxx"
21 #include <boost/algorithm/string/predicate.hpp>
22 #include <boost/algorithm/string/trim.hpp>
23 #include <boost/lexical_cast.hpp>
24 #include <boost/tokenizer.hpp>
29 //----------------------------------------------------------------------------
30 bool parseColor(std::string str, osg::Vec4& result)
33 osg::Vec4 color(0,0,0,1);
41 const int offsets[] = {2,2,2};
42 const boost::offset_separator hex_separator( boost::begin(offsets),
43 boost::end(offsets) );
44 typedef boost::tokenizer<boost::offset_separator> offset_tokenizer;
45 offset_tokenizer tokens(str.begin() + 1, str.end(), hex_separator);
48 for( offset_tokenizer::const_iterator tok = tokens.begin();
49 tok != tokens.end() && comp < 4;
52 color[comp] = strtol(std::string(*tok).c_str(), 0, 16) / 255.f;
57 else if( boost::ends_with(str, ")") )
59 const std::string RGB = "rgb(",
62 if( boost::starts_with(str, RGB) )
64 else if( boost::starts_with(str, RGBA) )
69 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
70 const boost::char_separator<char> del(", \t\n");
72 tokenizer tokens(str.begin() + pos, str.end() - 1, del);
74 for( tokenizer::const_iterator tok = tokens.begin();
75 tok != tokens.end() && comp < 4;
78 color[comp] = boost::lexical_cast<float>(*tok)
79 // rgb = [0,255], a = [0,1]
80 / (comp < 3 ? 255 : 1);
90 } // namespace simgear