#!/usr/bin/python
__revision__ = '0.1.1'
import random
import time
import gconf
from os import listdir
from sys import exit
def color2hex(color):
def convert_one(val):
val = hex(val)[2:].upper()
return ('%02s' % val).replace(' ','0')
return '#%s' % ''.join([convert_one(val) for val in color])
def hex2color(hex):
num = hex.lstrip('#').lower()
return (int(num[0:2], 16),
int(num[2:4], 16),
int(num[4:6], 16))
def rgb2hsl(color):
cR,cG,cB = (val/255.0 for val in color)
maxColor=cR
if cG>maxColor:
maxColor=cG
if cB>maxColor:
maxColor=cB
minColor=cR
if cG<minColor:
minColor=cG
if cB<minColor:
minColor=cB
cL = (maxColor+minColor)/2.0;
if maxColor == minColor:
cH=0
cS=0
else:
if cL < 0.5:
cS = (maxColor-minColor) / (maxColor+minColor)
if cL >= 0.5:
cS = (maxColor-minColor) / (2.0-maxColor-minColor)
if cR == maxColor:
cH = (cG-cB) / (maxColor-minColor)
if cG == maxColor:
cH = 2.0 + (cB-cR) / (maxColor-minColor)
if cB == maxColor:
cH = 4.0 + (cR-cG) / (maxColor-minColor)
cH *= 60
if cH<0:
cH+=360
cL*=100
cS*=100
return map(int, (cH, cS, cL))
def hsl2rgb(color):
"""Convert HSL colorspace to RGB"""
hue, saturation, light = color
def rgb(q1,q2,hue):
"""Make magic numbers"""
if (hue>360):
hue=hue-360
if (hue<0):
hue=hue+360
if (hue<60):
return q1+(q2-q1)*hue/60.0
elif (hue<180):
return q2
elif (hue<240):
return q1+(q2-q1)*(240-hue)/60.0
else:
return q1
if (saturation==0):
return map(int, [
round(light*255.0/100.0),
round(light*255.0/100.0),
round(light*255.0/100.0)
])
else:
light = light/100.0
saturation = saturation/100.0
if ((light)<0.5):
temp2 = light * (1.0+saturation)
else:
temp2 = light+saturation-(light*saturation)
temp1 = 2.0*light - temp2;
return map(int, [
round(rgb(temp1,temp2,int(hue)+120)*255.0),
round(rgb(temp1,temp2,hue)*255.0),
round(rgb(temp1,temp2,int(hue)-120)*255.0)
])
class Floater:
def __init__(self, step):
self.layers = []
self.step = step
def add_layer(self, color):
layer = [color, self.new_target(color)]
self.layers.append(layer)
return self.layers.index(layer)
def get_layer(self, index):
return self.layers[index]
def new_target(self, color):
return random.randint(0, 360), color[1], color[2]
def next_step(self, color, target, limit):
def clip(value, limit=limit):
if value > limit:
return value - limit
if value < 0:
return value + limit
return value
if target > color:
cw = target - color
else:
cw = target - color + limit
ccw = limit - cw
if cw < ccw:
color += self.step
else:
color -= self.step
color = clip(color)
if abs(color-target) < self.step:
color = target
return color
def do_step(self):
for layer in self.layers:
layer[0][0] = self.next_step(layer[0][0], layer[1][0], 360)
if layer[0][0] == layer[1][0]:
layer[1] = self.new_target(layer[0])
class GnomeBG:
"""Handle gconf settings"""
bg_root_key = '/desktop/gnome/background'
def __init__(self):
""" connect and import settings """
self.client = gconf.client_get_default()
def get(self, key):
"""Get HSL from gconf"""
return rgb2hsl(hex2color(
self.client.get_string("%s/%s" % (self.bg_root_key, key))))
def set(self, key, hsl):
"""Update gconf with HSL value"""
value = color2hex(hsl2rgb(hsl))
return self.client.set_string("%s/%s" % (self.bg_root_key, key), value)
return value
def is_running():
def parse(name):
return dict([line.strip().split(":\t") for line in open(name).readlines() if len(line.strip().split(":\t")) == 2])
self = parse("/proc/self/status")
for pid in [dir for dir in listdir("/proc/") if dir.isdigit()]:
process = parse("/proc/%s/status" % pid)
if process["Name"] == self["Name"] and process["Uid"] == self["Uid"] and process["Pid"] != self["Pid"]:
print process["Pid"],process["Name"]
return process
if __name__ == '__main__':
if is_running():
print "AAA!!! floatgnome is running!!!"
exit(1)
gbg = GnomeBG()
floater = Floater(1)
primary = floater.add_layer( gbg.get('primary_color') )
secondary = floater.add_layer( gbg.get('secondary_color') )
while 1:
floater.do_step()
gbg.set('primary_color', floater.get_layer(primary)[0])
gbg.set('secondary_color', floater.get_layer(secondary)[0])
time.sleep(4)
floatgnome.py