Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Fixed to-rgba with zero opacity
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Mar 30, 2018
1 parent f4d9fb8 commit d066e94
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
2 changes: 2 additions & 0 deletions include/mbgl/util/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <cassert>
#include <string>
#include <array>

namespace mbgl {

Expand Down Expand Up @@ -37,6 +38,7 @@ class Color {

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) {
Expand Down
2 changes: 1 addition & 1 deletion mapbox-gl-js
7 changes: 1 addition & 6 deletions src/mbgl/style/expression/compound_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions src/mbgl/style/expression/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 18 additions & 4 deletions src/mbgl/util/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d066e94

Please sign in to comment.