From b05e09700b94c321c1c933bf14ab4332d8dd47e3 Mon Sep 17 00:00:00 2001 From: artemp Date: Fri, 11 Dec 2015 13:14:37 +0000 Subject: [PATCH] add HSL(A) colors --- include/mapnik/svg2_color_grammar_def.hpp | 56 +++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/include/mapnik/svg2_color_grammar_def.hpp b/include/mapnik/svg2_color_grammar_def.hpp index aac65cce4..893049b1a 100644 --- a/include/mapnik/svg2_color_grammar_def.hpp +++ b/include/mapnik/svg2_color_grammar_def.hpp @@ -28,7 +28,9 @@ // boost #include #include +#include #include +#include #include BOOST_FUSION_ADAPT_STRUCT ( @@ -296,6 +298,37 @@ auto hex1_opacity = [](auto& ctx) _val(ctx).alpha_ = _attr(ctx) | _attr(ctx) << 4; }; +auto hsl_to_rgba = [] (auto& ctx) +{ + double h = std::get<0>(_attr(ctx)); + double s = std::get<1>(_attr(ctx)); + double l = std::get<2>(_attr(ctx)); + double m1; + double m2; + // normalise values + h /= 360.0; + s /= 100.0; + l /= 100.0; + if (l <= 0.5) + { + m2 = l * (s + 1.0); + } + else + { + m2 = l + s - l*s; + } + m1 = l * 2 - m2; + + double r = hue_to_rgb(m1, m2, h + 1.0/3.0); + double g = hue_to_rgb(m1, m2, h); + double b = hue_to_rgb(m1, m2, h - 1.0/3.0); + uint8_t alpha = uint8_t((255.0 * clip_opacity::call(std::get<3>(_attr(ctx)))) + 0.5); + _val(ctx) = color(safe_cast(std::lround(255.0 * r)), + safe_cast(std::lround(255.0 * g)), + safe_cast(std::lround(255.0 * b)), + alpha); +}; + auto const hex2_color_def = no_skip[lit('#') >> hex2 >> hex2 >> hex2 >> (hex2 | attr(255))] ; auto const hex1_color_def = no_skip[lit('#') @@ -328,6 +361,25 @@ auto const rgba_color_percent_def = lit("rgba") >> lit(',') >> dec3[percent_blue] >> lit('%') >> lit(',') >> double_[opacity] >> lit(')'); +auto const hsl_values = x3::rule> {} = + lit("hsl") + >> lit('(') >> dec3 + >> lit(',') >> dec3 >> lit('%') + >> lit(',') >> dec3 >> lit('%') + >> attr(1.0) >> lit(')') + ; + +auto const hsla_values = x3::rule> {} = + lit("hsla") + >> lit('(') >> dec3 + >> lit(',') >> dec3 >> lit('%') + >> lit(',') >> dec3 >> lit('%') + >> lit(',') >> double_ >> lit(')') + ; + +auto const hsl_color = x3::rule {} = hsl_values[hsl_to_rgba]; +auto const hsla_color = x3::rule {} = hsla_values[hsl_to_rgba]; + auto const svg2_color_def = no_case[named_colors] | @@ -342,6 +394,10 @@ auto const svg2_color_def = rgb_color_percent | rgba_color_percent + | + hsl_color + | + hsla_color ; BOOST_SPIRIT_DEFINE(