/home/andreas/src/svn/mapnik/include/mapnik/css_color_parser.hpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * 
00003  * This file is part of Mapnik (c++ mapping toolkit)
00004  *
00005  * Copyright (C) 2006 Artem Pavlenko
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  *
00021  *****************************************************************************/
00022 
00023 //$Id$
00024 
00025 #ifndef CSS_COLOR_PARSER_HPP
00026 #define CSS_COLOR_PARSER_HPP
00027 
00028 #include <boost/spirit/core.hpp>
00029 #include <boost/spirit/symbols.hpp>
00030 
00031 using namespace boost::spirit;
00032 
00033 namespace mapnik {
00034     template <typename ColorT>
00035     struct named_colors : public symbols<ColorT>
00036     {
00037         named_colors()
00038         {
00039             symbols<ColorT>::add
00040                 ("aliceblue", ColorT(240, 248, 255))
00041                 ("antiquewhite", ColorT(250, 235, 215))
00042                 ("aqua", ColorT(0, 255, 255))
00043                 ("aquamarine", ColorT(127, 255, 212))
00044                 ("azure", ColorT(240, 255, 255))
00045                 ("beige", ColorT(245, 245, 220))
00046                 ("bisque", ColorT(255, 228, 196))
00047                 ("black", ColorT(0, 0, 0))
00048                 ("blanchedalmond", ColorT(255,235,205))
00049                 ("blue", ColorT(0, 0, 255))
00050                 ("blueviolet", ColorT(138, 43, 226))
00051                 ("brown", ColorT(165, 42, 42))
00052                 ("burlywood", ColorT(222, 184, 135))
00053                 ("cadetblue", ColorT(95, 158, 160))
00054                 ("chartreuse", ColorT(127, 255, 0))
00055                 ("chocolate", ColorT(210, 105, 30))
00056                 ("coral", ColorT(255, 127, 80))
00057                 ("cornflowerblue", ColorT(100, 149, 237))
00058                 ("cornsilk", ColorT(255, 248, 220))
00059                 ("crimson", ColorT(220, 20, 60))
00060                 ("cyan", ColorT(0, 255, 255))
00061                 ("darkblue", ColorT(0, 0, 139))
00062                 ("darkcyan", ColorT(0, 139, 139))
00063                 ("darkgoldenrod", ColorT(184, 134, 11))
00064                 ("darkgray", ColorT(169, 169, 169))
00065                 ("darkgreen", ColorT(0, 100, 0))
00066                 ("darkgrey", ColorT(169, 169, 169))
00067                 ("darkkhaki", ColorT(189, 183, 107))
00068                 ("darkmagenta", ColorT(139, 0, 139))
00069                 ("darkolivegreen", ColorT(85, 107, 47))
00070                 ("darkorange", ColorT(255, 140, 0))
00071                 ("darkorchid", ColorT(153, 50, 204))
00072                 ("darkred", ColorT(139, 0, 0))
00073                 ("darksalmon", ColorT(233, 150, 122))
00074                 ("darkseagreen", ColorT(143, 188, 143))
00075                 ("darkslateblue", ColorT(72, 61, 139))
00076                 ("darkslategrey", ColorT(47, 79, 79))
00077                 ("darkturquoise", ColorT(0, 206, 209))
00078                 ("darkviolet", ColorT(148, 0, 211))
00079                 ("deeppink", ColorT(255, 20, 147))
00080                 ("deepskyblue", ColorT(0, 191, 255))
00081                 ("dimgray", ColorT(105, 105, 105))
00082                 ("dimgrey", ColorT(105, 105, 105))
00083                 ("dodgerblue", ColorT(30, 144, 255))
00084                 ("firebrick", ColorT(178, 34, 34))
00085                 ("floralwhite", ColorT(255, 250, 240))
00086                 ("forestgreen", ColorT(34, 139, 34))
00087                 ("fuchsia", ColorT(255, 0, 255))
00088                 ("gainsboro", ColorT(220, 220, 220))
00089                 ("ghostwhite", ColorT(248, 248, 255))
00090                 ("gold", ColorT(255, 215, 0))
00091                 ("goldenrod", ColorT(218, 165, 32))
00092                 ("gray", ColorT(128, 128, 128))
00093                 ("grey", ColorT(128, 128, 128))
00094                 ("green", ColorT(0, 128, 0))
00095                 ("greenyellow", ColorT(173, 255, 47))
00096                 ("honeydew", ColorT(240, 255, 240))
00097                 ("hotpink", ColorT(255, 105, 180))
00098                 ("indianred", ColorT(205, 92, 92))
00099                 ("indigo", ColorT(75, 0, 130))
00100                 ("ivory", ColorT(255, 255, 240))
00101                 ("khaki", ColorT(240, 230, 140))
00102                 ("lavender", ColorT(230, 230, 250))
00103                 ("lavenderblush", ColorT(255, 240, 245))
00104                 ("lawngreen", ColorT(124, 252, 0))
00105                 ("lemonchiffon", ColorT(255, 250, 205))
00106                 ("lightblue", ColorT(173, 216, 230))
00107                 ("lightcoral", ColorT(240, 128, 128))
00108                 ("lightcyan", ColorT(224, 255, 255))
00109                 ("lightgoldenrodyellow", ColorT(250, 250, 210))
00110                 ("lightgray", ColorT(211, 211, 211))
00111                 ("lightgreen", ColorT(144, 238, 144))
00112                 ("lightgrey", ColorT(211, 211, 211))
00113                 ("lightpink", ColorT(255, 182, 193))
00114                 ("lightsalmon", ColorT(255, 160, 122))
00115                 ("lightseagreen", ColorT(32, 178, 170))
00116                 ("lightskyblue", ColorT(135, 206, 250))
00117                 ("lightslategray", ColorT(119, 136, 153))
00118                 ("lightslategrey", ColorT(119, 136, 153))
00119                 ("lightsteelblue", ColorT(176, 196, 222))
00120                 ("lightyellow", ColorT(255, 255, 224))
00121                 ("lime", ColorT(0, 255, 0))
00122                 ("limegreen", ColorT(50, 205, 50))
00123                 ("linen", ColorT(250, 240, 230))
00124                 ("magenta", ColorT(255, 0, 255))
00125                 ("maroon", ColorT(128, 0, 0))
00126                 ("mediumaquamarine", ColorT(102, 205, 170))
00127                 ("mediumblue", ColorT(0, 0, 205))
00128                 ("mediumorchid", ColorT(186, 85, 211))
00129                 ("mediumpurple", ColorT(147, 112, 219))
00130                 ("mediumseagreen", ColorT(60, 179, 113))
00131                 ("mediumslateblue", ColorT(123, 104, 238))
00132                 ("mediumspringgreen", ColorT(0, 250, 154))
00133                 ("mediumturquoise", ColorT(72, 209, 204))
00134                 ("mediumvioletred", ColorT(199, 21, 133))
00135                 ("midnightblue", ColorT(25, 25, 112))
00136                 ("mintcream", ColorT(245, 255, 250))
00137                 ("mistyrose", ColorT(255, 228, 225))
00138                 ("moccasin", ColorT(255, 228, 181))
00139                 ("navajowhite", ColorT(255, 222, 173))
00140                 ("navy", ColorT(0, 0, 128))
00141                 ("oldlace", ColorT(253, 245, 230))
00142                 ("olive", ColorT(128, 128, 0))
00143                 ("olivedrab", ColorT(107, 142, 35))
00144                 ("orange", ColorT(255, 165, 0))
00145                 ("orangered", ColorT(255, 69, 0))
00146                 ("orchid", ColorT(218, 112, 214))
00147                 ("palegoldenrod", ColorT(238, 232, 170))
00148                 ("palegreen", ColorT(152, 251, 152))
00149                 ("paleturquoise", ColorT(175, 238, 238))
00150                 ("palevioletred", ColorT(219, 112, 147))
00151                 ("papayawhip", ColorT(255, 239, 213))
00152                 ("peachpuff", ColorT(255, 218, 185))
00153                 ("peru", ColorT(205, 133, 63))
00154                 ("pink", ColorT(255, 192, 203))
00155                 ("plum", ColorT(221, 160, 221))
00156                 ("powderblue", ColorT(176, 224, 230))
00157                 ("purple", ColorT(128, 0, 128))
00158                 ("red", ColorT(255, 0, 0))
00159                 ("rosybrown", ColorT(188, 143, 143))
00160                 ("royalblue", ColorT(65, 105, 225))
00161                 ("saddlebrown", ColorT(139, 69, 19))
00162                 ("salmon", ColorT(250, 128, 114))
00163                 ("sandybrown", ColorT(244, 164, 96))
00164                 ("seagreen", ColorT(46, 139, 87))
00165                 ("seashell", ColorT(255, 245, 238))
00166                 ("sienna", ColorT(160, 82, 45))
00167                 ("silver", ColorT(192, 192, 192))
00168                 ("skyblue", ColorT(135, 206, 235))
00169                 ("slateblue", ColorT(106, 90, 205))
00170                 ("slategray", ColorT(112, 128, 144))
00171                 ("slategrey", ColorT(112, 128, 144))
00172                 ("snow", ColorT(255, 250, 250))
00173                 ("springgreen", ColorT(0, 255, 127))
00174                 ("steelblue", ColorT(70, 130, 180))
00175                 ("tan", ColorT(210, 180, 140))
00176                 ("teal", ColorT(0, 128, 128))
00177                 ("thistle", ColorT(216, 191, 216))
00178                 ("tomato", ColorT(255, 99, 71))
00179                 ("turquoise", ColorT(64, 224, 208))
00180                 ("violet", ColorT(238, 130, 238))
00181                 ("wheat", ColorT(245, 222, 179))
00182                 ("white", ColorT(255, 255, 255))
00183                 ("whitesmoke", ColorT(245, 245, 245))
00184                 ("yellow", ColorT(255, 255, 0))
00185                 ("yellowgreen", ColorT(154, 205, 50))
00186                 ("transparent", ColorT(0, 0, 0, 0))
00187                 ;
00188         }
00189     };
00190     
00191     template <typename ActionsT>
00192     struct css_color_grammar : public grammar<css_color_grammar<ActionsT> >
00193     {
00194         css_color_grammar(ActionsT& actions_)
00195             : actions(actions_) {}
00196     
00197         template <typename ScannerT>
00198         struct definition
00199         {
00200             definition(css_color_grammar const& self)
00201             {
00202                 hex6 = ch_p('#') >> uint6x_p[self.actions.hex6_];
00203                 hex3 = ch_p('#') >> uint3x_p[self.actions.hex3_];
00204                 rgb = str_p("rgb") >> '(' >> uint3_p[self.actions.red_] 
00205                                    >> ',' >> uint3_p[self.actions.green_] 
00206                                    >> ',' >> uint3_p[self.actions.blue_] 
00207                                    >> ')';
00208                 rgb_percent = str_p("rgb") >> '(' >> ureal_p[self.actions.red_p_] >> '%' 
00209                                            >> ',' >> ureal_p[self.actions.green_p_] >> '%'
00210                                            >> ',' >> ureal_p[self.actions.blue_p_] >> '%'
00211                                            >> ')';
00212                 css_color = named_colors_p[self.actions.named_] | hex6 | hex3 | rgb_percent | rgb; 
00213             }
00214             boost::spirit::rule<ScannerT> rgb;
00215             boost::spirit::rule<ScannerT> rgb_percent;
00216             boost::spirit::rule<ScannerT> hex6;
00217             boost::spirit::rule<ScannerT> hex3;
00218             boost::spirit::rule<ScannerT> css_color;
00219             boost::spirit::rule<ScannerT> const& start() const
00220             {
00221                 return css_color;
00222             }
00223             uint_parser<unsigned, 10, 1, 3> uint3_p;
00224             uint_parser<unsigned, 16, 6, 6> uint6x_p;
00225             uint_parser<unsigned, 16, 3, 3> uint3x_p;
00226             named_colors<typename ActionsT::color_type> named_colors_p;
00227             
00228         };
00229         ActionsT& actions;      
00230     };
00231     
00232     template <typename ColorT>
00233     struct named_color_action
00234     {
00235         named_color_action(ColorT& c)
00236             : c_(c) {}
00237         
00238         void operator() (ColorT const&c) const
00239         {
00240             c_=c;
00241         }
00242         ColorT& c_;
00243     };
00244 
00245     template <typename ColorT>
00246     struct hex6_action
00247     {
00248         hex6_action(ColorT& c)
00249             : c_(c) {}
00250         
00251         void operator () (unsigned int hex) const
00252         {
00253             unsigned r = (hex >> 16) & 0xff;
00254             unsigned g = (hex >> 8) & 0xff;
00255             unsigned b = hex & 0xff;
00256             c_.set_red(r);
00257             c_.set_green(g);
00258             c_.set_blue(b);
00259         }
00260         ColorT& c_;
00261     };
00262     
00263     template <typename ColorT>
00264     struct hex3_action
00265     {
00266         hex3_action(ColorT& c)
00267             : c_(c) {}
00268         
00269         void operator () (unsigned int hex) const
00270         {
00271             unsigned int r = (hex >> 8) & 0xf;
00272             unsigned int g = (hex >> 4) & 0xf;
00273             unsigned int b = hex & 0xf;
00274             c_.set_red( r | r << 4);
00275             c_.set_green(g | g << 4);
00276             c_.set_blue(b | b << 4);
00277         }
00278         ColorT& c_;
00279     };
00280 
00281     template <typename ColorT>
00282     struct red_action
00283     {
00284         red_action(ColorT& c)
00285             : c_(c) {}
00286         
00287         void operator () (unsigned int r) const
00288         {
00289             c_.set_red(r);
00290         }
00291         ColorT& c_;
00292     };
00293 
00294     template <typename ColorT>
00295     struct green_action
00296     {
00297         green_action(ColorT& c)
00298             : c_(c) {}
00299         
00300         void operator () (unsigned int g) const
00301         {
00302             c_.set_green(g);
00303         }
00304         ColorT& c_;
00305     };
00306         
00307     template <typename ColorT>
00308     struct blue_action
00309     {
00310         blue_action(ColorT& c)
00311             : c_(c) {}
00312         
00313         void operator () (unsigned int b) const
00314         {
00315             c_.set_blue(b);
00316         }
00317         ColorT& c_;
00318     };
00319     
00320 
00321     template <typename ColorT>
00322     struct red_action_p
00323     {
00324         red_action_p(ColorT& c)
00325             : c_(c) {}
00326         
00327         void operator () (double r) const
00328         {
00329             c_.set_red(unsigned((255.0 * r)/100.0 + 0.5));
00330         }
00331         ColorT& c_;
00332     };
00333 
00334     template <typename ColorT>
00335     struct green_action_p
00336     {
00337         green_action_p(ColorT& c)
00338             : c_(c) {}
00339         
00340         void operator () (double g) const
00341         {
00342             c_.set_green(unsigned((255.0 * g)/100.0 + 0.5));
00343         }
00344         ColorT& c_;
00345     };
00346 
00347     template <typename ColorT>
00348     struct blue_action_p
00349     {
00350         blue_action_p(ColorT& c)
00351             : c_(c) {}
00352         
00353         void operator () (double b) const
00354         {
00355             c_.set_blue(unsigned((255.0 * b)/100.0 + 0.5));
00356         }
00357         ColorT& c_;
00358     };
00359 
00360 
00361     template <typename ColorT>
00362     struct actions
00363     {
00364         typedef ColorT color_type;
00365         actions(ColorT& c)
00366             : named_(c),
00367               hex6_(c),
00368               hex3_(c),
00369               red_(c),
00370               green_(c),
00371               blue_(c),
00372               red_p_(c),
00373               green_p_(c),
00374               blue_p_(c) {}
00375         
00376         named_color_action<ColorT> named_;
00377         hex6_action<ColorT> hex6_;
00378         hex3_action<ColorT> hex3_;
00379         red_action<ColorT> red_;
00380         green_action<ColorT> green_;
00381         blue_action<ColorT> blue_;
00382         red_action_p<ColorT> red_p_;
00383         green_action_p<ColorT> green_p_;
00384         blue_action_p<ColorT> blue_p_;
00385     };
00386 }
00387 
00388 #endif //CSS_COLOR_PARSER_HPP

Generated on Thu Jul 19 17:59:25 2007 for Mapnik by  doxygen 1.4.7