Repository navigation
Expand file tree
/
Copy pathbattery.ts
More file actions
60 lines (54 loc) · 1.63 KB
/
Copy pathbattery.ts
File metadata and controls
60 lines (54 loc) · 1.63 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
53
54
55
56
57
58
59
60
import { GrayImage, imageFromAsciiArt } from "./image";
const EMPTY_BATTERY_ICON = imageFromAsciiArt(
[
" ################## ",
" #................# ",
" #................###",
" #................###",
" #................###",
" #................###",
" #................###",
" #................###",
" #................# ",
" ################## ",
],
120,
);
const BATTERY_BOLT_ICON = imageFromAsciiArt(
[
"....#..",
"...##..",
"..##...",
".######",
"...##..",
"..##...",
"..#....",
],
255,
);
export const BATTERY_ICON_WIDTH = EMPTY_BATTERY_ICON.width;
const BATTERY_FILL_X = 3;
const BATTERY_FILL_Y = 2;
const BATTERY_FILL_WIDTH = 14;
const BATTERY_FILL_HEIGHT = 6;
export function drawBattery(percentCharge: number, isCharging: boolean): GrayImage {
const icon = new GrayImage(EMPTY_BATTERY_ICON.width, EMPTY_BATTERY_ICON.height, 0);
icon.bitBlt(EMPTY_BATTERY_ICON, 0, 0);
const clamped = Math.max(0, Math.min(100, percentCharge));
const fillWidth = Math.round((BATTERY_FILL_WIDTH * clamped) / 100);
icon.fillRect(BATTERY_FILL_X, BATTERY_FILL_Y, fillWidth, BATTERY_FILL_HEIGHT, 190);
if (isCharging) {
overlayImage(icon, BATTERY_BOLT_ICON, 7, 1, clamped > 50 ? 0 : 255);
}
return icon;
}
function overlayImage(target: GrayImage, source: GrayImage, dx: number, dy: number, value: number): void {
for (let y = 0; y < source.height; y++) {
for (let x = 0; x < source.width; x++) {
const sourceValue = source.pixels[y * source.width + x] ?? 0;
if (sourceValue > 0) {
target.setPixel(dx + x, dy + y, value);
}
}
}
}