copy pasted PyQtColorTriangle project

This commit is contained in:
iLLiCiTiT 2021-05-19 17:25:54 +02:00
parent b420ea6971
commit d3673ae627
6 changed files with 2392 additions and 0 deletions

View file

@ -0,0 +1,6 @@
from .color_picker_widget import ColorPickerWidget
__all__ = (
"ColorPickerWidget",
)

View file

@ -0,0 +1,514 @@
import re
from Qt import QtWidgets, QtCore, QtGui
slide_style = """
QSlider::groove:horizontal {
background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 #000, stop: 1 #fff);
height: 8px;
border-radius: 4px;
}
QSlider::handle:horizontal {
background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #ddd, stop:1 #bbb);
border: 1px solid #777;
width: 8px;
margin-top: -1px;
margin-bottom: -1px;
border-radius: 4px;
}
QSlider::handle:horizontal:hover {
background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #eee, stop:1 #ddd);
border: 1px solid #444;ff
border-radius: 4px;
}"""
class AlphaInputs(QtWidgets.QGroupBox):
alpha_changed = QtCore.Signal(int)
def __init__(self, parent=None):
super(AlphaInputs, self).__init__("Alpha", parent)
self._block_changes = False
self.alpha_value = None
# Opacity slider
alpha_slider = QtWidgets.QSlider(QtCore.Qt.Horizontal, self)
alpha_slider.setSingleStep(1)
alpha_slider.setMinimum(0)
alpha_slider.setMaximum(255)
alpha_slider.setStyleSheet(slide_style)
alpha_slider.setValue(255)
inputs_widget = QtWidgets.QWidget(self)
inputs_layout = QtWidgets.QHBoxLayout(inputs_widget)
inputs_layout.setContentsMargins(0, 0, 0, 0)
percent_input = QtWidgets.QDoubleSpinBox(self)
percent_input.setMinimum(0)
percent_input.setMaximum(100)
percent_input.setDecimals(2)
int_input = QtWidgets.QSpinBox(self)
int_input.setMinimum(0)
int_input.setMaximum(255)
inputs_layout.addWidget(int_input)
inputs_layout.addWidget(QtWidgets.QLabel("0-255"))
inputs_layout.addWidget(percent_input)
inputs_layout.addWidget(QtWidgets.QLabel("%"))
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(alpha_slider)
layout.addWidget(inputs_widget)
alpha_slider.valueChanged.connect(self._on_slider_change)
percent_input.valueChanged.connect(self._on_percent_change)
int_input.valueChanged.connect(self._on_int_change)
self.alpha_slider = alpha_slider
self.percent_input = percent_input
self.int_input = int_input
self.set_alpha(255)
def set_alpha(self, alpha):
if alpha == self.alpha_value:
return
self.alpha_value = alpha
self.update_alpha()
def _on_slider_change(self):
if self._block_changes:
return
self.alpha_value = self.alpha_slider.value()
self.alpha_changed.emit(self.alpha_value)
self.update_alpha()
def _on_percent_change(self):
if self._block_changes:
return
self.alpha_value = int(self.percent_input.value() * 255 / 100)
self.alpha_changed.emit(self.alpha_value)
self.update_alpha()
def _on_int_change(self):
if self._block_changes:
return
self.alpha_value = self.int_input.value()
self.alpha_changed.emit(self.alpha_value)
self.update_alpha()
def update_alpha(self):
self._block_changes = True
if self.alpha_slider.value() != self.alpha_value:
self.alpha_slider.setValue(self.alpha_value)
if self.int_input.value() != self.alpha_value:
self.int_input.setValue(self.alpha_value)
percent = round(100 * self.alpha_value / 255, 2)
if self.percent_input.value() != percent:
self.percent_input.setValue(percent)
self._block_changes = False
class RGBInputs(QtWidgets.QGroupBox):
value_changed = QtCore.Signal()
def __init__(self, color, parent=None):
super(RGBInputs, self).__init__("RGB", parent)
self._block_changes = False
self.color = color
input_red = QtWidgets.QSpinBox(self)
input_green = QtWidgets.QSpinBox(self)
input_blue = QtWidgets.QSpinBox(self)
input_red.setMinimum(0)
input_green.setMinimum(0)
input_blue.setMinimum(0)
input_red.setMaximum(255)
input_green.setMaximum(255)
input_blue.setMaximum(255)
layout = QtWidgets.QHBoxLayout(self)
layout.addWidget(input_red)
layout.addWidget(input_green)
layout.addWidget(input_blue)
input_red.valueChanged.connect(self._on_red_change)
input_green.valueChanged.connect(self._on_green_change)
input_blue.valueChanged.connect(self._on_blue_change)
self.input_red = input_red
self.input_green = input_green
self.input_blue = input_blue
def _on_red_change(self, value):
if self._block_changes:
return
self.color.setRed(value)
self._on_change()
def _on_green_change(self, value):
if self._block_changes:
return
self.color.setGreen(value)
self._on_change()
def _on_blue_change(self, value):
if self._block_changes:
return
self.color.setBlue(value)
self._on_change()
def _on_change(self):
self.value_changed.emit()
def color_changed(self):
if (
self.input_red.value() == self.color.red()
and self.input_green.value() == self.color.green()
and self.input_blue.value() == self.color.blue()
):
return
self._block_changes = True
self.input_red.setValue(self.color.red())
self.input_green.setValue(self.color.green())
self.input_blue.setValue(self.color.blue())
self._block_changes = False
class CMYKInputs(QtWidgets.QGroupBox):
value_changed = QtCore.Signal()
def __init__(self, color, parent=None):
super(CMYKInputs, self).__init__("CMYK", parent)
self.color = color
self._block_changes = False
input_cyan = QtWidgets.QSpinBox(self)
input_magenta = QtWidgets.QSpinBox(self)
input_yellow = QtWidgets.QSpinBox(self)
input_black = QtWidgets.QSpinBox(self)
input_cyan.setMinimum(0)
input_magenta.setMinimum(0)
input_yellow.setMinimum(0)
input_black.setMinimum(0)
input_cyan.setMaximum(255)
input_magenta.setMaximum(255)
input_yellow.setMaximum(255)
input_black.setMaximum(255)
layout = QtWidgets.QHBoxLayout(self)
layout.addWidget(input_cyan)
layout.addWidget(input_magenta)
layout.addWidget(input_yellow)
layout.addWidget(input_black)
input_cyan.valueChanged.connect(self._on_change)
input_magenta.valueChanged.connect(self._on_change)
input_yellow.valueChanged.connect(self._on_change)
input_black.valueChanged.connect(self._on_change)
self.input_cyan = input_cyan
self.input_magenta = input_magenta
self.input_yellow = input_yellow
self.input_black = input_black
def _on_change(self):
if self._block_changes:
return
self.color.setCmyk(
self.input_cyan.value(),
self.input_magenta.value(),
self.input_yellow.value(),
self.input_black.value()
)
self.value_changed.emit()
def color_changed(self):
if self._block_changes:
return
_cur_color = QtGui.QColor()
_cur_color.setCmyk(
self.input_cyan.value(),
self.input_magenta.value(),
self.input_yellow.value(),
self.input_black.value()
)
if (
_cur_color.red() == self.color.red()
and _cur_color.green() == self.color.green()
and _cur_color.blue() == self.color.blue()
):
return
c, m, y, k, _ = self.color.getCmyk()
self._block_changes = True
self.input_cyan.setValue(c)
self.input_magenta.setValue(m)
self.input_yellow.setValue(y)
self.input_black.setValue(k)
self._block_changes = False
class HEXInputs(QtWidgets.QGroupBox):
hex_regex = re.compile("^#(([0-9a-fA-F]{2}){3}|([0-9a-fA-F]){3})$")
value_changed = QtCore.Signal()
def __init__(self, color, parent=None):
super(HEXInputs, self).__init__("HEX", parent)
self.color = color
input_field = QtWidgets.QLineEdit()
layout = QtWidgets.QHBoxLayout(self)
layout.addWidget(input_field)
input_field.textChanged.connect(self._on_change)
self.input_field = input_field
def _on_change(self):
if self._block_changes:
return
input_value = self.input_field.text()
# TODO what if does not match?
if self.hex_regex.match(input_value):
self.color.setNamedColor(input_value)
self.value_changed.emit()
def color_changed(self):
input_value = self.input_field.text()
if self.hex_regex.match(input_value):
_cur_color = QtGui.QColor()
_cur_color.setNamedColor(input_value)
if (
_cur_color.red() == self.color.red()
and _cur_color.green() == self.color.green()
and _cur_color.blue() == self.color.blue()
):
return
self._block_changes = True
self.input_field.setText(self.color.name())
self._block_changes = False
class HSVInputs(QtWidgets.QGroupBox):
value_changed = QtCore.Signal()
def __init__(self, color, parent=None):
super(HSVInputs, self).__init__("HSV", parent)
self._block_changes = False
self.color = color
input_hue = QtWidgets.QSpinBox(self)
input_sat = QtWidgets.QSpinBox(self)
input_val = QtWidgets.QSpinBox(self)
input_hue.setMinimum(0)
input_sat.setMinimum(0)
input_val.setMinimum(0)
input_hue.setMaximum(359)
input_sat.setMaximum(255)
input_val.setMaximum(255)
layout = QtWidgets.QHBoxLayout(self)
layout.addWidget(input_hue)
layout.addWidget(input_sat)
layout.addWidget(input_val)
input_hue.valueChanged.connect(self._on_change)
input_sat.valueChanged.connect(self._on_change)
input_val.valueChanged.connect(self._on_change)
self.input_hue = input_hue
self.input_sat = input_sat
self.input_val = input_val
def _on_change(self):
if self._block_changes:
return
self.color.setHsv(
self.input_hue.value(),
self.input_sat.value(),
self.input_val.value()
)
self.value_changed.emit()
def color_changed(self):
_cur_color = QtGui.QColor()
_cur_color.setHsv(
self.input_hue.value(),
self.input_sat.value(),
self.input_val.value()
)
if (
_cur_color.red() == self.color.red()
and _cur_color.green() == self.color.green()
and _cur_color.blue() == self.color.blue()
):
return
self._block_changes = True
h, s, v, _ = self.color.getHsv()
self.input_hue.setValue(h)
self.input_sat.setValue(s)
self.input_val.setValue(v)
self._block_changes = False
class HSLInputs(QtWidgets.QGroupBox):
value_changed = QtCore.Signal()
def __init__(self, color, parent=None):
super(HSLInputs, self).__init__("HSL", parent)
self._block_changes = False
self.color = color
input_hue = QtWidgets.QSpinBox(self)
input_sat = QtWidgets.QSpinBox(self)
input_light = QtWidgets.QSpinBox(self)
input_hue.setMinimum(0)
input_sat.setMinimum(0)
input_light.setMinimum(0)
input_hue.setMaximum(359)
input_sat.setMaximum(255)
input_light.setMaximum(255)
layout = QtWidgets.QHBoxLayout(self)
layout.addWidget(input_hue)
layout.addWidget(input_sat)
layout.addWidget(input_light)
input_hue.valueChanged.connect(self._on_change)
input_sat.valueChanged.connect(self._on_change)
input_light.valueChanged.connect(self._on_change)
self.input_hue = input_hue
self.input_sat = input_sat
self.input_light = input_light
def _on_change(self):
if self._block_changes:
return
self.color.setHsl(
self.input_hue.value(),
self.input_sat.value(),
self.input_light.value()
)
self.value_changed.emit()
def color_changed(self):
_cur_color = QtGui.QColor()
_cur_color.setHsl(
self.input_hue.value(),
self.input_sat.value(),
self.input_light.value()
)
if (
_cur_color.red() == self.color.red()
and _cur_color.green() == self.color.green()
and _cur_color.blue() == self.color.blue()
):
return
self._block_changes = True
h, s, l, _ = self.color.getHsl()
self.input_hue.setValue(h)
self.input_sat.setValue(s)
self.input_light.setValue(l)
self._block_changes = False
class ColorInputsWidget(QtWidgets.QWidget):
color_changed = QtCore.Signal(QtGui.QColor)
def __init__(self, parent=None, **kwargs):
super(ColorInputsWidget, self).__init__(parent)
color = QtGui.QColor()
input_fields = []
if kwargs.get("use_hex", True):
input_fields.append(HEXInputs(color, self))
if kwargs.get("use_rgb", True):
input_fields.append(RGBInputs(color, self))
if kwargs.get("use_hsl", True):
input_fields.append(HSLInputs(color, self))
if kwargs.get("use_hsv", True):
input_fields.append(HSVInputs(color, self))
if kwargs.get("use_cmyk", True):
input_fields.append(CMYKInputs(color, self))
inputs_widget = QtWidgets.QWidget(self)
inputs_layout = QtWidgets.QVBoxLayout(inputs_widget)
for input_field in input_fields:
inputs_layout.addWidget(input_field)
input_field.value_changed.connect(self._on_value_change)
layout = QtWidgets.QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(inputs_widget, 0)
spacer = QtWidgets.QWidget(self)
layout.addWidget(spacer, 1)
self.input_fields = input_fields
self.color = color
def set_color(self, color):
if (
color.red() == self.color.red()
and color.green() == self.color.green()
and color.blue() == self.color.blue()
):
return
self.color.setRed(color.red())
self.color.setGreen(color.green())
self.color.setBlue(color.blue())
self._on_value_change()
def _on_value_change(self):
for input_field in self.input_fields:
input_field.color_changed()
self.color_changed.emit(self.color)

View file

@ -0,0 +1,115 @@
from Qt import QtWidgets, QtCore, QtGui
from .color_triangle import QtColorTriangle
from .color_view import ColorViewer
from .color_screen_pick import PickScreenColorWidget
from .color_inputs import (
ColorInputsWidget,
AlphaInputs
)
class ColorPickerWidget(QtWidgets.QWidget):
color_changed = QtCore.Signal(QtGui.QColor)
def __init__(self, color=None, parent=None):
super(ColorPickerWidget, self).__init__(parent)
# Eye picked widget
pick_widget = PickScreenColorWidget()
# Color utils
utils_widget = QtWidgets.QWidget(self)
utils_layout = QtWidgets.QVBoxLayout(utils_widget)
bottom_utils_widget = QtWidgets.QWidget(utils_widget)
# Color triangle
color_triangle = QtColorTriangle(utils_widget)
# Color preview
color_view = ColorViewer(bottom_utils_widget)
color_view.setMaximumHeight(50)
# Color pick button
btn_pick_color = QtWidgets.QPushButton(
"Pick a color", bottom_utils_widget
)
# Color inputs widget
color_inputs = ColorInputsWidget(self)
# Alpha inputs
alpha_input_wrapper_widget = QtWidgets.QWidget(self)
alpha_input_wrapper_layout = QtWidgets.QVBoxLayout(
alpha_input_wrapper_widget
)
alpha_inputs = AlphaInputs(alpha_input_wrapper_widget)
alpha_input_wrapper_layout.addWidget(alpha_inputs)
alpha_input_wrapper_layout.addWidget(QtWidgets.QWidget(), 1)
bottom_utils_layout = QtWidgets.QHBoxLayout(bottom_utils_widget)
bottom_utils_layout.setContentsMargins(0, 0, 0, 0)
bottom_utils_layout.addWidget(color_view, 1)
bottom_utils_layout.addWidget(btn_pick_color, 0)
utils_layout.addWidget(bottom_utils_widget, 0)
utils_layout.addWidget(color_triangle, 1)
layout = QtWidgets.QHBoxLayout(self)
layout.addWidget(utils_widget, 1)
layout.addWidget(color_inputs, 0)
layout.addWidget(alpha_input_wrapper_widget, 0)
color_view.set_color(color_triangle.cur_color)
color_inputs.set_color(color_triangle.cur_color)
color_triangle.color_changed.connect(self.triangle_color_changed)
pick_widget.color_selected.connect(self.on_color_change)
color_inputs.color_changed.connect(self.on_color_change)
alpha_inputs.alpha_changed.connect(self.alpha_changed)
btn_pick_color.released.connect(self.pick_color)
self.pick_widget = pick_widget
self.utils_widget = utils_widget
self.bottom_utils_widget = bottom_utils_widget
self.color_triangle = color_triangle
self.color_view = color_view
self.btn_pick_color = btn_pick_color
self.color_inputs = color_inputs
self.alpha_inputs = alpha_inputs
if color:
self.set_color(color)
self.alpha_changed(color.alpha())
def showEvent(self, event):
super(ColorPickerWidget, self).showEvent(event)
triangle_width = int((
self.utils_widget.height() - self.bottom_utils_widget.height()
) / 5 * 4)
self.color_triangle.setMinimumWidth(triangle_width)
def color(self):
return self.color_view.color()
def set_color(self, color):
self.alpha_inputs.set_alpha(color.alpha())
self.on_color_change(color)
def pick_color(self):
self.pick_widget.pick_color()
def triangle_color_changed(self, color):
self.color_view.set_color(color)
self.color_inputs.set_color(color)
def on_color_change(self, color):
self.color_view.set_color(color)
self.color_triangle.set_color(color)
self.color_inputs.set_color(color)
def alpha_changed(self, alpha):
self.color_view.set_alpha(alpha)

View file

@ -0,0 +1,248 @@
import Qt
from Qt import QtWidgets, QtCore, QtGui
class PickScreenColorWidget(QtWidgets.QWidget):
color_selected = QtCore.Signal(QtGui.QColor)
def __init__(self, parent=None):
super(PickScreenColorWidget, self).__init__(parent)
self.labels = []
self.magnification = 2
self._min_magnification = 1
self._max_magnification = 10
def add_magnification_delta(self, delta):
_delta = abs(delta / 1000)
if delta > 0:
self.magnification += _delta
else:
self.magnification -= _delta
if self.magnification > self._max_magnification:
self.magnification = self._max_magnification
elif self.magnification < self._min_magnification:
self.magnification = self._min_magnification
def pick_color(self):
if self.labels:
if self.labels[0].isVisible():
return
self.labels = []
for screen in QtWidgets.QApplication.screens():
label = PickLabel(self)
label.pick_color(screen)
label.color_selected.connect(self.on_color_select)
label.close_session.connect(self.end_pick_session)
self.labels.append(label)
def end_pick_session(self):
for label in self.labels:
label.close()
self.labels = []
def on_color_select(self, color):
self.color_selected.emit(color)
self.end_pick_session()
class PickLabel(QtWidgets.QLabel):
color_selected = QtCore.Signal(QtGui.QColor)
close_session = QtCore.Signal()
def __init__(self, pick_widget):
super(PickLabel, self).__init__()
self.setMouseTracking(True)
self.pick_widget = pick_widget
self.radius_pen = QtGui.QPen(QtGui.QColor(27, 27, 27), 2)
self.text_pen = QtGui.QPen(QtGui.QColor(127, 127, 127), 4)
self.text_bg = QtGui.QBrush(QtGui.QColor(27, 27, 27))
self._mouse_over = False
self.radius = 100
self.radius_ratio = 11
@property
def magnification(self):
return self.pick_widget.magnification
def pick_color(self, screen_obj):
self.show()
self.windowHandle().setScreen(screen_obj)
geo = screen_obj.geometry()
args = (
QtWidgets.QApplication.desktop().winId(),
geo.x(), geo.y(), geo.width(), geo.height()
)
if Qt.__binding__ in ("PyQt4", "PySide"):
pix = QtGui.QPixmap.grabWindow(*args)
else:
pix = screen_obj.grabWindow(*args)
if pix.width() > pix.height():
size = pix.height()
else:
size = pix.width()
self.radius = int(size / self.radius_ratio)
self.setPixmap(pix)
self.showFullScreen()
def wheelEvent(self, event):
y_delta = event.angleDelta().y()
self.pick_widget.add_magnification_delta(y_delta)
self.update()
def enterEvent(self, event):
self._mouse_over = True
super().enterEvent(event)
def leaveEvent(self, event):
self._mouse_over = False
super().leaveEvent(event)
self.update()
def mouseMoveEvent(self, event):
self.update()
def paintEvent(self, event):
super().paintEvent(event)
if not self._mouse_over:
return
mouse_pos_to_widet = self.mapFromGlobal(QtGui.QCursor.pos())
magnified_half_size = self.radius / self.magnification
magnified_size = magnified_half_size * 2
zoom_x_1 = mouse_pos_to_widet.x() - magnified_half_size
zoom_x_2 = mouse_pos_to_widet.x() + magnified_half_size
zoom_y_1 = mouse_pos_to_widet.y() - magnified_half_size
zoom_y_2 = mouse_pos_to_widet.y() + magnified_half_size
pix_width = magnified_size
pix_height = magnified_size
draw_pos_x = 0
draw_pos_y = 0
if zoom_x_1 < 0:
draw_pos_x = abs(zoom_x_1)
pix_width -= draw_pos_x
zoom_x_1 = 1
elif zoom_x_2 > self.pixmap().width():
pix_width -= zoom_x_2 - self.pixmap().width()
if zoom_y_1 < 0:
draw_pos_y = abs(zoom_y_1)
pix_height -= draw_pos_y
zoom_y_1 = 1
elif zoom_y_2 > self.pixmap().height():
pix_height -= zoom_y_2 - self.pixmap().height()
new_pix = QtGui.QPixmap(magnified_size, magnified_size)
new_pix.fill(QtCore.Qt.transparent)
new_pix_painter = QtGui.QPainter(new_pix)
new_pix_painter.drawPixmap(
QtCore.QRect(draw_pos_x, draw_pos_y, pix_width, pix_height),
self.pixmap().copy(zoom_x_1, zoom_y_1, pix_width, pix_height)
)
new_pix_painter.end()
painter = QtGui.QPainter(self)
ellipse_rect = QtCore.QRect(
mouse_pos_to_widet.x() - self.radius,
mouse_pos_to_widet.y() - self.radius,
self.radius * 2,
self.radius * 2
)
ellipse_rect_f = QtCore.QRectF(ellipse_rect)
path = QtGui.QPainterPath()
path.addEllipse(ellipse_rect_f)
painter.setClipPath(path)
new_pix_rect = QtCore.QRect(
mouse_pos_to_widet.x() - self.radius + 1,
mouse_pos_to_widet.y() - self.radius + 1,
new_pix.width() * self.magnification,
new_pix.height() * self.magnification
)
painter.drawPixmap(new_pix_rect, new_pix)
painter.setClipping(False)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setPen(self.radius_pen)
painter.drawEllipse(ellipse_rect_f)
image = self.pixmap().toImage()
if image.valid(mouse_pos_to_widet):
color = QtGui.QColor(image.pixel(mouse_pos_to_widet))
else:
color = QtGui.QColor()
color_text = "Red: {} - Green: {} - Blue: {}".format(
color.red(), color.green(), color.blue()
)
font = painter.font()
font.setPointSize(self.radius / 10)
painter.setFont(font)
text_rect_height = int(painter.fontMetrics().height() + 10)
text_rect = QtCore.QRect(
ellipse_rect.x(),
ellipse_rect.bottom(),
ellipse_rect.width(),
text_rect_height
)
if text_rect.bottom() > self.pixmap().height():
text_rect.moveBottomLeft(ellipse_rect.topLeft())
rect_radius = text_rect_height / 2
path = QtGui.QPainterPath()
path.addRoundedRect(
QtCore.QRectF(text_rect),
rect_radius,
rect_radius
)
painter.fillPath(path, self.text_bg)
painter.setPen(self.text_pen)
painter.drawText(
text_rect,
QtCore.Qt.AlignLeft | QtCore.Qt.AlignCenter,
color_text
)
color_rect_x = ellipse_rect.x() - text_rect_height
if color_rect_x < 0:
color_rect_x += (text_rect_height + ellipse_rect.width())
color_rect = QtCore.QRect(
color_rect_x,
ellipse_rect.y(),
text_rect_height,
ellipse_rect.height()
)
path = QtGui.QPainterPath()
path.addRoundedRect(
QtCore.QRectF(color_rect),
rect_radius,
rect_radius
)
painter.fillPath(path, color)
painter.drawRoundedRect(color_rect, rect_radius, rect_radius)
painter.end()
def mouseReleaseEvent(self, event):
color = QtGui.QColor(self.pixmap().toImage().pixel(event.pos()))
self.color_selected.emit(color)
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.close_session.emit()

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,78 @@
from Qt import QtWidgets, QtCore, QtGui
class ColorViewer(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ColorViewer, self).__init__(parent)
self.setMinimumSize(10, 10)
self.alpha = 255
self.actual_pen = QtGui.QPen()
self.actual_color = QtGui.QColor()
self._checkerboard = None
def checkerboard(self):
if not self._checkerboard:
checkboard_piece_size = 10
color_1 = QtGui.QColor(188, 188, 188)
color_2 = QtGui.QColor(90, 90, 90)
pix = QtGui.QPixmap(
checkboard_piece_size * 2,
checkboard_piece_size * 2
)
pix_painter = QtGui.QPainter(pix)
rect = QtCore.QRect(
0, 0, checkboard_piece_size, checkboard_piece_size
)
pix_painter.fillRect(rect, color_1)
rect.moveTo(checkboard_piece_size, checkboard_piece_size)
pix_painter.fillRect(rect, color_1)
rect.moveTo(checkboard_piece_size, 0)
pix_painter.fillRect(rect, color_2)
rect.moveTo(0, checkboard_piece_size)
pix_painter.fillRect(rect, color_2)
pix_painter.end()
self._checkerboard = pix
return self._checkerboard
def color(self):
return self.actual_color
def set_color(self, color):
if color == self.actual_color:
return
# Create copy of entered color
self.actual_color = QtGui.QColor(color)
# Set alpha by current alpha value
self.actual_color.setAlpha(self.alpha)
# Repaint
self.update()
def set_alpha(self, alpha):
if alpha == self.alpha:
return
# Change alpha of current color
self.actual_color.setAlpha(alpha)
# Store the value
self.alpha = alpha
# Repaint
self.update()
def paintEvent(self, event):
rect = event.rect()
# Paint everything to pixmap as it has transparency
pix = QtGui.QPixmap(rect.width(), rect.height())
pix_painter = QtGui.QPainter(pix)
pix_painter.drawTiledPixmap(rect, self.checkerboard())
pix_painter.fillRect(rect, self.actual_color)
pix_painter.end()
painter = QtGui.QPainter(self)
painter.drawPixmap(rect, pix)
painter.end()