Advertisement
virpara

ambiance-chameleon-lighter.py

Jun 19th, 2012
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. #!/usr/bin/python
  2. import re, os
  3. from gi.repository import GLib, Gio, GObject
  4. import shutil
  5. import gconf
  6. import time
  7.  
  8. USER = os.getenv('USER')
  9. BG = Gio.Settings.new("org.gnome.desktop.background")
  10. GSETTINGS = Gio.Settings.new("org.gnome.desktop.interface")
  11. GCONF = gconf.client_get_default()
  12.  
  13. class Daemon:
  14.     def __init__ (self):
  15.         BG.connect("changed::picture-uri", self.change_theme)
  16.  
  17.     def change_theme (self, *_):
  18.         print "Waiting a few seconds before applying the changes..."
  19.         time.sleep(3)
  20.         self.write_changes(self.get_color ())
  21.         GCONF.set_string("/apps/metacity/general/theme", "Ambiance")
  22.         GSETTINGS.set_string("gtk-theme", "Ambiance")
  23.         GSETTINGS.set_string("gtk-theme", "Ambiance-chameleon")
  24.  
  25.     def get_color (self):
  26.         xprop_value = GLib.spawn_command_line_sync("/usr/bin/xprop -root _GNOME_BACKGROUND_REPRESENTATIVE_COLORS")
  27.         rgb_set = re.match(r".*\((\d+)\,(\d+),(\d+).*", xprop_value[1]).groups ()
  28.    
  29.         additional_color = 70
  30.         rgb_red = int(rgb_set[0]) + additional_color
  31.         rgb_green = int(rgb_set[1]) + additional_color
  32.         rgb_blue = int(rgb_set[2]) + additional_color
  33.         if (rgb_red > 255): rgb_red = 255
  34.         if (rgb_green > 255): rgb_green = 255
  35.         if (rgb_blue > 255): rgb_blue = 255
  36.        
  37.         color_hex = '%02x%02x%02x' % (rgb_red, rgb_green, rgb_blue)
  38.         return str(color_hex)
  39.  
  40.     def write_changes (self, color_hex):
  41.         try:
  42.             shutil.copytree("/usr/share/themes/Ambiance", "/home/%s/.themes/Ambiance-chameleon" % USER)
  43.         except:
  44.             pass
  45.         print "Changing the highlight to #"+color_hex
  46.         with open("/home/%s/.themes/Ambiance-chameleon/gtk-3.0/gtk.css" % USER, "r") as sources:
  47.             lines = sources.readlines()
  48.         with open("/home/%s/.themes/Ambiance-chameleon/gtk-3.0/gtk.css" % USER, "w") as sources:
  49.             for line in lines:
  50.                 if line.startswith('@define-color selected_bg_color '):
  51.                     line = re.sub(r'#.+',r'#'+color_hex+';', line)
  52.                 sources.write(line)
  53.  
  54.         with open("/home/%s/.themes/Ambiance-chameleon/gtk-2.0/gtkrc" % USER, "r") as sources:
  55.             lines = sources.readlines()
  56.         with open("/home/%s/.themes/Ambiance-chameleon/gtk-2.0/gtkrc" % USER, "w") as sources:
  57.             for line in lines:
  58.                 if line.startswith('gtk-color-scheme = '):
  59.                     line = re.sub(r'selected_bg_color:#[\w]+',r'selected_bg_color:#'+color_hex, line)
  60.                 sources.write(line)
  61.  
  62. if __name__ == "__main__":
  63.     daemon = Daemon()
  64.     GObject.MainLoop().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement