dotfiles/scripts/dim-screen

42 lines
1.3 KiB
Plaintext
Raw Normal View History

2023-12-27 20:20:05 +01:00
#!/usr/bin/python3
# Gradually decrease brightness level. Thanks ChatGPT
import subprocess
import time
def set_brightness(brightness):
subprocess.call(['light', '-S', str(brightness)])
def smooth_dimming(initial_brightness, final_brightness, duration, steps):
brightness_step = (initial_brightness - final_brightness) / steps
sleep_time = duration / steps
for i in range(steps + 1):
current_brightness = round(initial_brightness - i * brightness_step,2)
set_brightness(current_brightness)
print(current_brightness)
time.sleep(sleep_time)
set_brightness(final_brightness)
# Save brighness value for later
subprocess.call(['light', '-O'])
initial_brightness = float(subprocess.getoutput('light'))
2023-12-29 08:23:21 +01:00
if initial_brightness < 20:
final_brightness = max(0, initial_brightness - 10)
elif initial_brightness < 40:
final_brightness = max(10, initial_brightness - 20)
elif initial_brightness < 60:
final_brightness = max(30, initial_brightness - 30)
elif initial_brightness < 80:
final_brightness = max(40, initial_brightness - 40)
elif initial_brightness < 100:
final_brightness = max(50, initial_brightness - 50)
2023-12-27 20:20:05 +01:00
dimming_duration = 0.3 if initial_brightness < 20 else .5
num_steps = 20 if initial_brightness < 20 else 50
smooth_dimming(initial_brightness, final_brightness, dimming_duration, num_steps)