pyqt5 通过QLinearGradient 绘制取色板

要绘制HSV取色板,一般通过绘制前景色和背景色的方式实现,先绘制前景,然后绘制背景,前景是HSV颜色空间,从左到右,背景是亮度,从上到下,xs和ys是鼠标的当前的位置。

pyqt5 通过QLinearGradient 绘制取色板_第1张图片

    def graphicsView_rgb_palette(self,xs,ys):
        try:
            rect = QRectF(0, 0, self.graphicsView_color_palette.width() - 2,
                          self.graphicsView_color_palette.height() - 2)
            self.graphicsView_color_palette_scene = QGraphicsScene(rect)  # 创建场景
            self.graphicsView_color_palette.setScene(self.graphicsView_color_palette_scene)
            #前景,HSV空间 ,从360到0
            colorGradient = QLinearGradient(0, 0, self.graphicsView_color_palette.width(),0)
            colorGradient.setSpread(QGradient.RepeatSpread);
            colorGradient.setColorAt(0, QColor(255, 0, 0, 255));
            colorGradient.setColorAt(0.166, QColor(255, 255, 0, 255));
            colorGradient.setColorAt(0.333, QColor(0, 255, 0, 255));
            colorGradient.setColorAt(0.5, QColor(0, 255, 255, 255));
            colorGradient.setColorAt(0.666, QColor(0, 0, 255, 255));
            colorGradient.setColorAt(0.833, QColor(255, 0, 255, 255));
            colorGradient.setColorAt(1, QColor(255, 0, 0, 255));

            #背景,调节亮度,最上面透明,最下面白色
            blackGradient = QLinearGradient(0, 0, 0,self.graphicsView_color_palette.height())
            blackGradient.setSpread(QGradient.RepeatSpread);
            blackGradient.setColorAt(0,QColor(0,0,0,0));
            blackGradient.setColorAt(1, QColor(255,255,255,250));
            #绘制取色板鼠标指示点
            item = QGraphicsEllipseItem(-10, -10, 20, 20)  # 创建椭圆--场景坐标
            item.setPos(xs, ys)  # 给图元设置在场景中的坐标(移动图元)--图元中心坐标
            item.setBrush(Qt.black)  # 设置画刷
            #
            self.graphicsView_color_palette_scene.setBackgroundBrush(colorGradient); #设置前景色
            self.graphicsView_color_palette_scene.setForegroundBrush(blackGradient); #设置背景色

            self.graphicsView_color_palette_scene.addItem(item)
            self.graphicsView_color_palette_scene.clearSelection()  # 【清除选择】
        except Exception as e:
            self.UART_Display_Text(str(e))

你可能感兴趣的:(pyqt5 通过QLinearGradient 绘制取色板)