Skip to content

Instantly share code, notes, and snippets.

@oguz-ismail
Created October 13, 2022 07:48
Show Gist options
  • Save oguz-ismail/19c9a45343775fd4c0688d2553c5c043 to your computer and use it in GitHub Desktop.
Save oguz-ismail/19c9a45343775fd4c0688d2553c5c043 to your computer and use it in GitHub Desktop.
generate color scheme from image for tilix
err_usage() {
printf 'Usage: %s [-l] [-n name] [-c comment] image\n' "$0" >&2
exit 1
}
name=Custom
comment=
light_theme=0
while getopts :n:c:l opt; do
case $opt in
(n) name=$OPTARG ;;
(c) comment=$OPTARG ;;
(l) light_theme=1 ;;
(*) err_usage
esac
done
shift $((OPTIND - 1))
if test $# -ne 1; then
err_usage
fi
filter='
trim=end_frame=1,
scale=iw/4:ih/4,
palettegen=17:0,
crop=16:2:0:0,
split[dark],
hue=s=1.5[light],
[dark][light]concat
'
ffmpeg -loglevel error \
-i "$1" \
-filter_complex "$filter" \
-f rawvideo \
-pix_fmt argb \
- \
| od -A n -t u1 -v \
| awk '
{
for (i = 1; i <= NF; i += 4) {
num_colors++
r[num_colors] = $(i + 1)
g[num_colors] = $(i + 2)
b[num_colors] = $(i + 3)
unique[r[num_colors], g[num_colors], b[num_colors]]
}
}
END {
for (i in unique)
num_unique++
if (num_unique < 32) {
system("echo fewer colors than required >&2")
exit 1
}
for (i = 2; i <= 9; i++)
copy(i + 7, i)
if ('$light_theme' == 1) {
copy(1, 8)
copy(1, 16)
copy(17, 1)
lighten(1, 0.85)
copy(17, 9)
darken(9, 0.4)
for (i = 2; i <= 7; i++) {
copy(i + 39, i)
copy(i + 39, i + 8)
}
}
else {
for (i = 10; i <= 16; i++)
copy(i - 8, i)
if (r[1] >= 0x10)
darken(1, 0.4)
blend(8, 0xEE, 0xEE, 0xEE)
copy(8, 9)
darken(9, 0.3)
blend(16, 0xEE, 0xEE, 0xEE)
}
for (i = 1; i <= 16; i++) {
printf "%s", color_block(i) > "/dev/tty"
if (i == 8 || i == 16)
print "" > "/dev/tty"
}
for (i = 1; i <= 16; i++)
print color_code(i)
}
function color_code(i) {
return sprintf("#%02x%02x%02x", r[i], g[i], b[i])
}
function color_block(i) {
return sprintf("\33[48;2;%d;%d;%dm \33[m",
r[i], g[i], b[i])
}
function copy(from, to) {
r[to] = r[from]
g[to] = g[from]
b[to] = b[from]
}
function lighten(i, amount) {
r[i] = int(r[i] + (255 - r[i]) * amount)
g[i] = int(g[i] + (255 - g[i]) * amount)
b[i] = int(b[i] + (255 - b[i]) * amount)
}
function darken(i, amount) {
r[i] = int(r[i] * (1 - amount))
g[i] = int(g[i] * (1 - amount))
b[i] = int(b[i] * (1 - amount))
}
function blend(i, r2, g2, b2) {
r[i] = int((r[i] + r2) / 2)
g[i] = int((g[i] + g2) / 2)
b[i] = int((b[i] + b2) / 2)
}
' \
| jq -R -n \
--arg name "$name" \
--arg comment "$comment" '
[inputs] as $palette | {
$name,
$comment,
"use-theme-colors": false,
"foreground-color": $palette[15],
"background-color": $palette[0],
$palette
}
'
# vim: ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment