25
25
#ifndef CSS_COLOR_PARSER_HPP
26
26
#define CSS_COLOR_PARSER_HPP
28
#include <boost/spirit/core.hpp>
29
#include <boost/spirit/symbols.hpp>
29
#include <boost/version.hpp>
31
#if BOOST_VERSION < 103800
32
#include <boost/spirit/core.hpp>
33
#include <boost/spirit/symbols.hpp>
35
#define BOOST_SPIRIT_USE_OLD_NAMESPACE
36
#include <boost/spirit/include/classic_core.hpp>
37
#include <boost/spirit/include/classic_symbols.hpp>
31
40
using namespace boost::spirit;
44
template <int MIN,int MAX>
45
inline int clip_int(int val)
47
if (val < MIN ) return MIN;
48
if (val > MAX ) return MAX;
34
52
template <typename ColorT>
35
53
struct named_colors : public symbols<ColorT>
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_]
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_] >> '%'
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_]
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_]
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_] >> '%'
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_]
240
css_color = named_colors_p[self.actions.named_] | hex6 | hex3 | rgb_percent | rgba_percent | rgb | rgba;
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;
221
251
return css_color;
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;
310
341
blue_action(ColorT& c)
313
void operator () (unsigned int b) const
344
void operator () (int b) const
346
c_.set_blue(clip_int<0,255>(b));
321
352
template <typename ColorT>
355
alpha_action(ColorT& c)
358
void operator () (double a) const
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));
369
template <typename ColorT>
322
370
struct red_action_p
324
372
red_action_p(ColorT& c)
327
void operator () (double r) const
329
c_.set_red(unsigned((255.0 * r)/100.0 + 0.5));
375
void operator () (double r) const
377
c_.set_red(clip_int<0,255>(int((255.0 * r)/100.0 + 0.5)));
334
382
template <typename ColorT>
335
383
struct green_action_p
353
401
void operator () (double b) const
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)));
361
409
template <typename ColorT>
364
412
typedef ColorT color_type;
365
413
actions(ColorT& c)
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_;