QPainter QColorDialog Unexpected Values (Colorspace issue?)

132 views Asked by At

I'm using PySide6 on MacOS Catalina (10.15.7).

I was trying to do something with QColorDialog and getting some unexpected values. I believe this must be due to some colorspace conversions, but I'm not clear on what is happening or how to correct it.

As an example, I painted a patch of color using the hex value #ACC0C6. When I launch a color picker and use the eyedropper to pick the color of that patch, the returned color is hex value #ADC0C7. The code I used is below.

When I used Mac's Digital Color Meter to check the color of the patch, the "Display native values" option shows the expected hex code and the "Display in sRGB" shows the same value given by QColorDialog.

I have a calibrated display, so I'm thinking the display calibration profile is transforming the color. I'm just not at all sure what to do about it.

#!/usr/bin/env python

import sys
from PySide6.QtWidgets import ( 
    QApplication, QColorDialog, QVBoxLayout, QPushButton, QWidget, )
from PySide6.QtCore import ( Qt, QRect, ) 
from PySide6.QtGui import ( QBrush, QColor, QPainter, QPen, )

class Test(QWidget):
    def __init__(self):
        super().__init__()
        self.setMinimumSize(200,200)
        self.layout = QVBoxLayout(self)
        btn = QPushButton("Pick")
        btn.clicked.connect(self.chooseColor) 
        self.layout.addWidget(btn)
        self.layout.addWidget(ColorPatch())
        self.show()

    def chooseColor(self):
        clrpick = QColorDialog()
        clrpick.setOption(QColorDialog.DontUseNativeDialog)
        print(clrpick.testOption(QColorDialog.DontUseNativeDialog))
        color = clrpick.getColor()
        if color.isValid():
            self.colorfg = color

class ColorPatch(QWidget):
    def __init__(self):
        super().__init__()
        self.painter = QPainter()
        self.nopen = QPen()
        self.nopen.setStyle(Qt.NoPen)
    
    def paintEvent(self, event):
        self.painter.begin(self)
        brush = QBrush()
        brush.setColor(QColor('#ACC0C6'))
        brush.setStyle(Qt.SolidPattern)
        self.painter.setPen(self.nopen)
        self.painter.setBrush(brush)
        self.painter.drawRect(0,0,self.width(),self.height())
        self.painter.end()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setAttribute(Qt.ApplicationAttribute.AA_DontShowIconsInMenus, True)
    window = Test()
    sys.exit(app.exec())

Screenshot

BONUS QUESTION: The QColorDialog.DontUseNativeDialog option doesn't seem to work for me even though testing that option before showing the dialog indicates it is set correctly. Is this an issue specific to MacOS or its version (10.15.7 in my case?)

0

There are 0 answers