-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiro
executable file
·224 lines (207 loc) · 5.34 KB
/
iro
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
172
173
174
175
176
177
178
179
180
181
182
183
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env bash
# Dependencies: xcolor, feh, imagemagick, expr (gnutils), xclip
# You may copy the CONFIGURABLES and put them in $XDG_CONFIG_HOME/iro/iro.conf
# and edit them as you please.
# CONFIGURABLES
font="Helvetica-Neue" # see convert -list font for a list of fonts available on system
imgsize="100x100"
fontsize="50x50"
if [ -n $XDG_DATA_HOME ]; then
outputdir="$XDG_DATA_HOME/iro"
else
outputdir="$HOME/.data/iro"
fi
palettepidfile="$outputdir/.palettepidfile"
copycmd="xclip -selection clipboard"
if [ -n $XDG_CONFIG_HOME ]; then
conffile="$XDG_CONFIG_HOME/iro/iro.conf"
else
conffile="$HOME/.config/iro/iro.conf"
fi
# load personal configs, if any
if [ -f "$conffile" ]; then
. "$conffile"
fi
usage() {
echo " Usage: iro [option] [argument]
option:
(default) Shows palettes in feh, copyable by hitting Enter while focused
-c Copy-only mode; directly copies colour value to clipboard
[mutually exclusive with -p]
-n No-text mode; will not print colour value to palette
-p Provide your own color code
[mutually exclusive with -c]
-h Show this help
argument:
hex (default) Shows palette with hex value
rgb Shows palette with rgb value
clear Remove all palettes from screen
clean Cleans palette in $outputdir"
}
checkOutputDir() {
if [ ! -d "$outputdir" ]; then
echo "$outputdir not found. Creating $outputdir..."
mkdir $outputdir
fi
if [ ! -f "$palettepidfile" ]; then
echo "$palettepidfile not found. Creating empty $palettepidfile..."
touch $palettepidfile
fi
}
createPalette() {
case "$2" in
"no-text")
convert \
\( -background transparent -size $imgsize xc:"$1" \) \
"$outputdir/$1.png"
;;
*)
convert \
\( -background transparent -size $imgsize xc:"$1" \) \
\( -background transparent -size $fontsize -fill $(setPaletteFontColor "$1" $2) -gravity center -font "$font" caption:"$1" \) \
-gravity center -composite "$outputdir/$1.png"
esac
showPalette "$1"
}
# Look through pidfile for feh instances.
# Kill process if process name is still feh,
# that is, that the process is still being used by a feh instance.
# This is because the user is free to remove the palettes
# by quitting feh normally
killallPalettes() {
for i in $(cat $palettepidfile)
do
procname=$(ps -p $i -o comm=)
if [ "$procname" = "feh" ]; then
pkill -P $i
kill -s TERM $i
fi
done
echo "" > $palettepidfile
}
# the idea and code themselves are simple
# take the total and use the midpoint as the thresholds
# for light or dark font colour
lightDarkDecider() {
red=$1
green=$2
blue=$3
total=$(expr $red + $green + $blue)
if [ $total -le 382 ]; then
echo 'white'
else
echo 'black'
fi
}
# we have to deal with different colour formats separately
setPaletteFontColor() {
case "$2" in
rgb) # format that it receives is rgb(rrr, ggg, bbb)
striped=$(echo -n ${1#rgb(} | tr -d ")")
red=$(echo -n $striped | cut -d, -f1)
green=$(echo -n $striped | cut -d, -f2)
blue=$(echo -n $striped | cut -d, -f3)
echo $(lightDarkDecider $red $green $blue)
;;
hex) # format that it receives is #rrggbb
hexval=${1#\#}
red=$(printf "%d" 0x${hexval:0:2})
green=$(printf "%d" 0x${hexval:2:2})
blue=$(printf "%d" 0x${hexval:4:2})
echo $(lightDarkDecider $red $green $blue)
;;
*)
echo 'black'
esac
}
showPalette() {
feh --action "echo -n \"$1\" | $copycmd" "$outputdir/$1.png" &
echo $! >> $palettepidfile
}
main() {
checkOutputDir
# high level options
c_flag=false # copy-only <---------- mutually exclusive
p_flag=false # provide color code ---|
n_flag=false # no-text
while getopts ":cnph" opt; do
case "${opt}" in
c)
if [ $p_flag = false ]; then
c_flag=true
fi
;;
n) n_flag=true ;;
p)
if [ $c_flag = false ]; then
p_flag=true
fi
;;
h)
usage
exit 0
;;
\? )
usage
exit 1
;;
esac
done
shift $((OPTIND -1)) # done with high level options
# main argument
subcmd=$1
if [ $p_flag = false ]; then
case "$subcmd" in
"rgb" | "hex")
sel=$(xcolor -f $subcmd)
if [ $c_flag = false ]; then
if [ $n_flag = false ]; then
createPalette "$sel" $subcmd
else
createPalette "$sel" no-text
fi
else
echo -n "$sel" | $copycmd
fi
;;
"clear")
killallPalettes
;;
"clean")
rm $outputdir/*.png
;;
*)
sel=$(xcolor -f hex)
if [ $c_flag = false ]; then
createPalette "$sel" hex
else
echo -n "$sel" | $copycmd
fi
esac
else
shift
ccode=$1
if [ -z $ccode ]; then
echo "Please provide $subcmd color code"
exit 1
fi
case "$subcmd" in
"rgb" | "hex")
if [ $n_flag = false ]; then
createPalette "$ccode" $subcmd
else
createPalette "$ccode" no-text
fi
;;
"clear")
killallPalettes
;;
"clean")
rm $outputdir/*.png
;;
*)
createPalette "$ccode" hex
esac
fi
}
main "$@"