Skip to content

Commit

Permalink
Fix color.rs.cpp impl, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
louwers committed Jan 15, 2025
1 parent 038fa16 commit 014f2ba
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/mbgl/util/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace mbgl {

std::optional<Color> Color::parse(const std::string& s) {
auto css_color = CSSColorParser::parse(s);
const auto css_color = CSSColorParser::parse(s);

// Premultiply the color.
if (css_color) {
Expand Down
5 changes: 3 additions & 2 deletions src/mbgl/util/color.rs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
namespace mbgl {

std::optional<Color> Color::parse(const std::string& s) {
auto css_color = rustutils::parse_css_color(s);
const auto css_color = rustutils::parse_css_color(s);
if (css_color.success) {
return {{css_color.r, css_color.g, css_color.b, css_color.a}};
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 {};
}
Expand Down
1 change: 1 addition & 0 deletions test/util/color.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const std::map<std::string, std::optional<Color>> testCases = {
{"#123", Color(0.067f, 0.133f, 0.2f, 1.0f)}, // Short hex format
{"rgb(-10, 0, 0)", Color(0.0f, 0.0f, 0.0f, 1.0f)}, // Clamped to 0
{"rgba(300, 0, 0, 1.0)", Color(1.0f, 0.0f, 0.0f, 1.0f)}, // Clamped to 1
{"rgba(100,100,100,0.2)", Color(20.0f / 255, 20.0f / 255, 20.0f / 255, 0.2f)},
// {"#GGGGGG", Color(0.0f, 0.0f, 0.0f, 1.0f)}, // Treated as fallback black
// not supported right now
// {"#0F0F", Color(0.0f, 1.0f, 0.0f, 1.0f)},
Expand Down

0 comments on commit 014f2ba

Please sign in to comment.