Skip to content

Commit

Permalink
Qt Style Sheets: fix range of hue in hsl/hsv functions
Browse files Browse the repository at this point in the history
The hue parameter in hsl/hsv was treated the same way as a the other
parameters although it's range is from 0-359 and not from 0-255.
Fix it by extending the maximum range for the first parameter when
parsing a color value given in hsv or hsl.

[ChangeLog][QtGui][CSS] Fix the range of the hue parameter when parsing
a color given in hsl or hsv

Fixes: QTBUG-70897
Change-Id: I9ffa65a89c0abcca62bae35777ca1cbde3375180
Reviewed-by: Allan Sandfeld Jensen <[email protected]>
  • Loading branch information
chehrlic committed Oct 18, 2018
1 parent d65631d commit aaae027
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/gui/text/qcssparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,8 @@ static ColorData parseColorValue(QCss::Value v)

for (int i = 0; i < qMin(tokenCount, 7); i += 2) {
if (colorDigits.at(i).type == Value::Percentage) {
colorDigits[i].variant = colorDigits.at(i).variant.toReal() * (255. / 100.);
const qreal maxRange = (rgb || i != 0) ? 255. : 359.;
colorDigits[i].variant = colorDigits.at(i).variant.toReal() * (maxRange / 100.);
colorDigits[i].type = Value::Number;
} else if (colorDigits.at(i).type != Value::Number) {
return ColorData();
Expand Down
7 changes: 5 additions & 2 deletions tests/auto/gui/text/qcssparser/tst_qcssparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,10 +840,13 @@ void tst_QCssParser::colorValue_data()
QTest::newRow("rgbaf") << "color: rgba(10, 20, 30, 0.5)" << QColor(10, 20, 30, 127);
QTest::newRow("hsv") << "color: hsv(10, 20, 30)" << QColor::fromHsv(10, 20, 30);
QTest::newRow("hsva") << "color: hsva(10, 20, 30, 40)" << QColor::fromHsv(10, 20, 30, 40);
QTest::newRow("hsvaf") << "color: hsva(10, 20, 30, 0.5)" << QColor::fromHsv(10, 20, 30, 127);
// the percent and float values are well chosen to not get in trouble due to rounding errors
QTest::newRow("hsva-percent") << "color: hsva(100%, 20%, 40%, 60%)" << QColor::fromHsv(359, 51, 102, 153);
QTest::newRow("hsva-float") << "color: hsva(180, 20%, 40%, 0.6)" << QColor::fromHsvF(0.5, 0.2, 0.4, 0.6);
QTest::newRow("hsl") << "color: hsl(60, 100%, 50%)" << QColor::fromHsl(60., 255, 127);
QTest::newRow("hsla") << "color: hsla(240, 255, 127, 192)" << QColor::fromHsl(240, 255, 127, 192);
QTest::newRow("hslaf") << "color: hsla(240, 255, 127, 0.25)" << QColor::fromHsl(240, 255, 127, 63);
QTest::newRow("hsla-percent") << "color: hsla(100%, 80%, 40%, 0%)" << QColor::fromHsl(359, 204, 102, 0);
QTest::newRow("hsla-float") << "color: hsla(252, 40%, 60%, 0.2)" << QColor::fromHslF(0.7, 0.4, 0.6, 0.2);
QTest::newRow("invalid1") << "color: rgb(why, does, it, always, rain, on, me)" << QColor();
QTest::newRow("invalid2") << "color: rgba(i, meant, norway)" << QColor();
QTest::newRow("invalid3") << "color: rgb(21)" << QColor();
Expand Down

0 comments on commit aaae027

Please sign in to comment.