]> git.mxchange.org Git - simgear.git/blob - simgear/misc/parse_color.cxx
Remove OpenVG dependency from header file
[simgear.git] / simgear / misc / parse_color.cxx
1 // Parse CSS colors
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
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.
9 //
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.
14 //
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
18
19 #include "parse_color.hxx"
20
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>
25
26 namespace simgear
27 {
28
29   //----------------------------------------------------------------------------
30   bool parseColor(std::string str, osg::Vec4& result)
31   {
32     boost::trim(str);
33     osg::Vec4 color(0,0,0,1);
34
35     if( str.empty() )
36       return false;
37
38     // #rrggbb
39     if( str[0] == '#' )
40     {
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);
46
47       int comp = 0;
48       for( offset_tokenizer::const_iterator tok = tokens.begin();
49            tok != tokens.end() && comp < 4;
50            ++tok, ++comp )
51       {
52         color._v[comp] = strtol(std::string(*tok).c_str(), 0, 16) / 255.f;
53       }
54     }
55     // rgb(r,g,b)
56     // rgba(r,g,b,a)
57     else if( boost::ends_with(str, ")") )
58     {
59       const std::string RGB = "rgb(",
60                         RGBA = "rgba(";
61       size_t pos;
62       if( boost::starts_with(str, RGB) )
63         pos = RGB.length();
64       else if( boost::starts_with(str, RGBA) )
65         pos = RGBA.length();
66       else
67         return false;
68
69       typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
70       const boost::char_separator<char> del(", \t\n");
71
72       tokenizer tokens(str.begin() + pos, str.end() - 1, del);
73       int comp = 0;
74       for( tokenizer::const_iterator tok = tokens.begin();
75            tok != tokens.end() && comp < 4;
76            ++tok, ++comp )
77       {
78         color._v[comp] = boost::lexical_cast<float>(*tok)
79                        // rgb = [0,255], a = [0,1]
80                        / (comp < 3 ? 255 : 1);
81       }
82     }
83     else
84       return false;
85
86     result = color;
87     return true;
88   }
89
90 } // namespace simgear