Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pick color and store #HEX in system clipboard
#!/usr/bin/env python
import gtk
import gobject
import pynotify
import sys
# Clipboard object
clipboard = None
# Initialize system notifications
pynotify.init("Color picker")
# Delay ID to implement "pause" for color changing events
delay = None
def main(argv):
global clipboard
# Create and run color selection dialog
dialog = gtk.ColorSelectionDialog("Color picker")
dialog.colorsel.connect("color-changed", copyColorDelayed) # Signal connection
response = dialog.run()
if response == gtk.RESPONSE_OK:
copyColor(dialog.colorsel.get_current_color())
# Need to store clipboard to prevent reset when exiting
if clipboard != None:
clipboard.set_can_store(None)
clipboard.store()
# Close dialog: this ends GTK main thread
dialog.destroy()
def copyColor(color):
global clipboard
# Parse color to HEX string
rgb = (color.red / 257, color.green / 257, color.blue / 257)
hexcolor = ('#%02x%02x%02x' % rgb).upper()
print '{} <- {}'.format(hexcolor, str(color))
# Copy HEX to clipboard
if clipboard == None: clipboard = gtk.Clipboard()
clipboard.set_text(hexcolor)
# Show notification
notification = pynotify.Notification(hexcolor, "Color copied to clipboard")
notification.set_timeout(2000)
notification.show()
return False
def copyColorDelayed(colorSelection):
global delay
# Sets color in clipboard only when color selection is idle for a certain amount of time
if delay != None: gobject.source_remove(delay)
delay = gobject.timeout_add(250, copyColor, colorSelection.get_current_color())
if __name__ == '__main__': sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.