Merge pull request #1695 from pypeclub/feature/smoother_edges_of_color_triangle

Smoother edges of color triangle
This commit is contained in:
Jakub Trllo 2021-06-16 16:37:51 +02:00 committed by GitHub
commit 9399054320
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 60 deletions

View file

@ -4,35 +4,6 @@ from Qt import QtWidgets, QtCore, QtGui
from .color_view import draw_checkerboard_tile
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 AlphaSlider(QtWidgets.QSlider):
def __init__(self, *args, **kwargs):
super(AlphaSlider, self).__init__(*args, **kwargs)
@ -80,7 +51,7 @@ class AlphaSlider(QtWidgets.QSlider):
painter.fillRect(event.rect(), QtCore.Qt.transparent)
painter.setRenderHint(QtGui.QPainter.SmoothPixmapTransform)
painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing)
rect = self.style().subControlRect(
QtWidgets.QStyle.CC_Slider,
opt,
@ -135,19 +106,8 @@ class AlphaSlider(QtWidgets.QSlider):
painter.save()
gradient = QtGui.QRadialGradient()
radius = handle_rect.height() / 2
center_x = handle_rect.width() / 2 + handle_rect.x()
center_y = handle_rect.height()
gradient.setCenter(center_x, center_y)
gradient.setCenterRadius(radius)
gradient.setFocalPoint(center_x, center_y)
gradient.setColorAt(0.9, QtGui.QColor(127, 127, 127))
gradient.setColorAt(1, QtCore.Qt.transparent)
painter.setPen(QtCore.Qt.NoPen)
painter.setBrush(gradient)
painter.setBrush(QtGui.QColor(127, 127, 127))
painter.drawEllipse(handle_rect)
painter.restore()

View file

@ -241,7 +241,11 @@ class QtColorTriangle(QtWidgets.QWidget):
# Blit the static generated background with the hue gradient onto
# the double buffer.
buf = QtGui.QImage(self.bg_image.copy())
buf = QtGui.QImage(
self.bg_image.width(),
self.bg_image.height(),
QtGui.QImage.Format_RGB32
)
# Draw the trigon
# Find the color with only the hue, and max value and saturation
@ -254,9 +258,21 @@ class QtColorTriangle(QtWidgets.QWidget):
)
# Slow step: convert the image to a pixmap
pix = QtGui.QPixmap.fromImage(buf)
pix = self.bg_image.copy()
pix_painter = QtGui.QPainter(pix)
pix_painter.setRenderHint(QtGui.QPainter.Antialiasing)
pix_painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing)
trigon_path = QtGui.QPainterPath()
trigon_path.moveTo(self.point_a)
trigon_path.lineTo(self.point_b)
trigon_path.lineTo(self.point_c)
trigon_path.closeSubpath()
pix_painter.setClipPath(trigon_path)
pix_painter.drawImage(0, 0, buf)
pix_painter.setClipping(False)
# Draw an outline of the triangle
pix_painter.setPen(self._triangle_outline_pen)
@ -724,27 +740,37 @@ class QtColorTriangle(QtWidgets.QWidget):
lx = leftX[y]
rx = rightX[y]
# if the xdist is 0, don't draw anything.
xdist = rx - lx
if xdist == 0.0:
continue
lxi = int(floor(lx))
rxi = int(floor(rx))
rc = rightColors[y]
lc = leftColors[y]
# if the xdist is 0, don't draw anything.
xdist = rx - lx
if xdist != 0.0:
r = lc.r
g = lc.g
b = lc.b
rdelta = (rc.r - r) / xdist
gdelta = (rc.g - g) / xdist
bdelta = (rc.b - b) / xdist
r = lc.r
g = lc.g
b = lc.b
rdelta = (rc.r - r) / xdist
gdelta = (rc.g - g) / xdist
bdelta = (rc.b - b) / xdist
# Inner loop 2. Draws the line from left to right.
for x in range(lxi, rxi + 1):
buf.setPixel(x, y, QtGui.qRgb(int(r), int(g), int(b)))
r += rdelta
g += gdelta
b += bdelta
# Draw 2 more pixels on left side for smoothing
for x in range(lxi - 2, lxi):
buf.setPixel(x, y, QtGui.qRgb(int(r), int(g), int(b)))
# Inner loop 2. Draws the line from left to right.
for x in range(lxi, rxi):
buf.setPixel(x, y, QtGui.qRgb(int(r), int(g), int(b)))
r += rdelta
g += gdelta
b += bdelta
# Draw 2 more pixels on right side for smoothing
for x in range(rxi, rxi + 3):
buf.setPixel(x, y, QtGui.qRgb(int(r), int(g), int(b)))
def _radius_at(self, pos, rect):
mousexdist = pos.x() - float(rect.center().x())