#!/usr/bin/env python3 # A minimal color picker that uses the PyQt color picker # Usage: pickcolor [color] # Result is printed to stdout import re import sys import PyQt5 from PyQt5.QtWidgets import QApplication, QWidget, QColorDialog from PyQt5.QtGui import QColor PyQt5.QtWidgets.QApplication.setAttribute(PyQt5.QtCore.Qt.AA_EnableHighDpiScaling, True) #enable highdpi scaling PyQt5.QtWidgets.QApplication.setAttribute(PyQt5.QtCore.Qt.AA_UseHighDpiPixmaps, True) #use highdpi icons class Picker(QWidget): def __init__(self, color='#888888'): super().__init__() self.setWindowTitle('Color picker') prefix = '' m = re.match(r'(rgb)?\((\d+), *(\d+), *(\d+)\)', color) if m: prefix = m.group(1) or '' color = QColorDialog.getColor(initial=QColor(int(m.group(2)), int(m.group(3)), int(m.group(4)))) rgb = True else: if color.startswith('#'): prefix = '#' else: color = "#"+color color = QColorDialog.getColor(initial=QColor(color)) rgb = False if color.isValid(): if rgb: s = f"{prefix}({color.red()}, {color.green()}, {color.blue()})" else: s = f"{prefix}{color.name().lstrip('#')}" print(s) sys.exit(0) else: sys.exit(1) if __name__ == '__main__': app = QApplication(sys.argv[:1]) colors = sys.argv[1:] if sys.argv[1:] or sys.stdin.isatty() else [sys.stdin.read().strip()] ex = Picker(*colors) sys.exit(app.exec_())