[core] return empty optional when color could not be parsed
This commit is contained in:
committed by
Ivo van Dongen
parent
4a4b6a8af3
commit
705caf0f26
Submodule mapbox-gl-js updated: 3d8ca27a1b...e2f0fa906a
@@ -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
|
||||
};
|
||||
}};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user