petits-scripts-persos/redshift.sh

119 lines
2.4 KiB
Bash
Raw Normal View History

2023-01-24 22:57:03 +00:00
#!/bin/bash
2023-09-20 13:21:25 +00:00
BRIGHTNESS_INCREMENT=.1
R_INCREMENT=0
G_INCREMENT=.1
B_INCREMENT=.2
xrandr_settings_of_display () {
if [ "$#" -ne 1 ] ; then
echo "Usage : xrandr_settings_of_display <display_name>" >&2
exit 1
fi
out="$(xrandr --verbose | sed -e "1,/^$1/d" | sed '/^ /,$d')"
if [ -z "$out" ] ; then
echo "ERROR, empty output for command: xrandr_settings_of_display $@" >&2
exit 1
fi
echo "$out"
}
brigthness_of_display () {
if [ "$#" -ne 1 ] ; then
echo "Usage : brigthness_of_display <display_name>" >&2
exit 1
fi
xrandr_settings_of_display "$1" | grep -Po 'Brightness: \K.*'
}
raw_gamma_of_display () {
if [ "$#" -ne 1 ] ; then
echo "Usage : raw_gamma_of_display <display_name>" >&2
exit 1
fi
xrandr_settings_of_display "$1" | grep -Po 'Gamma: \K.*' | tr ':' ' '
}
gamma_of_display () {
if [ "$#" -ne 1 ] ; then
echo "Usage : gamma_of_display <display_name>" >&2
exit 1
fi
read r g b <<< "$(raw_gamma_of_display "$1")"
echo "$r $g $b -> $(invert $r) $(invert $g) $(invert $b)" >&2
echo "$(invert $r) $(invert $g) $(invert $b)"
}
invert () {
if [ "$#" -ne 1 ] ; then
echo "Usage : invert <float_number>" >&2
exit 1
fi
echo "1/$1" | bc -l | grep -o '.*\...'
}
############### Args
if [ "$#" -ne 2 ] ; then
echo "Usage : $0 <brightness|redness> +|-|=" >&2
2023-01-24 22:57:03 +00:00
exit 1
fi
2023-09-20 13:21:25 +00:00
property="$1"
action="$2"
if [ ! "$action" = - ] && [ ! "$action" = + ] && [ ! "$action" = '=' ] ; then
echo "Bad action '$action'. Expected + - or =."
exit 1
fi
2023-02-02 07:20:13 +00:00
i=0
xrandr --listactivemonitors | tail -n +2 | cut -d ' ' -f 6 | while read monitor ; do
2023-09-20 13:21:25 +00:00
args=''
2023-02-02 07:20:13 +00:00
if [ "$i" -eq 0 ] ; then
((i=i+1))
2023-09-20 13:21:25 +00:00
case "$property" in
brightness)
if [ "$action" = '=' ] ; then
args="--brightness 1"
else
b="$(brigthness_of_display "$monitor")"
b="$( echo "$b $action $BRIGHTNESS_INCREMENT" | bc -l )"
args="--brightness $b"
fi
;;
redness)
if [ "$action" = '=' ] ; then
args="--gamma 1:1:1"
else
read r g b <<< "$(gamma_of_display "$monitor")"
rnew="$( echo "$r $action $R_INCREMENT" | bc -l )"
gnew="$( echo "$g $action $G_INCREMENT" | bc -l )"
bnew="$( echo "$b $action $B_INCREMENT" | bc -l )"
args="--gamma $rnew:$gnew:$bnew"
#echo "from $r:$g:$b to $rnew:$gnew:$bnew"
fi
;;
*)
echo "Invalid property $property"
exit 1
esac
else
args="--same-as $monitor"
2023-02-02 07:20:13 +00:00
fi
2023-09-20 13:21:25 +00:00
xrandr --output "$monitor" $args
echo xrandr --output "$monitor" $args
2023-02-02 07:20:13 +00:00
done