Skip to content

Commit 325ad38

Browse files
committed
fix hang when using very large angles for hue in hsl colors
1 parent 4f51c64 commit 325ad38

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

csscolorparser.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,9 @@ optional<Color> parse(const std::string& css_str) {
262262
}
263263

264264
float h = parseFloat(params[0]) / 360.0f;
265-
while (h < 0.0f) h++;
266-
while (h > 1.0f) h--;
265+
float i;
266+
// Normalize the hue to [0..1[
267+
h = std::modf(h, &i);
267268

268269
// NOTE(deanm): According to the CSS spec s/l should only be
269270
// percentages, but we don't bother and let float or percentage.

test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ int main() {
4747
ASSERT_EQUAL({}, "hsla(900, 15%, 90%)");
4848
ASSERT_EQUAL(Color{ 226, 233, 233, 1 }, "hsl(900, 15%, 90%)");
4949
ASSERT_EQUAL(Color{ 226, 233, 233, 1 }, "hsl(900, 0.15, 90%)"); // NOTE: not spec compliamt.
50+
ASSERT_EQUAL(Color{ 0, 0, 0, 1 }, "hsl(9999999999999999999, 0, 0)"); // NOTE: float precision loss
51+
ASSERT_EQUAL(Color{ 255, 191, 0, 1 }, "hsl(45, 100%, 50%)");
52+
ASSERT_EQUAL(Color{ 255, 191, 0, 1 }, "hsl(-315, 100%, 50%)");
53+
ASSERT_EQUAL(Color{ 255, 191, 0, 1 }, "hsl(-675, 100%, 50%)");
5054

5155
// Out of range:
5256
ASSERT_EQUAL({}, "xxx");

0 commit comments

Comments
 (0)