handle duplicate hsl hue_to_rgb code

This commit is contained in:
Dane Springmeyer 2013-07-24 19:26:26 -04:00
parent eaeccc3c3a
commit ec950bd983
2 changed files with 15 additions and 27 deletions

View file

@ -25,6 +25,7 @@
// mapnik // mapnik
#include <mapnik/color.hpp> #include <mapnik/color.hpp>
#include <mapnik/util/hsl.hpp>
// spirit2 // spirit2
#include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/qi.hpp>
@ -39,24 +40,6 @@
// stl // stl
#include <string> #include <string>
namespace mapnik {
// http://www.w3.org/TR/css3-color/#hsl-color
inline double hue_to_rgb( double m1, double m2, double h)
{
if (h < 0.0) h = h + 1.0;
else if (h > 1) h = h - 1.0;
if (h * 6 < 1.0)
return m1 + (m2 - m1) * h * 6.0;
if (h * 2 < 1.0)
return m2;
if (h * 3 < 2.0)
return m1 + (m2 - m1)* (2.0/3.0 - h) * 6.0;
return m1;
}
} // namespace mapnik
// boost // boost
#include <boost/version.hpp> #include <boost/version.hpp>

View file

@ -46,12 +46,17 @@ static inline void rgb2hsl(unsigned char red, unsigned char green, unsigned char
} }
} }
static inline double hueToRGB(double m1, double m2, double h) { // http://www.w3.org/TR/css3-color/#hsl-color
if(h < 0) h += 1; inline double hue_to_rgb( double m1, double m2, double h)
if(h > 1) h -= 1; {
if (h * 6 < 1) return m1 + (m2 - m1) * h * 6; if (h < 0.0) h = h + 1.0;
if (h * 2 < 1) return m2; else if (h > 1) h = h - 1.0;
if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6; if (h * 6 < 1.0)
return m1 + (m2 - m1) * h * 6.0;
if (h * 2 < 1.0)
return m2;
if (h * 3 < 2.0)
return m1 + (m2 - m1)* (2.0/3.0 - h) * 6.0;
return m1; return m1;
} }
@ -62,9 +67,9 @@ static inline void hsl2rgb(double h, double s, double l,
} }
double m2 = (l <= 0.5) ? l * (s + 1) : l + s - l * s; double m2 = (l <= 0.5) ? l * (s + 1) : l + s - l * s;
double m1 = l * 2 - m2; double m1 = l * 2 - m2;
r = static_cast<unsigned char>(std::floor((hueToRGB(m1, m2, h + 0.33333) * 255.0)+.5)); r = static_cast<unsigned char>(std::floor((hue_to_rgb(m1, m2, h + 1.0/3.0) * 255.0)+.5));
g = static_cast<unsigned char>(std::floor((hueToRGB(m1, m2, h) * 255.0)+.5)); g = static_cast<unsigned char>(std::floor((hue_to_rgb(m1, m2, h) * 255.0)+.5));
b = static_cast<unsigned char>(std::floor((hueToRGB(m1, m2, h - 0.33333) * 255.0)+.5)); b = static_cast<unsigned char>(std::floor((hue_to_rgb(m1, m2, h - 1.0/3.0) * 255.0)+.5));
} }
} }