fix(workspace): resolve terminal palette indices instead of writing raw indices as RGB
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 3 Nov 2026 19:29:04 +0000 (21:29 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 3 Nov 2026 19:29:04 +0000 (21:29 +0200)
workspace-v2/src/main/java/eu/svjatoslav/aukio/wsp/TerminalPanel.java

index a38729f..87aa348 100644 (file)
@@ -17,8 +17,33 @@ import eu.svjatoslav.aukio.wsp.pty.ScreenBuffer;
  */
 public final class TerminalPanel extends RectPanel implements AutoCloseable {
 
-    private static final int DEFAULT_BG = 0xFF0E1216;
-    private static final int DEFAULT_FG = 0xFFC8D0D8;
+    private static final int DEFAULT_BG = 0xFF101018;
+    private static final int DEFAULT_FG = 0xFFDCDCDC;
+
+    /**
+     * xterm palette, indices 0-15 - the SAME table V1's terminal used. The
+     * ScreenBuffer stores palette INDICES (0-7, 8-15, or 16+ for 256-colour
+     * later), never RGB: writing the index straight into a pixel made mc's
+     * blue background render as 0x04 = almost black.
+     */
+    private static final int[] PALETTE = {
+            0xFF000000, // 0 black
+            0xFFCD3131, // 1 red
+            0xFF0DBC79, // 2 green
+            0xFFE5E510, // 3 yellow
+            0xFF2472C8, // 4 blue
+            0xFFBC3FBC, // 5 magenta
+            0xFF11A8CD, // 6 cyan
+            0xFFE5E5E5, // 7 white
+            0xFF666666, // 8 bright black
+            0xFFF14C4C, // 9 bright red
+            0xFF23D18B, // 10 bright green
+            0xFFF5F543, // 11 bright yellow
+            0xFF3B8EEA, // 12 bright blue
+            0xFFD670D6, // 13 bright magenta
+            0xFF29B8DB, // 14 bright cyan
+            0xFFFFFFFF, // 15 bright white
+    };
     /** Sampling exactly 1.0 in u/v leaves the last cell: LifeHelp's rule. */
     private static final double EDGE = Math.nextDown(1.0);
 
@@ -91,10 +116,8 @@ public final class TerminalPanel extends RectPanel implements AutoCloseable {
             for (int row = 0; row < rows; row++) {
                 for (int column = 0; column < columns; column++) {
                     final ScreenBuffer.Cell cell = screen.getCell(row, column);
-                    int fg = cell.fg == ScreenBuffer.DEFAULT_COLOR
-                            ? DEFAULT_FG : 0xFF000000 | (cell.fg & 0xFFFFFF);
-                    int bg = cell.bg == ScreenBuffer.DEFAULT_COLOR
-                            ? DEFAULT_BG : 0xFF000000 | (cell.bg & 0xFFFFFF);
+                    int fg = resolve(cell.fg, DEFAULT_FG, cell.bold);
+                    int bg = resolve(cell.bg, DEFAULT_BG, false);
                     if (cell.reverse) {
                         final int swap = fg;
                         fg = bg;
@@ -154,6 +177,27 @@ public final class TerminalPanel extends RectPanel implements AutoCloseable {
         }
     }
 
+    /** Palette index to ARGB, V1's rule (bold promotes the low 8 to bright). */
+    private static int resolve(final int index, final int defaultArgb, final boolean bold) {
+        if (index == ScreenBuffer.DEFAULT_COLOR) return defaultArgb;
+        if (index < 16) {
+            return PALETTE[bold && index < 8 ? index + 8 : index];
+        }
+        final int i = index - 16;
+        if (i < 216) {
+            final int r = cubeChannel(i / 36);
+            final int g = cubeChannel((i / 6) % 6);
+            final int b = cubeChannel(i % 6);
+            return 0xFF000000 | r << 16 | g << 8 | b;
+        }
+        final int gray = Math.min(255, 8 + (i - 216) * 10);
+        return 0xFF000000 | gray << 16 | gray << 8 | gray;
+    }
+
+    private static int cubeChannel(final int level) {
+        return level == 0 ? 0 : 55 + level * 40;
+    }
+
     public void send(final String text) {
         session.send(text);
     }