File: gamma-auto.lua

package info (click to toggle)
mpv 0.41.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,404 kB
  • sloc: ansic: 155,875; python: 1,235; sh: 643; javascript: 612; cpp: 468; objc: 302; pascal: 49; xml: 29; makefile: 18
file content (29 lines) | stat: -rw-r--r-- 883 bytes parent folder | download
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
local function lux_to_gamma(lmin, lmax, rmin, rmax, lux)
    if lmax <= lmin or lux == 0 then
        return 1
    end

    local num = (rmax - rmin) * (math.log(lux, 10) - math.log(lmin, 10))
    local den = math.log(lmax, 10) - math.log(lmin, 10)
    local result = num / den + rmin

    -- clamp the result
    local max = math.max(rmax, rmin)
    local min = math.min(rmax, rmin)

    return math.max(math.min(result, max), min)
end

local function lux_changed(_, lux)
    local gamma = lux_to_gamma(16.0, 256.0, 1.0, 1.2, lux or 0)
    mp.set_property_number("gamma-factor", gamma)
end

if mp.get_property("platform") == "linux" then
    -- observing ambient-light isn't supported on Linux
    mp.add_periodic_timer(1, function()
        lux_changed(nil, mp.get_property_number("ambient-light"))
    end)
else
    mp.observe_property("ambient-light", "number", lux_changed)
end