-
Notifications
You must be signed in to change notification settings - Fork 1
/
tkp
executable file
·171 lines (145 loc) · 5.29 KB
/
tkp
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
#!/bin/bash
## Debugging
# set -x
## Editable variables
DARK_COLOR_SCHEME=/usr/share/color-schemes/BreezeDark.colors
LIGHT_COLOR_SCHEME=/usr/share/color-schemes/BreezeLight.colors
PREFIX="z_tkp_"
## Config file locations
KWINRULES=~/.config/kwinrulesrc
KCOLORSCHEMES=~/.local/share/color-schemes
## Make sure the folder for colorschemes exists
mkdir -p $KCOLORSCHEMES
echo -e "+-------------------------------------+"
echo -e "| Titlebar Kolor Picker |"
echo -e "+-------------------------------------+"
echo -e "> Click on app you want to configure"
## ----- | Find app WM class | get after '=' | remove " | remove , | change to lowercase | only last value
name=$(xprop | grep "WM_CLASS(STRING) =" | cut -f3- -d " " | sed 's/"//g' | sed 's/,//g' | sed -e 's/\(.*\)/\L\1/' | xargs -n1 | tail -n 1)
echo -e ""
echo -e ">> Selected app: $name"
app_name=${name// /}
rule_name="$PREFIX$app_name"
new_color_scheme=$KCOLORSCHEMES/$PREFIX$app_name.colors
hex_2_dec() {
echo "$1" | tr '[:lower:]' '[:upper:]' | cut -c"$2"-"$3" | xargs -I {} echo "ibase=16; {}" | bc
}
## Get color and convert hex (#RRGGBB) to dec (RR,GG,BB)
if [ -z "$1" ]; then
echo -e "> Select color on the screen"
color=$(kcolorchooser --print)
r=$(hex_2_dec "${color:1}" 1 2)
g=$(hex_2_dec "${color:1}" 3 4)
b=$(hex_2_dec "${color:1}" 5 6)
kde_color="$r,$g,$b"
echo -e ""
else
# Figure out which format was used
color=$1
if [[ $color =~ \#[a-fA-F0-9]{6}$ ]]; then
# RGB
r=$(hex_2_dec "${color:1}" 1 2)
g=$(hex_2_dec "${color:1}" 3 4)
b=$(hex_2_dec "${color:1}" 5 6)
kde_color="$r,$g,$b"
elif [[ $color =~ \#[a-fA-F0-9]{8}$ ]]; then
# ARGB
a=$(hex_2_dec "${color:1}" 1 2)
r=$(hex_2_dec "${color:1}" 3 4)
g=$(hex_2_dec "${color:1}" 5 6)
b=$(hex_2_dec "${color:1}" 7 8)
kde_color="$r,$g,$b,$a"
elif [[ $color =~ \d{1,3}(?:,\d{1,3}){2,3}$ ]]; then
# KDE format
r=$(echo "$color" | cut -d ',' -f1)
g=$(echo "$color" | cut -d ',' -f2)
b=$(echo "$color" | cut -d ',' -f3)
kde_color=$1
else
echo ">>! Specified color format not supported!"
exit 1
fi
fi
echo ">> Selected color: $color ($kde_color)"
## Func for calculating floats
calc() {
echo "$1" | bc -l | awk '{printf "%f", $0}'
}
## Check if selected color is more sutable with light colorscheme text
calc_color() {
c=$(calc "$1"/255)
l_comp=$(calc "$c"/12.92)
if (( $(echo "$c > 0.03928" | bc) )); then
l_comp=$(calc e\(l\("$c"+0.055\)*2.4\))
fi
echo "$l_comp"
}
## Calculate contrast
calc_r=$(calc_color "$r")
calc_g=$(calc_color "$g")
calc_b=$(calc_color "$b")
l=$(calc "$calc_r*0.2126 + $calc_g*0.7152 + $calc_b*0.0722 + 0.05")
contrast=$(calc 1.05/"$l")
## If contrast is less than recommended text (4.5:1), then switch to light
if (( $(echo "$contrast < 4.5" | bc) )); then
COLOR_SCHEME=$LIGHT_COLOR_SCHEME
else
COLOR_SCHEME=$DARK_COLOR_SCHEME
fi
## Create a new colorscheme for the app
cp $COLOR_SCHEME "$new_color_scheme"
INACTIVE_COLOR=$(kreadconfig5 --file "$new_color_scheme" --group "Colors:Header" --group "Inactive" --key "BackgroundNormal")
# sed -i "s/BackgroundAlternate=.*/BackgroundAlternate=$kde_color/g" $new_color_scheme
sed -i "s/BackgroundNormal=.*/BackgroundNormal=$kde_color/g" "$new_color_scheme"
sed -i "/Name*/d" "$new_color_scheme"
echo -e "Name=$app_name" >> "$new_color_scheme"
## Change inactive titlebar to default color
kwriteconfig5 --file "$new_color_scheme" --group "Colors:Header" --group "Inactive" --key "BackgroundNormal" "$INACTIVE_COLOR"
KGROUPNUM=$(kreadconfig5 --file $KWINRULES --group "General" --key count)
KRULESSTR=$(kreadconfig5 --file $KWINRULES --group "General" --key rules)
## Check wether the rule already exists
for i in $(seq 1 "$KGROUPNUM")
do
rule_name_value=$(kreadconfig5 --file $KWINRULES --key Description --group $i)
if [ "$rule_name_value" = "$rule_name" ]; then
group=$i
break
fi
done
## Func for writing config to a new group
wc() {
kwriteconfig5 --file $KWINRULES --group "${group}" --key "$1" "$2"
}
if [ -z ${group+x} ]; then
group=$((KGROUPNUM + 1))
echo -e ">> Creating new rule: $rule_name"
## New rule
wc Description "$rule_name"
wc decocolor "$rule_name"
wc decocolorrule 2
wc wmclass "$name"
wc wmclassmatch 1
## Increase config count number
kwriteconfig5 --file $KWINRULES --group "General" --key count ${group}
## Add new rule to rules string
[[ $KRULESSTR = "" ]] && rules=${group} || rules=${KRULESSTR},${group}
kwriteconfig5 --file $KWINRULES --group "General" --key rules ${rules}
## Apply changes
qdbus org.kde.KWin /KWin reconfigure 2> /dev/null
else
## Apply a different colorscheme, so it forces a refresh on an existing rule
echo -e ">> Forcing refresh on existing rule"
wc decocolor "BreezeDark"
{ qdbus org.kde.KWin /KWin reconfigure; } > /dev/null 2>&1
echo -e ">> Applying colorscheme to existing rule"
## Wait some time, so the color-scheme properly applies
sleep 1
## Apply modified custom color scheme
wc decocolor "$rule_name"
{ qdbus org.kde.KWin /KWin reconfigure; } > /dev/null 2>&1
fi
unset wc
unset calc
unset calc_color
unset hex_2_dec
echo -e ">> Done!"