[core] Fixed to-rgba with zero opacity

This commit is contained in:
Minh Nguyễn
2018-03-30 03:21:44 -07:00
parent f4d9fb816d
commit d066e94103
5 changed files with 27 additions and 15 deletions

View File

@@ -4,6 +4,7 @@
#include <cassert>
#include <string>
#include <array>
namespace mbgl {
@@ -37,6 +38,7 @@ public:
static optional<Color> parse(const std::string&);
std::string stringify() const;
std::array<double, 4> toArray() const;
};
inline bool operator==(const Color& colorA, const Color& colorB) {

View File

@@ -209,12 +209,7 @@ std::unordered_map<std::string, CompoundExpressionRegistry::Definition> initiali
);
});
define("to-rgba", [](const Color& color) -> Result<std::array<double, 4>> {
return std::array<double, 4> {{
255 * color.r / color.a,
255 * color.g / color.a,
255 * color.b / color.a,
color.a
}};
return color.toArray();
});
define("rgba", rgba);

View File

@@ -106,12 +106,13 @@ Value ValueConverter<mbgl::Value>::toExpressionValue(const mbgl::Value& value) {
mbgl::Value ValueConverter<mbgl::Value>::fromExpressionValue(const Value& value) {
return value.match(
[&](const Color& color)->mbgl::Value {
std::array<double, 4> array = color.toArray();
return std::vector<mbgl::Value>{
std::string("rgba"),
double(255 * color.r / color.a),
double(255 * color.g / color.a),
double(255 * color.b / color.a),
double(color.a)
array[0],
array[1],
array[2],
array[3],
};
},
[&](const std::vector<Value>& values)->mbgl::Value {

View File

@@ -23,11 +23,25 @@ optional<Color> Color::parse(const std::string& s) {
}
std::string Color::stringify() const {
std::array<double, 4> array = toArray();
return "rgba(" +
util::toString(r * 255 / a) + "," +
util::toString(g * 255 / a) + "," +
util::toString(b * 255 / a) + "," +
util::toString(a) + ")";
util::toString(array[0]) + "," +
util::toString(array[1]) + "," +
util::toString(array[2]) + "," +
util::toString(array[3]) + ")";
}
std::array<double, 4> Color::toArray() const {
if (a == 0) {
return {{ 0, 0, 0, 0 }};
} else {
return {{
r * 255 / a,
g * 255 / a,
b * 255 / a,
a,
}};
}
}
} // namespace mbgl