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