From 8e12dbeba6e01ff74f2e211b82c83b0271e05d9f Mon Sep 17 00:00:00 2001
From: Sam Hocevar <sam@hocevar.net>
Date: Tue, 18 Apr 2006 10:02:42 +0000
Subject: [PATCH]   * Truecolor support for the HTML and HTMLv3 exporters.

---
 cucul/export.c | 53 ++++++++++++++++----------------------------------
 1 file changed, 17 insertions(+), 36 deletions(-)

diff --git a/cucul/export.c b/cucul/export.c
index c7ebeda..83a1815 100644
--- a/cucul/export.c
+++ b/cucul/export.c
@@ -194,36 +194,23 @@ static void export_ansi(cucul_t *qq, cucul_buffer_t *ex)
 /* Generate HTML representation of current canvas. */
 static void export_html(cucul_t *qq, cucul_buffer_t *ex)
 {
-    static uint16_t const palette[] =
-    {
-        0x000, 0x008, 0x080, 0x088, 0x800, 0x808, 0x880, 0x888,
-        0x444, 0x44f, 0x4f4, 0x4ff, 0xf44, 0xf4f, 0xff4, 0xfff,
-    };
     char *cur;
     unsigned int x, y, len;
 
-    /* The CSS palette: roughly 13000 bytes
+    /* The HTML header: less than 1000 bytes
      * A line: 7 chars for "<br />\n"
-     * A glyph: 18 chars for "<span class='bxx'>"
+     * A glyph: 47 chars for "<span style="color:#xxx;background-color:#xxx">"
      *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
      *          7 chars for "</span>" */
-    ex->size = 13000 + qq->height * (7 + qq->width * (18 + 9 + 7));
+    ex->size = 1000 + qq->height * (7 + qq->width * (47 + 9 + 7));
     ex->data = malloc(ex->size);
 
     cur = ex->data;
 
     /* HTML header */
-    cur += sprintf(cur, "<html>\n<head>\n<title>Generated by libcaca %s</title>\n", VERSION);
-
-    /* CSS */
-    cur += sprintf(cur, "<style>\n");
-    cur += sprintf(cur, ".caca { font-family: monospace, fixed; font-weight: bold; }");
-    for(x = 0; x < 0x100; x++)
-    {
-        cur += sprintf(cur, ".b%02x { color:#%03x; background-color:#%03x; }\n",
-                       x, palette[x & 0xf ], palette[x >> 4]);
-    }
-    cur += sprintf(cur, "</style>\n</head>\n<body>\n");
+    cur += sprintf(cur, "<html><head>\n");
+    cur += sprintf(cur, "<title>Generated by libcaca %s</title>\n", VERSION);
+    cur += sprintf(cur, "</head><body>\n");
 
     cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
                         "font-family: monospace, fixed; font-weight: bold;");
@@ -235,8 +222,10 @@ static void export_html(cucul_t *qq, cucul_buffer_t *ex)
 
         for(x = 0; x < qq->width; x += len)
         {
-            cur += sprintf(cur, "<span class='b%02x'>",
-                           _cucul_argb32_to_ansi8(lineattr[x]));
+            cur += sprintf(cur, "<span style=\"color:#%.03x;"
+                                "background-color:#%.03x\">",
+                                _cucul_argb32_to_rgb12fg(lineattr[x]),
+                                _cucul_argb32_to_rgb12bg(lineattr[x]));
 
             for(len = 0;
                 x + len < qq->width && lineattr[x + len] == lineattr[x];
@@ -268,23 +257,15 @@ static void export_html(cucul_t *qq, cucul_buffer_t *ex)
  * correct header. */
 static void export_html3(cucul_t *qq, cucul_buffer_t *ex)
 {
-    static uint32_t const palette[] =
-    {
-        0x000000, 0x000088, 0x008800, 0x008888,
-        0x880000, 0x880088, 0x888800, 0x888888,
-        0x444444, 0x4444ff, 0x44ff44, 0x44ffff,
-        0xff4444, 0xff44ff, 0xffff44, 0xffffff,
-    };
-
     char *cur;
     unsigned int x, y, len;
 
-    /* The CSS palette: roughly 13000 bytes
+    /* The HTML table markup: less than 1000 bytes
      * A line: 10 chars for "<tr></tr>\n"
      * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
      *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
      *          12 chars for "</font></td>" */
-    ex->size = 13000 + qq->height * (10 + qq->width * (40 + 9 + 12));
+    ex->size = 1000 + qq->height * (10 + qq->width * (40 + 9 + 12));
     ex->data = malloc(ex->size);
 
     cur = ex->data;
@@ -304,20 +285,20 @@ static void export_html3(cucul_t *qq, cucul_buffer_t *ex)
         {
             unsigned int i;
 
-            /* Use colspan option to factorize cells with same attributes
+            /* Use colspan option to factor cells with same attributes
              * (see below) */
             len = 1;
             while(x + len < qq->width && lineattr[x + len] == lineattr[x])
                 len++;
 
-            cur += sprintf(cur, "<td bgcolor=#%06x",
-                           palette[_cucul_argb32_to_ansi4bg(lineattr[x])]);
+            cur += sprintf(cur, "<td bgcolor=#%.06x",
+                           _cucul_argb32_to_rgb24bg(lineattr[x]));
 
             if(len > 1)
                 cur += sprintf(cur, " colspan=%d", len);
 
-            cur += sprintf(cur, "><font color=#%06x>",
-                           palette[_cucul_argb32_to_ansi4fg(lineattr[x])]);
+            cur += sprintf(cur, "><font color=#%.06x>",
+                           _cucul_argb32_to_rgb24fg(lineattr[x]));
 
             for(i = 0; i < len; i++)
             {