Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of HSL color with floats #58170

Closed
2 tasks done
lazaa32 opened this issue Jul 18, 2024 · 5 comments · Fixed by #58177
Closed
2 tasks done

Fix parsing of HSL color with floats #58170

lazaa32 opened this issue Jul 18, 2024 · 5 comments · Fixed by #58177
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter!

Comments

@lazaa32
Copy link
Contributor

lazaa32 commented Jul 18, 2024

What is the bug or the crash?

HSL color definition consists of:

  • hue - in degrees 0-360
  • saturation - in percentage 0-100
  • lightness - in percentage 0-100

Unlike RGB where parameters are integers, for HSL valid color definition can be e.g. hsl(74.5, 15.6%, 77.1%) (float), however trying to parse this fails. On the other hand parsing hsl(74, 15%, 77%) (only integers) goes without any problem. I'm using gsSymbolLayerUtils.parseColor() method.

Steps to reproduce the issue

Open Python console (Ctrl+Alt+P)
Run this code:

from qgis.core import QgsSymbolLayerUtils
QgsSymbolLayerUtils.parseColor('hsl(77,50%,50%)')
>>> <QColor: HSLA 77, 127, 127, 255>
QgsSymbolLayerUtils.parseColor('hsl(77.1,50.2%,50.2%)')
>>> <QColor: invalid>

Versions

<style type="text/css"> p, li { white-space: pre-wrap; } </style>
QGIS version 3.38.0-Grenoble QGIS code revision 37aa618
Qt version 5.15.8
Python version 3.11.4
GDAL/OGR version 3.6.2
PROJ version 9.1.1
EPSG Registry database version v10.076 (2022-08-31)
GEOS version 3.11.1-CAPI-1.17.1
SQLite version 3.40.1
PostgreSQL client version 15.5 (Ubuntu 15.5-0ubuntu0.23.04.1)
SpatiaLite version 5.0.1
QWT version 6.1.4
QScintilla2 version 2.13.3
OS version Ubuntu 23.04
       
Active Python plugins
processing 2.12.99
grassprovider 2.12.99
MetaSearch 0.3.6
db_manager 0.1.20
QGIS version 3.38.0-Grenoble QGIS code revision [37aa618](https://github.com/qgis/QGIS/commit/37aa6188bc3) Qt version 5.15.8 Python version 3.11.4 GDAL/OGR version 3.6.2 PROJ version 9.1.1 EPSG Registry database version v10.076 (2022-08-31) GEOS version 3.11.1-CAPI-1.17.1 SQLite version 3.40.1 PostgreSQL client version 15.5 (Ubuntu 15.5-0ubuntu0.23.04.1) SpatiaLite version 5.0.1 QWT version 6.1.4 QScintilla2 version 2.13.3 OS version Ubuntu 23.04

Active Python plugins
processing
2.12.99
grassprovider
2.12.99
MetaSearch
0.3.6
db_manager
0.1.20

Supported QGIS version

  • I'm running a supported QGIS version according to the roadmap.

New profile

Additional context

No response

@lazaa32 lazaa32 added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label Jul 18, 2024
@roya0045
Copy link
Contributor

Qt itself only accepts int, https://doc.qt.io/qt-6/qcolor.html#getHsl

But i find strange that the parsing need the % char.

@lazaa32
Copy link
Contributor Author

lazaa32 commented Jul 18, 2024

The values are casted to int before giving to Qt.

const int h = match.captured( 1 ).toInt();
const int s = match.captured( 2 ).toInt();
const int l = match.captured( 3 ).toInt();

But the regex
const thread_local QRegularExpression hslFormatRx( "^\\s*hsl\\(?\\s*(\\d+)\\s*,\\s*(\\d+)\\s*%\\s*,\\s*(\\d+)\\s*%\\s*\\)?\\s*;?\\s*$" );
matches only integer "^\\s*hsl\\(?\\s*(\\d+)\\s*,\\s*(\\d+)\\s*%\\s*,\\s*(\\d+)\\s*%\\s*\\)?\\s*;?\\s*$"

@lazaa32
Copy link
Contributor Author

lazaa32 commented Jul 18, 2024

Qt itself only accepts int, https://doc.qt.io/qt-6/qcolor.html#getHsl

Qt accepts float as well, https://doc.qt.io/qt-6/qcolor.html#setHslF

@roya0045
Copy link
Contributor

Qt itself only accepts int, https://doc.qt.io/qt-6/qcolor.html#getHsl

Qt accepts float as well, https://doc.qt.io/qt-6/qcolor.html#setHslF

If they are in the range 0 to 1.0, not bigger.

@lazaa32
Copy link
Contributor Author

lazaa32 commented Jul 18, 2024

Qt itself only accepts int, https://doc.qt.io/qt-6/qcolor.html#getHsl

Qt accepts float as well, https://doc.qt.io/qt-6/qcolor.html#setHslF

If they are in the range 0 to 1.0, not bigger.

Oh yes, you're right.
I would still expect this:

QgsSymbolLayerUtils.parseColor('hsl(77,50%,50%)')
>>> <QColor: HSLA 77, 127, 127, 255>
QgsSymbolLayerUtils.parseColor('hsl(77.1,50.2%,50.2%)')
>>> <QColor: HSLA 77, 127, 127, 255>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants