Skip to content

Instantly share code, notes, and snippets.

@lorenzos
Created October 28, 2014 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lorenzos/944e02cfb95703e32194 to your computer and use it in GitHub Desktop.
Save lorenzos/944e02cfb95703e32194 to your computer and use it in GitHub Desktop.
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