-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygtk-color-chooser
executable file
·140 lines (117 loc) · 4.99 KB
/
pygtk-color-chooser
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
#!/bin/env python
import sys
import subprocess
import gi
from os import path
from argparse import ArgumentParser
def rgba_to_hex(rgba):
r = rgba.replace('rgb(', '')
r = r.replace(')', '')
sp = r.split(',')
return rgb_to_hex(int(sp[0]), int(sp[1]), int(sp[2]))
def rgb_to_hex(red, green, blue):
"""Return color as #rrggbb for the given color values."""
return '#%02x%02x%02x' % (red, green, blue)
def on_button_clicked(button):
col = colorchooser.get_rgba()
print(rgba_to_hex(Gdk.RGBA.to_string(col)))
Gtk.main_quit()
def on_button_clicked_rgba(button):
col = colorchooser.get_rgba()
print(Gdk.RGBA.to_string(col).replace('rgb(', 'rgba(').replace(')', ',1)'))
Gtk.main_quit()
def on_button_clicked_rgb(button):
col = colorchooser.get_rgba()
print(Gdk.RGBA.to_string(col))
Gtk.main_quit()
def is_xcolor_installed():
p = subprocess.Popen('which xcolor',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL
)
ret = p.communicate()[0]
return False if ret == b'' else True
def get_color_from_xcolor(button):
colorchooser.set_property('show-editor', True)
p = subprocess.Popen('xcolor',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL
)
ret = str(p.communicate()[0]).replace("b'", "").replace("\\n'", "")
a_col = Gdk.RGBA(0, 0, 0, 0)
if Gdk.RGBA.parse(a_col, ret):
colorchooser.set_rgba(a_col)
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk
from gi.repository import Gtk
from gi.repository import Gdk
parser = ArgumentParser(description=r'A Gtk Color Chooser to be used in scripts. It will print the selected color value to stdout (will print nothing if Cancel is clicked) and terminate.')
parser.add_argument('-e', '--editor', default=False, action='store_true',
help='show the color editor only')
parser.add_argument('-r', '--rgb', default=False, action='store_true',
help='print color in rgb format instead of HTML')
parser.add_argument('-a', '--rgba', default=False, action='store_true',
help='print color in rgba format instead of HTML')
parser.add_argument('-t', '--title', default=None,
help='set window title')
parser.add_argument('-i', '--icon', default=None,
help='set window icon')
parser.add_argument('-o', '--ok-label', default=None,
help='set ok button label')
parser.add_argument('-c', '--cancel-label', default=None,
help='set cancel button label')
parser.add_argument('-p', '--pick-label', default=None,
help='set pick button label')
parser.add_argument('color', default='', nargs='?', type=str,
help='Specify a default color. This can be in any of the following formats: CSS name, HTML, RGB or RGBA. The widget will automatically change to the color editor view to display the color specified here. If the color value is invalid, it will be ignored.')
args = parser.parse_args()
window = Gtk.Window(title=args.title if args.title else 'Select a color')
Gtk.Window.set_resizable(window, False)
Gtk.Window.set_keep_above(window, True)
window.connect("destroy", Gtk.main_quit)
if args.icon:
from os.path import exists
if exists(args.icon):
Gtk.Window.set_icon_from_file(window, args.icon)
accel_group = Gtk.AccelGroup()
window.add_accel_group(accel_group)
key, modifier = Gtk.accelerator_parse('Escape')
accel_group.connect(key, modifier, Gtk.AccelFlags.VISIBLE, Gtk.main_quit)
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
Gtk.Box.set_spacing(box, 5)
window.add(box)
colorchooser = Gtk.ColorChooserWidget(show_editor=args.editor)
colorchooser.set_use_alpha(False)
box.add(colorchooser)
if args.color:
a_col = Gdk.RGBA(0, 0, 0, 0)
if Gdk.RGBA.parse(a_col, args.color):
colorchooser.set_property('show-editor', True)
colorchooser.set_rgba(a_col)
buttons_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
Gtk.Box.set_spacing(buttons_box, 5)
btn_ok = Gtk.Button(label=args.ok_label if args.ok_label else "OK")
if args.rgb:
btn_ok.connect("clicked", on_button_clicked_rgb)
elif args.rgba:
btn_ok.connect("clicked", on_button_clicked_rgba)
else:
btn_ok.connect("clicked", on_button_clicked)
buttons_box.add(btn_ok)
btn_cancel = Gtk.Button(label=args.cancel_label if args.cancel_label else "Cancel")
btn_cancel.connect("clicked", Gtk.main_quit)
buttons_box.add(btn_cancel)
Gtk.Box.set_homogeneous(buttons_box, True)
if is_xcolor_installed():
btn_pick = Gtk.Button(label=args.cancel_label if args.pick_label else "Pick a color")
btn_pick.connect("clicked", get_color_from_xcolor)
box.add(btn_pick)
box.add(buttons_box)
Gtk.Box.set_border_width(box, 5)
Gtk.Widget.set_can_default(btn_ok, True)
Gtk.Widget.grab_focus(btn_ok)
Gtk.Window.set_default(window, btn_ok)
window.show_all()
Gtk.main()