-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorpicker.py
More file actions
171 lines (139 loc) · 5.2 KB
/
Copy pathcolorpicker.py
File metadata and controls
171 lines (139 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""Embedded visual color picker: a saturation/value square + a hue bar.
Self-contained: shows its own swatch + hex readout and emits `colorChanged`
on every move (click or drag). The host debounces the actual BLE writes.
"""
from PySide6.QtCore import QPointF, Qt, Signal
from PySide6.QtGui import QBrush, QColor, QLinearGradient, QPainter, QPen
from PySide6.QtWidgets import QHBoxLayout, QLabel, QSizePolicy, QVBoxLayout, QWidget
class _SVSquare(QWidget):
"""Saturation (x: 0->255) by Value (y: 255->0) for a fixed hue."""
changed = Signal()
def __init__(self):
super().__init__()
# Grows to the card width; height fixed so it stays a pleasant rectangle.
self.setFixedHeight(200)
self.setMinimumWidth(240)
self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
self.setCursor(Qt.CursorShape.CrossCursor)
self._hue = 0
self._sat = 0
self._val = 255
def sat(self):
return self._sat
def val(self):
return self._val
def set_hue(self, hue):
self._hue = hue
self.update()
def set_sv(self, sat, val):
self._sat, self._val = sat, val
self.update()
def paintEvent(self, _event):
p = QPainter(self)
w, h = self.width(), self.height()
sat = QLinearGradient(0, 0, w, 0)
sat.setColorAt(0.0, QColor(255, 255, 255))
sat.setColorAt(1.0, QColor.fromHsv(self._hue, 255, 255))
p.fillRect(self.rect(), QBrush(sat))
val = QLinearGradient(0, 0, 0, h)
val.setColorAt(0.0, QColor(0, 0, 0, 0))
val.setColorAt(1.0, QColor(0, 0, 0, 255))
p.fillRect(self.rect(), QBrush(val))
x = self._sat / 255 * w
y = (1 - self._val / 255) * h
p.setPen(QPen(QColor(255, 255, 255), 1))
p.drawEllipse(QPointF(x, y), 7, 7)
p.setPen(QPen(QColor(0, 0, 0), 2))
p.drawEllipse(QPointF(x, y), 6, 6)
def mousePressEvent(self, event):
self._pick(event)
def mouseMoveEvent(self, event):
self._pick(event)
def _pick(self, event):
w, h = self.width(), self.height()
x = min(max(event.position().x(), 0), w)
y = min(max(event.position().y(), 0), h)
self._sat = round(x / w * 255)
self._val = round((1 - y / h) * 255)
self.update()
self.changed.emit()
class _HueBar(QWidget):
"""Vertical rainbow strip selecting hue 0-359."""
changed = Signal()
def __init__(self):
super().__init__()
self.setFixedSize(28, 200)
self._hue = 0
def hue(self):
return self._hue
def set_hue(self, hue):
self._hue = hue
self.update()
def paintEvent(self, _event):
p = QPainter(self)
w, h = self.width(), self.height()
grad = QLinearGradient(0, 0, 0, h)
for i in range(7):
grad.setColorAt(i / 6, QColor.fromHsv(round(359 * i / 6), 255, 255))
p.fillRect(self.rect(), QBrush(grad))
y = int(self._hue / 359 * h)
p.setPen(QPen(QColor(255, 255, 255), 2))
p.drawLine(0, y, w, y)
def mousePressEvent(self, event):
self._pick(event)
def mouseMoveEvent(self, event):
self._pick(event)
def _pick(self, event):
h = self.height()
y = min(max(event.position().y(), 0), h)
self._hue = round(y / h * 359)
self.update()
self.changed.emit()
class ColorPicker(QWidget):
colorChanged = Signal(QColor)
def __init__(self, show_preview=True):
super().__init__()
self._show_preview = show_preview
self._sv = _SVSquare()
self._bar = _HueBar()
self._swatch = QLabel()
self._swatch.setFixedHeight(28)
self._hex = QLabel()
self._hex.setObjectName("cardSub") # muted readout, matches the theme
self._sv.changed.connect(self._emit)
self._bar.changed.connect(self._on_hue)
top = QHBoxLayout()
top.setContentsMargins(0, 0, 0, 0)
top.setSpacing(12)
top.addWidget(self._sv)
top.addWidget(self._bar)
root = QVBoxLayout(self)
root.setContentsMargins(0, 0, 0, 0)
root.setSpacing(10)
root.addLayout(top)
if show_preview:
# The host can hide these and show its own preview instead.
root.addWidget(self._swatch)
root.addWidget(self._hex)
def color(self):
return QColor.fromHsv(self._bar.hue(), self._sv.sat(), self._sv.val())
def set_color(self, c):
hue = c.hue() if c.hue() >= 0 else 0 # -1 == achromatic; keep a real hue
self._bar.set_hue(hue)
self._sv.set_hue(hue)
self._sv.set_sv(c.saturation(), c.value())
self._emit()
def _on_hue(self):
self._sv.set_hue(self._bar.hue())
self._emit()
def _emit(self):
c = self.color()
if self._show_preview:
self._swatch.setStyleSheet(
f"background:{c.name()}; border:1px solid #D6DCE5; border-radius:10px;"
)
self._hex.setText(
f"{c.name()} R{c.red()} G{c.green()} B{c.blue()} "
f"H{max(c.hue(), 0)} S{c.saturation()} V{c.value()}"
)
self.colorChanged.emit(c)