diff --git a/include/mapnik/css_color_parser.hpp b/include/mapnik/css_color_parser.hpp index de960ba2d..85ced8758 100644 --- a/include/mapnik/css_color_parser.hpp +++ b/include/mapnik/css_color_parser.hpp @@ -31,6 +31,15 @@ using namespace boost::spirit; namespace mapnik { + + template + inline int clip_int(int val) + { + if (val < MIN ) return MIN; + if (val > MAX ) return MAX; + return val; + } + template struct named_colors : public symbols { @@ -201,22 +210,22 @@ namespace mapnik { { hex6 = ch_p('#') >> uint6x_p[self.actions.hex6_]; hex3 = ch_p('#') >> uint3x_p[self.actions.hex3_]; - rgb = str_p("rgb") >> '(' >> uint3_p[self.actions.red_] - >> ',' >> uint3_p[self.actions.green_] - >> ',' >> uint3_p[self.actions.blue_] + rgb = str_p("rgb") >> '(' >> int_p [self.actions.red_] + >> ',' >> int_p [self.actions.green_] + >> ',' >> int_p [self.actions.blue_] >> ')'; - rgba = str_p("rgba") >> '(' >> uint3_p[self.actions.red_] - >> ',' >> uint3_p[self.actions.green_] - >> ',' >> uint3_p[self.actions.blue_] + rgba = str_p("rgba") >> '(' >> int_p [self.actions.red_] + >> ',' >> int_p [self.actions.green_] + >> ',' >> int_p [self.actions.blue_] >> ',' >> real_p[self.actions.alpha_] >> ')'; - rgb_percent = str_p("rgb") >> '(' >> ureal_p[self.actions.red_p_] >> '%' - >> ',' >> ureal_p[self.actions.green_p_] >> '%' - >> ',' >> ureal_p[self.actions.blue_p_] >> '%' + rgb_percent = str_p("rgb") >> '(' >> real_p[self.actions.red_p_] >> '%' + >> ',' >> real_p[self.actions.green_p_] >> '%' + >> ',' >> real_p[self.actions.blue_p_] >> '%' >> ')'; - rgba_percent = str_p("rgba") >> '(' >> ureal_p[self.actions.red_p_] >> '%' - >> ',' >> ureal_p[self.actions.green_p_] >> '%' - >> ',' >> ureal_p[self.actions.blue_p_] >> '%' + rgba_percent = str_p("rgba") >> '(' >> real_p[self.actions.red_p_] >> '%' + >> ',' >> real_p[self.actions.green_p_] >> '%' + >> ',' >> real_p[self.actions.blue_p_] >> '%' >> ',' >> real_p[self.actions.alpha_] >> ')'; css_color = named_colors_p[self.actions.named_] | hex6 | hex3 | rgb_percent | rgba_percent | rgb | rgba; @@ -232,7 +241,7 @@ namespace mapnik { { return css_color; } - uint_parser uint3_p; + int_parser int_p; uint_parser uint6x_p; uint_parser uint3x_p; real_parser > real_p; @@ -297,9 +306,9 @@ namespace mapnik { red_action(ColorT& c) : c_(c) {} - void operator () (unsigned int r) const + void operator () (int r) const { - c_.set_red(r); + c_.set_red(clip_int<0,255>(r)); } ColorT& c_; }; @@ -310,9 +319,9 @@ namespace mapnik { green_action(ColorT& c) : c_(c) {} - void operator () (unsigned int g) const + void operator () (int g) const { - c_.set_green(g); + c_.set_green(clip_int<0,255>(g)); } ColorT& c_; }; @@ -323,9 +332,9 @@ namespace mapnik { blue_action(ColorT& c) : c_(c) {} - void operator () (unsigned int b) const + void operator () (int b) const { - c_.set_blue(b); + c_.set_blue(clip_int<0,255>(b)); } ColorT& c_; }; @@ -336,12 +345,14 @@ namespace mapnik { { alpha_action(ColorT& c) : c_(c) {} - - void operator () (float a) const + + void operator () (double a) const { + if (a < 0.0) a = 0.0; + if (a > 1.0) a = 1.0; c_.set_alpha(unsigned(a * 255.0 + 0.5)); } - + ColorT& c_; }; @@ -352,13 +363,13 @@ namespace mapnik { red_action_p(ColorT& c) : c_(c) {} - void operator () (double r) const - { - c_.set_red(unsigned((255.0 * r)/100.0 + 0.5)); - } - ColorT& c_; + void operator () (double r) const + { + c_.set_red(clip_int<0,255>(int((255.0 * r)/100.0 + 0.5))); + } + ColorT& c_; }; - + template struct green_action_p { @@ -367,7 +378,7 @@ namespace mapnik { void operator () (double g) const { - c_.set_green(unsigned((255.0 * g)/100.0 + 0.5)); + c_.set_green(clip_int<0,255>(int((255.0 * g)/100.0 + 0.5))); } ColorT& c_; }; @@ -380,12 +391,12 @@ namespace mapnik { void operator () (double b) const { - c_.set_blue(unsigned((255.0 * b)/100.0 + 0.5)); + c_.set_blue(clip_int<0,255>(((255.0 * b)/100.0 + 0.5))); } - ColorT& c_; + ColorT& c_; }; - - + + template struct actions {