This commit is contained in:
2024-01-20 16:20:14 +01:00
parent 65959a5c4b
commit 04ad311289
3 changed files with 24 additions and 36 deletions

View File

@ -8,34 +8,22 @@ const batteryIcon = batteryPercent => ({
"-1": "battery-ac-adapter"
})[batteryPercent] ?? "audio-headphones-symbolic";
const hex2rgb = hex => ({
r: parseInt(hex.slice(1, 3), 16),
g: parseInt(hex.slice(3, 5), 16),
b: parseInt(hex.slice(5, 7), 16)
});
const rgb2Hex = rgb => `#${((1 << 24) + (rgb.r << 16) + (rgb.g << 8) + rgb.b).toString(16).slice(1)}`;
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);
const interpolateColor = (f, c0, c1, c3) => {
if(isNaN(f)) return "transparent";
c0 = hex2rgb(c0);
c1 = hex2rgb(c1);
c3 = hex2rgb(c3);
let color1 = c0;
let color2 = c1;
let fade = f;
const interpolateColor = (ff, c) => {
if(!ff) return "transparent";
if(ff >= 1) return c.pop();
if(ff <= 0) return c[0];
if(c3) {
fade *= 2;
if(fade >= 1) {
fade--;
color1 = c1;
color2 = c3;
}
}
let f = ff * (c.length - 1);
const i = ~~f;
f -= i;
const [ c1, c2 ] = [ hex2rgb(c[i]), hex2rgb(c[i + 1]) ];
return rgb2Hex({
r: parseInt(~~(color1.r + ((color2.r - color1.r) * fade)), 10),
g: parseInt(~~(color1.g + ((color2.g - color1.g) * fade)), 10),
b: parseInt(~~(color1.b + ((color2.b - color1.b) * fade)), 10),
r: parseInt(~~(c1.r + ((c2.r - c1.r) * f)), 10),
g: parseInt(~~(c1.g + ((c2.g - c1.g) * f)), 10),
b: parseInt(~~(c1.b + ((c2.b - c1.b) * f)), 10)
});
};