headsetcontrol-battery-widget/package/contents/ui/lib/helper.js
2024-01-20 13:16:48 +01:00

21 lines
730 B
JavaScript

const batteryLevel = batteryPercent => ({
"-2": "not connected",
"-1": "charging"
})[batteryPercent] ?? batteryPercent + "%";
const batteryIcon = batteryPercent => ({
"-2": "action-unavailable",
"-1": "battery-ac-adapter"
})[batteryPercent] ?? "audio-headphones-symbolic";
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");
};