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

import sys
import os
import subprocess
import time
import colorsys
from PIL import Image
phob1an's avatar
phob1an committed
11
import colorz
phob1an's avatar
phob1an committed
12
13
14
15
16
17
18
19
20
21
22
23


#Template for our Plasma theme
plasma_colors = """[Colors:Window]
ForegroundNormal=bbb
BackgroundNormal=aaa
ForegroundNegative=212,84,0

[Colors:Selection]
BackgroundNormal=ddd

[Colors:Complimentary]
phob1an's avatar
release    
phob1an committed
24
25
BackgroundNormal=aaa
ForegroundNormal=bbb
phob1an's avatar
phob1an committed
26
27
28
29
30

[Colors:Button]
ForegroundNormal=eee
BackgroundNormal=fff
DecorationFocus=ddd
phob1an's avatar
sync    
phob1an committed
31
DecorationHover=hhh
phob1an's avatar
phob1an committed
32
33
34
35

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

[Colors:Header]
phob1an's avatar
sync    
phob1an committed
40
ForegroundNormal=ccc"""
phob1an's avatar
phob1an committed
41
42


phob1an's avatar
phob1an committed
43
44
45
46
47

def rgb_to_hex(rgb):
    return '%02x%02x%02x' % rgb


phob1an's avatar
phob1an committed
48
49
50
51
52
53
54
55
def notify_user(message):
    """ Simple notification to show something's happening """
    icon_path = os.path.expanduser('~/.local/share/pixmaps/cullax.png')
    icon = '--icon={}'.format(icon_path)

    try:
        subprocess.run(['notify-send',
                        icon,
phob1an's avatar
phob1an committed
56
                        '--expire-time=2000',
phob1an's avatar
phob1an committed
57
58
59
60
61
                        message])
    except:
        pass

def color_triplet(h, l, s):
phob1an's avatar
phob1an committed
62
    """Return color triplet to use in colorschemes"""
phob1an's avatar
phob1an committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    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
phob1an committed
80
    
phob1an's avatar
phob1an committed
81
82
83
84

# ----  CullaX  ----

# Try sending a notification to show we're working
phob1an's avatar
sync    
phob1an committed
85
notify_user("CullaX - Beautifying the Desktop")
phob1an's avatar
phob1an committed
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

# Raise flag when finding correct session in plasmarc
flag = False
# Holder for current activity ID
activity = ""

try:
    with open(os.path.expanduser(
            '~/.config/plasma-org.kde.plasma.desktop-appletsrc')) as f:
        plasmaconfig = f.readlines()
except:
    sys.exit('Unable to find plasma config.')


try:
    with open(os.path.expanduser('~/.config/kactivitymanagerdrc')) as f:
        activityrc = f.readlines()
except:
    print('Unable to find kactivity manager rc. Presuming only default activity.')
    activityrc = None
    flag = True   # There is only default activity

# Retrieve current activity
if activityrc is not None:
    a = [a for a in activityrc if 'current' in a]
    a = a[0].split('=')
    activity = a[1].rstrip()

# Flag if wallpaper is found
found = False

# Find current activity then grab next Image= key
for line in plasmaconfig:
    if activity in line:
        flag = True
    if 'Image=' in line and flag:
        found = True
        break

if not found:
    notify_user(
"""You need to set your own wallpaper\n\
before I can work."""
                )
    sys.exit("I didn't find your wallpaper. Have you set one yet?")

#tmp, wallpaper = line.split('//')
line = line[6:]
if line[0] == 'f':
    line = line[7:]

wallpaper = line.strip()

if not os.path.exists(wallpaper):
    notify_user("I can't your wallpaper."
             .format(wallpaper))
    sys.exit("I think the wallpaper is {0} but I can't find it."
             .format(wallpaper))

phob1an's avatar
phob1an committed
145
146
147
148
try:
    image_filename = open(wallpaper.rstrip(), 'rb')
except:
    sys.exit("Could not open image file.")
phob1an's avatar
phob1an committed
149

phob1an's avatar
sync    
phob1an committed
150
colors_list = colorz.colorz(image_filename, 8, 80, 220, 80)
phob1an's avatar
phob1an committed
151
152
153
r,g,b = colors_list[0][0]
primary_h, primary_l, primary_s = colorsys.rgb_to_hls(r/255,g/255,b/255)
print("Colorz HSL:\t {}, {}, {}  ".format(primary_h, primary_s, primary_l))
phob1an's avatar
sync    
phob1an committed
154
155
156
r,g,b = colors_list[0][1]
bright_h, bright_l, bright_s = colorsys.rgb_to_hls(r/255,g/255,b/255)
bright_l = min(bright_l, 0.98)
phob1an's avatar
sync    
phob1an committed
157
bright_l = max(bright_l, 0.0)
phob1an's avatar
sync    
phob1an committed
158
bright_s = min(bright_s, 0.98)
phob1an's avatar
sync    
phob1an committed
159
bright_s = max(bright_s, 0.0)
phob1an's avatar
sync    
phob1an committed
160
print("Colorz HSL:\t {}, {}, {}  ".format(bright_h, bright_s, bright_l))
phob1an's avatar
sync    
phob1an committed
161
162
titlebar_s = 0.25

phob1an's avatar
sync    
phob1an committed
163
164
165
166
print(primary_l/1.5, min(primary_l/1.25, 0.4))
panel_background = color_triplet(primary_h, min(primary_l/1.25, 0.4), primary_s)
panel_header = color_triplet(bright_h, 0.9, bright_s)
button_hover = color_triplet(primary_h, min(primary_l/1.25, 0.7), primary_s)
phob1an's avatar
phob1an committed
167
foreground = "255,255,255"
phob1an's avatar
sync    
phob1an committed
168
task_focus = color_triplet(primary_h, min(primary_l/1.25, 0.7), primary_s/1.5)
phob1an's avatar
sync    
phob1an committed
169
button_foreground = "255,255,255"
phob1an's avatar
sync    
phob1an committed
170
highlight_color = color_triplet(bright_h, bright_l, bright_s)
phob1an's avatar
sync    
phob1an committed
171
button_background = color_triplet(primary_h, 0.5, primary_s)
phob1an's avatar
sync    
phob1an committed
172
titlebar = color_triplet(primary_h, 0.5, titlebar_s)
phob1an's avatar
phob1an committed
173

phob1an's avatar
phob1an committed
174
175
176
177
178
179

# Write the colorscheme
plasma_colors = plasma_colors.replace('aaa', panel_background)
plasma_colors = plasma_colors.replace('bbb', foreground)
plasma_colors = plasma_colors.replace('ddd', highlight_color)
plasma_colors = plasma_colors.replace('eee', button_foreground)
phob1an's avatar
sync    
phob1an committed
180
plasma_colors = plasma_colors.replace('fff', button_background)
phob1an's avatar
phob1an committed
181
plasma_colors = plasma_colors.replace('ccc', panel_header)
phob1an's avatar
sync    
phob1an committed
182
plasma_colors = plasma_colors.replace('ggg', task_focus)
phob1an's avatar
sync    
phob1an committed
183
plasma_colors = plasma_colors.replace('hhh', button_hover)
phob1an's avatar
phob1an committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

try:
    with open(os.path.expanduser(
            '~/.local/share/plasma/desktoptheme/CullaX/colors'), 'w') as f:
        f.write(plasma_colors)
except:
    sys.exit("Unable to open Culla Plasma colors. Is it installed?")

# Call kwriteconfig5 to change Plasma theme
try:
    subprocess.run(['kwriteconfig5', '--file=plasmarc',
                    '--group=Theme', '--key=name', 'Default'])

    # Do this too quickly and Plasma won't reload
    time.sleep(0.8)

    subprocess.run(['kwriteconfig5', '--file=plasmarc',
                    '--group=Theme', '--key=name', 'CullaX'])
except IOError as e:
    sys.exit(e)

# ---- Set Global Colours ----
try:
    subprocess.run(['kwriteconfig5', '--file=kdeglobals',
                    '--group=Colors:Selection',
phob1an's avatar
sync    
phob1an committed
209
                    '--key=BackgroundNormal', highlight_color])
phob1an's avatar
phob1an committed
210
211
212
    subprocess.run(['kwriteconfig5', '--file=kdeglobals',
                    '--group=Colors:View',
                    '--key=DecorationFocus',
phob1an's avatar
sync    
phob1an committed
213
214
215
216
217
218
219
220
221
222
223
224
225
                    button_background])
    subprocess.run(['kwriteconfig5', '--file=kdeglobals',
                    '--group=WM',
                    '--key=activeBackground',
                    titlebar])
    subprocess.run(['kwriteconfig5', '--file=kdeglobals',
                    '--group=WM',
                    '--key=activeForeground',
                    foreground])
    subprocess.run(['kwriteconfig5', '--file=kdeglobals',
                    '--group=Colors:Button',
                    '--key=activeForeground',
                    foreground])
phob1an's avatar
phob1an committed
226
227
except IOError as e:
    sys.exit(e)