+ normalize rgb color ranges to 0-255,

+ clip alpha to 0.0 - 1.0 range
This commit is contained in:
Artem Pavlenko 2009-01-26 15:59:10 +00:00
parent 9b1c4b2413
commit eeb759d9e1

View file

@ -31,6 +31,15 @@
using namespace boost::spirit;
namespace mapnik {
template <int MIN,int MAX>
inline int clip_int(int val)
{
if (val < MIN ) return MIN;
if (val > MAX ) return MAX;
return val;
}
template <typename ColorT>
struct named_colors : public symbols<ColorT>
{
@ -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<unsigned, 10, 1, 3> uint3_p;
int_parser<int, 10, 1, -1> int_p;
uint_parser<unsigned, 16, 6, 6> uint6x_p;
uint_parser<unsigned, 16, 3, 3> uint3x_p;
real_parser<double, real_parser_policies<double> > 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_;
};
@ -337,8 +346,10 @@ 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));
}
@ -354,7 +365,7 @@ namespace mapnik {
void operator () (double r) const
{
c_.set_red(unsigned((255.0 * r)/100.0 + 0.5));
c_.set_red(clip_int<0,255>(int((255.0 * r)/100.0 + 0.5)));
}
ColorT& c_;
};
@ -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,7 +391,7 @@ 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_;
};