-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp_xresources
executable file
·96 lines (81 loc) · 3.37 KB
/
cp_xresources
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
#!/bin/python3
#copies colors from xresources to various config files
#
#to use, copy the config file in question, add .in extension, and add the path
#(without .in extension) to targets list below. The script will copy the .in file
#to the real config path and replace all codes with their corresponding colors
#
#codes are in the following format:
# %cl[!r]NAME%
#for numbered colors, the name is just the number, for foreground and background
#it is abreviated to fg and bg, and for anything else it is the same as whatever
#you set it to in the xresources file.
#
#add the !r if you want the output to be comma separated rgb format, otherwise it
#will be hexadecimal
targets = [
"~/.config/alacritty/alacritty.yml",
"~/.config/dunst/dunstrc",
"~/.config/qutebrowser/start.html",
"~/.config/polybar/config.ini",
"~/.config/eww/eww.scss",
#"~/.config/X11/debug",
]
import os, sys, subprocess, re
class color():
def __init__(self, ip):
if (type(ip) == type(str())):
self.hex = ip
self.rgb = [int(ip.lstrip("#")[i:i+2],16) for i in range(0,6,2)]
elif (type(ip) == type(list())):
self.hex = "#"+"".join([str(hex(ip[i])).lstrip("0x") for i in range(0,3)])
self.rgb = ip
def interp(c1, c2, t, d):
return color([(((c1.rgb[i]*(d-t)) + (c2.rgb[i]*(t)))//d) for i in range(0,3)])
def to_str(self):
return ",".join([str(v) for v in self.rgb])
xrdb=subprocess.run(['xrdb', '-query'], stdout=subprocess.PIPE).stdout.decode( 'utf-8' ).rstrip().split("\n")
c = {}
for line in xrdb:
#split line into key and value
kv=line.split(":\t")
#replace color names used by xrdb with those used in the color codes. also make sure the color is a valid hex
if kv[0].find("*.") == 0 and re.match("^#[a-zA-Z0-9]{6}$", kv[1]) != None:
if kv[0].find("*.color") == 0:
c[kv[0][7:]] = color(kv[1])
elif kv[0].find("*.background") == 0:
c["bg"] = color(kv[1])
elif kv[0].find("*.foreground") == 0:
c["fg"] = color(kv[1])
else:
c[kv[0][2:]] = color(kv[1])
for i in range(0,8):
#generate aditional shades of the foreground and background colors by interpolation
try:
c["g"+str(i+8)] = color.interp(c["8"],c["fg"],i,8)
c["g"+str(i)] = color.interp(c["bg"],c["light_bg"],i,8)
except KeyError:
c["g"+str(i+8)] = color.interp(c["8"],c["fg"],i,8)
c["g"+str(i)] = color.interp(c["bg"],c["warn_bg"],i,8)
for target in targets:
with open(os.path.expanduser(target) + ".in", "rt") as file:
f=file.read()
search=re.finditer("%cl[!a-zA-Z0-9_]*%", f)
lnew=[]
#go through each color code, add the section proceeding it to the new file,
#then replace the code with the hex
sel=0
for match in search:
#add proceeding section of file to new file
lnew.append(f[sel:match.start()])
if (match.group()[3:5] != '!r'):
lnew.append(c[match.group()[3:-1]].hex)
else:
lnew.append(c[match.group()[5:-1]].to_str())
sel=match.end()
#add the last section of the file
lnew.append(f[sel:])
new = "".join(lnew)
#write to the new file
with open(os.path.expanduser(target), "w") as newfile:
newfile.write(new)