File: gamma-auto.lua

package info (click to toggle)
mpv 0.40.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,676 kB
  • sloc: ansic: 152,062; python: 1,228; sh: 646; javascript: 612; cpp: 461; objc: 302; pascal: 49; xml: 29; makefile: 19
file content (22 lines) | stat: -rw-r--r-- 652 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

mp.observe_property("ambient-light", "number", lux_changed)