-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.js
More file actions
52 lines (44 loc) · 1.49 KB
/
Copy pathModel.js
File metadata and controls
52 lines (44 loc) · 1.49 KB
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
.pragma library
// The light reports colour temperature in mireds and accepts nothing outside
// 143-344 — it ignores out-of-range values rather than clamping them, so a
// slider allowed past the end would silently stop responding.
var KELVIN_MIN = 2900
var KELVIN_MAX = 7000
var ICON_ON = ""
var ICON_OFF = ""
var ICON_DOWN = ""
function clamp(value, low, high) {
if (isNaN(value)) return low
return Math.max(low, Math.min(high, value))
}
// Kelvin steps of 50 match what the device can actually resolve; finer steps
// just produce values that read back as something else.
function snapKelvin(kelvin) {
return Math.round(clamp(kelvin, KELVIN_MIN, KELVIN_MAX) / 50) * 50
}
// omarchy-keylight prints one short line; anything longer didn't come from it.
var MAX_OUTPUT = 4096
function parse(text) {
var raw = String(text || "").trim()
if (raw.length > MAX_OUTPUT) return null
try {
var parsed = JSON.parse(raw)
if (!parsed || parsed.reachable !== true) return null
return {
on: parsed.on === 1,
brightness: clamp(parseInt(parsed.brightness, 10), 0, 100),
kelvin: snapKelvin(parseInt(parsed.kelvin, 10))
}
} catch (e) {
return null
}
}
function icon(reachable, on) {
if (!reachable) return ICON_DOWN
return on ? ICON_ON : ICON_OFF
}
function tooltip(reachable, on, brightness, kelvin) {
if (!reachable) return "Key Light — not reachable"
if (!on) return "Key Light — off"
return "Key Light — " + brightness + "%, " + kelvin + "K"
}