mix.sh
Bourne-Again shell script, ASCII text executable
1
#!/usr/bin/env bash
2
#set -x
3
4
mix_channel() {
5
value1=$(printf '%03d' 0x${1})
6
value2=$(printf '%03d' 0x${2})
7
ratio=${3}
8
result=$(echo "scale=0; (${value1} * 100 * ${ratio} + ${value2} * 100 * (1 - ${ratio}))/100" | bc)
9
if [[ ${result} -lt 0 ]] ; then
10
result=0
11
elif [[ ${result} -gt 255 ]] ; then
12
result=255
13
fi
14
echo "${result}"
15
}
16
17
18
mix() {
19
hexinput1=$(echo $1 | tr '[:lower:]' '[:upper:]')
20
hexinput2=$(echo $2 | tr '[:lower:]' '[:upper:]')
21
ratio=${3-0.5}
22
23
a=$(echo $hexinput1 | cut -c-2)
24
b=$(echo $hexinput1 | cut -c3-4)
25
c=$(echo $hexinput1 | cut -c5-6)
26
d=$(echo $hexinput2 | cut -c-2)
27
e=$(echo $hexinput2 | cut -c3-4)
28
f=$(echo $hexinput2 | cut -c5-6)
29
30
r=$(mix_channel ${a} ${d} ${ratio})
31
g=$(mix_channel ${b} ${e} ${ratio})
32
b=$(mix_channel ${c} ${f} ${ratio})
33
34
printf '%02x%02x%02x\n' ${r} ${g} ${b}
35
}
36
37
mix $@
38