fix(core): accept alpha in hsl colors (#4435)

This commit is contained in:
Chris Gervang
2026-07-28 08:29:01 -07:00
committed by GitHub
parent 5bcdc704b8
commit 83ce54ed3e
3 changed files with 20 additions and 0 deletions

View File

@@ -29,6 +29,7 @@
### 🐞 Bug fixes ### 🐞 Bug fixes
- *...Add new stuff here...* - *...Add new stuff here...*
- [core] Accept alpha values in legacy comma-separated `hsl()` colors ([#4434](https://github.com/maplibre/maplibre-native/issues/4434)).
- [core] Fix thread-unsafe headless OpenGL display singleton initialization [#4332](https://github.com/maplibre/maplibre-native/pull/4332) - [core] Fix thread-unsafe headless OpenGL display singleton initialization [#4332](https://github.com/maplibre/maplibre-native/pull/4332)
- [macos] Fix `mlt-cpp` and `mbgl-vendor-icu` not being included in the amalgamation - [macos] Fix `mlt-cpp` and `mbgl-vendor-icu` not being included in the amalgamation
- [core] Fix memory access violation exception in vector_tile_data.cpp [#632](https://github.com/maplibre/maplibre-native/pull/632) - [core] Fix memory access violation exception in vector_tile_data.cpp [#632](https://github.com/maplibre/maplibre-native/pull/632)

View File

@@ -26,6 +26,8 @@ const std::map<std::string, std::optional<Color>> testCases = {
{"rgb(-10, 0, 0)", Color(0.0f, 0.0f, 0.0f, 1.0f)}, // Clamped to 0 {"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(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)}, {"rgba(100,100,100,0.2)", Color(20.0f / 255, 20.0f / 255, 20.0f / 255, 0.2f)},
{"hsl(120,100%,25%)", Color(0.0f, 0.5f, 0.0f, 1.0f)},
{"hsl(240,0%,55%,0.2)", Color(0.11f, 0.11f, 0.11f, 0.2f)},
// 4-digit hex with alpha (#RGBA) // 4-digit hex with alpha (#RGBA)
{"#0F0F", Color(0.0f, 1.0f, 0.0f, 1.0f)}, {"#0F0F", Color(0.0f, 1.0f, 0.0f, 1.0f)},
{"#F00C", Color(0.8f, 0.0f, 0.0f, 0.8f)}, // Red with ~80% alpha {"#F00C", Color(0.8f, 0.0f, 0.0f, 0.8f)}, // Red with ~80% alpha
@@ -38,6 +40,9 @@ const std::map<std::string, std::optional<Color>> testCases = {
// Invalid inputs // Invalid inputs
{"not-a-color", std::nullopt}, {"not-a-color", std::nullopt},
{"", std::nullopt}, {"", std::nullopt},
{"hsl(120,100%)", std::nullopt},
{"hsl(120,100%,50%,1,0)", std::nullopt},
{"hsla(120,100%,50%)", std::nullopt},
}; };
TEST(ColorParse, AllCases) { TEST(ColorParse, AllCases) {
@@ -61,3 +66,15 @@ TEST(ColorParse, AllCases) {
} }
} }
} }
TEST(ColorParse, HSLWithAlphaMatchesHSLA) {
const auto hsl = Color::parse("hsl(114,55%,73%,0.8)");
const auto hsla = Color::parse("hsla(114,55%,73%,0.8)");
ASSERT_TRUE(hsl.has_value());
ASSERT_TRUE(hsla.has_value());
EXPECT_FLOAT_EQ(hsl->r, hsla->r);
EXPECT_FLOAT_EQ(hsl->g, hsla->g);
EXPECT_FLOAT_EQ(hsl->b, hsla->b);
EXPECT_FLOAT_EQ(hsl->a, hsla->a);
}

View File

@@ -282,6 +282,8 @@ std::optional<Color> parse(const std::string& css_str) {
return {}; return {};
} }
alpha = parse_css_float(params.back()); alpha = parse_css_float(params.back());
} else if (params.size() == 4) {
alpha = parse_css_float(params.back());
} else { } else {
if (params.size() != 3) { if (params.size() != 3) {
return {}; return {};