-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_picker.py
284 lines (248 loc) · 12 KB
/
color_picker.py
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import os
import sys
import traceback
from PyQt5 import QtCore, QtGui, QtWidgets
import PyQt5.uic
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5.uic import *
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
from common.books import *
from common import bdd
from common import lang
from common import vars
class RgbPicker(QtWidgets.QLabel):
# create a vertical color gradient similar to the "Color Shower"
# used in QColorDialog
colorGrads = QtGui.QLinearGradient(0, 0, 1, 0)
colorGrads.setCoordinateMode(colorGrads.ObjectBoundingMode)
xRatio = 1. / 6
colorGrads.setColorAt(0, QtCore.Qt.red)
colorGrads.setColorAt(xRatio, QtCore.Qt.red)
colorGrads.setColorAt(xRatio * 2, QtCore.Qt.magenta)
colorGrads.setColorAt(xRatio * 3, QtCore.Qt.blue)
colorGrads.setColorAt(xRatio * 4, QtCore.Qt.cyan)
colorGrads.setColorAt(xRatio * 5, QtCore.Qt.green)
colorGrads.setColorAt(xRatio * 6, QtCore.Qt.yellow)
# add a "mask" gradient to support gradients to lighter colors
maskGrad = QtGui.QLinearGradient(0, 0, 0, 1)
maskGrad.setCoordinateMode(maskGrad.ObjectBoundingMode)
maskGrad.setColorAt(0, QtCore.Qt.black)
maskGrad.setColorAt(0.5, QtCore.Qt.transparent)
maskGrad.setColorAt(1, QtCore.Qt.white)
# create a cross cursor to show the selected color, if any
cursorPath = QtGui.QPainterPath()
cursorPath.moveTo(-10, 0)
cursorPath.lineTo(-4, 0)
cursorPath.moveTo(0, -10)
cursorPath.lineTo(0, -4)
cursorPath.moveTo(4, 0)
cursorPath.lineTo(10, 0)
cursorPath.moveTo(0, 4)
cursorPath.lineTo(0, 10)
cursorPen = QtGui.QPen(QtCore.Qt.black, 3)
colorChanged = QtCore.pyqtSignal(QtGui.QColor)
colorSelected = QtCore.pyqtSignal(QtGui.QColor)
showCursor = False
cursorPos = QtCore.QPoint()
clicked = False
pix_size = QtCore.QSize(250, 250)
def __init__(self, parent=None):
super().__init__(parent)
self.setMouseTracking(True)
self.setFixedSize(self.pix_size)
# create a pixmap and paint it with the gradients
pixmap = QtGui.QPixmap(self.pix_size)
qp = QtGui.QPainter(pixmap)
qp.fillRect(pixmap.rect(), self.colorGrads)
qp.fillRect(pixmap.rect(), self.maskGrad)
qp.end()
self.setPixmap(pixmap)
# a QImage is required to get the color of a specific pixel
self.image = pixmap.toImage()
self.currentColor = QtGui.QColor()
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.clicked = True
# set the current color and emit the colorChanged signal
self.currentColor = QtGui.QColor(self.image.pixel(event.pos()))
self.cursorPos = event.pos()
self.showCursor = True
self.update()
self.colorSelected.emit(self.currentColor)
def mouseReleaseEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.clicked = False
def mouseMoveEvent(self, event):
if self.clicked is True:
pos = event.pos()
if pos.x() >= self.rect().topRight().x():
pos.setX(self.rect().topRight().x())
if pos.x() <= self.rect().topLeft().x():
pos.setX(self.rect().topLeft().x())
if pos.y() >= self.rect().bottomRight().y():
pos.setY(self.rect().bottomRight().y())
if pos.y() <= self.rect().topRight().y():
pos.setY(self.rect().topRight().y())
if event.buttons() == QtCore.Qt.LeftButton:
color = QtGui.QColor(self.image.pixel(pos))
self.colorChanged.emit(color)
self.currentColor = color
self.cursorPos = pos
self.update()
def paintEvent(self, event):
# paint the "color shower"
QtWidgets.QLabel.paintEvent(self, event)
if self.showCursor:
# paint the color "cursor"
qp = QtGui.QPainter(self)
qp.setPen(self.cursorPen)
qp.translate(self.cursorPos)
qp.drawPath(self.cursorPath)
class ColorPicker(QtWidgets.QDialog):
def __init__(self, parent: any = None):
super(ColorPicker, self).__init__(None, QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint)
PyQt5.uic.loadUi(os.path.dirname(os.path.realpath(__file__)) + os.sep + 'color_picker.ui'.replace('/', os.sep), self)
self.lang = lang.Lang()
self.BDD = parent.BDD
self.style = self.BDD.get_param('style')
self.lang.set_lang(self.BDD.get_param('lang'))
self.setStyleSheet(get_style_var(self.style, 'EditorColorPicker'))
self.setWindowTitle(self.lang['Editor/ColorPicker/WindowTitle'])
self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setText(self.lang['Editor/FilesWindow/btnOk'])
self.buttonBox.button(QtWidgets.QDialogButtonBox.Cancel).setText(self.lang['Editor/FilesWindow/btnCancel'])
cursor = QtGui.QCursor(QtCore.Qt.PointingHandCursor)
self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setCursor(cursor)
self.buttonBox.button(QtWidgets.QDialogButtonBox.Cancel).setCursor(cursor)
self.buttonBox.button(QtWidgets.QDialogButtonBox.Cancel).setStyleSheet(get_style_var(self.style, 'EditorColorPickerFullAltButton'))
self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setStyleSheet(get_style_var(self.style, 'EditorColorPickerFullAltButton'))
self.paletteLabel.setText(self.lang['Editor/ColorPicker/Palette'])
self.chromaGraphLabel.setText(self.lang['Editor/ColorPicker/ChromaGraph'])
self.RGB_GROUP.setTitle(self.lang['Editor/ColorPicker/RgbBox'])
self.RGB_R_LABEL.setText(self.lang['Editor/ColorPicker/RgbR'])
self.RGB_G_LABEL.setText(self.lang['Editor/ColorPicker/RgbG'])
self.RGB_B_LABEL.setText(self.lang['Editor/ColorPicker/RgbB'])
self.RGB_HEXA_LABEL.setText(self.lang['Editor/ColorPicker/RgbHexa'])
self.previewLabel.setText(self.lang['Editor/ColorPicker/Preview'])
try:
self.rgbPicker.colorChanged.connect(self.__set_color_changed)
self.rgbPicker.colorSelected.connect(self.__color_clicked)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
paletteColors = [
"#000000", "#404040", "#FF0000", "#FF6A00", "#FFD800", "#B6FF00", "#4CFF00", "#00FF21",
"#00FF90", "#00FFFF", "#0094FF", "#0026FF", "#4800FF", "#B200FF", "#FF00DC", "#FF006E",
"#FFFFFF", "#808080", "#7F0000", "#7F3300", "#7F6A00", "#5B7F00", "#267F00", "#007F0E",
"#007F46", "#007F7F", "#004A7F", "#00137F", "#21007F", "#57007F", "#7F006E", "#7F0037",
"#A0A0A0", "#303030", "#FF7F7F", "#FFB27F", "#FFE97F", "#DAFF7F", "#A5FF7F", "#7FFF8E",
"#7FFFC5", "#7FFFFF", "#7FC9FF", "#7F92FF", "#A17FFF", "#D67FFF", "#FF7FED", "#FF7FB6",
"#C0C0C0", "#606060", "#7F3F3F", "#7F593F", "#7F743F", "#6D7F3F", "#527F3F", "#3F7F47",
"#3F7F62", "#3F7F7F", "#3F647F", "#3F497F", "#503F7F", "#6B3F7F", "#7F3F76", "#7F3F5B"
]
for i in range(0, len(paletteColors)):
name = 'pal'
if i < 9: name += '0'
name += '{}'.format(i+1)
button = QtWidgets.QPushButton()
button.setObjectName(name)
button.setFixedWidth(16)
button.setFixedHeight(16)
button.setCursor(cursor)
button.setText("█")
button.setStyleSheet(
get_style_var(self.style, 'EditorColorPickerColorBtn')
.replace('%1', paletteColors[i])
.replace('%2', paletteColors[i])
.replace('%3', self.__invert_color(QtGui.QColor(paletteColors[i])).name())
)
button.setToolTip("%s" % paletteColors[i])
button.clicked.connect(self.__palette_set_color)
row = int(float(i / 16))
self.gridLayout.addWidget(button, row, i - (row * 16))
self.__set_color(QtGui.QColor(paletteColors[0]))
self.RGB_R_SLIDER.valueChanged.connect(lambda: self.__update_color('R', 'SLIDER'))
self.RGB_R_SPIN.valueChanged.connect(lambda: self.__update_color('R', 'SPIN'))
self.RGB_G_SLIDER.valueChanged.connect(lambda: self.__update_color('G', 'SLIDER'))
self.RGB_G_SPIN.valueChanged.connect(lambda: self.__update_color('G', 'SPIN'))
self.RGB_B_SLIDER.valueChanged.connect(lambda: self.__update_color('B', 'SLIDER'))
self.RGB_B_SPIN.valueChanged.connect(lambda: self.__update_color('B', 'SPIN'))
except Exception:
traceback.print_exc()
def __invert_color(self, ColourToInvert: QtGui.QColor) -> QtGui.QColor:
RGBMAX = 255
R = RGBMAX - ColourToInvert.red()
G = RGBMAX - ColourToInvert.green()
B = RGBMAX - ColourToInvert.blue()
if ColourToInvert.red() + 50 > R > ColourToInvert.red() - 50:
R = 255
if ColourToInvert.green() + 50 > G > ColourToInvert.green() - 50:
G = 255
if ColourToInvert.blue() + 50 > B > ColourToInvert.blue() - 50:
B = 255
return QtGui.QColor.fromRgb(R, G, B)
def __palette_set_color(self):
try:
color = QtGui.QColor(self.sender().toolTip())
self.__set_color(color)
self.rgbPicker.showCursor = False
self.rgbPicker.update()
except Exception:
traceback.print_exc()
def __update_color(self, input_type: str, input_mode: str):
try:
name_base = ''
if input_type in ['R', 'G', 'B']:
name_base = 'RGB_'+input_type+'_'
else:
return
if input_mode not in ['SLIDER', 'SPIN']:
return
name = name_base + input_mode
if input_mode == 'SLIDER':
name2 = name_base + 'SPIN'
getattr(self, name2).setValue(getattr(self, name).value())
elif input_mode == 'SPIN':
name2 = name_base + 'SLIDER'
getattr(self, name2).setValue(getattr(self, name).value())
color = QtGui.QColor(self.RGB_R_SPIN.value(), self.RGB_G_SPIN.value(), self.RGB_B_SPIN.value(), 255)
self.__set_color(color)
except Exception:
traceback.print_exc()
def __set_color(self, color: QtGui.QColor = None):
max = color.red()
if max < color.green():
max = color.green()
if max < color.blue():
max = color.blue()
max = int(100 * max / 255)
self.RGB_R_SLIDER.setValue(color.red())
self.RGB_R_SPIN.setValue(color.red())
self.RGB_G_SLIDER.setValue(color.green())
self.RGB_G_SPIN.setValue(color.green())
self.RGB_B_SLIDER.setValue(color.blue())
self.RGB_B_SPIN.setValue(color.blue())
self.RGB_HEXA_EDIT.setText(color.name())
self.preview.setStyleSheet("background-color:%s;" % color.name())
self.preview.style().unpolish(self.preview)
self.preview.style().polish(self.preview)
def __set_color_changed(self, color):
self.__set_color(color)
def __color_clicked(self):
self.__set_color(self.rgbPicker.currentColor)
def getColor(self, color: QtGui.QColor = None):
if isinstance(color, QtGui.QColor):
print('color=', (color.red(), color.green(), color.blue()))
self.__set_color(color)
# return a color only if the dialog is accepted
if self.exec_():
color = QtGui.QColor(self.RGB_R_SPIN.value(), self.RGB_G_SPIN.value(), self.RGB_B_SPIN.value(), 255)
return color
else:
return None
#
# if __name__ == '__main__':
# app = QtWidgets.QApplication(sys.argv)
# w = ColorPicker(None)
# w.show()
# sys.exit(app.exec_())