Skip to content

Commit

Permalink
PyQt doesn't provide repr for QColor, but it's highly desirable!
Browse files Browse the repository at this point in the history
So monkey patch one in ourselves
  • Loading branch information
nyalldawson committed Apr 5, 2023
1 parent 973549c commit 7e0e421
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions python/PyQt/PyQt5/QtGui.py
Expand Up @@ -22,3 +22,48 @@
__copyright__ = '(C) 2015, Matthias Kuhn'

from PyQt5.QtGui import *


def __qcolor_repr__(self: QColor):
if not self.isValid():
return '<QColor: invalid>'
elif self.spec() == QColor.Rgb:
return '<QColor: RGB {}, {}, {}, {}>'.format(
self.red(),
self.green(),
self.blue(),
self.alpha()
)
elif self.spec() == QColor.Hsv:
return '<QColor: HSV {}, {}, {}, {}>'.format(
self.hsvHue(),
self.hsvSaturation(),
self.value(),
self.alpha()
)
elif self.spec() == QColor.Cmyk:
return '<QColor: CMYK {}, {}, {}, {}, {}>'.format(
self.cyan(),
self.magenta(),
self.yellow(),
self.black(),
self.alpha()
)
elif self.spec() == QColor.Hsl:
return '<QColor: HSL {}, {}, {}, {}>'.format(
self.hslHue(),
self.hslSaturation(),
self.lightness(),
self.alpha()
)
elif self.spec() == QColor.ExtendedRgb:
return '<QColor: Extended RGB {}, {}, {}, {}>'.format(
self.redF(),
self.greenF(),
self.blueF(),
self.alphaF()
)


# PyQt doesn't provide __repr__ for QColor, but it's highly desirable!
QColor.__repr__ = __qcolor_repr__

0 comments on commit 7e0e421

Please sign in to comment.