floatgnome.py

  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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/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)