Skip to content

Commit

Permalink
Sync with term-background
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Jun 17, 2024
1 parent 7ada4de commit a6eeab1
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions init/term-background.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ksh
# ^^^^^^^^^^^^ Use env rather ksh installed somewhere

# Copyright (C) 2019-2020, 2023 Rocky Bernstein <rocky@gnu.org>
# Copyright (C) 2019-2020, 2023-2024 Rocky Bernstein <rocky@gnu.org>
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2, or
Expand Down Expand Up @@ -61,12 +61,13 @@ get_default_colorfgbg() {
is_dark_rgb() {
typeset fg_r fg_g fg_b
typeset bg_r bg_g bg_b
fg_r=$1
fg_g=$2
fg_b=$3
bg_r=$4
bg_g=$5
bg_b=$6
typeset a_fg a_bg
fg_r=${1:-0}
fg_g=${2:-0}
fg_b=${3:-0}
bg_r=${4:-FF}
bg_g=${5:-FF}
bg_b=${6:-FF}
a_fg=$((16#"$fg_r" + 16#"$fg_g" + 16#"$fg_b"))
a_bg=$((16#"$bg_r" + 16#"$bg_g" + 16#"$bg_b"))
if [[ $a_fg -gt $a_bg ]]; then
Expand All @@ -76,6 +77,21 @@ is_dark_rgb() {
fi
}

# NOTE: We could have FG=#403020 BG=#203040 and tie
# On return, variable is_dark_bg is set
is_dark_rgb_from_bg() {
midpoint=32767
bg_r=$1
bg_g=$2
bg_b=$3
typeset -i a_bg=$((16#"$bg_r" + 16#"$bg_g" + 16#"$bg_b"))
if (( a_bg < midpoint )); then
is_dark_bg=1
else
is_dark_bg=0
fi
}

# Consult (environment) variable COLORFGB
# On return, variable is_dark_bg is set
is_dark_colorfgbg() {
Expand Down Expand Up @@ -166,11 +182,12 @@ osx_get_terminal_fg_bg() {
method="COLORFGBG"
is_dark_colorfgbg
else
# shellcheck disable=SC2207
RGB_bg=($(osascript -e 'tell application "Terminal" to get the background color of the current settings of the selected tab of front window'))
retsts=$?
# typeset -p RGB_bg
((retsts != 0)) && return 1
is_dark_rgb ${RGB_fg[@]} ${RGB_bg[@]}
is_dark_rgb_from_bg "${RGB_bg[@]}"
method="OSX osascript"
success=1
fi
Expand All @@ -185,7 +202,7 @@ typeset -i xterm_osc_done=0
if (( 3711 < VTE_VERSION )) && [[ -z "$COLORFGBG" ]]; then
# Try Xterm Operating System Command (OSC) (Esc-])
if xterm_compatible_fg_bg; then
is_dark_rgb ${RGB_fg[@]} ${RGB_bg[@]}
is_dark_rgb "${RGB_fg[@]}" "${RGB_bg[@]}"
if [[ $is_dark_bg == 1 ]]; then
# Even though, we're xterm, assist COLORFGBG
export COLORFGBG='0;15'
Expand All @@ -206,11 +223,11 @@ fi

if ((!success)) && [[ -n $TERM ]]; then
case $TERM in
xterm* | gnome | rxvt*)
xterm* | gnome | rxvt* | linux)
typeset -a RGB_fg RGB_bg
if [[ $xterm_osc_done -eq 1 ]]; then
if xterm_compatible_fg_bg; then
is_dark_rgb ${RGB_fg[@]} ${RGB_bg[@]}
is_dark_rgb "${RGB_fg[@]}" "${RGB_bg[@]}"
fi
success=1
fi
Expand Down Expand Up @@ -243,7 +260,7 @@ elif [[ -n $COLORFGBG ]]; then
;;
esac
else
# echo "Can't decide"
echo "Can't decide"
exit_if_not_sourced 1
fi

Expand All @@ -254,11 +271,15 @@ if is_sourced; then
if ((is_dark_bg == 1)); then
export DARK_BG=1 # deprecated
export LC_DARK_BG=1
[[ -z $COLORFGBG ]] && export COLORFGBG='0;15'
if [[ -z "$COLORFGBG" ]]; then
export COLORFGBG='0;15'
fi
else
export DARK_BG=0
export LC_DARK_BG=0
[[ -z $COLORFGBG ]] && export COLORFGBG='15;0'
if [[ -z "$COLORFGBG" ]] ; then
export COLORFGBG='15;0'
fi
fi
fi
else
Expand Down

0 comments on commit a6eeab1

Please sign in to comment.