From eccbd5d41e3307e8d08c7025cacafb4cd0b2ebc0 Mon Sep 17 00:00:00 2001 From: Kevin Lange Date: Sat, 7 Jun 2014 22:14:03 -0700 Subject: [PATCH] Map 256-color palette to VGA colors --- userspace/gui/terminal/terminal-vga.c | 11 +- userspace/gui/terminal/vga-palette.h | 259 ++++++++++++++++++++++++++ util/generate-vga-palette.c | 76 ++++++++ 3 files changed, 343 insertions(+), 3 deletions(-) create mode 100644 userspace/gui/terminal/vga-palette.h create mode 100644 util/generate-vga-palette.c diff --git a/userspace/gui/terminal/terminal-vga.c b/userspace/gui/terminal/terminal-vga.c index cdd8f8b0..65804d1d 100644 --- a/userspace/gui/terminal/terminal-vga.c +++ b/userspace/gui/terminal/terminal-vga.c @@ -25,6 +25,7 @@ #include "lib/kbd.h" #include "lib/graphics.h" +#include "gui/terminal/vga-palette.h" #include "gui/terminal/lib/termemu.h" #define USE_BELL 0 @@ -122,11 +123,15 @@ term_write_char( uint32_t bg, uint8_t flags ) { - if (fg > 16) fg = TERM_DEFAULT_FG; - if (bg > 16) bg = TERM_DEFAULT_BG; + if (val > 128) val = ununicode(val); + if (fg > 16) { + fg = vga_colors[fg]; + } + if (bg > 16) { + bg = vga_colors[bg]; + } if (fg == 16) fg = 0; if (bg == 16) bg = 0; - if (val > 128) val = ununicode(val); placech(val, x, y, (vga_to_ansi[fg] & 0xF) | (vga_to_ansi[bg] << 4)); } diff --git a/userspace/gui/terminal/vga-palette.h b/userspace/gui/terminal/vga-palette.h new file mode 100644 index 00000000..ed4775f4 --- /dev/null +++ b/userspace/gui/terminal/vga-palette.h @@ -0,0 +1,259 @@ +#define PALETTE_COLORS 256 +uint32_t vga_colors[PALETTE_COLORS] = { + 0x0, + 0x1, + 0x2, + 0x3, + 0x4, + 0x5, + 0x6, + 0x7, + 0x8, + 0x9, + 0xa, + 0xb, + 0xc, + 0xd, + 0xe, + 0xf, + 0x0, /* #000000 -> #000000 */ + 0x4, /* #00005f -> #0000aa */ + 0x4, /* #000087 -> #0000aa */ + 0x4, /* #0000af -> #0000aa */ + 0x4, /* #0000d7 -> #0000aa */ + 0xc, /* #0000ff -> #5555ff */ + 0x2, /* #005f00 -> #00aa00 */ + 0x8, /* #005f5f -> #555555 */ + 0x6, /* #005f87 -> #00aaaa */ + 0x6, /* #005faf -> #00aaaa */ + 0xc, /* #005fd7 -> #5555ff */ + 0xc, /* #005fff -> #5555ff */ + 0x2, /* #008700 -> #00aa00 */ + 0xa, /* #00875f -> #55aa55 */ + 0x6, /* #008787 -> #00aaaa */ + 0x6, /* #0087af -> #00aaaa */ + 0x6, /* #0087d7 -> #00aaaa */ + 0xc, /* #0087ff -> #5555ff */ + 0x2, /* #00af00 -> #00aa00 */ + 0xa, /* #00af5f -> #55aa55 */ + 0x6, /* #00af87 -> #00aaaa */ + 0x6, /* #00afaf -> #00aaaa */ + 0x6, /* #00afd7 -> #00aaaa */ + 0xe, /* #00afff -> #55ffff */ + 0x2, /* #00d700 -> #00aa00 */ + 0xa, /* #00d75f -> #55aa55 */ + 0x6, /* #00d787 -> #00aaaa */ + 0x6, /* #00d7af -> #00aaaa */ + 0x6, /* #00d7d7 -> #00aaaa */ + 0xe, /* #00d7ff -> #55ffff */ + 0x2, /* #00ff00 -> #00aa00 */ + 0xa, /* #00ff5f -> #55aa55 */ + 0x6, /* #00ff87 -> #00aaaa */ + 0x6, /* #00ffaf -> #00aaaa */ + 0xe, /* #00ffd7 -> #55ffff */ + 0xe, /* #00ffff -> #55ffff */ + 0x1, /* #5f0000 -> #aa0000 */ + 0x8, /* #5f005f -> #555555 */ + 0x5, /* #5f0087 -> #aa00aa */ + 0x5, /* #5f00af -> #aa00aa */ + 0x5, /* #5f00d7 -> #aa00aa */ + 0xc, /* #5f00ff -> #5555ff */ + 0x3, /* #5f5f00 -> #aa5500 */ + 0x8, /* #5f5f5f -> #555555 */ + 0x8, /* #5f5f87 -> #555555 */ + 0x7, /* #5f5faf -> #aaaaaa */ + 0xc, /* #5f5fd7 -> #5555ff */ + 0xc, /* #5f5fff -> #5555ff */ + 0x2, /* #5f8700 -> #00aa00 */ + 0xa, /* #5f875f -> #55aa55 */ + 0xa, /* #5f8787 -> #55aa55 */ + 0x7, /* #5f87af -> #aaaaaa */ + 0xc, /* #5f87d7 -> #5555ff */ + 0xc, /* #5f87ff -> #5555ff */ + 0x2, /* #5faf00 -> #00aa00 */ + 0xa, /* #5faf5f -> #55aa55 */ + 0xa, /* #5faf87 -> #55aa55 */ + 0x7, /* #5fafaf -> #aaaaaa */ + 0x7, /* #5fafd7 -> #aaaaaa */ + 0xe, /* #5fafff -> #55ffff */ + 0x2, /* #5fd700 -> #00aa00 */ + 0xa, /* #5fd75f -> #55aa55 */ + 0xa, /* #5fd787 -> #55aa55 */ + 0x7, /* #5fd7af -> #aaaaaa */ + 0xe, /* #5fd7d7 -> #55ffff */ + 0xe, /* #5fd7ff -> #55ffff */ + 0x2, /* #5fff00 -> #00aa00 */ + 0xb, /* #5fff5f -> #ffff55 */ + 0xb, /* #5fff87 -> #ffff55 */ + 0x7, /* #5fffaf -> #aaaaaa */ + 0xe, /* #5fffd7 -> #55ffff */ + 0xe, /* #5fffff -> #55ffff */ + 0x1, /* #870000 -> #aa0000 */ + 0x8, /* #87005f -> #555555 */ + 0x5, /* #870087 -> #aa00aa */ + 0x5, /* #8700af -> #aa00aa */ + 0x5, /* #8700d7 -> #aa00aa */ + 0xc, /* #8700ff -> #5555ff */ + 0x3, /* #875f00 -> #aa5500 */ + 0x8, /* #875f5f -> #555555 */ + 0x8, /* #875f87 -> #555555 */ + 0x7, /* #875faf -> #aaaaaa */ + 0xc, /* #875fd7 -> #5555ff */ + 0xc, /* #875fff -> #5555ff */ + 0x3, /* #878700 -> #aa5500 */ + 0xa, /* #87875f -> #55aa55 */ + 0x7, /* #878787 -> #aaaaaa */ + 0x7, /* #8787af -> #aaaaaa */ + 0x7, /* #8787d7 -> #aaaaaa */ + 0xc, /* #8787ff -> #5555ff */ + 0x2, /* #87af00 -> #00aa00 */ + 0xa, /* #87af5f -> #55aa55 */ + 0x7, /* #87af87 -> #aaaaaa */ + 0x7, /* #87afaf -> #aaaaaa */ + 0x7, /* #87afd7 -> #aaaaaa */ + 0xe, /* #87afff -> #55ffff */ + 0x2, /* #87d700 -> #00aa00 */ + 0xa, /* #87d75f -> #55aa55 */ + 0x7, /* #87d787 -> #aaaaaa */ + 0x7, /* #87d7af -> #aaaaaa */ + 0xe, /* #87d7d7 -> #55ffff */ + 0xe, /* #87d7ff -> #55ffff */ + 0x2, /* #87ff00 -> #00aa00 */ + 0xb, /* #87ff5f -> #ffff55 */ + 0xb, /* #87ff87 -> #ffff55 */ + 0x7, /* #87ffaf -> #aaaaaa */ + 0xe, /* #87ffd7 -> #55ffff */ + 0xe, /* #87ffff -> #55ffff */ + 0x1, /* #af0000 -> #aa0000 */ + 0x5, /* #af005f -> #aa00aa */ + 0x5, /* #af0087 -> #aa00aa */ + 0x5, /* #af00af -> #aa00aa */ + 0x5, /* #af00d7 -> #aa00aa */ + 0xd, /* #af00ff -> #ff55ff */ + 0x3, /* #af5f00 -> #aa5500 */ + 0x9, /* #af5f5f -> #ff5555 */ + 0x9, /* #af5f87 -> #ff5555 */ + 0x7, /* #af5faf -> #aaaaaa */ + 0xd, /* #af5fd7 -> #ff55ff */ + 0xd, /* #af5fff -> #ff55ff */ + 0x3, /* #af8700 -> #aa5500 */ + 0xa, /* #af875f -> #55aa55 */ + 0x7, /* #af8787 -> #aaaaaa */ + 0x7, /* #af87af -> #aaaaaa */ + 0x7, /* #af87d7 -> #aaaaaa */ + 0xd, /* #af87ff -> #ff55ff */ + 0x2, /* #afaf00 -> #00aa00 */ + 0xa, /* #afaf5f -> #55aa55 */ + 0x7, /* #afaf87 -> #aaaaaa */ + 0x7, /* #afafaf -> #aaaaaa */ + 0x7, /* #afafd7 -> #aaaaaa */ + 0xf, /* #afafff -> #ffffff */ + 0x2, /* #afd700 -> #00aa00 */ + 0xb, /* #afd75f -> #ffff55 */ + 0x7, /* #afd787 -> #aaaaaa */ + 0x7, /* #afd7af -> #aaaaaa */ + 0x7, /* #afd7d7 -> #aaaaaa */ + 0xf, /* #afd7ff -> #ffffff */ + 0x2, /* #afff00 -> #00aa00 */ + 0xb, /* #afff5f -> #ffff55 */ + 0xb, /* #afff87 -> #ffff55 */ + 0x7, /* #afffaf -> #aaaaaa */ + 0xf, /* #afffd7 -> #ffffff */ + 0xf, /* #afffff -> #ffffff */ + 0x1, /* #d70000 -> #aa0000 */ + 0x9, /* #d7005f -> #ff5555 */ + 0x5, /* #d70087 -> #aa00aa */ + 0x5, /* #d700af -> #aa00aa */ + 0x5, /* #d700d7 -> #aa00aa */ + 0xd, /* #d700ff -> #ff55ff */ + 0x3, /* #d75f00 -> #aa5500 */ + 0x9, /* #d75f5f -> #ff5555 */ + 0x9, /* #d75f87 -> #ff5555 */ + 0x7, /* #d75faf -> #aaaaaa */ + 0xd, /* #d75fd7 -> #ff55ff */ + 0xd, /* #d75fff -> #ff55ff */ + 0x3, /* #d78700 -> #aa5500 */ + 0x9, /* #d7875f -> #ff5555 */ + 0x7, /* #d78787 -> #aaaaaa */ + 0x7, /* #d787af -> #aaaaaa */ + 0x7, /* #d787d7 -> #aaaaaa */ + 0xd, /* #d787ff -> #ff55ff */ + 0x2, /* #d7af00 -> #00aa00 */ + 0xa, /* #d7af5f -> #55aa55 */ + 0x7, /* #d7af87 -> #aaaaaa */ + 0x7, /* #d7afaf -> #aaaaaa */ + 0x7, /* #d7afd7 -> #aaaaaa */ + 0xf, /* #d7afff -> #ffffff */ + 0x2, /* #d7d700 -> #00aa00 */ + 0xb, /* #d7d75f -> #ffff55 */ + 0x7, /* #d7d787 -> #aaaaaa */ + 0x7, /* #d7d7af -> #aaaaaa */ + 0xf, /* #d7d7d7 -> #ffffff */ + 0xf, /* #d7d7ff -> #ffffff */ + 0xb, /* #d7ff00 -> #ffff55 */ + 0xb, /* #d7ff5f -> #ffff55 */ + 0xb, /* #d7ff87 -> #ffff55 */ + 0x7, /* #d7ffaf -> #aaaaaa */ + 0xf, /* #d7ffd7 -> #ffffff */ + 0xf, /* #d7ffff -> #ffffff */ + 0x1, /* #ff0000 -> #aa0000 */ + 0x9, /* #ff005f -> #ff5555 */ + 0x5, /* #ff0087 -> #aa00aa */ + 0x5, /* #ff00af -> #aa00aa */ + 0x5, /* #ff00d7 -> #aa00aa */ + 0xd, /* #ff00ff -> #ff55ff */ + 0x3, /* #ff5f00 -> #aa5500 */ + 0x9, /* #ff5f5f -> #ff5555 */ + 0x9, /* #ff5f87 -> #ff5555 */ + 0x7, /* #ff5faf -> #aaaaaa */ + 0xd, /* #ff5fd7 -> #ff55ff */ + 0xd, /* #ff5fff -> #ff55ff */ + 0x3, /* #ff8700 -> #aa5500 */ + 0x9, /* #ff875f -> #ff5555 */ + 0x9, /* #ff8787 -> #ff5555 */ + 0x7, /* #ff87af -> #aaaaaa */ + 0xd, /* #ff87d7 -> #ff55ff */ + 0xd, /* #ff87ff -> #ff55ff */ + 0x2, /* #ffaf00 -> #00aa00 */ + 0xb, /* #ffaf5f -> #ffff55 */ + 0x7, /* #ffaf87 -> #aaaaaa */ + 0x7, /* #ffafaf -> #aaaaaa */ + 0x7, /* #ffafd7 -> #aaaaaa */ + 0xf, /* #ffafff -> #ffffff */ + 0x2, /* #ffd700 -> #00aa00 */ + 0xb, /* #ffd75f -> #ffff55 */ + 0xb, /* #ffd787 -> #ffff55 */ + 0x7, /* #ffd7af -> #aaaaaa */ + 0xf, /* #ffd7d7 -> #ffffff */ + 0xf, /* #ffd7ff -> #ffffff */ + 0xb, /* #ffff00 -> #ffff55 */ + 0xb, /* #ffff5f -> #ffff55 */ + 0xb, /* #ffff87 -> #ffff55 */ + 0xf, /* #ffffaf -> #ffffff */ + 0xf, /* #ffffd7 -> #ffffff */ + 0xf, /* #ffffff -> #ffffff */ + 0x0, /* #080808 -> #000000 */ + 0x0, /* #121212 -> #000000 */ + 0x0, /* #1c1c1c -> #000000 */ + 0x0, /* #262626 -> #000000 */ + 0x8, /* #303030 -> #555555 */ + 0x8, /* #3a3a3a -> #555555 */ + 0x8, /* #444444 -> #555555 */ + 0x8, /* #4e4e4e -> #555555 */ + 0x8, /* #585858 -> #555555 */ + 0x8, /* #626262 -> #555555 */ + 0x8, /* #6c6c6c -> #555555 */ + 0x8, /* #767676 -> #555555 */ + 0x7, /* #808080 -> #aaaaaa */ + 0x7, /* #8a8a8a -> #aaaaaa */ + 0x7, /* #949494 -> #aaaaaa */ + 0x7, /* #9e9e9e -> #aaaaaa */ + 0x7, /* #a8a8a8 -> #aaaaaa */ + 0x7, /* #b2b2b2 -> #aaaaaa */ + 0x7, /* #bcbcbc -> #aaaaaa */ + 0x7, /* #c6c6c6 -> #aaaaaa */ + 0x7, /* #d0d0d0 -> #aaaaaa */ + 0xf, /* #dadada -> #ffffff */ + 0xf, /* #e4e4e4 -> #ffffff */ + 0xf, /* #eeeeee -> #ffffff */ +}; diff --git a/util/generate-vga-palette.c b/util/generate-vga-palette.c new file mode 100644 index 00000000..e1b7cd23 --- /dev/null +++ b/util/generate-vga-palette.c @@ -0,0 +1,76 @@ +#include +#include + +#include "../userspace/gui/terminal/terminal-palette.h" + +static int abs(int a) { + return a > 0 ? a : -a; +} + +static int color_distance(uint32_t a, uint32_t b) { + int a_r = (a & 0xFF0000) >> 16; + int a_g = (a & 0xFF00) >> 8; + int a_b = (a & 0xFF); + + int b_r = (b & 0xFF0000) >> 16; + int b_g = (b & 0xFF00) >> 8; + int b_b = (b & 0xFF); + + int distance = 0; + distance += abs(a_r - b_r) * 3; + distance += abs(a_g - b_g) * 6; + distance += abs(a_b - b_b) * 10; + + return distance; +} + +static uint32_t vga_colors[] = { + 0x000000, + 0xAA0000, + 0x00AA00, + 0xAA5500, + 0x0000AA, + 0xAA00AA, + 0x00AAAA, + 0xAAAAAA, + 0x555555, + 0xFF5555, + 0x55AA55, + 0xFFFF55, + 0x5555FF, + 0xFF55FF, + 0x55FFFF, + 0xFFFFFF, +}; + +static int is_gray(uint32_t a) { + int a_r = (a & 0xFF0000) >> 16; + int a_g = (a & 0xFF00) >> 8; + int a_b = (a & 0xFF); + + return (a_r == a_g && a_g == a_b); +} + +int main(int argc, char * argv[]) { + printf("#define PALETTE_COLORS 256\n"); + printf("uint32_t vga_colors[PALETTE_COLORS] = {\n"); + for (int i = 0; i < 16; ++i) { + printf("\t0x%x,\n", i); + } + for (int i = 16; i < 256; ++i) { + int best_distance = INT32_MAX; + int best_index = 0; + for (int j = 0; j < 16; ++j) { + if (is_gray(term_colors[i]) && !is_gray(vga_colors[j])); + int distance = color_distance(term_colors[i], vga_colors[j]); + if (distance < best_distance) { + best_index = j; + best_distance = distance; + } + } + printf("\t0x%x, /* #%06x -> #%06x */\n", best_index, term_colors[i], vga_colors[best_index]); + } + printf("};\n"); + return 0; +} +