-
Notifications
You must be signed in to change notification settings - Fork 7
/
color2.c
34 lines (31 loc) · 925 Bytes
/
color2.c
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
#include <api.h>
unsigned char rgb2pal(int r, int g, int b, int x, int y);
void Main(void) {
char *buf;
int win, x, y;
api_initmalloc();
buf = api_malloc(144 * 164);
win = api_openwin(buf, 144, 164, -1, "color2");
for (y = 0; y < 128; y++) {
for (x = 0; x < 128; x++) {
buf[(x + 8) + (y + 28) * 144] = rgb2pal(x * 2, y * 2, 0, x, y);
}
}
api_refreshwin(win, 8, 28, 136, 156);
api_getkey(1); /* てきとうなキー入力を待つ */
api_end();
}
unsigned char rgb2pal(int r, int g, int b, int x, int y) {
static int table[4] = {3, 1, 0, 2};
int i;
x &= 1; /* 偶数か奇数か */
y &= 1;
i = table[x + y * 2]; /* 中間色を作るための定数 */
r = (r * 21) / 256; /* これで 0〜20 になる */
g = (g * 21) / 256;
b = (b * 21) / 256;
r = (r + i) / 4; /* これで 0〜5 になる */
g = (g + i) / 4;
b = (b + i) / 4;
return 16 + r + g * 6 + b * 36;
}