Changeset 4235


Ignore:
Timestamp:
Jan 6, 2010, 9:56:29 PM (13 years ago)
Author:
Pascal Terjan
Message:
  • Map colors to the ones known by groff, defining mines don't work
File:
1 edited

Legend:

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

    r4228 r4235  
    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 */
     993static 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        {
     999                r += ((color >> (8*i+7))&1) << i;
     1000        }
     1001        return r;
     1002}
     1003
    9841004/* Generate troff representation of current canvas. */
    9851005static void *export_troff(caca_canvas_t const *cv, size_t *bytes)
     
    9871007    char *data, *cur;
    9881008    int x, y;
     1009
     1010    const char * colors[8] = { "black", "blue", "green", "cyan",
     1011                               "red", "magenta", "yellow", "white" };
    9891012
    9901013    uint32_t prevfg = 0;
     
    9931016
    9941017    /* Each char is at most
    995      *  2x.defcolor (2x29)
    996      *  + 2x\mM (2x10)
     1018     *  2x\mM (2x10)
    9971019     *  + \fB + \fI + \fR (9)
    998      *  + 4 bytes = 91
     1020     *  + 4 bytes = 33
    9991021     * Each line has a \n (1)
    1000      * Header has .color 1\n.nf\n (12)
     1022     * Header has .nf\n (3)
    10011023     */
    1002     *bytes = 12 + cv->height + (cv->width * cv->height * 91);
     1024    *bytes = 3 + cv->height + (cv->width * cv->height * 33);
    10031025    cur = data = malloc(*bytes);
    1004 
    1005     cur += sprintf(cur, ".color 1\n");
    1006 
    1007     for(y = 0; y < cv->height; y++)
    1008     {
    1009         uint32_t *lineattr = cv->attrs + y * cv->width;
    1010 
    1011         for(x = 0; x < cv->width; x++)
    1012         {
    1013    
    1014             uint32_t fg = _caca_attr_to_rgb24bg(lineattr[x]);
    1015             uint32_t bg = _caca_attr_to_rgb24fg(lineattr[x]);
    1016             if(fg != prevfg || !started)
    1017                 cur += sprintf(cur, ".defcolor %.06x rgb #%.06x\n", fg, fg);
    1018             if(bg != prevbg || !started)
    1019                 cur += sprintf(cur, ".defcolor %.06x rgb #%.06x\n", bg, bg);
    1020 
    1021             prevfg = fg;
    1022             prevbg = bg;
    1023             started = 1;
    1024         }
    1025     }
    10261026
    10271027    cur += sprintf(cur, ".nf\n");
     
    10381038        for(x = 0; x < cv->width; x++)
    10391039        {
    1040    
    1041             uint32_t fg = _caca_attr_to_rgb24bg(lineattr[x]);
    1042             uint32_t bg = _caca_attr_to_rgb24fg(lineattr[x]);
     1040            uint32_t fg = _rgb_to_troff_index(_caca_attr_to_rgb24fg(lineattr[x]));
     1041            uint32_t bg = _rgb_to_troff_index(_caca_attr_to_rgb24bg(lineattr[x]));
    10431042            uint32_t ch = linechar[x];
    10441043            if(fg != prevfg || !started)
    1045                 cur += sprintf(cur, "\\m[%.06x]", fg);
     1044                cur += sprintf(cur, "\\m[%s]", colors[fg]);
    10461045            if(bg != prevbg || !started)
    1047                 cur += sprintf(cur, "\\M[%.06x]", bg);
     1046                cur += sprintf(cur, "\\M[%s]", colors[bg]);
    10481047            if(lineattr[x] & CACA_BOLD)
    10491048                cur += sprintf(cur, "\\fB");
Note: See TracChangeset for help on using the changeset viewer.