CullaX.py 4.94 KB
Newer Older
phob1an's avatar
phob1an committed
1
2
3
4
5
#!/usr/bin/python3
"""Culla generates a desktop theme using colours
from the current wallpaper"""

import sys
phob1an's avatar
sync    
phob1an committed
6
import os
phob1an's avatar
phob1an committed
7
8
9
import subprocess
import time
import colorsys
phob1an's avatar
readme    
phob1an committed
10
from PIL import Image
phob1an's avatar
sync    
phob1an committed
11
import colorgram
phob1an's avatar
sync    
phob1an committed
12

phob1an's avatar
phob1an committed
13
14


phob1an's avatar
readme    
phob1an committed
15
#Template for our Plasma theme
phob1an's avatar
phob1an committed
16
17
18
plasma_colors = """[Colors:Window]
ForegroundNormal=bbb
BackgroundNormal=aaa
phob1an's avatar
sync    
phob1an committed
19
ForegroundNegative=212,84,0
phob1an's avatar
phob1an committed
20
21

[Colors:Selection]
phob1an's avatar
sync    
phob1an committed
22
BackgroundNormal=ddd
phob1an's avatar
phob1an committed
23

phob1an's avatar
sync    
phob1an committed
24
25
26
27
[Colors:Complimentary]
BackgroundNormal=255,0,0
ForegroundNormal=0,255,0

phob1an's avatar
phob1an committed
28
[Colors:Button]
phob1an's avatar
sync    
phob1an committed
29
ForegroundNormal=eee
phob1an's avatar
sync    
phob1an committed
30
BackgroundNormal=fff
phob1an's avatar
sync    
phob1an committed
31
DecorationFocus=ddd
phob1an's avatar
sync    
phob1an committed
32
DecorationHover=0,255,0
phob1an's avatar
phob1an committed
33
34

[Colors:View]
phob1an's avatar
sync    
phob1an committed
35
ForegroundNormal=bbb
phob1an's avatar
sync    
phob1an committed
36
BackgroundNormal=ccc
phob1an's avatar
phob1an committed
37
38
39
DecorationHover=ddd"""


40
def notify_user(message):
phob1an's avatar
sync    
phob1an committed
41
    """ Simple notification to show something's happening """
phob1an's avatar
sync    
phob1an committed
42
    icon_path = os.path.expanduser('~/.local/share/pixmaps/cullax.png')
phob1an's avatar
sync    
phob1an committed
43
    icon = '--icon={}'.format(icon_path)
phob1an's avatar
sync    
phob1an committed
44

phob1an's avatar
sync    
phob1an committed
45
46
    try:
        subprocess.run(['notify-send',
phob1an's avatar
sync    
phob1an committed
47
48
                        icon,
                        '--expire-time=3000',
49
                        message])
phob1an's avatar
sync    
phob1an committed
50
51
52
    except:
        pass

phob1an's avatar
phob1an committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def color_triplet(h, l, s):
    r, g, b = colorsys.hls_to_rgb(h, l, s)

    if r > 1.0:
        r = 1.0
    r = int(r * 255)

    if g > 1.0:
        g = 1.0
    g = int(g * 255)

    if b > 1.0:
        r = 1.0
    b = int(b * 255)

    return ','.join([str(r), str(g), str(b)])

phob1an's avatar
sync    
phob1an committed
70

phob1an's avatar
sync    
phob1an committed
71

phob1an's avatar
phob1an committed
72

phob1an's avatar
sync    
phob1an committed
73
# ----  CullaX  ----
phob1an's avatar
sync    
phob1an committed
74

phob1an's avatar
sync    
phob1an committed
75

phob1an's avatar
sync    
phob1an committed
76
# Try sending a notification to show we're working
phob1an's avatar
phob1an committed
77
notify_user("CullaX - Beautifying Desktop ;) ")
phob1an's avatar
sync    
phob1an committed
78
79

# Raise flag when finding correct session in plasmarc
phob1an's avatar
phob1an committed
80
flag = False
phob1an's avatar
sync    
phob1an committed
81
# Holder for current activity ID
phob1an's avatar
phob1an committed
82
83
84
activity = ""

try:
phob1an's avatar
sync    
phob1an committed
85
86
    with open(os.path.expanduser(
            '~/.config/plasma-org.kde.plasma.desktop-appletsrc')) as f:
phob1an's avatar
phob1an committed
87
88
        plasmaconfig = f.readlines()
except:
phob1an's avatar
sync    
phob1an committed
89
    sys.exit('Unable to find plasma config.')
phob1an's avatar
phob1an committed
90
91
92


try:
phob1an's avatar
phob1an committed
93
    with open(os.path.expanduser('~/.config/kactivitymanagerdrc')) as f:
phob1an's avatar
phob1an committed
94
95
        activityrc = f.readlines()
except:
phob1an's avatar
sync    
phob1an committed
96
    print('Unable to find kactivity manager rc. Presuming only default activity.')
phob1an's avatar
phob1an committed
97
    activityrc = None
phob1an's avatar
sync    
phob1an committed
98
    flag = True   # There is only default activity
phob1an's avatar
phob1an committed
99

phob1an's avatar
sync    
phob1an committed
100
# Retrieve current activity
phob1an's avatar
phob1an committed
101
102
103
104
105
if activityrc is not None:
    a = [a for a in activityrc if 'current' in a]
    a = a[0].split('=')
    activity = a[1].rstrip()

phob1an's avatar
sync    
phob1an committed
106
# Flag if wallpaper is found
phob1an's avatar
phob1an committed
107
108
found = False

phob1an's avatar
sync    
phob1an committed
109
# Find current activity then grab next Image= key
phob1an's avatar
phob1an committed
110
111
112
113
114
115
116
117
for line in plasmaconfig:
    if activity in line:
        flag = True
    if 'Image=' in line and flag:
        found = True
        break

if not found:
118
119
120
121
    notify_user(
"""You need to set your own wallpaper\n\
before I can work."""
                )
phob1an's avatar
sync    
phob1an committed
122
    sys.exit("I didn't find your wallpaper. Have you set one yet?")
phob1an's avatar
phob1an committed
123

phob1an's avatar
sync    
phob1an committed
124
125
126
127
128
129
#tmp, wallpaper = line.split('//')
line = line[6:]
if line[0] == 'f':
    line = line[7:]

wallpaper = line.strip()
phob1an's avatar
phob1an committed
130

phob1an's avatar
sync    
phob1an committed
131
if not os.path.exists(wallpaper):
phob1an's avatar
phob1an committed
132
133
134
    notify_user("I can't your wallpaper."
             .format(wallpaper))
    sys.exit("I think the wallpaper is {0} but I can't find it."
phob1an's avatar
phob1an committed
135
             .format(wallpaper))
phob1an's avatar
phob1an committed
136

phob1an's avatar
sync    
phob1an committed
137
# Resize and write to temp file - massive speedup
phob1an's avatar
phob1an committed
138
tmp_img = Image.open(wallpaper.rstrip())
phob1an's avatar
sync    
phob1an committed
139
tmp_img = tmp_img.resize((512, 512))
phob1an's avatar
phob1an committed
140
tmp_img_path = os.path.expanduser('~/.cullax.png')
phob1an's avatar
sync    
phob1an committed
141
tmp_img.save(tmp_img_path)
phob1an's avatar
sync    
phob1an committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
colors = colorgram.extract(tmp_img_path, 3)

#background_hue = colors[0].hsl[0]
#higlight_hsl = colors[0].hsl
#diff = 0


#for i in colors:
    #print(i.hsl, i)
    ##tmp_hue = i.hsl[0]
    
    #if abs(background_hue - i.hsl[0]) > diff:
        #contrast_color = i.hsl
        #diff = abs(background_hue - i.hsl[0])

#print(contrast_color)
#sys.exit(0)


phob1an's avatar
phob1an committed
161
162
163

h,s,l = [ c/255.0 for c in colors[1].hsl ]
hb,sb,lb = [ c/255.0 for c in colors[0].hsl ]
phob1an's avatar
sync    
phob1an committed
164
165

# Cleanup temp file
phob1an's avatar
sync    
phob1an committed
166
os.remove(tmp_img_path)
phob1an's avatar
sync    
phob1an committed
167

phob1an's avatar
phob1an committed
168

phob1an's avatar
sync    
phob1an committed
169
#print(h,s,l)
phob1an's avatar
phob1an committed
170
171
#sys.exit(0)

phob1an's avatar
sync    
phob1an committed
172
# Remove hue for b/w images
phob1an's avatar
sync    
phob1an committed
173
174
175
if s < 0.011:
    s = 0.0
    h = 0.0
phob1an's avatar
sync    
phob1an committed
176

phob1an's avatar
sync    
phob1an committed
177
#l_midlight = (1.0 - l) / 4.0 + l
phob1an's avatar
sync    
phob1an committed
178
179
panel_background = color_triplet(hb, 0.15, 0.05)
panel_header = color_triplet(hb, 0.25, 0.4)
phob1an's avatar
phob1an committed
180
181
button_foreground = color_triplet(h, 0.85, s/3.0)
foreground = color_triplet(h, 0.99, 0.9)
phob1an's avatar
sync    
phob1an committed
182
highlight_color = color_triplet(h, 0.88, 1)
phob1an's avatar
sync    
phob1an committed
183
midlight = color_triplet(h, 0.5, 0.6)
phob1an's avatar
phob1an committed
184

phob1an's avatar
sync    
phob1an committed
185
print(panel_background)
phob1an's avatar
phob1an committed
186

phob1an's avatar
sync    
phob1an committed
187
# Write the colorscheme
phob1an's avatar
phob1an committed
188
189
plasma_colors = plasma_colors.replace('aaa', panel_background)
plasma_colors = plasma_colors.replace('bbb', foreground)
phob1an's avatar
phob1an committed
190
plasma_colors = plasma_colors.replace('ddd', highlight_color)
phob1an's avatar
sync    
phob1an committed
191
plasma_colors = plasma_colors.replace('eee', button_foreground)
phob1an's avatar
sync    
phob1an committed
192
plasma_colors = plasma_colors.replace('fff', midlight)
phob1an's avatar
sync    
phob1an committed
193
plasma_colors = plasma_colors.replace('ccc', panel_header)
phob1an's avatar
phob1an committed
194
195

try:
phob1an's avatar
sync    
phob1an committed
196
    with open(os.path.expanduser(
phob1an's avatar
phob1an committed
197
            '~/.local/share/plasma/desktoptheme/CullaX/colors'), 'w') as f:
phob1an's avatar
phob1an committed
198
199
        f.write(plasma_colors)
except:
phob1an's avatar
sync    
phob1an committed
200
    sys.exit("Unable to open Culla Plasma colors. Is it installed?")
phob1an's avatar
phob1an committed
201

phob1an's avatar
sync    
phob1an committed
202
# Call kwriteconfig5 to change Plasma theme
phob1an's avatar
phob1an committed
203
204
205
206
try:
    subprocess.run(['kwriteconfig5', '--file=plasmarc',
                    '--group=Theme', '--key=name', 'Default'])

phob1an's avatar
phob1an committed
207
    # Do this too quickly and Plasma won't reload
phob1an's avatar
sync    
phob1an committed
208
    time.sleep(0.6)
phob1an's avatar
phob1an committed
209
210
211
212

    subprocess.run(['kwriteconfig5', '--file=plasmarc',
                    '--group=Theme', '--key=name', 'CullaX'])
except IOError as e:
phob1an's avatar
sync    
phob1an committed
213
    sys.exit(e)
phob1an's avatar
phob1an committed
214