From a812f37c604805e003bec44e00b73cc982e0aff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 14 May 2014 15:04:33 +0200 Subject: [PATCH] make sure to premultiply the colors during stylesheet parsing fixes #193 --- include/llmr/style/properties.hpp | 1 + src/style/style_parser.cpp | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/include/llmr/style/properties.hpp b/include/llmr/style/properties.hpp index 22d5cf9cc..eae2351f4 100644 --- a/include/llmr/style/properties.hpp +++ b/include/llmr/style/properties.hpp @@ -8,6 +8,7 @@ namespace llmr { +// Stores a premultiplied color, with all four channels ranging from 0..1 typedef std::array Color; enum class Winding { diff --git a/src/style/style_parser.cpp b/src/style/style_parser.cpp index 9860f56c6..83c408dd3 100644 --- a/src/style/style_parser.cpp +++ b/src/style/style_parser.cpp @@ -333,18 +333,25 @@ Color StyleParser::parseColor(JSVal value) { throw Style::exception("color values must be numbers"); } - return {{static_cast(r.GetDouble()), - static_cast(g.GetDouble()), - static_cast(b.GetDouble()), - static_cast(a.GetDouble())}}; + // Premultiply the color. + const double alpha = a.GetDouble(); + return {{static_cast(alpha * r.GetDouble()), + static_cast(alpha * g.GetDouble()), + static_cast(alpha * b.GetDouble()), + static_cast(alpha)}}; } else if (!rvalue.IsString()) { throw Style::exception("color value must be a string"); } CSSColorParser::Color css_color = CSSColorParser::parse({ rvalue.GetString(), rvalue.GetStringLength() }); - return {{(float)css_color.r / 255, (float)css_color.g / 255, - (float)css_color.b / 255, css_color.a}}; + + // Premultiply the color. + const float factor = css_color.a / 255; + return {{(float)css_color.r * factor, + (float)css_color.g * factor, + (float)css_color.b * factor, + css_color.a}}; } FunctionProperty::fn StyleParser::parseFunctionType(JSVal type) {