1
0
mirror of https://github.com/mpv-player/mpv synced 2025-04-26 05:09:29 +00:00
mpv/TOOLS/python/gamma-auto.py
8lurry fea5db26bb
python: optimize source, add feature
- lint
- better error message through check_error when mpv api(s) return with an error
- add function hook_add
- add function hook_continue
- add event handler for MPV_EVENT_HOOK
2025-04-09 13:43:16 +06:00

24 lines
614 B
Python

import math
from mpvclient import mpv # type: ignore
def lux_to_gamma(lmin, lmax, rmin, rmax, lux):
if lmax <= lmin or lux == 0:
return 1
num = (rmax - rmin) * (math.log(lux, 10) - math.log(lmin, 10))
den = math.log(lmax, 10) - math.log(lmin, 10)
result = num / den + rmin
# clamp the result
_max = max(rmax, rmin)
_min = min(rmax, rmin)
return max(min(result, _max), _min)
@mpv.observe_property("ambient-light", mpv.MPV_FORMAT_DOUBLE)
def lux_changed(lux):
gamma = lux_to_gamma(16.0, 256.0, 1.0, 1.2, lux or 0)
mpv.set_property_float("gamma-factor", gamma)