def setBrush(self, brush: QBrush, autoTextColor: bool = True): super().setBrush(brush) if autoTextColor: # Calculate the perceptive luminance (aka luma) - human eye favors green color... # See https://stackoverflow.com/questions/1855884/determine-font-color-based-on-background-color color = QBrush(brush).color() if color.alpha() < 128: self._text_color = QColor(Qt.black) else: luma = (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255 self._text_color = QColor(Qt.black) if luma > 0.5 else QColor(Qt.white)