Repository navigation
Expand file tree
/
Copy pathBitmapMaker.cs
More file actions
77 lines (68 loc) · 2.54 KB
/
Copy pathBitmapMaker.cs
File metadata and controls
77 lines (68 loc) · 2.54 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
static class BitmapMaker
{
// 8-color palette — one per button slot (cycles when > 8 buttons)
public static readonly (byte R, byte G, byte B)[] Palette =
[
(0x00, 0x78, 0xD7), // blue
(0x10, 0x7C, 0x10), // green
(0xD8, 0x3B, 0x01), // orange
(0x87, 0x64, 0xB8), // purple
(0x00, 0x82, 0x72), // teal
(0xE8, 0x11, 0x23), // red
(0xFF, 0xB9, 0x00), // amber
(0x00, 0xB7, 0xC3), // cyan
];
public static byte[] Make16x16((byte R, byte G, byte B) color) =>
Make16x16(color.R, color.G, color.B);
// 16x16 24-bit BMP: colored background, white cross, black border
public static byte[] Make16x16(byte r, byte g, byte b)
{
const int width = 16, height = 16, bpp = 3;
int rowSize = width * bpp; // 48 — already DWORD-aligned
int pixelBytes = rowSize * height; // 768
int fileSize = 14 + 40 + pixelBytes; // 822
var buf = new byte[fileSize];
int p = 0;
// BITMAPFILEHEADER
buf[p++] = 0x42; buf[p++] = 0x4D;
WriteI32(buf, ref p, fileSize);
WriteI16(buf, ref p, 0);
WriteI16(buf, ref p, 0);
WriteI32(buf, ref p, 54);
// BITMAPINFOHEADER
WriteI32(buf, ref p, 40);
WriteI32(buf, ref p, width);
WriteI32(buf, ref p, height);
WriteI16(buf, ref p, 1);
WriteI16(buf, ref p, 24);
WriteI32(buf, ref p, 0);
WriteI32(buf, ref p, 0);
WriteI32(buf, ref p, 0);
WriteI32(buf, ref p, 0);
WriteI32(buf, ref p, 0);
WriteI32(buf, ref p, 0);
// Pixel data — bottom-up, BGR order
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
bool border = x == 0 || x == 15 || y == 0 || y == 15;
bool cross = x == 7 || x == 8 || y == 7 || y == 8;
if (border) { buf[p++] = 0x00; buf[p++] = 0x00; buf[p++] = 0x00; }
else if (cross) { buf[p++] = 0xFF; buf[p++] = 0xFF; buf[p++] = 0xFF; }
else { buf[p++] = b; buf[p++] = g; buf[p++] = r; }
}
return buf;
}
static void WriteI32(byte[] buf, ref int p, int v)
{
buf[p++] = (byte)( v & 0xFF);
buf[p++] = (byte)((v >> 8) & 0xFF);
buf[p++] = (byte)((v >> 16) & 0xFF);
buf[p++] = (byte)((v >> 24) & 0xFF);
}
static void WriteI16(byte[] buf, ref int p, short v)
{
buf[p++] = (byte)( v & 0xFF);
buf[p++] = (byte)((v >> 8) & 0xFF);
}
}