Repository navigation
Expand file tree
/
Copy pathcwtools.py
More file actions
156 lines (129 loc) · 4.21 KB
/
Copy pathcwtools.py
File metadata and controls
156 lines (129 loc) · 4.21 KB
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
#!/usr/bin/env python3
import svgwrite
from cairosvg import svg2png
import os
from PIL import Image
import gi
from gi.repository import Gio
import subprocess
"""
ClockWorks
Author: Jacob Vlijm
Copyright © 2017 Ubuntu Budgie Developers
Website=https://ubuntubudgie.org
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or any later version. This
program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details. You
should have received a copy of the GNU General Public License along with this
program. If not, see <https://www.gnu.org/licenses/>.
"""
def get(command):
try:
return subprocess.check_output(command).decode("utf-8").strip()
except subprocess.CalledProcessError:
pass
# dirs etc.
home = os.environ["HOME"]
settingsdir = os.path.join(home, ".config/budgie-extras/clockworks")
hrs_path = os.path.join(settingsdir, "hrs")
mins_path = os.path.join(settingsdir, "mins")
misc_dir = os.path.join(settingsdir, "misc")
user = os.environ["USER"]
tmp = os.path.join("/tmp", user + "_clockworks")
clock_datafile = os.path.join(settingsdir, "clockdata")
key = "org.ubuntubudgie.plugins.budgie-clockworks"
subkeys = ["background", "hour", "minute"]
settings = Gio.Settings.new(key)
# make sure directories exist
for dr in [hrs_path, mins_path, misc_dir, tmp]:
try:
os.makedirs(dr)
except FileExistsError:
pass
def prepare_rgb(hx):
return [int(hx.strip("#")[i:i + 2], 16) for i in (0, 2, 4)]
def hex2rgb(hx):
# convert hex color to rgb
return "rgb(" + ", ".join([str(n) for n in prepare_rgb(hx)]) + ")"
def rgb2hex(r, g, b):
hx = "#{:02x}{:02x}{:02x}".format(r, g, b)
return hx
def get_current_colors():
# produce current gsettings values
return [
settings.get_string(val) for val in [
"background", "hour", "minute"
]
]
def create_png(svg, target):
lines = open(svg).readlines()[1:][0]
svg2png(bytestring=lines, write_to=target)
def create_bg(hx):
# temp file
tmp_bg = os.path.join(tmp, "tmp_bg.svg")
# create svg file
bg = svgwrite.Drawing(filename=tmp_bg, size=("100px", "100px"))
# define circle
bg.add(
bg.circle(
center=(50, 50), r=48, stroke_width="4",
stroke=hex2rgb(hx), fill="none",
)
)
# save & convert
bg.save()
create_png(tmp_bg, os.path.join(misc_dir, "background_image.png"))
def save_togsettings(color, subkey):
settings.set_string(subkey, color)
def create_minutes(hx):
tmp_min = os.path.join(tmp, "tmp_min.svg")
minute = svgwrite.Drawing(
filename=tmp_min, size=("100px", "100px")
)
minute.add(minute.line(
start=(50, 45), end=(50, 10), stroke_width="3",
stroke=hex2rgb(hx),
)
)
minute.save()
temp_minspng = tmp_min.replace(".svg", ".png")
create_png(tmp_min, temp_minspng)
# minutes
min_source = Image.open(temp_minspng)
for n in range(60):
rotate = n * 6
newminspath = os.path.join(mins_path, str(n) + ".png")
new_min = min_source.rotate(
rotate, resample=Image.BICUBIC, expand=False
)
new_min.save(newminspath)
def create_hours(hx):
tmp_hr = os.path.join(tmp, "tmp_hr.svg")
hr = svgwrite.Drawing(
filename=tmp_hr, size=("100px", "100px")
)
hr.add(hr.line(
start=(50, 45), end=(50, 20), stroke_width="3",
stroke=hex2rgb(hx),
)
)
hr.save()
temp_hrspng = tmp_hr.replace(".svg", ".png")
create_png(tmp_hr, temp_hrspng)
# hours
hr_source = Image.open(temp_hrspng)
for n in range(60):
rotate = n * 6
newhrpath = os.path.join(hrs_path, str(n) + ".png")
new_hr = hr_source.rotate(
rotate, resample=Image.BICUBIC, expand=False
)
new_hr.save(newhrpath)
def create_set():
curr_settings = get_current_colors()
create_bg(curr_settings[0])
create_hours(curr_settings[1])
create_minutes(curr_settings[2])