Changeset 4302


Ignore:
Timestamp:
Jan 25, 2010, 2:47:21 AM (8 years ago)
Author:
Sam Hocevar
Message:

Simpler and more accurate troff colour selection.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca/codec/export.c

    r4301 r4302  
    982982}
    983983
    984 /* Get troff color index from RGB
    985  *
    986  * We want for each component to know if it is >= 80
    987  * Then we use the 3 bits to index colors list:
    988  * 000 -> black
    989  * 001 -> blue
    990  * ...
    991  * 111 -> white
    992  */
    993 static uint8_t _rgb_to_troff_index(uint32_t color)
    994 {
    995     int i;
    996     uint8_t r = 0;
    997     for(i = 0; i < 3; i++)
    998         r += ((color >> (8 * i + 7)) & 1) << i;
    999 
    1000     return r;
    1001 }
    1002 
    1003984/* Generate troff representation of current canvas. */
    1004985static void *export_troff(caca_canvas_t const *cv, size_t *bytes)
     
    1006987    char *data, *cur;
    1007988    int x, y;
    1008 
    1009     const char * colors[8] = { "black", "blue", "green", "cyan",
    1010                                "red", "magenta", "yellow", "white" };
    1011989
    1012990    uint32_t prevfg = 0;
     
    10371015        for(x = 0; x < cv->width; x++)
    10381016        {
    1039             uint32_t fg = _rgb_to_troff_index(_caca_attr_to_rgb24fg(lineattr[x]));
    1040             uint32_t bg = _rgb_to_troff_index(_caca_attr_to_rgb24bg(lineattr[x]));
     1017            static char const * ansi2troff[16] =
     1018            {
     1019                /* Dark */
     1020                "black", "blue", "green", "cyan",
     1021                "red", "magenta", "yellow", "white",
     1022                /* Bright */
     1023                "black", "blue", "green", "cyan",
     1024                "red", "magenta", "yellow", "white",
     1025            };
     1026            uint8_t fg = caca_attr_to_ansi_fg(lineattr[x]);
     1027            uint8_t bg = caca_attr_to_ansi_bg(lineattr[x]);
    10411028            uint32_t ch = linechar[x];
    10421029
    10431030            if(fg != prevfg || !started)
    1044                 cur += sprintf(cur, "\\m[%s]", colors[fg]);
     1031                cur += sprintf(cur, "\\m[%s]", ansi2troff[fg]);
    10451032            if(bg != prevbg || !started)
    1046                 cur += sprintf(cur, "\\M[%s]", colors[bg]);
     1033                cur += sprintf(cur, "\\M[%s]", ansi2troff[bg]);
    10471034            if(lineattr[x] & CACA_BOLD)
    10481035                cur += sprintf(cur, "\\fB");
Note: See TracChangeset for help on using the changeset viewer.