Use QColor.fromString instead of deprecated QColor.setNamedColor#2877
Use QColor.fromString instead of deprecated QColor.setNamedColor#2877j9ac9k merged 6 commits intopyqtgraph:masterfrom
Conversation
|
We could use the constructor form instead, which is not deprecated and works across all supported versions, and thus avoid having to differentiate between versions. >>> QtGui.QColor("steelblue") |
|
Thanks for the PR @zariiii9003 ...appreciate the lookout for deprecated methods! Probably should go w/ the constructor form instead, |
|
Thanks for your comments, i'll update the PR soon |
7e38ec8 to
95b1597
Compare
|
Your changes have made the following to no longer work: In [1]: import pyqtgraph as pg
In [2]: pg.mkColor('steelblue')
Out[2]: PySide6.QtGui.QColor.fromRgbF(0.274510, 0.509804, 0.705882, 1.000000)Referring to: |
pyqtgraph/functions.py
Outdated
| qcol = QtGui.QColor() | ||
| qcol.setNamedColor(c) | ||
| # match hex color codes | ||
| match = re.match(r"#([0-9a-fA-F]{3,8})", c) |
There was a problem hiding this comment.
My personal impression is that it might be better to try avoiding the overhead of running the regex parser just to confirm the hex nature of the input string. As a relatively low level function, I am sure that there's a use case where mkColor gets called a lot.
Can we fix the deprecation problem with setNamedColor by using qcol = QtGui.QColor(c)?
| if qcol.isValid(): | ||
| return qcol | ||
| # on failure, fallback to pyqtgraph parsing | ||
| # this includes the deprecated case of non-#-prefixed hex strings |
There was a problem hiding this comment.
Did we intend to throw a deprecation warning when the code falls through into the path for non #-prefixed hex codes?
Hex codes without leading '#' character have been out of the documentation for a while (two years?) now, but it seems that we've continued to just silently accept them.
If we change the code to handle all valid string cases ('#'-prefixed hex and valid color names) above, then we can raise a deprecation warning here, and it should only trigger for hex strings with missing prefix.
There was a problem hiding this comment.
Did we intend to throw a deprecation warning when the code falls through into the path for non #-prefixed hex codes?
We killed support for this in 0.13.0 but I think it still works...
https://github.com/pyqtgraph/pyqtgraph/blob/pyqtgraph-0.12.4/pyqtgraph/functions.py#L272-L276
| have_alpha = len(c) in [5, 9] and c[0] == '#' # "#RGBA" and "#RRGGBBAA" | ||
| if not have_alpha: | ||
| # try parsing SVG named colors, including "#RGB" and "#RRGGBB". | ||
| # note that QColor.setNamedColor() treats a 9-char hex string as "#AARRGGBB". |
There was a problem hiding this comment.
We could convert pyqtgraph's internal #RRGGBBAA hex string format to Qt's #AARRGGBB format by simply reshuffling the string, then the code could go through the QColor(string) constructor in all cases where the input is a string.
If length is '#' + 4, then the format is RGBA and needs to be expanded manually by e.g. the neat "".join([x + x for x in c]) construction used below.
After that, or when length is '#' + 8, then reshuffle c = '#' + c[8:10] + c[1:7]
Then, QColor(c) should handle everything.
bac84ef to
0d05c04
Compare
|
Hi @zariiii9003 Thanks for the PR, this LGTM, I'm about to merge. I wanted to apologize for the delay on the merge. Thanks! |
QColor.setNamedColoris deprecated since Qt 6.6. UseQColor.fromStringif available.