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

31 lines
1023 B
JavaScript
Raw Normal View History

2024-06-06 23:56:43 +00:00
const batteryLevel = (batteryStatus, batteryPercent) => ({
"BATTERY_UNAVAILABLE": "not connected",
"BATTERY_CHARGING": "charging (" + batteryPercent + "%)"
})[batteryStatus] ?? batteryPercent + "%";
2024-01-20 03:49:58 +00:00
2024-06-06 23:56:43 +00:00
const batteryIcon = batteryStatus => ({
"BATTERY_UNAVAILABLE": "action-unavailable",
"BATTERY_AVAILABLE": "audio-headphones-symbolic",
"BATTERY_CHARGING": "reload"
})[batteryStatus] ?? "audio-headphones-symbolic";
2024-01-20 12:16:48 +00:00
2024-01-20 15:20:14 +00:00
const hex2rgb = (hex, h = hex.replace('#', '0x')) => ({ r: h >> 16 & 255, g: h >> 8 & 255, b: h & 255 });
const rgb2Hex = rgb => "#" + ((1 << 24) + (rgb.r << 16) + (rgb.g << 8) + rgb.b).toString(16).slice(1);
2024-01-20 14:16:18 +00:00
2024-01-20 15:47:54 +00:00
const interpolateColor = (f, c) => {
if(!f) return "transparent";
if(f >= 1) return c.pop();
2024-01-22 02:08:25 +00:00
if(f <= 0) return c.shift();
2024-01-20 14:16:18 +00:00
2024-01-20 15:47:54 +00:00
f *= c.length - 1;
2024-01-20 15:20:14 +00:00
const i = ~~f;
f -= i;
const [ c1, c2 ] = [ hex2rgb(c[i]), hex2rgb(c[i + 1]) ];
2024-01-20 14:16:18 +00:00
return rgb2Hex({
2024-01-20 15:47:54 +00:00
r: c1.r + ((c2.r - c1.r) * f) | 10,
g: c1.g + ((c2.g - c1.g) * f) | 10,
b: c1.b + ((c2.b - c1.b) * f) | 10
2024-01-20 14:16:18 +00:00
});
2024-01-20 12:16:48 +00:00
};