diff --git a/csscolorparser.cpp b/csscolorparser.cpp index 5658f2b..106dae6 100644 --- a/csscolorparser.cpp +++ b/csscolorparser.cpp @@ -185,10 +185,9 @@ optional parse(const std::string& css_str) { // Convert to lowercase. std::transform(str.begin(), str.end(), str.begin(), ::tolower); - - for (size_t i = 0; i < namedColorCount; i++) { - if (str == namedColors[i].name) { - return { namedColors[i].color }; + for (const auto& namedColor : namedColors) { + if (str == namedColor.name) { + return { namedColor.color }; } } @@ -262,8 +261,9 @@ optional parse(const std::string& css_str) { } float h = parseFloat(params[0]) / 360.0f; - while (h < 0.0f) h++; - while (h > 1.0f) h--; + float i; + // Normalize the hue to [0..1[ + h = std::modf(h, &i); // NOTE(deanm): According to the CSS spec s/l should only be // percentages, but we don't bother and let float or percentage. diff --git a/test.cpp b/test.cpp index fc93644..25fa852 100644 --- a/test.cpp +++ b/test.cpp @@ -47,6 +47,10 @@ int main() { ASSERT_EQUAL({}, "hsla(900, 15%, 90%)"); ASSERT_EQUAL(Color{ 226, 233, 233, 1 }, "hsl(900, 15%, 90%)"); ASSERT_EQUAL(Color{ 226, 233, 233, 1 }, "hsl(900, 0.15, 90%)"); // NOTE: not spec compliamt. + ASSERT_EQUAL(Color{ 0, 0, 0, 1 }, "hsl(9999999999999999999, 0, 0)"); // NOTE: float precision loss + ASSERT_EQUAL(Color{ 255, 191, 0, 1 }, "hsl(45, 100%, 50%)"); + ASSERT_EQUAL(Color{ 255, 191, 0, 1 }, "hsl(-315, 100%, 50%)"); + ASSERT_EQUAL(Color{ 255, 191, 0, 1 }, "hsl(-675, 100%, 50%)"); // Out of range: ASSERT_EQUAL({}, "xxx");