summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2017-03-31 18:07:45 +0300
committerIvo van Dongen <ivovandongen@users.noreply.github.com>2017-04-04 18:31:11 +0300
commit705caf0f269af30f4aa72e314034f2225d7a9be1 (patch)
tree9a7ff560c939645ecfd28977367647f6e0854664
parent4a4b6a8af306691588602fbc6f3ed17c5f193355 (diff)
downloadqtlocation-mapboxgl-705caf0f269af30f4aa72e314034f2225d7a9be1.tar.gz
[core] return empty optional when color could not be parsed
m---------mapbox-gl-js0
-rw-r--r--src/csscolorparser/csscolorparser.cpp20
-rw-r--r--src/csscolorparser/csscolorparser.hpp6
-rw-r--r--src/mbgl/util/color.cpp20
4 files changed, 27 insertions, 19 deletions
diff --git a/mapbox-gl-js b/mapbox-gl-js
-Subproject 3d8ca27a1bf384142d2322b3b6ca1be17611555
+Subproject e2f0fa906ad1f91e306557e7ef0361dbb37f0f0
diff --git a/src/csscolorparser/csscolorparser.cpp b/src/csscolorparser/csscolorparser.cpp
index eb75523f18..e4db8c7db9 100644
--- a/src/csscolorparser/csscolorparser.cpp
+++ b/src/csscolorparser/csscolorparser.cpp
@@ -179,7 +179,7 @@ std::vector<std::string> split(const std::string& s, char delim) {
return elems;
}
-Color parse(const std::string& css_str) {
+optional<Color> parse(const std::string& css_str) {
std::string str = css_str;
// Remove all whitespace, not compliant, but should just be more accepting.
@@ -191,7 +191,7 @@ Color parse(const std::string& css_str) {
for (size_t i = 0; i < namedColorCount; i++) {
if (str == namedColors[i].name) {
- return namedColors[i].color;
+ return { namedColors[i].color };
}
}
@@ -202,24 +202,24 @@ Color parse(const std::string& css_str) {
if (!(iv >= 0 && iv <= 0xfff)) {
return {};
} else {
- return {
+ return {{
static_cast<uint8_t>(((iv & 0xf00) >> 4) | ((iv & 0xf00) >> 8)),
static_cast<uint8_t>((iv & 0xf0) | ((iv & 0xf0) >> 4)),
static_cast<uint8_t>((iv & 0xf) | ((iv & 0xf) << 4)),
1
- };
+ }};
}
} else if (str.length() == 7) {
int64_t iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing.
if (!(iv >= 0 && iv <= 0xffffff)) {
return {}; // Covers NaN.
} else {
- return {
+ return {{
static_cast<uint8_t>((iv & 0xff0000) >> 16),
static_cast<uint8_t>((iv & 0xff00) >> 8),
static_cast<uint8_t>(iv & 0xff),
1
- };
+ }};
}
}
@@ -245,12 +245,12 @@ Color parse(const std::string& css_str) {
}
}
- return {
+ return {{
parse_css_int(params[0]),
parse_css_int(params[1]),
parse_css_int(params[2]),
alpha
- };
+ }};
} else if (fname == "hsla" || fname == "hsl") {
if (fname == "hsla") {
@@ -276,12 +276,12 @@ Color parse(const std::string& css_str) {
float m2 = l <= 0.5f ? l * (s + 1.0f) : l + s - l * s;
float m1 = l * 2.0f - m2;
- return {
+ return {{
clamp_css_byte(css_hue_to_rgb(m1, m2, h + 1.0f / 3.0f) * 255.0f),
clamp_css_byte(css_hue_to_rgb(m1, m2, h) * 255.0f),
clamp_css_byte(css_hue_to_rgb(m1, m2, h - 1.0f / 3.0f) * 255.0f),
alpha
- };
+ }};
}
}
diff --git a/src/csscolorparser/csscolorparser.hpp b/src/csscolorparser/csscolorparser.hpp
index c0ef767f0e..0d5feeb8ce 100644
--- a/src/csscolorparser/csscolorparser.hpp
+++ b/src/csscolorparser/csscolorparser.hpp
@@ -27,9 +27,13 @@
#include <string>
#include <cmath>
+#include <experimental/optional>
namespace CSSColorParser {
+template <class T>
+using optional = std::experimental::optional<T>;
+
struct Color {
inline Color() {
}
@@ -48,7 +52,7 @@ inline bool operator!=(const Color& lhs, const Color& rhs) {
return !(lhs == rhs);
}
-Color parse(const std::string& css_str);
+optional<Color> parse(const std::string& css_str);
} // namespace CSSColorParser
diff --git a/src/mbgl/util/color.cpp b/src/mbgl/util/color.cpp
index d5751b405f..fcb7f4ec16 100644
--- a/src/mbgl/util/color.cpp
+++ b/src/mbgl/util/color.cpp
@@ -6,16 +6,20 @@
namespace mbgl {
optional<Color> Color::parse(const std::string& s) {
- CSSColorParser::Color css_color = CSSColorParser::parse(s);
+ auto css_color = CSSColorParser::parse(s);
// Premultiply the color.
- const float factor = css_color.a / 255;
- return {{
- css_color.r * factor,
- css_color.g * factor,
- css_color.b * factor,
- css_color.a
- }};
+ if (css_color) {
+ const float factor = css_color->a / 255;
+ return {{
+ css_color->r * factor,
+ css_color->g * factor,
+ css_color->b * factor,
+ css_color->a
+ }};
+ } else {
+ return {};
+ }
}
std::string Color::stringify() const {