code / scripts

Lines748 Shell528 Python72 Lua67 Bourne Again Shell62 make15
1 others 4
Markdown4
(46 lines)
1 #!/usr/bin/env python3
2 # A minimal color picker that uses the PyQt color picker
3 # Usage: pickcolor [color]
4 # Result is printed to stdout
5 import re
6 import sys
7 import PyQt5
8 from PyQt5.QtWidgets import QApplication, QWidget, QColorDialog
9 from PyQt5.QtGui import QColor
11 PyQt5.QtWidgets.QApplication.setAttribute(PyQt5.QtCore.Qt.AA_EnableHighDpiScaling, True) #enable highdpi scaling
12 PyQt5.QtWidgets.QApplication.setAttribute(PyQt5.QtCore.Qt.AA_UseHighDpiPixmaps, True) #use highdpi icons
14 class Picker(QWidget):
15 def __init__(self, color='#888888'):
16 super().__init__()
17 self.setWindowTitle('Color picker')
18 prefix = ''
19 m = re.match(r'(rgb)?\((\d+), *(\d+), *(\d+)\)', color)
20 if m:
21 prefix = m.group(1) or ''
22 color = QColorDialog.getColor(initial=QColor(int(m.group(2)), int(m.group(3)), int(m.group(4))))
23 rgb = True
24 else:
25 if color.startswith('#'):
26 prefix = '#'
27 else:
28 color = "#"+color
29 color = QColorDialog.getColor(initial=QColor(color))
30 rgb = False
32 if color.isValid():
33 if rgb:
34 s = f"{prefix}({color.red()}, {color.green()}, {color.blue()})"
35 else:
36 s = f"{prefix}{color.name().lstrip('#')}"
37 print(s)
38 sys.exit(0)
39 else:
40 sys.exit(1)
42 if __name__ == '__main__':
43 app = QApplication(sys.argv[:1])
44 colors = sys.argv[1:] if sys.argv[1:] or sys.stdin.isatty() else [sys.stdin.read().strip()]
45 ex = Picker(*colors)
46 sys.exit(app.exec_())