~ubuntu-branches/ubuntu/trusty/mapnik/trusty

« back to all changes in this revision

Viewing changes to include/mapnik/css_color_parser.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Andres Rodriguez
  • Date: 2009-05-20 15:39:58 UTC
  • mfrom: (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090520153958-cf6z1ql9zva4y4dq
Tags: 0.6.0-1ubuntu1
* Merge from debian unstable (LP: #378819), remaining changes:
  - debian/control:
    + Change bdeps from python2.5-dev to python-all-dev (>= 2.5)
    + Change XS-Python-Version from 2.5 to >= 2.5
  - debian/rules:
    + Various changes to enable python2.5 and python2.6 builds
* debian/patches/libtool2_2.diff Dropped. Included upsteam.
* Removed quilt support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#ifndef CSS_COLOR_PARSER_HPP
26
26
#define CSS_COLOR_PARSER_HPP
27
27
 
28
 
#include <boost/spirit/core.hpp>
29
 
#include <boost/spirit/symbols.hpp>
 
28
// boost
 
29
#include <boost/version.hpp>
 
30
 
 
31
#if BOOST_VERSION < 103800
 
32
  #include <boost/spirit/core.hpp>
 
33
  #include <boost/spirit/symbols.hpp>
 
34
#else
 
35
  #define BOOST_SPIRIT_USE_OLD_NAMESPACE
 
36
  #include <boost/spirit/include/classic_core.hpp>
 
37
  #include <boost/spirit/include/classic_symbols.hpp>
 
38
#endif
30
39
 
31
40
using namespace boost::spirit;
32
41
 
33
42
namespace mapnik {
 
43
 
 
44
   template <int MIN,int MAX>
 
45
   inline int clip_int(int val)
 
46
   {
 
47
      if (val < MIN ) return MIN;
 
48
      if (val > MAX ) return MAX;
 
49
      return val;
 
50
   }
 
51
   
34
52
    template <typename ColorT>
35
53
    struct named_colors : public symbols<ColorT>
36
54
    {
201
219
            {
202
220
                hex6 = ch_p('#') >> uint6x_p[self.actions.hex6_];
203
221
                hex3 = ch_p('#') >> uint3x_p[self.actions.hex3_];
204
 
                rgb = str_p("rgb") >> '(' >> uint3_p[self.actions.red_] 
205
 
                                   >> ',' >> uint3_p[self.actions.green_] 
206
 
                                   >> ',' >> uint3_p[self.actions.blue_] 
207
 
                                   >> ')';
208
 
                rgb_percent = str_p("rgb") >> '(' >> ureal_p[self.actions.red_p_] >> '%' 
209
 
                                           >> ',' >> ureal_p[self.actions.green_p_] >> '%'
210
 
                                           >> ',' >> ureal_p[self.actions.blue_p_] >> '%'
211
 
                                           >> ')';
212
 
                css_color = named_colors_p[self.actions.named_] | hex6 | hex3 | rgb_percent | rgb; 
 
222
                rgb = str_p("rgb") >> '(' >> int_p [self.actions.red_] 
 
223
                                   >> ',' >> int_p [self.actions.green_] 
 
224
                                   >> ',' >> int_p [self.actions.blue_] 
 
225
                                   >> ')';
 
226
                rgba = str_p("rgba") >> '(' >> int_p [self.actions.red_]
 
227
                                   >> ',' >> int_p [self.actions.green_]
 
228
                                   >> ',' >> int_p [self.actions.blue_]
 
229
                                   >> ',' >> real_p[self.actions.alpha_]
 
230
                                   >> ')';
 
231
                rgb_percent = str_p("rgb") >> '(' >> real_p[self.actions.red_p_] >> '%' 
 
232
                                           >> ',' >> real_p[self.actions.green_p_] >> '%'
 
233
                                           >> ',' >> real_p[self.actions.blue_p_] >> '%'
 
234
                                           >> ')';
 
235
                rgba_percent = str_p("rgba") >> '(' >> real_p[self.actions.red_p_] >> '%'
 
236
                                           >> ',' >> real_p[self.actions.green_p_] >> '%'
 
237
                                           >> ',' >> real_p[self.actions.blue_p_] >> '%'
 
238
                                           >> ',' >> real_p[self.actions.alpha_]
 
239
                                           >> ')';
 
240
                css_color = named_colors_p[self.actions.named_] | hex6 | hex3 | rgb_percent | rgba_percent | rgb | rgba; 
213
241
            }
214
242
            boost::spirit::rule<ScannerT> rgb;
 
243
            boost::spirit::rule<ScannerT> rgba;
215
244
            boost::spirit::rule<ScannerT> rgb_percent;
 
245
            boost::spirit::rule<ScannerT> rgba_percent;
216
246
            boost::spirit::rule<ScannerT> hex6;
217
247
            boost::spirit::rule<ScannerT> hex3;
218
248
            boost::spirit::rule<ScannerT> css_color;
220
250
            {
221
251
                return css_color;
222
252
            }
223
 
            uint_parser<unsigned, 10, 1, 3> uint3_p;
 
253
            int_parser<int, 10, 1, -1> int_p;
224
254
            uint_parser<unsigned, 16, 6, 6> uint6x_p;
225
255
            uint_parser<unsigned, 16, 3, 3> uint3x_p;
 
256
            real_parser<double, real_parser_policies<double>  > real_p;
226
257
            named_colors<typename ActionsT::color_type> named_colors_p;
227
258
            
228
259
        };
284
315
        red_action(ColorT& c)
285
316
            : c_(c) {}
286
317
        
287
 
        void operator () (unsigned int r) const
 
318
        void operator () (int r) const
288
319
        {
289
 
            c_.set_red(r);
 
320
           c_.set_red(clip_int<0,255>(r));
290
321
        }
291
322
        ColorT& c_;
292
323
    };
297
328
        green_action(ColorT& c)
298
329
            : c_(c) {}
299
330
        
300
 
        void operator () (unsigned int g) const
 
331
        void operator () (int g) const
301
332
        {
302
 
            c_.set_green(g);
 
333
           c_.set_green(clip_int<0,255>(g));
303
334
        }
304
335
        ColorT& c_;
305
336
    };
310
341
        blue_action(ColorT& c)
311
342
            : c_(c) {}
312
343
        
313
 
        void operator () (unsigned int b) const
 
344
        void operator () (int b) const
314
345
        {
315
 
            c_.set_blue(b);
 
346
           c_.set_blue(clip_int<0,255>(b));
316
347
        }
317
348
        ColorT& c_;
318
349
    };
319
350
    
320
351
 
321
352
    template <typename ColorT>
 
353
    struct alpha_action
 
354
    {
 
355
        alpha_action(ColorT& c)
 
356
            : c_(c) {}
 
357
        
 
358
        void operator () (double a) const
 
359
        {
 
360
           if (a < 0.0) a = 0.0;
 
361
           if (a > 1.0) a = 1.0;
 
362
           c_.set_alpha(unsigned(a * 255.0 + 0.5));
 
363
        }
 
364
         
 
365
        ColorT& c_;
 
366
    };
 
367
 
 
368
 
 
369
    template <typename ColorT>
322
370
    struct red_action_p
323
371
    {
324
372
        red_action_p(ColorT& c)
325
373
            : c_(c) {}
326
374
        
327
 
        void operator () (double r) const
328
 
        {
329
 
            c_.set_red(unsigned((255.0 * r)/100.0 + 0.5));
330
 
        }
331
 
        ColorT& c_;
 
375
       void operator () (double r) const
 
376
       {
 
377
           c_.set_red(clip_int<0,255>(int((255.0 * r)/100.0 + 0.5)));
 
378
       }
 
379
       ColorT& c_;
332
380
    };
333
 
 
 
381
   
334
382
    template <typename ColorT>
335
383
    struct green_action_p
336
384
    {
339
387
        
340
388
        void operator () (double g) const
341
389
        {
342
 
            c_.set_green(unsigned((255.0 * g)/100.0 + 0.5));
 
390
           c_.set_green(clip_int<0,255>(int((255.0 * g)/100.0 + 0.5)));
343
391
        }
344
392
        ColorT& c_;
345
393
    };
352
400
        
353
401
        void operator () (double b) const
354
402
        {
355
 
            c_.set_blue(unsigned((255.0 * b)/100.0 + 0.5));
 
403
           c_.set_blue(clip_int<0,255>(int((255.0 * b)/100.0 + 0.5)));
356
404
        }
357
 
        ColorT& c_;
 
405
       ColorT& c_;
358
406
    };
359
 
 
360
 
 
 
407
   
 
408
   
361
409
    template <typename ColorT>
362
410
    struct actions
363
411
    {
364
412
        typedef ColorT color_type;
365
413
        actions(ColorT& c)
366
 
            : named_(c),
367
 
              hex6_(c),
368
 
              hex3_(c),
369
 
              red_(c),
370
 
              green_(c),
371
 
              blue_(c),
372
 
              red_p_(c),
373
 
              green_p_(c),
374
 
              blue_p_(c) {}
 
414
           : 
 
415
           named_(c),
 
416
           hex6_(c),
 
417
           hex3_(c),
 
418
           red_(c),
 
419
           green_(c),
 
420
           blue_(c),
 
421
           alpha_(c),
 
422
           red_p_(c),
 
423
           green_p_(c),
 
424
           blue_p_(c) 
 
425
       {
 
426
          c.set_alpha (255);
 
427
       }
375
428
        
376
 
        named_color_action<ColorT> named_;
377
 
        hex6_action<ColorT> hex6_;
378
 
        hex3_action<ColorT> hex3_;
379
 
        red_action<ColorT> red_;
380
 
        green_action<ColorT> green_;
381
 
        blue_action<ColorT> blue_;
382
 
        red_action_p<ColorT> red_p_;
383
 
        green_action_p<ColorT> green_p_;
384
 
        blue_action_p<ColorT> blue_p_;
 
429
       named_color_action<ColorT> named_;
 
430
       hex6_action<ColorT> hex6_;
 
431
       hex3_action<ColorT> hex3_;
 
432
       red_action<ColorT> red_;
 
433
       green_action<ColorT> green_;
 
434
       blue_action<ColorT> blue_;
 
435
       alpha_action<ColorT> alpha_;
 
436
       red_action_p<ColorT> red_p_;
 
437
       green_action_p<ColorT> green_p_;
 
438
       blue_action_p<ColorT> blue_p_;
385
439
    };
386
440
}
387
441