summaryrefslogtreecommitdiffstats
path: root/bin/bright
blob: 87126e1dbd1105c05efb946fb684049814896c26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/sh

set -eu

get_b()
{
	local b=

	b=$(brightnessctl --class=backlight get) || return 1
	printf '%.0f\n' $(printf \
		'scale = 4; (7 * sqrt((2 - %d) * (%d - 3102))) / 1550\n' \
		${b} ${b} | bc)

	return 0
}

set_b()
{
	local b="${1}"
	shift 1

	brightnessctl --class=backlight set $(printf '%.0f\n' \
		$(printf 'scale = 4; 2 + (1 - sqrt(1 - (%d / 7)^2)) * 1550\n' \
		${b} | bc)) || return 1

	return 0
}

usage()
{
	printf 'Usage: %s {-|+}\n' "${0}"

	return 0
}

main()
{
	local dir=
	local b=

	if [ ${#} -ne 1 ]; then
		usage 1>&2
		return 1
	fi
	dir="${1}"
	shift 1

	case "${dir}" in
		-|+);;
		*)
			usage 1>&2
			return 1
			;;
	esac

	b=$(get_b) || return 1
	: $((b += 1))
	: $((b ${dir}= 1))
	[ ${b} -gt 8 ] && b=8
	[ ${b} -lt 1 ] && b=1
	: $((b -= 1))
	set_b ${b} || return 1

	return 0
}

main "${@}"