headsetcontrol-battery-widget/package/contents/ui/lib/helper.js

21 lines
730 B
JavaScript
Raw Normal View History

2024-01-20 03:55:08 +00:00
const batteryLevel = batteryPercent => ({
"-2": "not connected",
"-1": "charging"
})[batteryPercent] ?? batteryPercent + "%";
2024-01-20 03:49:58 +00:00
2024-01-20 03:55:08 +00:00
const batteryIcon = batteryPercent => ({
"-2": "action-unavailable",
"-1": "battery-ac-adapter"
})[batteryPercent] ?? "audio-headphones-symbolic";
2024-01-20 12:16:48 +00:00
const interpolateColor = (c0, c1, f) => { // https://stackoverflow.com/a/63775249
if(isNaN(f)) return "transparent";
c0 = c0.replace('#', '').match(/.{1,2}/g).map(oct => parseInt(oct, 16) * (1-f));
c1 = c1.replace('#', '').match(/.{1,2}/g).map(oct => parseInt(oct, 16) * f);
return "#" + [0, 1, 2]
.map(i => Math.min(Math.round(c0[i] + c1[i]), 255))
.reduce((a, v) => ((a << 8) + v), 0)
.toString(16)
.padStart(6, "0");
};