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